diff --git a/code/map_data_render.js b/code/map_data_render.js index c084e875..612ad048 100644 --- a/code/map_data_render.js +++ b/code/map_data_render.js @@ -258,6 +258,8 @@ window.Render.prototype.createPortalEntity = function(ent) { data: ent[2] }; + window.pushPortalGuidPositionCache(ent[0], ent[2].latE6, ent[2].lngE6); + var marker = createMarker(latlng, dataOptions); marker.on('click', function() { window.renderPortalDetails(ent[0]); }); diff --git a/code/portal_data.js b/code/portal_data.js index 6d687935..0ac4d743 100644 --- a/code/portal_data.js +++ b/code/portal_data.js @@ -88,7 +88,32 @@ window.findPortalLatLng = function(guid) { // no luck finding portal lat/lng return undefined; -} +}; + + +(function() { + var cache = {}; + var GC_LIMIT = 5000; // run garbage collector when cache has more that 5000 items + var GC_KEEP = 4000; // keep the 4000 most recent items + + window.findPortalGuidByPositionE6 = function(latE6, lngE6) { + var item = cache[latE6+","+lngE6]; + if(!item) return null; + return item[0]; + }; + + window.pushPortalGuidPositionCache = function(guid, latE6, lngE6) { + cache[latE6+","+lngE6] = [guid, Date.now()]; + + if(Object.keys(cache).length > GC_LIMIT) { + Object.keys(cache) // get all latlngs + .map(function(latlng) { return [latlng, cache[latlng][1]]; }) // map them to [latlng, timestamp] + .sort(function(a,b) { return b[1] - a[1]; }) // sort them + .slice(GC_KEEP) // drop the MRU + .forEach(function(item) { delete cache[item[0]] }); // delete the rest + } + } +})(); // get the AP gains from a portal, based only on the brief summary data from portals, links and fields