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 a27cd8d commit 8abcad5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ deno add npm:lru.min
import { createLRU } from 'lru.min';

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

const LRU = createLRU({
max,
stale,
staleAt,
onEviction,
});

Expand Down
23 changes: 12 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export type CacheOptions<Key = unknown, Value = unknown> = {
*
* @default undefined
*/
stale?: number;
staleAt?: number;
/**
* When `true` and `stale` is set, items remain stale based on their initial expiration.
* When `true` and `staleAt` is set, items remain staleAt based on their initial expiration.
*
* @default undefined
*/
Expand All @@ -22,16 +22,16 @@ export type CacheOptions<Key = unknown, Value = unknown> = {
};

export const createLRU = <Key, Value>(options: CacheOptions<Key, Value>) => {
let { max, onEviction, stale, keepStale } = options;
let { max, onEviction, staleAt, keepStale } = options;

if (!(Number.isInteger(max) && max > 0))
throw new TypeError('`max` must be a positive integer');

if (
(typeof stale !== 'undefined' && typeof stale !== 'number') ||
(typeof stale === 'number' && stale <= 0)
(typeof staleAt !== 'undefined' && typeof staleAt !== 'number') ||
(typeof staleAt === 'number' && staleAt <= 0)
)
throw new TypeError('`stale` must be a positive number');
throw new TypeError('`staleAt` must be a positive number');

Check warning on line 34 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L34

Added line #L34 was not covered by tests

const Age = (() => {
try {
Expand Down Expand Up @@ -169,7 +169,7 @@ export const createLRU = <Key, Value>(options: CacheOptions<Key, Value>) => {

return {
/** Adds a key-value pair to the cache. Updates the value if the key already exists. */
set(key: Key, value: Value, options?: { stale?: number }): undefined {
set(key: Key, value: Value, options?: { staleAt?: number }): undefined {
if (key === undefined) return;

let index = keyMap.get(key);
Expand All @@ -183,14 +183,15 @@ export const createLRU = <Key, Value>(options: CacheOptions<Key, Value>) => {

valList[index] = value;

const keyMaxAge = options?.stale !== undefined ? options.stale : stale;
const keyMaxAge =
options?.staleAt !== undefined ? options.staleAt : staleAt;

if (keyMaxAge !== undefined) {
if (
(typeof stale !== 'undefined' && typeof stale !== 'number') ||
(typeof stale === 'number' && stale <= 0)
(typeof staleAt !== 'undefined' && typeof staleAt !== 'number') ||
(typeof staleAt === 'number' && staleAt <= 0)
)
throw new TypeError('`stale` must be a positive number');
throw new TypeError('`staleAt` must be a positive number');

Check warning on line 194 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L190-L194

Added lines #L190 - L194 were not covered by tests

expList[index] = Age.now() + keyMaxAge;
ageList[index] = keyMaxAge;

Check warning on line 197 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L196-L197

Added lines #L196 - L197 were not covered by tests
Expand Down

0 comments on commit 8abcad5

Please sign in to comment.