Skip to content

Commit

Permalink
Add onEviction option (sindresorhus#20)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
please-rewrite and sindresorhus authored Apr 6, 2020
1 parent b0feed4 commit b941c2f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ declare namespace QuickLRU {
The maximum number of items before evicting the least recently used items.
*/
readonly maxSize: number;

/**
Called right before an item is evicted from the cache.
Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
*/
onEviction?: <KeyType, ValueType>(key: KeyType, value: ValueType) => void;
}
}

Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class QuickLRU {
}

this.maxSize = options.maxSize;
this.onEviction = options.onEviction;
this.cache = new Map();
this.oldCache = new Map();
this._size = 0;
Expand All @@ -18,6 +19,13 @@ class QuickLRU {

if (this._size >= this.maxSize) {
this._size = 0;

if (typeof this.onEviction === 'function') {
for (const [key, value] of this.oldCache.entries()) {
this.onEviction(key, value);
}
}

this.oldCache = this.cache;
this.cache = new Map();
}
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ Type: `number`

The maximum number of items before evicting the least recently used items.

#### onEviction

*Optional*\
Type: `(key, value) => void`

Called right before an item is evicted from the cache.

Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).

### Instance

The instance is [`iterable`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols) so you can use it directly in a [`for…of`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of) loop.
Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,24 @@ test('checks total cache size does not exceed `maxSize`', t => {
lru.get('1');
t.is(lru.oldCache.has('1'), false);
});

test('`onEviction` option method is called after `maxSize` is exceeded', t => {
const expectKey = '1';
const expectValue = 1;
let isCalled = false;
let actualKey;
let actualValue;

const onEviction = (key, value) => {
actualKey = key;
actualValue = value;
isCalled = true;
};

const lru = new QuickLRU({maxSize: 1, onEviction});
lru.set(expectKey, expectValue);
lru.set('2', 2);
t.is(actualKey, expectKey);
t.is(actualValue, expectValue);
t.true(isCalled);
});

0 comments on commit b941c2f

Please sign in to comment.