Created
March 27, 2014 18:22
-
-
Save cvan/9814579 to your computer and use it in GitHub Desktop.
snippet for LRU cache for object cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get list of cache keys sorted by timestamp. | |
var keysAsc = _.sortBy(_.pairs(cache), function (x) { | |
return x[1].__time; // Timestamp | |
}).map(function(x) { | |
return x[0]; // URL | |
}); | |
// Keep popping off expired keys until we're under the quota. | |
var i = 0; | |
while (JSON.stringify(cache).length >= settings.offline_cache_limit_request) { | |
if (typeof keysAsc[i] !== 'undefined') { | |
console.log('Quota exceeded; removing from request cache', keysAsc[i]); | |
delete cache[keysAsc[i]]; | |
} | |
i++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment