Skip to content

Commit

Permalink
Clarify LRUCache method names
Browse files Browse the repository at this point in the history
  • Loading branch information
jingsam authored and jfirebaugh committed Nov 18, 2017
1 parent cffdf53 commit b06c86b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/util/lru_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ class LRUCache<T> {
}

/**
* 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];
Expand All @@ -117,7 +117,7 @@ class LRUCache<T> {
* @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];
Expand Down Expand Up @@ -153,7 +153,7 @@ class LRUCache<T> {
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);
}

Expand Down
10 changes: 5 additions & 5 deletions test/unit/util/lru_cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});
Expand All @@ -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();
});

Expand Down

0 comments on commit b06c86b

Please sign in to comment.