diff --git a/cache/bounded.js b/cache/bounded.js index 281fc47..e924234 100644 --- a/cache/bounded.js +++ b/cache/bounded.js @@ -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) { diff --git a/cache/boundless.js b/cache/boundless.js index 934d71d..5ffa819 100644 --- a/cache/boundless.js +++ b/cache/boundless.js @@ -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) { diff --git a/test/bounded.test.js b/test/bounded.test.js index 817bbad..38c028d 100644 --- a/test/bounded.test.js +++ b/test/bounded.test.js @@ -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; @@ -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 { + expect(cache.keys().length).to.equal(0); + cb(); + }, 1080); + }); }); describe('With maxNoReadAge', function() {