From b06c86ba3b9a90eac77061076d8aa9f5e6206520 Mon Sep 17 00:00:00 2001 From: jingsam Date: Wed, 15 Nov 2017 10:43:21 +0800 Subject: [PATCH] Clarify LRUCache method names --- src/source/source_cache.js | 4 ++-- src/util/lru_cache.js | 10 +++++----- test/unit/util/lru_cache.test.js | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/source/source_cache.js b/src/source/source_cache.js index 1dc87769616..cc0ef35e3f1 100644 --- a/src/source/source_cache.js +++ b/src/source/source_cache.js @@ -332,7 +332,7 @@ class SourceCache extends Evented { } if (this._cache.has(id)) { retain[id] = true; - return this._cache.getWithoutRemoving(id); + return this._cache.get(id); } } } @@ -536,7 +536,7 @@ class SourceCache extends Evented { return tile; - tile = this._cache.get((tileCoord.id: any)); + tile = this._cache.getAndRemove((tileCoord.id: any)); if (tile) { this._updatePlacement(); if (this.map) diff --git a/src/util/lru_cache.js b/src/util/lru_cache.js index 70d843bf695..ff7aa31f37d 100644 --- a/src/util/lru_cache.js +++ b/src/util/lru_cache.js @@ -91,14 +91,14 @@ class LRUCache { } /** - * Get the value attached to a specific key. If the key is not found, - * returns `null` + * Get the value attached to a specific key and remove data from cache. + * If the key is not found, returns `null` * * @param {string} key the key to look up * @returns {*} the data, or null if it isn't found * @private */ - get(key: string): ?T { + getAndRemove(key: string): ?T { if (!this.has(key)) { return null; } const data = this.data[key]; @@ -117,7 +117,7 @@ class LRUCache { * @returns {*} the data, or null if it isn't found * @private */ - getWithoutRemoving(key: string): ?T { + get(key: string): ?T { if (!this.has(key)) { return null; } const data = this.data[key]; @@ -153,7 +153,7 @@ class LRUCache { this.max = max; while (this.order.length > this.max) { - const removedData = this.get(this.order[0]); + const removedData = this.getAndRemove(this.order[0]); if (removedData) this.onRemove(removedData); } diff --git a/test/unit/util/lru_cache.test.js b/test/unit/util/lru_cache.test.js index a1e66d6c52b..e45824d0ea5 100644 --- a/test/unit/util/lru_cache.test.js +++ b/test/unit/util/lru_cache.test.js @@ -7,12 +7,12 @@ test('LRUCache', (t) => { const cache = new LRUCache(10, (removed) => { t.equal(removed, 'dc'); }); - t.equal(cache.get('foo'), null, '.get() to null'); + t.equal(cache.getAndRemove('foo'), null, '.getAndRemove() to null'); t.equal(cache.add('washington', 'dc'), cache, '.add()'); t.deepEqual(cache.keys(), ['washington'], '.keys()'); t.equal(cache.has('washington'), true, '.has()'); - t.equal(cache.get('washington'), 'dc', '.get()'); - t.equal(cache.get('washington'), null, '.get()'); + t.equal(cache.getAndRemove('washington'), 'dc', '.getAndRemove()'); + t.equal(cache.getAndRemove('washington'), null, '.getAndRemove()'); t.equal(cache.has('washington'), false, '.has()'); t.deepEqual(cache.keys(), [], '.keys()'); t.end(); @@ -23,7 +23,7 @@ test('LRUCache - getWithoutRemoving', (t) => { t.fail(); }); t.equal(cache.add('washington', 'dc'), cache, '.add()'); - t.equal(cache.getWithoutRemoving('washington'), 'dc', '.getWithoutRemoving()'); + t.equal(cache.get('washington'), 'dc', '.get()'); t.deepEqual(cache.keys(), ['washington'], '.keys()'); t.end(); }); @@ -38,7 +38,7 @@ test('LRUCache - duplicate add', (t) => { t.deepEqual(cache.keys(), ['a']); t.ok(cache.has('a')); - t.equal(cache.get('a'), 'c'); + t.equal(cache.getAndRemove('a'), 'c'); t.end(); });