HTTPBindingClient.collectCacheItems = function()

in modules/js/htdocs/component.js [249:305]


HTTPBindingClient.collectCacheItems = function() {
    var ls = window.lstorage || localStorage;
    var nkeys = window.lstorage? function() { return ls.length(); } : function() { return ls.length; };

    // Get the current cache size
    var size = 0;
    var s = ls.getItem('dc.size');
    if(!s) {
        // Calculate and store initial cache size
        debug('component calculating cache size');
        var n = nkeys();
        for(var i = 0; i < n; i++) {
            var k = ls.key(i);
            if(!k || k.substr(0, 5) != 'dc.d.')
                continue;
            var v = ls.getItem(k);
            if(!v)
                continue;
            size += v.length;
        }
        ls.setItem('dc.size', size.toString());
    } else
        size = parseInt(s);

    // Nothing to do if it's below the max size
    debug('component cache size', size);
    if (size <= HTTPBindingClient.maxCacheSize)
        return false;

    // Collect random cache entries until we reach our min size
    debug('component collecting cache items');
    var keys = [];
    var n = nkeys();
    for(var i = 0; i < n; i++) {
        var k = ls.key(i);
        if(!k || k.substr(0, 5) != 'dc.d.')
            continue;
        keys[keys.length] = k;
    }
    while (keys.length != 0 && size >= HTTPBindingClient.collectCacheSize) {
        var r = Math.floor(keys.length * Math.random());
        if (r == keys.length)
            continue;
        var k = keys[r];
        var v = ls.getItem(k);
        debug('component collect cache item', k);
        ls.removeItem(k);
        keys.splice(r, 1);
        if (v)
            size = size - v.length;
    }

    // Store the new cache size
    debug('component updated cache size', size);
    ls.setItem('dc.size', size.toString());
    return true;
};