From fea2a4efd1ad085def7cf84cc444325d0815b62e Mon Sep 17 00:00:00 2001 From: mistic100 Date: Sat, 24 May 2014 14:18:04 +0000 Subject: feature 3077 : improve cache invalidation - add "lastmodified" automatic field for categories, groups, users, tags and images tables - provide a "server key" to the client cache manager git-svn-id: http://piwigo.org/svn/trunk@28532 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/themes/default/js/LocalStorageCache.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'admin/themes/default/js') diff --git a/admin/themes/default/js/LocalStorageCache.js b/admin/themes/default/js/LocalStorageCache.js index 49a4fa98d..c18171efc 100644 --- a/admin/themes/default/js/LocalStorageCache.js +++ b/admin/themes/default/js/LocalStorageCache.js @@ -1,7 +1,8 @@ -var LocalStorageCache = function(key, lifetime, loader) { - this.key = key; - this.lifetime = lifetime*1000; - this.loader = loader; +var LocalStorageCache = function(options) { + this.key = options.key + '-' + options.serverId; + this.serverKey = options.serverKey; + this.lifetime = options.lifetime ? options.lifetime*1000 : 3600*1000; + this.loader = options.loader; this.storage = window.localStorage; this.ready = !!this.storage; @@ -14,28 +15,23 @@ LocalStorageCache.prototype.get = function(callback) { if (this.ready && this.storage[this.key] != undefined) { var cache = JSON.parse(this.storage[this.key]); - if (now - cache.timestamp <= this.lifetime) { + if (now - cache.timestamp <= this.lifetime && cache.key == this.serverKey) { callback(cache.data); return; } } this.loader(function(data) { - if (that.ready) { - that.storage[that.key] = JSON.stringify({ - timestamp: now, - data: data - }); - } - + that.set.call(that, data); callback(data); }); }; LocalStorageCache.prototype.set = function(data) { if (this.ready) { - that.storage[that.key] = JSON.stringify({ + this.storage[this.key] = JSON.stringify({ timestamp: new Date().getTime(), + key: this.serverKey, data: data }); } -- cgit v1.2.3