tweaks to data tile fetching and cached tiles - should end up with fresher data when the niantic servers are struggling

This commit is contained in:
Jon Atkins 2013-08-08 11:56:00 +01:00
parent 1fa95072a4
commit b9a4f91f39

View File

@ -40,7 +40,7 @@ window.debugSetTileColour = function(qk,bordercol,fillcol) {
window._requestCache = {} window._requestCache = {}
// cache entries older than the fresh age, and younger than the max age, are stale. they're used in the case of an error from the server // cache entries older than the fresh age, and younger than the max age, are stale. they're used in the case of an error from the server
window.REQUEST_CACHE_FRESH_AGE = 60; // if younger than this, use data in the cache rather than fetching from the server window.REQUEST_CACHE_FRESH_AGE = 90; // if younger than this, use data in the cache rather than fetching from the server
window.REQUEST_CACHE_MAX_AGE = 60*60; // maximum cache age. entries are deleted from the cache after this time window.REQUEST_CACHE_MAX_AGE = 60*60; // maximum cache age. entries are deleted from the cache after this time
window.REQUEST_CACHE_MIN_SIZE = 200; // if fewer than this many entries, don't expire any window.REQUEST_CACHE_MIN_SIZE = 200; // if fewer than this many entries, don't expire any
window.REQUEST_CACHE_MAX_SIZE = 2000; // if more than this many entries, expire early window.REQUEST_CACHE_MAX_SIZE = 2000; // if more than this many entries, expire early
@ -63,6 +63,11 @@ window.getDataCache = function(qk) {
return undefined; return undefined;
} }
window.getCacheItemTime = function(qk) {
if (qk in window._requestCache) return window._requestCache[qk].time;
else return 0;
}
window.isDataCacheFresh = function(qk) { window.isDataCacheFresh = function(qk) {
if (qk in window._requestCache) { if (qk in window._requestCache) {
var d = new Date(); var d = new Date();
@ -187,15 +192,7 @@ window.requestData = function() {
lngEast lngEast
); );
// when the server is returning 'timeout' errors for some tiles in the list, it's always the tiles tiles[bucket].push(boundsParam);
// at the end of the request. therefore, let's push tiles we don't have cache entries for to the front, and those we do to the back
if (getDataCache(tile_id)) {
// cache entry exists - push to back
tiles[bucket].push(boundsParam);
} else {
// no cache entry - unshift to front
tiles[bucket].unshift(boundsParam);
}
debugSetTileColour(tile_id,'#00f','#000'); debugSetTileColour(tile_id,'#00f','#000');
} }
@ -209,6 +206,16 @@ window.requestData = function() {
// send ajax requests // send ajax requests
console.log('requesting '+requestTileCount+' tiles in '+Object.keys(tiles).length+' requests'); console.log('requesting '+requestTileCount+' tiles in '+Object.keys(tiles).length+' requests');
$.each(tiles, function(ind, tls) { $.each(tiles, function(ind, tls) {
// sort the tiles by the cache age - oldest/missing first. the server often times out requests and the first listed
// are more likely to succeed. this will ensure we're more likely to have fresh data
tls.sort(function(a,b) {
var timea = getCacheItemTime(a.qk);
var timeb = getCacheItemTime(b.qk);
if (timea < timeb) return -1;
if (timea > timeb) return 1;
return 0;
});
data = { zoom: z }; data = { zoom: z };
data.boundsParamsList = tls; data.boundsParamsList = tls;
// keep a list of tile_ids with each request. in the case of a server error, we can try and use cached tiles if available // keep a list of tile_ids with each request. in the case of a server error, we can try and use cached tiles if available