tick-cache 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +422 -0
- package/dist/index.cjs +718 -0
- package/dist/index.d.cts +64 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +691 -0
- package/package.json +70 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
interface TimeSource {
|
|
2
|
+
nowMs(): number;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
type DisposeReason = "ttl" | "lru" | "delete" | "clear" | "set";
|
|
6
|
+
interface Stats {
|
|
7
|
+
size: number;
|
|
8
|
+
}
|
|
9
|
+
interface Options<K extends string | number, V> {
|
|
10
|
+
maxEntries: number;
|
|
11
|
+
tickMs?: number;
|
|
12
|
+
wheelSize?: number;
|
|
13
|
+
updateTTLOnGet?: boolean;
|
|
14
|
+
budgetPerTick?: number;
|
|
15
|
+
/**
|
|
16
|
+
* When true (default), a background interval will automatically process expirations.
|
|
17
|
+
* When false, expirations are only processed during cache operations (get/set/has/delete).
|
|
18
|
+
* Use false to avoid background timers if you access the cache frequently enough.
|
|
19
|
+
*/
|
|
20
|
+
ttlAutopurge?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Optional custom time source (primarily for testing).
|
|
23
|
+
* Defaults to performance.now() if not provided.
|
|
24
|
+
*/
|
|
25
|
+
time?: TimeSource;
|
|
26
|
+
/**
|
|
27
|
+
* Optional callback invoked when an entry is disposed (evicted, deleted, cleared, or replaced).
|
|
28
|
+
*/
|
|
29
|
+
onDispose?: (key: K, value: V, reason: DisposeReason) => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare class TtlWheelCache<K extends string | number, V> {
|
|
33
|
+
private readonly store;
|
|
34
|
+
private readonly wheel;
|
|
35
|
+
private readonly ticker;
|
|
36
|
+
private readonly keyIndex;
|
|
37
|
+
private readonly lru;
|
|
38
|
+
private readonly maxEntries;
|
|
39
|
+
private readonly updateTTLOnGet;
|
|
40
|
+
private readonly ttlAutopurge;
|
|
41
|
+
private readonly tickMs;
|
|
42
|
+
private readonly onDispose?;
|
|
43
|
+
private intervalId?;
|
|
44
|
+
constructor(options: Options<K, V>);
|
|
45
|
+
set(key: K, value: V, ttlMs: number): void;
|
|
46
|
+
get(key: K): V | undefined;
|
|
47
|
+
has(key: K): boolean;
|
|
48
|
+
delete(key: K): boolean;
|
|
49
|
+
clear(): void;
|
|
50
|
+
size(): number;
|
|
51
|
+
stats(): Stats;
|
|
52
|
+
close(): void;
|
|
53
|
+
/**
|
|
54
|
+
* Advance the timer wheel and process expirations.
|
|
55
|
+
* Called automatically in active mode (ttlAutopurge=false).
|
|
56
|
+
*/
|
|
57
|
+
private advanceWheel;
|
|
58
|
+
private removeEntry;
|
|
59
|
+
private onExpireEntry;
|
|
60
|
+
private evictLru;
|
|
61
|
+
private startCleanupInterval;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { type DisposeReason, type Options, type Stats, TtlWheelCache };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
interface TimeSource {
|
|
2
|
+
nowMs(): number;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
type DisposeReason = "ttl" | "lru" | "delete" | "clear" | "set";
|
|
6
|
+
interface Stats {
|
|
7
|
+
size: number;
|
|
8
|
+
}
|
|
9
|
+
interface Options<K extends string | number, V> {
|
|
10
|
+
maxEntries: number;
|
|
11
|
+
tickMs?: number;
|
|
12
|
+
wheelSize?: number;
|
|
13
|
+
updateTTLOnGet?: boolean;
|
|
14
|
+
budgetPerTick?: number;
|
|
15
|
+
/**
|
|
16
|
+
* When true (default), a background interval will automatically process expirations.
|
|
17
|
+
* When false, expirations are only processed during cache operations (get/set/has/delete).
|
|
18
|
+
* Use false to avoid background timers if you access the cache frequently enough.
|
|
19
|
+
*/
|
|
20
|
+
ttlAutopurge?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Optional custom time source (primarily for testing).
|
|
23
|
+
* Defaults to performance.now() if not provided.
|
|
24
|
+
*/
|
|
25
|
+
time?: TimeSource;
|
|
26
|
+
/**
|
|
27
|
+
* Optional callback invoked when an entry is disposed (evicted, deleted, cleared, or replaced).
|
|
28
|
+
*/
|
|
29
|
+
onDispose?: (key: K, value: V, reason: DisposeReason) => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare class TtlWheelCache<K extends string | number, V> {
|
|
33
|
+
private readonly store;
|
|
34
|
+
private readonly wheel;
|
|
35
|
+
private readonly ticker;
|
|
36
|
+
private readonly keyIndex;
|
|
37
|
+
private readonly lru;
|
|
38
|
+
private readonly maxEntries;
|
|
39
|
+
private readonly updateTTLOnGet;
|
|
40
|
+
private readonly ttlAutopurge;
|
|
41
|
+
private readonly tickMs;
|
|
42
|
+
private readonly onDispose?;
|
|
43
|
+
private intervalId?;
|
|
44
|
+
constructor(options: Options<K, V>);
|
|
45
|
+
set(key: K, value: V, ttlMs: number): void;
|
|
46
|
+
get(key: K): V | undefined;
|
|
47
|
+
has(key: K): boolean;
|
|
48
|
+
delete(key: K): boolean;
|
|
49
|
+
clear(): void;
|
|
50
|
+
size(): number;
|
|
51
|
+
stats(): Stats;
|
|
52
|
+
close(): void;
|
|
53
|
+
/**
|
|
54
|
+
* Advance the timer wheel and process expirations.
|
|
55
|
+
* Called automatically in active mode (ttlAutopurge=false).
|
|
56
|
+
*/
|
|
57
|
+
private advanceWheel;
|
|
58
|
+
private removeEntry;
|
|
59
|
+
private onExpireEntry;
|
|
60
|
+
private evictLru;
|
|
61
|
+
private startCleanupInterval;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { type DisposeReason, type Options, type Stats, TtlWheelCache };
|