Skip to content

Commit

Permalink
chore: use staleAt
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel committed Sep 20, 2024
1 parent 8abcad5 commit 29afa2d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ deno add npm:lru.min
```js
import { createLRU } from 'lru.min';

const max = 2;
const staleAt = 300000;
const onEviction = (key, value) => {
console.log(`Key "${key}" with value "${value}" has been evicted.`);
};

const LRU = createLRU({
max,
staleAt,
max: 2,
staleAt: 300000,
onEviction,
});

Expand All @@ -75,7 +73,7 @@ LRU.clear(); // LRU.evict(max)
// => Key "C" with value "Another Value" has been evicted.

LRU.set('D', "You're amazing 💛", {
maxAge: Number.POSITIVE_INFINITY,
staleAt: Number.POSITIVE_INFINITY,
});

LRU.size; // 1
Expand All @@ -97,16 +95,16 @@ LRU.set('E', 'Yet Another Value');
* key: 'E',
* value: 'Yet Another Value',
* maxAge: 300000,
* expiresAt: 299999.90000000596,
* isExpired: false,
* staleAt: 299999.90000000596,
* isStale: false,
* position: 0
* },
* {
* key: 'D',
* value: "You're amazing 💛",
* maxAge: Infinity,
* expiresAt: Infinity,
* isExpired: false,
* staleAt: Infinity,
* isStale: false,
* position: 1
* },
* ]
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const createLRU = <Key, Value>(options: CacheOptions<Key, Value>) => {
key,
value: valList[index],
maxAge: ageItem,
expiresAt: timeRemaining > 0 ? timeRemaining : 0,
staleAt: timeRemaining > 0 ? timeRemaining : 0,
isStale,
position,
};
Expand Down Expand Up @@ -290,7 +290,7 @@ export const createLRU = <Key, Value>(options: CacheOptions<Key, Value>) => {
/** Time in milliseconds. */
maxAge: number;

Check warning on line 291 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L291

Added line #L291 was not covered by tests
/** Time in milliseconds. */
expiresAt: number;
staleAt: number;

Check warning on line 293 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L293

Added line #L293 was not covered by tests
/** When `true`, the next interaction with the key will evict it. */
isStale: boolean;

Check warning on line 295 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L295

Added line #L295 was not covered by tests
/** From the most recent (`0`) to the oldest (`max`). */
Expand Down

0 comments on commit 29afa2d

Please sign in to comment.