Skip to content

Commit

Permalink
Merge pull request #4 from voxpelli/peek
Browse files Browse the repository at this point in the history
Add peek(key)
  • Loading branch information
aholstenson authored Oct 26, 2017
2 parents 2af3aed + fcc8495 commit ff0e9b1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
coverage
/yarn.lock
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ There are a few basic things that all caches support.
Get a cached value. The key can be either a string or a number. Will return
any cached value and update is usage frequency.

* `cache.getIfPresent(key): mixed|null`
* `cache.getIfPresent(key, [recordStats]): mixed|null`

Same as `get(key)` except this will never load a value if it does not exist.
Usually used together with a loading cache to bypass loading if not needed.
If `recordStats` is set to `false`, then the get won't increase any metrics
or affect any stats of a bounded cache.

* `cache.peek(key): mixed|null`

Same as `getIfPresent(key, false)`. Will return the cached value, but never
load a value if it does not exist and not affect any metrics or stats of
the cache.

* `cache.has(key): boolean`

Expand Down
6 changes: 5 additions & 1 deletion cache/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ module.exports = class BaseCache {
throwError();
}

getIfPresent(key) {
getIfPresent(key, recordStats=true) {
throwError();
}

peek(key) {
return this.getIfPresent(key, false);
}

delete(key) {
throwError();
}
Expand Down
5 changes: 5 additions & 0 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe('BaseCache', function() {
expect(() => cache.getIfPresent('key')).to.throw();
});

it('peek throws', function() {
const cache = new BaseCache();
expect(() => cache.peek('key')).to.throw();
});

it('delete throws', function() {
const cache = new BaseCache();
expect(() => cache.delete('key')).to.throw();
Expand Down
1 change: 1 addition & 0 deletions test/bounded.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('BoundedCache', function() {

expect(cache.has('key')).to.equal(true);
expect(cache.get('key')).to.equal('value');
expect(cache.peek('key')).to.equal('value');
});

it('Get non-existent value in cache', function() {
Expand Down
10 changes: 10 additions & 0 deletions test/metrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ describe('MetricsCache', function() {
expect(cache.metrics.misses).to.equal(0);
expect(cache.metrics.hitRate).to.equal(1);
});

it('Peek without recording stats', function() {
const cache = newCache();

expect(cache.peek('key')).to.equal(null);

expect(cache.metrics.hits).to.equal(0);
expect(cache.metrics.misses).to.equal(0);
expect(cache.metrics.hitRate).to.equal(1);
});
});

0 comments on commit ff0e9b1

Please sign in to comment.