Skip to content

Instantly share code, notes, and snippets.

@cvan
Created March 27, 2014 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvan/9814579 to your computer and use it in GitHub Desktop.
Save cvan/9814579 to your computer and use it in GitHub Desktop.
snippet for LRU cache for object cache
// 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