layercache 1.2.0 → 1.2.1
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/README.md +71 -5
- package/dist/chunk-46UH7LNM.js +312 -0
- package/dist/{chunk-BWM4MU2X.js → chunk-GF47Y3XR.js} +13 -38
- package/dist/chunk-ZMDB5KOK.js +159 -0
- package/dist/cli.cjs +121 -21
- package/dist/cli.js +57 -2
- package/dist/edge-C1sBhTfv.d.cts +667 -0
- package/dist/edge-C1sBhTfv.d.ts +667 -0
- package/dist/edge.cjs +399 -0
- package/dist/edge.d.cts +2 -0
- package/dist/edge.d.ts +2 -0
- package/dist/edge.js +14 -0
- package/dist/index.cjs +969 -195
- package/dist/index.d.cts +43 -567
- package/dist/index.d.ts +43 -567
- package/dist/index.js +849 -496
- package/package.json +7 -2
- package/packages/nestjs/dist/index.cjs +913 -375
- package/packages/nestjs/dist/index.d.cts +75 -0
- package/packages/nestjs/dist/index.d.ts +75 -0
- package/packages/nestjs/dist/index.js +901 -373
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Thrown by `CacheStack.getOrThrow()` when no value is found for the given key
|
|
5
|
+
* (fetcher returned null or no fetcher was provided and the key is absent).
|
|
6
|
+
*/
|
|
7
|
+
declare class CacheMissError extends Error {
|
|
8
|
+
readonly key: string;
|
|
9
|
+
constructor(key: string);
|
|
10
|
+
}
|
|
11
|
+
interface LayerTtlMap {
|
|
12
|
+
[layerName: string]: number | undefined;
|
|
13
|
+
}
|
|
14
|
+
interface CacheWriteOptions {
|
|
15
|
+
tags?: string[];
|
|
16
|
+
ttl?: number | LayerTtlMap;
|
|
17
|
+
ttlPolicy?: CacheTtlPolicy;
|
|
18
|
+
negativeCache?: boolean;
|
|
19
|
+
negativeTtl?: number | LayerTtlMap;
|
|
20
|
+
staleWhileRevalidate?: number | LayerTtlMap;
|
|
21
|
+
staleIfError?: number | LayerTtlMap;
|
|
22
|
+
ttlJitter?: number | LayerTtlMap;
|
|
23
|
+
slidingTtl?: boolean;
|
|
24
|
+
refreshAhead?: number | LayerTtlMap;
|
|
25
|
+
adaptiveTtl?: boolean | CacheAdaptiveTtlOptions;
|
|
26
|
+
circuitBreaker?: CacheCircuitBreakerOptions;
|
|
27
|
+
fetcherRateLimit?: CacheRateLimitOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Optional predicate called with the fetcher's return value before caching.
|
|
30
|
+
* Return `false` to skip storing the value in the cache (but still return it
|
|
31
|
+
* to the caller). Useful for not caching failed API responses or empty results.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* cache.get('key', fetchData, { shouldCache: (v) => v.status === 200 })
|
|
35
|
+
*/
|
|
36
|
+
shouldCache?: (value: unknown) => boolean;
|
|
37
|
+
}
|
|
38
|
+
interface CacheGetOptions extends CacheWriteOptions {
|
|
39
|
+
}
|
|
40
|
+
interface CacheMGetEntry<T> {
|
|
41
|
+
key: string;
|
|
42
|
+
fetch?: () => Promise<T>;
|
|
43
|
+
options?: CacheGetOptions;
|
|
44
|
+
}
|
|
45
|
+
interface CacheMSetEntry<T> {
|
|
46
|
+
key: string;
|
|
47
|
+
value: T;
|
|
48
|
+
options?: CacheWriteOptions;
|
|
49
|
+
}
|
|
50
|
+
/** Interface that all cache backend implementations must satisfy. */
|
|
51
|
+
interface CacheLayer {
|
|
52
|
+
readonly name: string;
|
|
53
|
+
readonly defaultTtl?: number;
|
|
54
|
+
readonly isLocal?: boolean;
|
|
55
|
+
get<T>(key: string): Promise<T | null>;
|
|
56
|
+
getEntry?<T = unknown>(key: string): Promise<T | null>;
|
|
57
|
+
/**
|
|
58
|
+
* Bulk read fast-path. Implementations should return raw stored entries using
|
|
59
|
+
* the same semantics as `getEntry()` so CacheStack can resolve envelopes,
|
|
60
|
+
* stale windows, and negative-cache markers consistently.
|
|
61
|
+
*/
|
|
62
|
+
getMany?<T>(keys: string[]): Promise<Array<T | null>>;
|
|
63
|
+
setMany?(entries: CacheLayerSetManyEntry[]): Promise<void>;
|
|
64
|
+
set(key: string, value: unknown, ttl?: number): Promise<void>;
|
|
65
|
+
delete(key: string): Promise<void>;
|
|
66
|
+
clear(): Promise<void>;
|
|
67
|
+
deleteMany?(keys: string[]): Promise<void>;
|
|
68
|
+
keys?(): Promise<string[]>;
|
|
69
|
+
ping?(): Promise<boolean>;
|
|
70
|
+
dispose?(): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Returns true if the key exists and has not expired.
|
|
73
|
+
* Implementations may omit this; CacheStack will fall back to `get()`.
|
|
74
|
+
*/
|
|
75
|
+
has?(key: string): Promise<boolean>;
|
|
76
|
+
/**
|
|
77
|
+
* Returns the remaining TTL in seconds for the key, or null if the key
|
|
78
|
+
* does not exist, has no TTL, or has already expired.
|
|
79
|
+
* Implementations may omit this.
|
|
80
|
+
*/
|
|
81
|
+
ttl?(key: string): Promise<number | null>;
|
|
82
|
+
/**
|
|
83
|
+
* Returns the number of entries currently held by this layer.
|
|
84
|
+
* Implementations may omit this.
|
|
85
|
+
*/
|
|
86
|
+
size?(): Promise<number>;
|
|
87
|
+
}
|
|
88
|
+
interface CacheSerializer {
|
|
89
|
+
serialize(value: unknown): string | Buffer;
|
|
90
|
+
deserialize<T>(payload: string | Buffer): T;
|
|
91
|
+
}
|
|
92
|
+
/** Per-layer latency statistics (rolling window of sampled read durations). */
|
|
93
|
+
interface CacheLayerLatency {
|
|
94
|
+
/** Average read latency in milliseconds. */
|
|
95
|
+
avgMs: number;
|
|
96
|
+
/** Maximum observed read latency in milliseconds. */
|
|
97
|
+
maxMs: number;
|
|
98
|
+
/** Number of samples used to compute the statistics. */
|
|
99
|
+
count: number;
|
|
100
|
+
}
|
|
101
|
+
/** Snapshot of cumulative cache counters. */
|
|
102
|
+
interface CacheMetricsSnapshot {
|
|
103
|
+
hits: number;
|
|
104
|
+
misses: number;
|
|
105
|
+
fetches: number;
|
|
106
|
+
sets: number;
|
|
107
|
+
deletes: number;
|
|
108
|
+
backfills: number;
|
|
109
|
+
invalidations: number;
|
|
110
|
+
staleHits: number;
|
|
111
|
+
refreshes: number;
|
|
112
|
+
refreshErrors: number;
|
|
113
|
+
writeFailures: number;
|
|
114
|
+
singleFlightWaits: number;
|
|
115
|
+
negativeCacheHits: number;
|
|
116
|
+
circuitBreakerTrips: number;
|
|
117
|
+
degradedOperations: number;
|
|
118
|
+
hitsByLayer: Record<string, number>;
|
|
119
|
+
missesByLayer: Record<string, number>;
|
|
120
|
+
/** Per-layer read latency statistics (sampled from successful reads). */
|
|
121
|
+
latencyByLayer: Record<string, CacheLayerLatency>;
|
|
122
|
+
/** Timestamp (ms since epoch) when metrics were last reset. */
|
|
123
|
+
resetAt: number;
|
|
124
|
+
}
|
|
125
|
+
/** Computed hit-rate statistics derived from CacheMetricsSnapshot. */
|
|
126
|
+
interface CacheHitRateSnapshot {
|
|
127
|
+
/** Overall hit rate across all layers (0–1). */
|
|
128
|
+
overall: number;
|
|
129
|
+
/** Per-layer hit rates (0–1 each). */
|
|
130
|
+
byLayer: Record<string, number>;
|
|
131
|
+
}
|
|
132
|
+
interface CacheLogger {
|
|
133
|
+
debug?(message: string, context?: Record<string, unknown>): void;
|
|
134
|
+
info?(message: string, context?: Record<string, unknown>): void;
|
|
135
|
+
warn?(message: string, context?: Record<string, unknown>): void;
|
|
136
|
+
error?(message: string, context?: Record<string, unknown>): void;
|
|
137
|
+
}
|
|
138
|
+
interface CacheTagIndex {
|
|
139
|
+
touch(key: string): Promise<void>;
|
|
140
|
+
track(key: string, tags: string[]): Promise<void>;
|
|
141
|
+
remove(key: string): Promise<void>;
|
|
142
|
+
keysForTag(tag: string): Promise<string[]>;
|
|
143
|
+
keysForPrefix?(prefix: string): Promise<string[]>;
|
|
144
|
+
/** Returns the tags associated with a specific key, or an empty array. */
|
|
145
|
+
tagsForKey?(key: string): Promise<string[]>;
|
|
146
|
+
matchPattern(pattern: string): Promise<string[]>;
|
|
147
|
+
clear(): Promise<void>;
|
|
148
|
+
}
|
|
149
|
+
interface CacheLayerSetManyEntry {
|
|
150
|
+
key: string;
|
|
151
|
+
value: unknown;
|
|
152
|
+
ttl?: number;
|
|
153
|
+
}
|
|
154
|
+
interface InvalidationMessage {
|
|
155
|
+
scope: 'key' | 'keys' | 'clear';
|
|
156
|
+
sourceId: string;
|
|
157
|
+
keys?: string[];
|
|
158
|
+
operation?: 'write' | 'delete' | 'invalidate' | 'clear';
|
|
159
|
+
}
|
|
160
|
+
interface InvalidationBus {
|
|
161
|
+
subscribe(handler: (message: InvalidationMessage) => Promise<void> | void): Promise<() => Promise<void> | void>;
|
|
162
|
+
publish(message: InvalidationMessage): Promise<void>;
|
|
163
|
+
}
|
|
164
|
+
interface CacheSingleFlightExecutionOptions {
|
|
165
|
+
leaseMs: number;
|
|
166
|
+
waitTimeoutMs: number;
|
|
167
|
+
pollIntervalMs: number;
|
|
168
|
+
}
|
|
169
|
+
interface CacheSingleFlightCoordinator {
|
|
170
|
+
execute<T>(key: string, options: CacheSingleFlightExecutionOptions, worker: () => Promise<T>, waiter: () => Promise<T>): Promise<T>;
|
|
171
|
+
}
|
|
172
|
+
interface CacheStackOptions {
|
|
173
|
+
logger?: CacheLogger | boolean;
|
|
174
|
+
metrics?: boolean;
|
|
175
|
+
stampedePrevention?: boolean;
|
|
176
|
+
invalidationBus?: InvalidationBus;
|
|
177
|
+
tagIndex?: CacheTagIndex;
|
|
178
|
+
generation?: number;
|
|
179
|
+
broadcastL1Invalidation?: boolean;
|
|
180
|
+
/**
|
|
181
|
+
* @deprecated Use `broadcastL1Invalidation` instead.
|
|
182
|
+
*/
|
|
183
|
+
publishSetInvalidation?: boolean;
|
|
184
|
+
negativeCaching?: boolean;
|
|
185
|
+
negativeTtl?: number | LayerTtlMap;
|
|
186
|
+
staleWhileRevalidate?: number | LayerTtlMap;
|
|
187
|
+
staleIfError?: number | LayerTtlMap;
|
|
188
|
+
ttlJitter?: number | LayerTtlMap;
|
|
189
|
+
refreshAhead?: number | LayerTtlMap;
|
|
190
|
+
adaptiveTtl?: boolean | CacheAdaptiveTtlOptions;
|
|
191
|
+
circuitBreaker?: CacheCircuitBreakerOptions;
|
|
192
|
+
gracefulDegradation?: boolean | CacheDegradationOptions;
|
|
193
|
+
writePolicy?: 'strict' | 'best-effort';
|
|
194
|
+
writeStrategy?: 'write-through' | 'write-behind';
|
|
195
|
+
writeBehind?: CacheWriteBehindOptions;
|
|
196
|
+
fetcherRateLimit?: CacheRateLimitOptions;
|
|
197
|
+
singleFlightCoordinator?: CacheSingleFlightCoordinator;
|
|
198
|
+
singleFlightLeaseMs?: number;
|
|
199
|
+
singleFlightTimeoutMs?: number;
|
|
200
|
+
singleFlightPollMs?: number;
|
|
201
|
+
/**
|
|
202
|
+
* Maximum number of entries in `accessProfiles` and `circuitBreakers` maps
|
|
203
|
+
* before the oldest entries are pruned. Prevents unbounded memory growth.
|
|
204
|
+
* Defaults to 100 000.
|
|
205
|
+
*/
|
|
206
|
+
maxProfileEntries?: number;
|
|
207
|
+
}
|
|
208
|
+
interface CacheAdaptiveTtlOptions {
|
|
209
|
+
hotAfter?: number;
|
|
210
|
+
step?: number | LayerTtlMap;
|
|
211
|
+
maxTtl?: number | LayerTtlMap;
|
|
212
|
+
}
|
|
213
|
+
type CacheTtlPolicy = 'until-midnight' | 'next-hour' | {
|
|
214
|
+
alignTo: number;
|
|
215
|
+
} | ((context: CacheTtlPolicyContext) => number | undefined);
|
|
216
|
+
interface CacheTtlPolicyContext {
|
|
217
|
+
key: string;
|
|
218
|
+
value: unknown;
|
|
219
|
+
}
|
|
220
|
+
interface CacheCircuitBreakerOptions {
|
|
221
|
+
failureThreshold?: number;
|
|
222
|
+
cooldownMs?: number;
|
|
223
|
+
}
|
|
224
|
+
interface CacheDegradationOptions {
|
|
225
|
+
retryAfterMs?: number;
|
|
226
|
+
}
|
|
227
|
+
interface CacheRateLimitOptions {
|
|
228
|
+
maxConcurrent?: number;
|
|
229
|
+
intervalMs?: number;
|
|
230
|
+
maxPerInterval?: number;
|
|
231
|
+
}
|
|
232
|
+
interface CacheWriteBehindOptions {
|
|
233
|
+
flushIntervalMs?: number;
|
|
234
|
+
batchSize?: number;
|
|
235
|
+
maxQueueSize?: number;
|
|
236
|
+
}
|
|
237
|
+
interface CacheWarmEntry<T = unknown> {
|
|
238
|
+
key: string;
|
|
239
|
+
fetcher: () => Promise<T>;
|
|
240
|
+
options?: CacheGetOptions;
|
|
241
|
+
priority?: number;
|
|
242
|
+
}
|
|
243
|
+
/** Options controlling the cache warm-up process. */
|
|
244
|
+
interface CacheWarmOptions {
|
|
245
|
+
concurrency?: number;
|
|
246
|
+
continueOnError?: boolean;
|
|
247
|
+
/** Called after each entry is processed (success or failure). */
|
|
248
|
+
onProgress?: (progress: CacheWarmProgress) => void;
|
|
249
|
+
}
|
|
250
|
+
/** Progress information delivered to `CacheWarmOptions.onProgress`. */
|
|
251
|
+
interface CacheWarmProgress {
|
|
252
|
+
completed: number;
|
|
253
|
+
total: number;
|
|
254
|
+
key: string;
|
|
255
|
+
success: boolean;
|
|
256
|
+
}
|
|
257
|
+
interface CacheWrapOptions<TArgs extends unknown[] = unknown[]> extends CacheGetOptions {
|
|
258
|
+
keyResolver?: (...args: TArgs) => string;
|
|
259
|
+
}
|
|
260
|
+
interface CacheSnapshotEntry {
|
|
261
|
+
key: string;
|
|
262
|
+
value: unknown;
|
|
263
|
+
ttl?: number;
|
|
264
|
+
}
|
|
265
|
+
interface CacheStatsSnapshot {
|
|
266
|
+
metrics: CacheMetricsSnapshot;
|
|
267
|
+
layers: Array<{
|
|
268
|
+
name: string;
|
|
269
|
+
isLocal: boolean;
|
|
270
|
+
degradedUntil: number | null;
|
|
271
|
+
}>;
|
|
272
|
+
backgroundRefreshes: number;
|
|
273
|
+
}
|
|
274
|
+
interface CacheHealthCheckResult {
|
|
275
|
+
layer: string;
|
|
276
|
+
healthy: boolean;
|
|
277
|
+
latencyMs: number;
|
|
278
|
+
error?: string;
|
|
279
|
+
}
|
|
280
|
+
/** Detailed inspection result for a single cache key. */
|
|
281
|
+
interface CacheInspectResult {
|
|
282
|
+
key: string;
|
|
283
|
+
/** Layers in which the key is currently stored (not expired). */
|
|
284
|
+
foundInLayers: string[];
|
|
285
|
+
/** Remaining fresh TTL in seconds, or null if no expiry or not an envelope. */
|
|
286
|
+
freshTtlSeconds: number | null;
|
|
287
|
+
/** Remaining stale-while-revalidate window in seconds, or null. */
|
|
288
|
+
staleTtlSeconds: number | null;
|
|
289
|
+
/** Remaining stale-if-error window in seconds, or null. */
|
|
290
|
+
errorTtlSeconds: number | null;
|
|
291
|
+
/** Whether the key is currently serving stale-while-revalidate. */
|
|
292
|
+
isStale: boolean;
|
|
293
|
+
/** Tags associated with this key (from the TagIndex). */
|
|
294
|
+
tags: string[];
|
|
295
|
+
}
|
|
296
|
+
/** All events emitted by CacheStack and their payload shapes. */
|
|
297
|
+
interface CacheStackEvents {
|
|
298
|
+
/** Fired on every cache hit. */
|
|
299
|
+
hit: {
|
|
300
|
+
key: string;
|
|
301
|
+
layer: string;
|
|
302
|
+
state: 'fresh' | 'stale-while-revalidate' | 'stale-if-error';
|
|
303
|
+
};
|
|
304
|
+
/** Fired on every cache miss before the fetcher runs. */
|
|
305
|
+
miss: {
|
|
306
|
+
key: string;
|
|
307
|
+
mode: string;
|
|
308
|
+
};
|
|
309
|
+
/** Fired after a value is stored in the cache. */
|
|
310
|
+
set: {
|
|
311
|
+
key: string;
|
|
312
|
+
kind: string;
|
|
313
|
+
tags?: string[];
|
|
314
|
+
};
|
|
315
|
+
/** Fired after one or more keys are deleted. */
|
|
316
|
+
delete: {
|
|
317
|
+
keys: string[];
|
|
318
|
+
};
|
|
319
|
+
/** Fired when a value is backfilled into a faster layer. */
|
|
320
|
+
backfill: {
|
|
321
|
+
key: string;
|
|
322
|
+
layer: string;
|
|
323
|
+
};
|
|
324
|
+
/** Fired when a stale value is returned to the caller. */
|
|
325
|
+
'stale-serve': {
|
|
326
|
+
key: string;
|
|
327
|
+
state: string;
|
|
328
|
+
layer: string;
|
|
329
|
+
};
|
|
330
|
+
/** Fired when a duplicate request is deduplicated in stampede prevention. */
|
|
331
|
+
'stampede-dedupe': {
|
|
332
|
+
key: string;
|
|
333
|
+
};
|
|
334
|
+
/** Fired after a key is successfully warmed. */
|
|
335
|
+
warm: {
|
|
336
|
+
key: string;
|
|
337
|
+
};
|
|
338
|
+
/** Fired when an error occurs (layer failure, circuit breaker, etc.). */
|
|
339
|
+
error: {
|
|
340
|
+
operation: string;
|
|
341
|
+
[key: string]: unknown;
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
interface MemoryLayerSnapshotEntry {
|
|
346
|
+
key: string;
|
|
347
|
+
value: unknown;
|
|
348
|
+
expiresAt: number | null;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Eviction policy applied when `maxSize` is reached.
|
|
352
|
+
* - `lru` (default): evicts the Least Recently Used entry.
|
|
353
|
+
* - `lfu`: evicts the Least Frequently Used entry.
|
|
354
|
+
* - `fifo`: evicts the oldest inserted entry.
|
|
355
|
+
*/
|
|
356
|
+
type EvictionPolicy = 'lru' | 'lfu' | 'fifo';
|
|
357
|
+
interface MemoryLayerOptions {
|
|
358
|
+
ttl?: number;
|
|
359
|
+
maxSize?: number;
|
|
360
|
+
name?: string;
|
|
361
|
+
evictionPolicy?: EvictionPolicy;
|
|
362
|
+
cleanupIntervalMs?: number;
|
|
363
|
+
onEvict?: (key: string, value: unknown) => void;
|
|
364
|
+
}
|
|
365
|
+
declare class MemoryLayer implements CacheLayer {
|
|
366
|
+
readonly name: string;
|
|
367
|
+
readonly defaultTtl?: number;
|
|
368
|
+
readonly isLocal = true;
|
|
369
|
+
private readonly maxSize;
|
|
370
|
+
private readonly evictionPolicy;
|
|
371
|
+
private readonly onEvict?;
|
|
372
|
+
private readonly entries;
|
|
373
|
+
private cleanupTimer?;
|
|
374
|
+
constructor(options?: MemoryLayerOptions);
|
|
375
|
+
get<T>(key: string): Promise<T | null>;
|
|
376
|
+
getEntry<T = unknown>(key: string): Promise<T | null>;
|
|
377
|
+
getMany<T>(keys: string[]): Promise<Array<T | null>>;
|
|
378
|
+
setMany(entries: CacheLayerSetManyEntry[]): Promise<void>;
|
|
379
|
+
set(key: string, value: unknown, ttl?: number | undefined): Promise<void>;
|
|
380
|
+
has(key: string): Promise<boolean>;
|
|
381
|
+
ttl(key: string): Promise<number | null>;
|
|
382
|
+
size(): Promise<number>;
|
|
383
|
+
delete(key: string): Promise<void>;
|
|
384
|
+
deleteMany(keys: string[]): Promise<void>;
|
|
385
|
+
clear(): Promise<void>;
|
|
386
|
+
ping(): Promise<boolean>;
|
|
387
|
+
dispose(): Promise<void>;
|
|
388
|
+
keys(): Promise<string[]>;
|
|
389
|
+
exportState(): MemoryLayerSnapshotEntry[];
|
|
390
|
+
importState(entries: MemoryLayerSnapshotEntry[]): void;
|
|
391
|
+
private evict;
|
|
392
|
+
private pruneExpired;
|
|
393
|
+
private isExpired;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
declare class PatternMatcher {
|
|
397
|
+
/**
|
|
398
|
+
* Tests whether a glob-style pattern matches a value.
|
|
399
|
+
* Supports `*` (any sequence of characters) and `?` (any single character).
|
|
400
|
+
* Uses a two-pointer algorithm to avoid ReDoS vulnerabilities and
|
|
401
|
+
* quadratic memory usage on long patterns/keys.
|
|
402
|
+
*/
|
|
403
|
+
static matches(pattern: string, value: string): boolean;
|
|
404
|
+
/**
|
|
405
|
+
* Linear-time glob matching with O(1) extra memory.
|
|
406
|
+
*/
|
|
407
|
+
private static matchLinear;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
interface TagIndexOptions {
|
|
411
|
+
/**
|
|
412
|
+
* Maximum number of keys tracked in `knownKeys`. When exceeded, the oldest
|
|
413
|
+
* 10 % of keys are pruned to keep memory bounded.
|
|
414
|
+
* Defaults to unlimited.
|
|
415
|
+
*/
|
|
416
|
+
maxKnownKeys?: number;
|
|
417
|
+
}
|
|
418
|
+
declare class TagIndex implements CacheTagIndex {
|
|
419
|
+
private readonly tagToKeys;
|
|
420
|
+
private readonly keyToTags;
|
|
421
|
+
private readonly knownKeys;
|
|
422
|
+
private readonly maxKnownKeys;
|
|
423
|
+
constructor(options?: TagIndexOptions);
|
|
424
|
+
touch(key: string): Promise<void>;
|
|
425
|
+
track(key: string, tags: string[]): Promise<void>;
|
|
426
|
+
remove(key: string): Promise<void>;
|
|
427
|
+
keysForTag(tag: string): Promise<string[]>;
|
|
428
|
+
keysForPrefix(prefix: string): Promise<string[]>;
|
|
429
|
+
tagsForKey(key: string): Promise<string[]>;
|
|
430
|
+
matchPattern(pattern: string): Promise<string[]>;
|
|
431
|
+
clear(): Promise<void>;
|
|
432
|
+
private pruneKnownKeysIfNeeded;
|
|
433
|
+
private removeKey;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
declare class CacheNamespace {
|
|
437
|
+
private readonly cache;
|
|
438
|
+
private readonly prefix;
|
|
439
|
+
private static readonly metricsMutexes;
|
|
440
|
+
private metrics;
|
|
441
|
+
constructor(cache: CacheStack, prefix: string);
|
|
442
|
+
get<T>(key: string, fetcher?: () => Promise<T>, options?: CacheGetOptions): Promise<T | null>;
|
|
443
|
+
getOrSet<T>(key: string, fetcher: () => Promise<T>, options?: CacheGetOptions): Promise<T | null>;
|
|
444
|
+
/**
|
|
445
|
+
* Like `get()`, but throws `CacheMissError` instead of returning `null`.
|
|
446
|
+
*/
|
|
447
|
+
getOrThrow<T>(key: string, fetcher?: () => Promise<T>, options?: CacheGetOptions): Promise<T>;
|
|
448
|
+
has(key: string): Promise<boolean>;
|
|
449
|
+
ttl(key: string): Promise<number | null>;
|
|
450
|
+
set<T>(key: string, value: T, options?: CacheWriteOptions): Promise<void>;
|
|
451
|
+
delete(key: string): Promise<void>;
|
|
452
|
+
mdelete(keys: string[]): Promise<void>;
|
|
453
|
+
clear(): Promise<void>;
|
|
454
|
+
mget<T>(entries: CacheMGetEntry<T>[]): Promise<Array<T | null>>;
|
|
455
|
+
mset<T>(entries: CacheMSetEntry<T>[]): Promise<void>;
|
|
456
|
+
invalidateByTag(tag: string): Promise<void>;
|
|
457
|
+
invalidateByTags(tags: string[], mode?: 'any' | 'all'): Promise<void>;
|
|
458
|
+
invalidateByPattern(pattern: string): Promise<void>;
|
|
459
|
+
invalidateByPrefix(prefix: string): Promise<void>;
|
|
460
|
+
/**
|
|
461
|
+
* Returns detailed metadata about a single cache key within this namespace.
|
|
462
|
+
*/
|
|
463
|
+
inspect(key: string): Promise<CacheInspectResult | null>;
|
|
464
|
+
wrap<TArgs extends unknown[], TResult>(keyPrefix: string, fetcher: (...args: TArgs) => Promise<TResult>, options?: CacheWrapOptions<TArgs>): (...args: TArgs) => Promise<TResult | null>;
|
|
465
|
+
warm(entries: CacheWarmEntry[], options?: CacheWarmOptions): Promise<void>;
|
|
466
|
+
getMetrics(): CacheMetricsSnapshot;
|
|
467
|
+
getHitRate(): CacheHitRateSnapshot;
|
|
468
|
+
/**
|
|
469
|
+
* Creates a nested namespace. Keys are prefixed with `parentPrefix:childPrefix:`.
|
|
470
|
+
*
|
|
471
|
+
* ```ts
|
|
472
|
+
* const tenant = cache.namespace('tenant:abc')
|
|
473
|
+
* const posts = tenant.namespace('posts')
|
|
474
|
+
* // keys become: "tenant:abc:posts:mykey"
|
|
475
|
+
* ```
|
|
476
|
+
*/
|
|
477
|
+
namespace(childPrefix: string): CacheNamespace;
|
|
478
|
+
qualify(key: string): string;
|
|
479
|
+
private trackMetrics;
|
|
480
|
+
private getMetricsMutex;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** Typed overloads for EventEmitter so callers get autocomplete on event names. */
|
|
484
|
+
interface CacheStack {
|
|
485
|
+
on<K extends keyof CacheStackEvents>(event: K, listener: (data: CacheStackEvents[K]) => void): this;
|
|
486
|
+
once<K extends keyof CacheStackEvents>(event: K, listener: (data: CacheStackEvents[K]) => void): this;
|
|
487
|
+
off<K extends keyof CacheStackEvents>(event: K, listener: (data: CacheStackEvents[K]) => void): this;
|
|
488
|
+
removeAllListeners<K extends keyof CacheStackEvents>(event?: K): this;
|
|
489
|
+
listeners<K extends keyof CacheStackEvents>(event: K): Array<(data: CacheStackEvents[K]) => void>;
|
|
490
|
+
listenerCount<K extends keyof CacheStackEvents>(event: K): number;
|
|
491
|
+
emit<K extends keyof CacheStackEvents>(event: K, data: CacheStackEvents[K]): boolean;
|
|
492
|
+
}
|
|
493
|
+
declare class CacheStack extends EventEmitter {
|
|
494
|
+
private readonly layers;
|
|
495
|
+
private readonly options;
|
|
496
|
+
private readonly stampedeGuard;
|
|
497
|
+
private readonly metricsCollector;
|
|
498
|
+
private readonly instanceId;
|
|
499
|
+
private readonly startup;
|
|
500
|
+
private unsubscribeInvalidation?;
|
|
501
|
+
private readonly logger;
|
|
502
|
+
private readonly tagIndex;
|
|
503
|
+
private readonly fetchRateLimiter;
|
|
504
|
+
private readonly backgroundRefreshes;
|
|
505
|
+
private readonly layerDegradedUntil;
|
|
506
|
+
private readonly ttlResolver;
|
|
507
|
+
private readonly circuitBreakerManager;
|
|
508
|
+
private currentGeneration?;
|
|
509
|
+
private readonly writeBehindQueue;
|
|
510
|
+
private writeBehindTimer?;
|
|
511
|
+
private writeBehindFlushPromise?;
|
|
512
|
+
private isDisconnecting;
|
|
513
|
+
private disconnectPromise?;
|
|
514
|
+
constructor(layers: CacheLayer[], options?: CacheStackOptions);
|
|
515
|
+
/**
|
|
516
|
+
* Read-through cache get.
|
|
517
|
+
* Returns the cached value if present and fresh, or invokes `fetcher` on a miss
|
|
518
|
+
* and stores the result across all layers. Returns `null` if the key is not found
|
|
519
|
+
* and no `fetcher` is provided.
|
|
520
|
+
*/
|
|
521
|
+
get<T>(key: string, fetcher?: () => Promise<T>, options?: CacheGetOptions): Promise<T | null>;
|
|
522
|
+
/**
|
|
523
|
+
* Alias for `get(key, fetcher, options)` — explicit get-or-set pattern.
|
|
524
|
+
* Fetches and caches the value if not already present.
|
|
525
|
+
*/
|
|
526
|
+
getOrSet<T>(key: string, fetcher: () => Promise<T>, options?: CacheGetOptions): Promise<T | null>;
|
|
527
|
+
/**
|
|
528
|
+
* Like `get()`, but throws `CacheMissError` instead of returning `null`.
|
|
529
|
+
* Useful when the value is expected to exist or the fetcher is expected to
|
|
530
|
+
* return non-null.
|
|
531
|
+
*/
|
|
532
|
+
getOrThrow<T>(key: string, fetcher?: () => Promise<T>, options?: CacheGetOptions): Promise<T>;
|
|
533
|
+
/**
|
|
534
|
+
* Returns true if the given key exists and is not expired in any layer.
|
|
535
|
+
*/
|
|
536
|
+
has(key: string): Promise<boolean>;
|
|
537
|
+
/**
|
|
538
|
+
* Returns the remaining TTL in seconds for the key in the fastest layer
|
|
539
|
+
* that has it, or null if the key is not found / has no TTL.
|
|
540
|
+
*/
|
|
541
|
+
ttl(key: string): Promise<number | null>;
|
|
542
|
+
/**
|
|
543
|
+
* Stores a value in all cache layers. Overwrites any existing value.
|
|
544
|
+
*/
|
|
545
|
+
set<T>(key: string, value: T, options?: CacheWriteOptions): Promise<void>;
|
|
546
|
+
/**
|
|
547
|
+
* Deletes the key from all layers and publishes an invalidation message.
|
|
548
|
+
*/
|
|
549
|
+
delete(key: string): Promise<void>;
|
|
550
|
+
clear(): Promise<void>;
|
|
551
|
+
/**
|
|
552
|
+
* Deletes multiple keys at once. More efficient than calling `delete()` in a loop.
|
|
553
|
+
*/
|
|
554
|
+
mdelete(keys: string[]): Promise<void>;
|
|
555
|
+
mget<T>(entries: CacheMGetEntry<T>[]): Promise<Array<T | null>>;
|
|
556
|
+
mset<T>(entries: CacheMSetEntry<T>[]): Promise<void>;
|
|
557
|
+
warm(entries: CacheWarmEntry[], options?: CacheWarmOptions): Promise<void>;
|
|
558
|
+
/**
|
|
559
|
+
* Returns a cached version of `fetcher`. The cache key is derived from
|
|
560
|
+
* `prefix` plus the serialized arguments unless a `keyResolver` is provided.
|
|
561
|
+
*/
|
|
562
|
+
wrap<TArgs extends unknown[], TResult>(prefix: string, fetcher: (...args: TArgs) => Promise<TResult>, options?: CacheWrapOptions<TArgs>): (...args: TArgs) => Promise<TResult | null>;
|
|
563
|
+
/**
|
|
564
|
+
* Creates a `CacheNamespace` that automatically prefixes all keys with
|
|
565
|
+
* `prefix:`. Useful for multi-tenant or module-level isolation.
|
|
566
|
+
*/
|
|
567
|
+
namespace(prefix: string): CacheNamespace;
|
|
568
|
+
invalidateByTag(tag: string): Promise<void>;
|
|
569
|
+
invalidateByTags(tags: string[], mode?: 'any' | 'all'): Promise<void>;
|
|
570
|
+
invalidateByPattern(pattern: string): Promise<void>;
|
|
571
|
+
invalidateByPrefix(prefix: string): Promise<void>;
|
|
572
|
+
getMetrics(): CacheMetricsSnapshot;
|
|
573
|
+
getStats(): CacheStatsSnapshot;
|
|
574
|
+
resetMetrics(): void;
|
|
575
|
+
/**
|
|
576
|
+
* Returns computed hit-rate statistics (overall and per-layer).
|
|
577
|
+
*/
|
|
578
|
+
getHitRate(): CacheHitRateSnapshot;
|
|
579
|
+
healthCheck(): Promise<CacheHealthCheckResult[]>;
|
|
580
|
+
bumpGeneration(nextGeneration?: number): number;
|
|
581
|
+
/**
|
|
582
|
+
* Returns detailed metadata about a single cache key: which layers contain it,
|
|
583
|
+
* remaining fresh/stale/error TTLs, and associated tags.
|
|
584
|
+
* Returns `null` if the key does not exist in any layer.
|
|
585
|
+
*/
|
|
586
|
+
inspect(key: string): Promise<CacheInspectResult | null>;
|
|
587
|
+
exportState(): Promise<CacheSnapshotEntry[]>;
|
|
588
|
+
importState(entries: CacheSnapshotEntry[]): Promise<void>;
|
|
589
|
+
persistToFile(filePath: string): Promise<void>;
|
|
590
|
+
restoreFromFile(filePath: string): Promise<void>;
|
|
591
|
+
disconnect(): Promise<void>;
|
|
592
|
+
private initialize;
|
|
593
|
+
private fetchWithGuards;
|
|
594
|
+
private waitForFreshValue;
|
|
595
|
+
private fetchAndPopulate;
|
|
596
|
+
private storeEntry;
|
|
597
|
+
private writeBatch;
|
|
598
|
+
private readFromLayers;
|
|
599
|
+
private readLayerEntry;
|
|
600
|
+
private backfill;
|
|
601
|
+
private writeAcrossLayers;
|
|
602
|
+
private executeLayerOperations;
|
|
603
|
+
private resolveFreshTtl;
|
|
604
|
+
private resolveLayerSeconds;
|
|
605
|
+
private shouldNegativeCache;
|
|
606
|
+
private scheduleBackgroundRefresh;
|
|
607
|
+
private resolveSingleFlightOptions;
|
|
608
|
+
private deleteKeys;
|
|
609
|
+
private publishInvalidation;
|
|
610
|
+
private handleInvalidationMessage;
|
|
611
|
+
private getTagsForKey;
|
|
612
|
+
private formatError;
|
|
613
|
+
private sleep;
|
|
614
|
+
private shouldBroadcastL1Invalidation;
|
|
615
|
+
private initializeWriteBehind;
|
|
616
|
+
private shouldWriteBehind;
|
|
617
|
+
private enqueueWriteBehind;
|
|
618
|
+
private flushWriteBehindQueue;
|
|
619
|
+
private buildLayerSetEntry;
|
|
620
|
+
private intersectKeys;
|
|
621
|
+
private qualifyKey;
|
|
622
|
+
private qualifyPattern;
|
|
623
|
+
private stripQualifiedKey;
|
|
624
|
+
private generationPrefix;
|
|
625
|
+
private deleteKeysFromLayers;
|
|
626
|
+
private validateConfiguration;
|
|
627
|
+
private validateWriteOptions;
|
|
628
|
+
private validateLayerNumberOption;
|
|
629
|
+
private validatePositiveNumber;
|
|
630
|
+
private validateNonNegativeNumber;
|
|
631
|
+
private validateCacheKey;
|
|
632
|
+
private validateTtlPolicy;
|
|
633
|
+
private assertActive;
|
|
634
|
+
private awaitStartup;
|
|
635
|
+
private serializeOptions;
|
|
636
|
+
private validateAdaptiveTtlOptions;
|
|
637
|
+
private validateCircuitBreakerOptions;
|
|
638
|
+
private applyFreshReadPolicies;
|
|
639
|
+
private shouldSkipLayer;
|
|
640
|
+
private handleLayerFailure;
|
|
641
|
+
private isGracefulDegradationEnabled;
|
|
642
|
+
private recordCircuitFailure;
|
|
643
|
+
private isNegativeStoredValue;
|
|
644
|
+
private emitError;
|
|
645
|
+
private serializeKeyPart;
|
|
646
|
+
private isCacheSnapshotEntries;
|
|
647
|
+
private normalizeForSerialization;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
interface HonoLikeRequest {
|
|
651
|
+
method?: string;
|
|
652
|
+
url?: string;
|
|
653
|
+
path?: string;
|
|
654
|
+
query?: Record<string, unknown>;
|
|
655
|
+
}
|
|
656
|
+
interface HonoLikeContext {
|
|
657
|
+
req: HonoLikeRequest;
|
|
658
|
+
header?: (name: string, value: string) => void;
|
|
659
|
+
json: (body: unknown, status?: number) => Response | Promise<Response> | unknown;
|
|
660
|
+
}
|
|
661
|
+
interface HonoCacheMiddlewareOptions extends CacheGetOptions {
|
|
662
|
+
keyResolver?: (request: HonoLikeRequest) => string;
|
|
663
|
+
methods?: string[];
|
|
664
|
+
}
|
|
665
|
+
declare function createHonoCacheMiddleware(cache: CacheStack, options?: HonoCacheMiddlewareOptions): (context: HonoLikeContext, next: () => Promise<void>) => Promise<void>;
|
|
666
|
+
|
|
667
|
+
export { type CacheStatsSnapshot as A, type CacheTtlPolicy as B, type CacheLogger as C, type CacheTtlPolicyContext as D, type CacheWarmEntry as E, type CacheWarmOptions as F, type CacheWarmProgress as G, type CacheWriteBehindOptions as H, type InvalidationBus as I, type CacheWriteOptions as J, type EvictionPolicy as K, type LayerTtlMap as L, MemoryLayer as M, type MemoryLayerOptions as N, type MemoryLayerSnapshotEntry as O, PatternMatcher as P, createHonoCacheMiddleware as Q, TagIndex as T, type InvalidationMessage as a, type CacheTagIndex as b, CacheStack as c, type CacheWrapOptions as d, type CacheGetOptions as e, type CacheLayer as f, type CacheSerializer as g, type CacheLayerSetManyEntry as h, type CacheSingleFlightCoordinator as i, type CacheSingleFlightExecutionOptions as j, type CacheAdaptiveTtlOptions as k, type CacheCircuitBreakerOptions as l, type CacheDegradationOptions as m, type CacheHealthCheckResult as n, type CacheHitRateSnapshot as o, type CacheInspectResult as p, type CacheLayerLatency as q, type CacheMGetEntry as r, type CacheMSetEntry as s, type CacheMetricsSnapshot as t, CacheMissError as u, CacheNamespace as v, type CacheRateLimitOptions as w, type CacheSnapshotEntry as x, type CacheStackEvents as y, type CacheStackOptions as z };
|