From 0b49e16cae2162fa708ff150fbd2c873b6201c6c Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Thu, 11 Jun 2015 14:00:12 +0200 Subject: [PATCH] mblinks: when loading the cache, clear expired entries --- lib/mblinks.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/mblinks.js b/lib/mblinks.js index 802ec13..c8c1325 100644 --- a/lib/mblinks.js +++ b/lib/mblinks.js @@ -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); };