mblinks: when loading the cache, clear expired entries

This commit is contained in:
Laurent Monin 2015-06-11 14:00:12 +02:00
parent c9a26fd00f
commit 0b49e16cae

View file

@ -83,6 +83,8 @@ var MBLinks = function (cachekey, expiration) {
if (!this.supports_local_storage) return;
// Check if we already added links for this content
this.cache = JSON.parse(localStorage.getItem(this.cache_key) || '{}');
// remove old entries
this.clearCacheExpired();
};
this.saveCache = function () {
@ -94,6 +96,22 @@ var MBLinks = function (cachekey, expiration) {
}
};
this.clearCacheExpired = function() {
//var old_cache_entries = Object.keys(this.cache).length;
//console.log("clearCacheExpired " + old_cache_entries);
var now = new Date().getTime();
var new_cache = {};
var that = this;
$.each(this.cache, function (key, value) {
if (that.is_cached(key)) {
new_cache[key] = that.cache[key];
}
});
//var new_cache_entries = Object.keys(new_cache).length;
//console.log("Cleared cache entries: " + old_cache_entries + ' -> ' + new_cache_entries);
this.cache = new_cache;
};
this.is_cached = function (url) {
return (this.cache[url] && this.expirationMinutes > 0 && new Date().getTime() < this.cache[url].timestamp + this.expirationMinutes*60*1000);
};