re-tune the cache code so it's much more short-term storage

the original reason for longer term storage were the frequent error==TIMEOUT from map tiles. since these are now retried, as the stock site does, the remaining use for the cache is more short term, while panning around an area
This commit is contained in:
Jon Atkins 2013-09-04 04:32:04 +01:00
parent fce9f8e0bc
commit 4ccfbdfa45

View File

@ -2,20 +2,19 @@
// cache for map data tiles.
window.DataCache = function() {
this.REQUEST_CACHE_FRESH_AGE = 90; // if younger than this, use data in the cache rather than fetching from the server
this.REQUEST_CACHE_MAX_AGE = 60*60; // maximum cache age. entries are deleted from the cache after this time
this.REQUEST_CACHE_FRESH_AGE = 60; // if younger than this, use data in the cache rather than fetching from the server
this.REQUEST_CACHE_MAX_AGE = 180; // maximum cache age. entries are deleted from the cache after this time
if (L.Browser.mobile) {
// on mobile devices, smaller cache size
this.REQUEST_CACHE_MIN_SIZE = 200; // if fewer than this many entries, don't expire any
this.REQUEST_CACHE_MAX_SIZE = 1000; // if more than this many entries, expire early
this.REQUEST_CACHE_MAX_SIZE = 300; // if more than this many entries, expire early
} else {
// but on desktop, allow more
this.REQUEST_CACHE_MIN_SIZE = 500; // if fewer than this many entries, don't expire any
this.REQUEST_CACHE_MAX_SIZE = 4000; // if more than this many entries, expire early
this.REQUEST_CACHE_MAX_SIZE = 1000; // if more than this many entries, expire early
}
this._cache = {};
this._interval = undefined;
}
@ -51,16 +50,29 @@ window.DataCache.prototype.isFresh = function(qk) {
return undefined;
}
window.DataCache.prototype.startExpireInterval = function(period) {
if (this._interval === undefined) {
var savedContext = this;
this._interval = setInterval (function() { savedContext.runExpire(); }, period*1000);
}
}
window.DataCache.prototype.expire = function() {
window.DataCache.prototype.stopExpireInterval = function() {
if (this._interval !== undefined) {
stopInterval (this._interval);
this._interval = undefined;
}
}
window.DataCache.prototype.runExpire = function() {
var d = new Date();
var t = d.getTime()-this.REQUEST_CACHE_MAX_AGE*1000;
var cacheSize = Object.keys(this._cache).length;
for(var qk in this._cache) {
// stop processing once we hit the minimum size
if (cacheSize < this.REQUEST_CACHE_MIN_SIZE) break;
// fixme? our MAX_SIZE test here assumes we're processing the oldest first. this relies
// on looping over object properties in the order they were added. this is true in most browsers,