private registerInMemory()

in src/geocoder/geocodingCache.ts [124:166]


        private registerInMemory(key: string, coordinate: IGeocodeCoordinate): void {
            let geocodeCache: _.Dictionary<GeocodeCacheEntry> = this.geocodeCache;
            let maxCacheSize: number = this.maxCacheSize;
            let maxCacheCount: number = maxCacheSize + this.maxCacheSizeOverflow;

            // are we about to exceed the maximum?
            if (this.geocodeCacheCount >= maxCacheCount) {
                let keys: string[] = Object.keys(geocodeCache);
                let cacheSize: number = keys.length;

                // sort keys in *descending* hitCount order
                keys.sort((a: string, b: string) => {
                    let cachedA: GeocodeCacheEntry = geocodeCache[a];
                    let cachedB: GeocodeCacheEntry = geocodeCache[b];
                    let ca: number = cachedA ? cachedA.hitCount : 0;
                    let cb: number = cachedB ? cachedB.hitCount : 0;
                    return ca < cb ? 1 : (ca > cb ? -1 : 0);
                });

                // whack ones with the lower hitCounts.
                // - while # whacked keys is small, do a quick wipe
                // - after awhile we get lots of keys whose cached value is undefined.
                //   when there are "too many," make a whole new memory cache.
                if (cacheSize < 2 * maxCacheCount) {
                    for (let i = maxCacheSize; i < cacheSize; i++) {
                        geocodeCache[keys[i]] = undefined;
                    }
                }
                else {
                    let newGeocodeCache: _.Dictionary<GeocodeCacheEntry> = {};
                    for (let i = 0; i < maxCacheSize; ++i) {
                        newGeocodeCache[keys[i]] = geocodeCache[keys[i]];
                    }

                    geocodeCache = this.geocodeCache = newGeocodeCache;
                }

                this.geocodeCacheCount = maxCacheSize;
            }

            geocodeCache[key] = { coordinate: coordinate, hitCount: 1 };
            ++this.geocodeCacheCount;
        }