mirror of
https://github.com/murdos/musicbrainz-userscripts
synced 2025-03-04 22:37:16 +00:00
mblinks: when loading the cache, clear expired entries
This commit is contained in:
parent
c9a26fd00f
commit
0b49e16cae
1 changed files with 18 additions and 0 deletions
|
@ -83,6 +83,8 @@ var MBLinks = function (cachekey, expiration) {
|
||||||
if (!this.supports_local_storage) return;
|
if (!this.supports_local_storage) return;
|
||||||
// Check if we already added links for this content
|
// Check if we already added links for this content
|
||||||
this.cache = JSON.parse(localStorage.getItem(this.cache_key) || '{}');
|
this.cache = JSON.parse(localStorage.getItem(this.cache_key) || '{}');
|
||||||
|
// remove old entries
|
||||||
|
this.clearCacheExpired();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.saveCache = function () {
|
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) {
|
this.is_cached = function (url) {
|
||||||
return (this.cache[url] && this.expirationMinutes > 0 && new Date().getTime() < this.cache[url].timestamp + this.expirationMinutes*60*1000);
|
return (this.cache[url] && this.expirationMinutes > 0 && new Date().getTime() < this.cache[url].timestamp + this.expirationMinutes*60*1000);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue