From 10af59bfceab73e3c97ee319d2319c4a6ec85751 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sat, 7 Oct 2017 10:03:45 -0600 Subject: [PATCH] Allow items to be removed from the cache --- src/ol/structs/lrucache.js | 28 +++++++++++++ test/spec/ol/structs/lrucache.test.js | 60 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/src/ol/structs/lrucache.js b/src/ol/structs/lrucache.js index bdced622cc..e0f74ec850 100644 --- a/src/ol/structs/lrucache.js +++ b/src/ol/structs/lrucache.js @@ -116,6 +116,34 @@ ol.structs.LRUCache.prototype.get = function(key) { }; +/** + * Remove an entry from the cache. + * @param {string} key The entry key. + * @return {T} The removed entry. + */ +ol.structs.LRUCache.prototype.remove = function(key) { + var entry = this.entries_[key]; + ol.asserts.assert(entry !== undefined, 15); // Tried to get a value for a key that does not exist in the cache + if (entry === this.newest_) { + this.newest_ = /** @type {ol.LRUCacheEntry} */ (entry.older); + if (this.newest_) { + this.newest_.newer = null; + } + } else if (entry === this.oldest_) { + this.oldest_ = /** @type {ol.LRUCacheEntry} */ (entry.newer); + if (this.oldest_) { + this.oldest_.older = null; + } + } else { + entry.newer.older = entry.older; + entry.older.newer = entry.newer; + } + delete this.entries_[key]; + --this.count_; + return entry.value_; +}; + + /** * @return {number} Count. */ diff --git a/test/spec/ol/structs/lrucache.test.js b/test/spec/ol/structs/lrucache.test.js index 71b662d661..fd25890692 100644 --- a/test/spec/ol/structs/lrucache.test.js +++ b/test/spec/ol/structs/lrucache.test.js @@ -188,6 +188,66 @@ describe('ol.structs.LRUCache', function() { }); }); + describe('#remove()', function() { + it('removes an item from the cache', function() { + var cache = new ol.structs.LRUCache(); + cache.set('oldest', 'oldest'); + cache.set('oldish', 'oldish'); + cache.set('newish', 'newish'); + cache.set('newest', 'newest'); + + cache.remove('oldish'); + expect(cache.getCount()).to.eql(3); + expect(cache.getValues()).to.eql(['newest', 'newish', 'oldest']); + }); + + it('works when removing the oldest item', function() { + var cache = new ol.structs.LRUCache(); + cache.set('oldest', 'oldest'); + cache.set('oldish', 'oldish'); + cache.set('newish', 'newish'); + cache.set('newest', 'newest'); + + cache.remove('oldest'); + expect(cache.getCount()).to.eql(3); + expect(cache.peekLastKey()).to.eql('oldish'); + expect(cache.getValues()).to.eql(['newest', 'newish', 'oldish']); + }); + + it('works when removing the newest item', function() { + var cache = new ol.structs.LRUCache(); + cache.set('oldest', 'oldest'); + cache.set('oldish', 'oldish'); + cache.set('newish', 'newish'); + cache.set('newest', 'newest'); + + cache.remove('newest'); + expect(cache.getCount()).to.eql(3); + expect(cache.peekFirstKey()).to.eql('newish'); + expect(cache.getValues()).to.eql(['newish', 'oldish', 'oldest']); + }); + + it('returns the removed item', function() { + var cache = new ol.structs.LRUCache(); + var item = {}; + cache.set('key', item); + + var returned = cache.remove('key'); + expect(returned).to.be(item); + }); + + it('throws if the key does not exist', function() { + var cache = new ol.structs.LRUCache(); + cache.set('foo', 'foo'); + cache.set('bar', 'bar'); + + var call = function() { + cache.remove('bam'); + }; + expect(call).to.throwException(); + }); + }); + describe('clearing the cache', function() { it('clears the cache', function() { fillLRUCache(lruCache);