Skip to content

Commit

Permalink
Adding support for getting keys of cache
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jun 15, 2017
1 parent 6916a70 commit 2458695
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cache/bounded.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ class BoundedCache {
}
}

keys() {
this[EVICT]();
return Array.from(this[DATA].values.keys());
}

[ON_REMOVE](key, value, cause) {
const data = this[DATA];
if(data.removalListener) {
Expand Down
5 changes: 5 additions & 0 deletions cache/boundless.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ class BoundlessCache {
}
}

keys() {
this[EVICT]();
return Array.from(this[DATA].values.keys());
}

[ON_REMOVE](key, value, cause) {
const data = this[DATA];
if(data.removalListener) {
Expand Down
18 changes: 18 additions & 0 deletions test/bounded.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ describe('BoundedCache', function() {
expect(cache.size).to.equal(0);
});

it('Getting keys work', function() {
const cache = new BoundedCache({ maxSize: 50 });
cache.set('key', 'value');

expect(cache.keys()).to.deep.equal([ 'key' ]);
});

describe('Eviction', function() {
it('Does not exceed maxSize', function() {
const maxSize = 10;
Expand Down Expand Up @@ -96,6 +103,17 @@ describe('BoundedCache', function() {
expect(cache.get(2)).to.equal(2);
expect(cache.get(3)).to.equal(3);
});

it('Keys evicted before array returned', function() {
const maxSize = 10;
const cache = new BoundedCache({ maxSize });

for(let i=0; i<maxSize*2; i++) {
cache.set(i, i);
}

expect(cache.keys().length).to.equal(maxSize);
});
});

describe('Removal listeners', function() {
Expand Down
7 changes: 7 additions & 0 deletions test/boundless.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ describe('BoundlessCache', function() {
expect(cache.size).to.equal(0);
});

it('Getting keys work', function() {
const cache = new BoundlessCache({});
cache.set('key', 'value');

expect(cache.keys()).to.deep.equal([ 'key' ]);
});

describe('Removal listeners', function() {
it('Triggers on delete', function() {
const listener = removalListener();
Expand Down
10 changes: 10 additions & 0 deletions test/expiration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ describe('ExpirationCache', function() {
cb();
}, 1080);
});

it('Keys evicted before array returned', function(cb) {
const cache = newCache();
cache.set('key', 'value', { maxAge: '50 ms' });

setTimeout(() => {
expect(cache.keys().length).to.equal(0);
cb();
}, 1080);
});
});

describe('With maxNoReadAge', function() {
Expand Down

0 comments on commit 2458695

Please sign in to comment.