Skip to content

Commit

Permalink
clear() added to caches
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jun 15, 2017
1 parent 7dc3cd9 commit 6916a70
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
24 changes: 24 additions & 0 deletions cache/bounded.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,30 @@ class BoundedCache {
return data.values.has(key);
}

clear() {
const data = this[DATA];

const oldValues = data.values;
data.values = new Map();
for(let [key, node] of oldValues) {
this[ON_REMOVE](key, node.value, RemovalCause.EXPLICIT);
}
data.weightedSize = 0;

data.window.head.remove();
data.window.size = 0;

data.probation.head.remove();

data.protected.head.remove();
data.protected.size = 0;

if(data.evictionTimeout) {
clearTimeout(data.evictionTimeout);
data.evictionTimeout = null;
}
}

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

clear() {
const data = this[DATA];
const oldValues = data.values;
data.values = new Map();
for(let [key, value] of oldValues) {
this[ON_REMOVE](key, value, RemovalCause.EXPLICIT);
}
}

[ON_REMOVE](key, value, cause) {
const data = this[DATA];
if(data.removalListener) {
Expand Down
35 changes: 34 additions & 1 deletion test/bounded.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,21 @@ describe('BoundedCache', function() {

cache.delete('key');
expect(cache.weightedSize).to.equal(1);
})
});

it('Clear for empty', function() {
const cache = new BoundedCache({ maxSize: 50 });
cache.clear();
expect(cache.size).to.equal(0);
});

it('Clear for single', function() {
const cache = new BoundedCache({ maxSize: 50 });
cache.set('key', 'value');

cache.clear();
expect(cache.size).to.equal(0);
});

describe('Eviction', function() {
it('Does not exceed maxSize', function() {
Expand Down Expand Up @@ -151,6 +165,25 @@ describe('BoundedCache', function() {
reason: RemovalCause.SIZE
});
});

it('Triggers on clear', function() {
const listener = removalListener();
const cache = new BoundedCache({
maxSize: 10,
removalListener: listener
});

cache.set('one', 1234);
cache.__await();
expect(listener.removed).to.equal(null);

cache.clear();
expect(listener.removed).to.deep.equal({
key: 'one',
value: 1234,
reason: RemovalCause.EXPLICIT
});
});
});

describe('Weighted', function() {
Expand Down
31 changes: 31 additions & 0 deletions test/boundless.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ describe('BoundlessCache', function() {
expect(cache.get('key')).to.equal(null);
});

it('Clear for empty', function() {
const cache = new BoundlessCache({});
cache.clear();
expect(cache.size).to.equal(0);
});

it('Clear for single', function() {
const cache = new BoundlessCache({});
cache.set('key', 'value');

cache.clear();
expect(cache.size).to.equal(0);
});

describe('Removal listeners', function() {
it('Triggers on delete', function() {
const listener = removalListener();
Expand Down Expand Up @@ -64,6 +78,23 @@ describe('BoundlessCache', function() {
reason: RemovalCause.REPLACED
});
});

it('Triggers on clear', function() {
const listener = removalListener();
const cache = new BoundlessCache({
removalListener: listener
});

cache.set('one', 1234);
expect(listener.removed).to.equal(null);

cache.clear();
expect(listener.removed).to.deep.equal({
key: 'one',
value: 1234,
reason: RemovalCause.EXPLICIT
});
});
})
});

Expand Down

0 comments on commit 6916a70

Please sign in to comment.