diff --git a/debug/site.js b/debug/site.js index 60e206f479d..a1eb10630d9 100644 --- a/debug/site.js +++ b/debug/site.js @@ -5,7 +5,7 @@ var map = new mapboxgl.Map({ container: 'map', zoom: 12.5, center: [-77.01866, 38.888], - style: 'mapbox://styles/mapbox/streets-v8', + style: 'mapbox://styles/mapbox/satellite-hybrid-v8', hash: true }); diff --git a/js/util/lru_cache.js b/js/util/lru_cache.js index 6c225aebe33..b12a7e9c8be 100644 --- a/js/util/lru_cache.js +++ b/js/util/lru_cache.js @@ -44,12 +44,20 @@ LRUCache.prototype.reset = function() { * @private */ LRUCache.prototype.add = function(key, data) { - this.data[key] = data; - this.order.push(key); - if (this.order.length > this.max) { - var removedData = this.get(this.order[0]); - if (removedData) this.onRemove(removedData); + if (this.has(key)) { + this.order.splice(this.order.indexOf(key), 1); + this.data[key] = data; + this.order.push(key); + + } else { + this.data[key] = data; + this.order.push(key); + + if (this.order.length > this.max) { + var removedData = this.get(this.order[0]); + if (removedData) this.onRemove(removedData); + } } return this; diff --git a/test/js/util/lru_cache.test.js b/test/js/util/lru_cache.test.js index 21e5339e841..add78174a59 100644 --- a/test/js/util/lru_cache.test.js +++ b/test/js/util/lru_cache.test.js @@ -18,13 +18,27 @@ test('LRUCache', function(t) { t.end(); }); +test('LRUCache - duplicate add', function(t) { + var cache = new LRUCache(10, function() { + t.fail(); + }); + + cache.add('a', 'b'); + cache.add('a', 'c'); + + t.deepEqual(cache.keys(), ['a']); + t.ok(cache.has('a')); + t.equal(cache.get('a'), 'c'); + t.end(); +}); + test('LRUCache - overflow', function(t) { var cache = new LRUCache(1, function(removed) { - t.equal(removed, 'c'); + t.equal(removed, 'b'); t.end(); }); cache.add('a', 'b'); - cache.add('a', 'c'); + cache.add('c', 'd'); }); test('LRUCache#reset', function(t) {