8000 Add `onEviction` option by please-rewrite · Pull Request #20 · sindresorhus/quick-lru · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add onEviction option #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.>
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. '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 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