Skip to content
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
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ export default class QuickLRU<KeyType, ValueType> extends Map<KeyType, ValueType
*/
get maxSize(): number;

/**
The set max age.
*/
get maxAge(): number;

/**
Iterable for all the keys.
*/
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ export default class QuickLRU extends Map {
return this.#maxSize;
}

get maxAge() {
return this.#maxAge;
}

entries() {
return this.entriesAscending();
}
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ expectType<number | undefined>(lru.peek('🦄'));
expectType<number | undefined>(lru.expiresIn('🦄'));
expectType<boolean>(lru.delete('🦄'));
expectType<number>(lru.size);
expectType<number>(lru.maxAge);

for (const [key, value] of lru) {
expectType<string>(key);
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ The stored item count.

The set max size.

#### .maxAge *(getter)*

The set max age.

## Algorithm

This library implements a variant of the [hashlru algorithm](https://github.com/dominictarr/hashlru#algorithm) using JavaScript's `Map` for broader key type support.
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ test('.maxSize', t => {
t.is(lru.maxSize, maxSize);
});

test('.maxAge', t => {
const lru = new QuickLRU({maxSize: 1});
t.is(lru.maxAge, Number.POSITIVE_INFINITY);
});

test('checks total cache size does not exceed `maxSize`', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
Expand Down Expand Up @@ -293,6 +298,12 @@ test('set(maxAge): setting the same key refreshes expiration', async t => {
t.true(lru.has('1'));
});

test('set(maxAge): is returned by getter', t => {
const maxAge = 100;
const lru = new QuickLRU({maxSize: 1, maxAge});
t.is(lru.maxAge, maxAge);
});

test('maxAge: get() removes an expired item', async t => {
const lru = new QuickLRU({maxSize: 10, maxAge: 90});
lru.set('1', 'test');
Expand Down