implement latlng-to-guid cache

This commit is contained in:
fkloft 2014-09-13 20:55:40 +02:00
parent 2def40c847
commit e9bd88fe31
2 changed files with 28 additions and 1 deletions

View File

@ -258,6 +258,8 @@ window.Render.prototype.createPortalEntity = function(ent) {
data: ent[2] data: ent[2]
}; };
window.pushPortalGuidPositionCache(ent[0], ent[2].latE6, ent[2].lngE6);
var marker = createMarker(latlng, dataOptions); var marker = createMarker(latlng, dataOptions);
marker.on('click', function() { window.renderPortalDetails(ent[0]); }); marker.on('click', function() { window.renderPortalDetails(ent[0]); });

View File

@ -88,7 +88,32 @@ window.findPortalLatLng = function(guid) {
// no luck finding portal lat/lng // no luck finding portal lat/lng
return undefined; 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 // get the AP gains from a portal, based only on the brief summary data from portals, links and fields