layercache 1.4.0 → 2.1.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/dist/{chunk-7KMKQ6QZ.js → chunk-6X7NV5BG.js} +33 -0
- package/dist/{chunk-FFZCC7EQ.js → chunk-IVX6ABFX.js} +87 -0
- package/dist/cli.cjs +33 -0
- package/dist/cli.js +1 -1
- package/dist/{edge-D2FpRlyS.d.cts → edge-BCU8D-Yd.d.cts} +504 -0
- package/dist/{edge-D2FpRlyS.d.ts → edge-BCU8D-Yd.d.ts} +504 -0
- package/dist/edge.cjs +87 -0
- package/dist/edge.d.cts +1 -1
- package/dist/edge.d.ts +1 -1
- package/dist/edge.js +1 -1
- package/dist/index.cjs +485 -0
- package/dist/index.d.cts +244 -2
- package/dist/index.d.ts +244 -2
- package/dist/index.js +367 -2
- package/package.json +1 -1
|
@@ -8,30 +8,52 @@ declare class CacheMissError extends Error {
|
|
|
8
8
|
readonly key: string;
|
|
9
9
|
constructor(key: string);
|
|
10
10
|
}
|
|
11
|
+
/** Per-layer millisecond values keyed by each layer's `name`. */
|
|
11
12
|
interface LayerTtlMap {
|
|
13
|
+
/** Millisecond value for the named layer, or `undefined` to fall back. */
|
|
12
14
|
[layerName: string]: number | undefined;
|
|
13
15
|
}
|
|
16
|
+
/** Internal write classification used when resolving TTLs and entry options. */
|
|
14
17
|
type CacheEntryWriteKind = 'value' | 'empty';
|
|
18
|
+
/** Options that describe how a cache entry should be stored. */
|
|
15
19
|
interface CacheEntryWriteOptions {
|
|
20
|
+
/** Tags to associate with the key for tag-based invalidation or expiration. */
|
|
16
21
|
tags?: string[];
|
|
22
|
+
/** Fresh TTL in milliseconds, either uniform or per layer. */
|
|
17
23
|
ttl?: number | LayerTtlMap;
|
|
24
|
+
/** Dynamic TTL policy used instead of, or before falling back to, `ttl`. */
|
|
18
25
|
ttlPolicy?: CacheTtlPolicy;
|
|
26
|
+
/** TTL in milliseconds for cached null/empty results when negative caching is enabled. */
|
|
19
27
|
negativeTtl?: number | LayerTtlMap;
|
|
28
|
+
/** Extra window in milliseconds where stale values are returned while refresh runs in the background. */
|
|
20
29
|
staleWhileRevalidate?: number | LayerTtlMap;
|
|
30
|
+
/** Extra window in milliseconds where stale values are returned if refresh fails. */
|
|
21
31
|
staleIfError?: number | LayerTtlMap;
|
|
32
|
+
/** Random +/- jitter in milliseconds applied to resolved TTLs to avoid synchronized expiry. */
|
|
22
33
|
ttlJitter?: number | LayerTtlMap;
|
|
34
|
+
/** Increase TTL for frequently accessed keys using the adaptive TTL policy. */
|
|
23
35
|
adaptiveTtl?: boolean | CacheAdaptiveTtlOptions;
|
|
24
36
|
}
|
|
37
|
+
/** Context passed to `contextOptions` before a value is written. */
|
|
25
38
|
interface CacheContextOptionsContext {
|
|
39
|
+
/** Fully qualified cache key being written. */
|
|
26
40
|
key: string;
|
|
41
|
+
/** Value returned by the fetcher or passed to `set()`. */
|
|
27
42
|
value: unknown;
|
|
43
|
+
/** Whether the write stores a normal value or an empty/negative-cache marker. */
|
|
28
44
|
kind: CacheEntryWriteKind;
|
|
29
45
|
}
|
|
46
|
+
/** Options accepted by write operations and read-through fetch writes. */
|
|
30
47
|
interface CacheWriteOptions extends CacheEntryWriteOptions {
|
|
48
|
+
/** Cache `null` fetcher results using `negativeTtl` instead of treating them as misses. */
|
|
31
49
|
negativeCache?: boolean;
|
|
50
|
+
/** Extend a key's TTL on fresh reads. */
|
|
32
51
|
slidingTtl?: boolean;
|
|
52
|
+
/** Refresh in the background when the remaining TTL is at or below this threshold in milliseconds. */
|
|
33
53
|
refreshAhead?: number | LayerTtlMap;
|
|
54
|
+
/** Circuit breaker controls for this operation's fetcher. */
|
|
34
55
|
circuitBreaker?: CacheCircuitBreakerOptions;
|
|
56
|
+
/** Rate limit concurrent fetcher execution for this operation. */
|
|
35
57
|
fetcherRateLimit?: CacheRateLimitOptions;
|
|
36
58
|
/**
|
|
37
59
|
* Optional predicate called with the fetcher's return value before caching.
|
|
@@ -61,31 +83,51 @@ interface CacheWriteOptions extends CacheEntryWriteOptions {
|
|
|
61
83
|
*/
|
|
62
84
|
contextOptions?: (context: CacheContextOptionsContext) => CacheEntryWriteOptions | undefined;
|
|
63
85
|
}
|
|
86
|
+
/** Options accepted by read-through `get()` operations. */
|
|
64
87
|
interface CacheGetOptions extends CacheWriteOptions {
|
|
65
88
|
}
|
|
89
|
+
/** Metadata passed to a read-through fetcher. */
|
|
66
90
|
interface CacheFetcherContext<T = unknown> {
|
|
91
|
+
/** Fully qualified cache key being fetched. */
|
|
67
92
|
key: string;
|
|
93
|
+
/** Existing stale value, when a refresh is triggered from stale state. */
|
|
68
94
|
currentValue: T | undefined;
|
|
95
|
+
/** Cache state that caused the fetcher to run. */
|
|
69
96
|
state: 'miss' | 'fresh' | 'stale-while-revalidate' | 'stale-if-error';
|
|
97
|
+
/** Layer that supplied the existing value, when available. */
|
|
70
98
|
layer?: string;
|
|
71
99
|
}
|
|
100
|
+
/** Async function used by read-through cache operations to produce a value on miss or refresh. */
|
|
72
101
|
type CacheFetcher<T = unknown> = (context: CacheFetcherContext<T>) => Promise<T>;
|
|
102
|
+
/** Entry descriptor for `CacheStack.mget()`. */
|
|
73
103
|
interface CacheMGetEntry<T> {
|
|
104
|
+
/** Cache key to read. */
|
|
74
105
|
key: string;
|
|
106
|
+
/** Optional read-through fetcher for this key. */
|
|
75
107
|
fetch?: CacheFetcher<T>;
|
|
108
|
+
/** Per-entry get/write options. */
|
|
76
109
|
options?: CacheGetOptions;
|
|
77
110
|
}
|
|
111
|
+
/** Entry descriptor for `CacheStack.mset()`. */
|
|
78
112
|
interface CacheMSetEntry<T> {
|
|
113
|
+
/** Cache key to write. */
|
|
79
114
|
key: string;
|
|
115
|
+
/** Value to store. */
|
|
80
116
|
value: T;
|
|
117
|
+
/** Per-entry write options. */
|
|
81
118
|
options?: CacheWriteOptions;
|
|
82
119
|
}
|
|
83
120
|
/** Interface that all cache backend implementations must satisfy. */
|
|
84
121
|
interface CacheLayer {
|
|
122
|
+
/** Human-readable unique layer name used for metrics and per-layer options. */
|
|
85
123
|
readonly name: string;
|
|
124
|
+
/** Default TTL in milliseconds used when a write does not provide one. */
|
|
86
125
|
readonly defaultTtl?: number;
|
|
126
|
+
/** Whether the layer is local to this process, such as memory or disk. */
|
|
87
127
|
readonly isLocal?: boolean;
|
|
128
|
+
/** Read and unwrap a fresh value, returning `null` on miss or expiry. */
|
|
88
129
|
get<T>(key: string): Promise<T | null>;
|
|
130
|
+
/** Read the raw stored value or envelope so CacheStack can resolve stale state. */
|
|
89
131
|
getEntry?<T = unknown>(key: string): Promise<T | null>;
|
|
90
132
|
/**
|
|
91
133
|
* Bulk read fast-path. Implementations should return raw stored entries using
|
|
@@ -93,14 +135,23 @@ interface CacheLayer {
|
|
|
93
135
|
* stale windows, and negative-cache markers consistently.
|
|
94
136
|
*/
|
|
95
137
|
getMany?<T>(keys: string[]): Promise<Array<T | null>>;
|
|
138
|
+
/** Bulk write entries; implementations may use this as an optimized fast path. */
|
|
96
139
|
setMany?(entries: CacheLayerSetManyEntry[]): Promise<void>;
|
|
140
|
+
/** Store a raw value for `ttl` milliseconds, or without expiry when omitted. */
|
|
97
141
|
set(key: string, value: unknown, ttl?: number): Promise<void>;
|
|
142
|
+
/** Delete a key from this layer. */
|
|
98
143
|
delete(key: string): Promise<void>;
|
|
144
|
+
/** Remove all keys from this layer. */
|
|
99
145
|
clear(): Promise<void>;
|
|
146
|
+
/** Delete several keys from this layer; CacheStack falls back to `delete()` when absent. */
|
|
100
147
|
deleteMany?(keys: string[]): Promise<void>;
|
|
148
|
+
/** Return known keys in this layer for pattern or prefix invalidation. */
|
|
101
149
|
keys?(): Promise<string[]>;
|
|
150
|
+
/** Visit known keys without materializing the full key list. */
|
|
102
151
|
forEachKey?(visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
152
|
+
/** Health check hook used by `CacheStack.healthCheck()`. */
|
|
103
153
|
ping?(): Promise<boolean>;
|
|
154
|
+
/** Release sockets, timers, or other resources held by the layer. */
|
|
104
155
|
dispose?(): Promise<void>;
|
|
105
156
|
/**
|
|
106
157
|
* Returns true if the key exists and has not expired.
|
|
@@ -119,8 +170,11 @@ interface CacheLayer {
|
|
|
119
170
|
*/
|
|
120
171
|
size?(): Promise<number>;
|
|
121
172
|
}
|
|
173
|
+
/** Serializer used by layers that convert values to bytes or strings. */
|
|
122
174
|
interface CacheSerializer {
|
|
175
|
+
/** Convert a value to a storable payload. */
|
|
123
176
|
serialize(value: unknown): string | Buffer;
|
|
177
|
+
/** Restore a value from a payload. */
|
|
124
178
|
deserialize<T>(payload: string | Buffer): T;
|
|
125
179
|
}
|
|
126
180
|
/** Per-layer latency statistics (rolling window of sampled read durations). */
|
|
@@ -134,22 +188,39 @@ interface CacheLayerLatency {
|
|
|
134
188
|
}
|
|
135
189
|
/** Snapshot of cumulative cache counters. */
|
|
136
190
|
interface CacheMetricsSnapshot {
|
|
191
|
+
/** Number of successful cache reads. */
|
|
137
192
|
hits: number;
|
|
193
|
+
/** Number of reads that did not find a usable value. */
|
|
138
194
|
misses: number;
|
|
195
|
+
/** Number of fetcher executions. */
|
|
139
196
|
fetches: number;
|
|
197
|
+
/** Number of cache write operations. */
|
|
140
198
|
sets: number;
|
|
199
|
+
/** Number of delete operations. */
|
|
141
200
|
deletes: number;
|
|
201
|
+
/** Number of values backfilled from slower layers into faster layers. */
|
|
142
202
|
backfills: number;
|
|
203
|
+
/** Number of invalidation operations. */
|
|
143
204
|
invalidations: number;
|
|
205
|
+
/** Number of stale values returned to callers. */
|
|
144
206
|
staleHits: number;
|
|
207
|
+
/** Number of background refresh attempts. */
|
|
145
208
|
refreshes: number;
|
|
209
|
+
/** Number of failed refresh attempts. */
|
|
146
210
|
refreshErrors: number;
|
|
211
|
+
/** Number of layer write failures. */
|
|
147
212
|
writeFailures: number;
|
|
213
|
+
/** Number of requests that waited for a single-flight result. */
|
|
148
214
|
singleFlightWaits: number;
|
|
215
|
+
/** Number of cached negative/empty results served. */
|
|
149
216
|
negativeCacheHits: number;
|
|
217
|
+
/** Number of times circuit breakers blocked a fetcher. */
|
|
150
218
|
circuitBreakerTrips: number;
|
|
219
|
+
/** Number of operations skipped or retried due to degraded layers. */
|
|
151
220
|
degradedOperations: number;
|
|
221
|
+
/** Hit counts grouped by layer name. */
|
|
152
222
|
hitsByLayer: Record<string, number>;
|
|
223
|
+
/** Miss counts grouped by layer name. */
|
|
153
224
|
missesByLayer: Record<string, number>;
|
|
154
225
|
/** Per-layer read latency statistics (sampled from successful reads). */
|
|
155
226
|
latencyByLayer: Record<string, CacheLayerLatency>;
|
|
@@ -164,86 +235,154 @@ interface CacheHitRateSnapshot {
|
|
|
164
235
|
byLayer: Record<string, number>;
|
|
165
236
|
}
|
|
166
237
|
interface CacheLogger {
|
|
238
|
+
/** Low-volume diagnostic events. */
|
|
167
239
|
debug?(message: string, context?: Record<string, unknown>): void;
|
|
240
|
+
/** Informational operational messages. */
|
|
168
241
|
info?(message: string, context?: Record<string, unknown>): void;
|
|
242
|
+
/** Recoverable problems or risky configuration warnings. */
|
|
169
243
|
warn?(message: string, context?: Record<string, unknown>): void;
|
|
244
|
+
/** Errors surfaced by cache operations or layer integrations. */
|
|
170
245
|
error?(message: string, context?: Record<string, unknown>): void;
|
|
171
246
|
}
|
|
247
|
+
/** Index used to map cache keys to tags and discover keys for invalidation. */
|
|
172
248
|
interface CacheTagIndex {
|
|
249
|
+
/** Record that a key exists, even when it has no tags. */
|
|
173
250
|
touch(key: string): Promise<void>;
|
|
251
|
+
/** Associate a key with the provided tags, replacing any previous tag set. */
|
|
174
252
|
track(key: string, tags: string[]): Promise<void>;
|
|
253
|
+
/** Remove all tag/index metadata for a key. */
|
|
175
254
|
remove(key: string): Promise<void>;
|
|
255
|
+
/** Return keys currently associated with a tag. */
|
|
176
256
|
keysForTag(tag: string): Promise<string[]>;
|
|
257
|
+
/** Visit keys for a tag without materializing all results. */
|
|
177
258
|
forEachKeyForTag?(tag: string, visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
259
|
+
/** Return known keys with the given prefix. */
|
|
178
260
|
keysForPrefix?(prefix: string): Promise<string[]>;
|
|
261
|
+
/** Visit known keys with the given prefix without materializing all results. */
|
|
179
262
|
forEachKeyForPrefix?(prefix: string, visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
180
263
|
/** Returns the tags associated with a specific key, or an empty array. */
|
|
181
264
|
tagsForKey?(key: string): Promise<string[]>;
|
|
265
|
+
/** Return known keys matching a wildcard pattern. */
|
|
182
266
|
matchPattern(pattern: string): Promise<string[]>;
|
|
267
|
+
/** Visit known keys matching a wildcard pattern without materializing all results. */
|
|
183
268
|
forEachKeyMatchingPattern?(pattern: string, visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
269
|
+
/** Remove all index state. */
|
|
184
270
|
clear(): Promise<void>;
|
|
185
271
|
}
|
|
272
|
+
/** Raw layer bulk-write entry. */
|
|
186
273
|
interface CacheLayerSetManyEntry {
|
|
274
|
+
/** Cache key to write. */
|
|
187
275
|
key: string;
|
|
276
|
+
/** Raw value or envelope to store. */
|
|
188
277
|
value: unknown;
|
|
278
|
+
/** TTL in milliseconds for this layer entry. */
|
|
189
279
|
ttl?: number;
|
|
190
280
|
}
|
|
281
|
+
/** Cross-process invalidation message sent through an `InvalidationBus`. */
|
|
191
282
|
interface InvalidationMessage {
|
|
283
|
+
/** Message target scope. */
|
|
192
284
|
scope: 'key' | 'keys' | 'clear';
|
|
285
|
+
/** Sender instance id; receivers ignore messages from themselves. */
|
|
193
286
|
sourceId: string;
|
|
287
|
+
/** Keys affected by key- or keys-scoped messages. */
|
|
194
288
|
keys?: string[];
|
|
289
|
+
/** Operation that produced this invalidation. */
|
|
195
290
|
operation?: 'write' | 'delete' | 'invalidate' | 'expire' | 'clear';
|
|
196
291
|
}
|
|
292
|
+
/** Pub/sub abstraction used to broadcast invalidation across CacheStack instances. */
|
|
197
293
|
interface InvalidationBus {
|
|
294
|
+
/** Register a message handler and return an unsubscribe function. */
|
|
198
295
|
subscribe(handler: (message: InvalidationMessage) => Promise<void> | void): Promise<() => Promise<void> | void>;
|
|
296
|
+
/** Publish an invalidation message to other subscribers. */
|
|
199
297
|
publish(message: InvalidationMessage): Promise<void>;
|
|
200
298
|
}
|
|
299
|
+
/** Timing controls for a distributed single-flight execution. */
|
|
201
300
|
interface CacheSingleFlightExecutionOptions {
|
|
301
|
+
/** Lease duration in milliseconds for the worker lock. */
|
|
202
302
|
leaseMs: number;
|
|
303
|
+
/** Maximum time in milliseconds a waiter should poll for the worker result. */
|
|
203
304
|
waitTimeoutMs: number;
|
|
305
|
+
/** Poll interval in milliseconds for waiters. */
|
|
204
306
|
pollIntervalMs: number;
|
|
307
|
+
/** Optional interval in milliseconds for renewing a worker lock lease. */
|
|
205
308
|
renewIntervalMs?: number;
|
|
206
309
|
}
|
|
310
|
+
/** Coordinator that deduplicates fetchers across processes. */
|
|
207
311
|
interface CacheSingleFlightCoordinator {
|
|
312
|
+
/** Run `worker` when this process wins the lock, otherwise run `waiter`. */
|
|
208
313
|
execute<T>(key: string, options: CacheSingleFlightExecutionOptions, worker: () => Promise<T>, waiter: () => Promise<T>): Promise<T>;
|
|
209
314
|
}
|
|
315
|
+
/** Global options for a `CacheStack` instance. */
|
|
210
316
|
interface CacheStackOptions {
|
|
317
|
+
/** Logger instance, `true` for console debug logging, or `false`/omitted for quiet mode. */
|
|
211
318
|
logger?: CacheLogger | boolean;
|
|
319
|
+
/** Enable metrics collection. Currently metrics are always collected; this is kept for API compatibility. */
|
|
212
320
|
metrics?: boolean;
|
|
321
|
+
/** Deduplicate concurrent local fetches for the same key. */
|
|
213
322
|
stampedePrevention?: boolean;
|
|
323
|
+
/** Maximum number of distinct keys allowed in the local stampede guard. */
|
|
214
324
|
stampedeMaxInFlight?: number;
|
|
325
|
+
/** Timeout in milliseconds for a local stampede-guarded fetch. */
|
|
215
326
|
stampedeEntryTimeoutMs?: number;
|
|
327
|
+
/** Bus used to receive and publish cross-process invalidation messages. */
|
|
216
328
|
invalidationBus?: InvalidationBus;
|
|
329
|
+
/** Tag index implementation used for tag, prefix, and pattern discovery. */
|
|
217
330
|
tagIndex?: CacheTagIndex;
|
|
331
|
+
/** Generation number prefixed onto all keys for instant bulk invalidation. */
|
|
218
332
|
generation?: number;
|
|
333
|
+
/** Enable cleanup for keys from previous generations. */
|
|
219
334
|
generationCleanup?: boolean | CacheGenerationCleanupOptions;
|
|
335
|
+
/** Broadcast writes to other instances so their L1/local layers drop stale values. */
|
|
220
336
|
broadcastL1Invalidation?: boolean;
|
|
221
337
|
/**
|
|
222
338
|
* @deprecated Use `broadcastL1Invalidation` instead.
|
|
223
339
|
*/
|
|
224
340
|
publishSetInvalidation?: boolean;
|
|
341
|
+
/** Cache null fetcher results as negative entries. */
|
|
225
342
|
negativeCaching?: boolean;
|
|
343
|
+
/** Default negative-cache TTL in milliseconds. */
|
|
226
344
|
negativeTtl?: number | LayerTtlMap;
|
|
345
|
+
/** Default stale-while-revalidate window in milliseconds. */
|
|
227
346
|
staleWhileRevalidate?: number | LayerTtlMap;
|
|
347
|
+
/** Default stale-if-error window in milliseconds. */
|
|
228
348
|
staleIfError?: number | LayerTtlMap;
|
|
349
|
+
/** Default TTL jitter in milliseconds. */
|
|
229
350
|
ttlJitter?: number | LayerTtlMap;
|
|
351
|
+
/** Default refresh-ahead threshold in milliseconds. */
|
|
230
352
|
refreshAhead?: number | LayerTtlMap;
|
|
353
|
+
/** Default adaptive TTL policy. */
|
|
231
354
|
adaptiveTtl?: boolean | CacheAdaptiveTtlOptions;
|
|
355
|
+
/** Default circuit breaker settings for fetchers. */
|
|
232
356
|
circuitBreaker?: CacheCircuitBreakerOptions;
|
|
357
|
+
/** Keep using healthy layers when another layer fails temporarily. */
|
|
233
358
|
gracefulDegradation?: boolean | CacheDegradationOptions;
|
|
359
|
+
/** Write behavior: fail on any layer error (`strict`) or only if all layers fail (`best-effort`). */
|
|
234
360
|
writePolicy?: 'strict' | 'best-effort';
|
|
361
|
+
/** Write immediately to all layers or queue writes behind the response path. */
|
|
235
362
|
writeStrategy?: 'write-through' | 'write-behind';
|
|
363
|
+
/** Queue controls used when `writeStrategy` is `write-behind`. */
|
|
236
364
|
writeBehind?: CacheWriteBehindOptions;
|
|
365
|
+
/** Default fetcher rate limit settings. */
|
|
237
366
|
fetcherRateLimit?: CacheRateLimitOptions;
|
|
367
|
+
/** Max milliseconds allowed for background refresh before it is aborted. */
|
|
238
368
|
backgroundRefreshTimeoutMs?: number;
|
|
369
|
+
/** Coordinator used for distributed single-flight fetcher deduplication. */
|
|
239
370
|
singleFlightCoordinator?: CacheSingleFlightCoordinator;
|
|
371
|
+
/** Distributed single-flight lock lease in milliseconds. */
|
|
240
372
|
singleFlightLeaseMs?: number;
|
|
373
|
+
/** Maximum milliseconds waiters poll for distributed single-flight completion. */
|
|
241
374
|
singleFlightTimeoutMs?: number;
|
|
375
|
+
/** Poll interval in milliseconds for distributed single-flight waiters. */
|
|
242
376
|
singleFlightPollMs?: number;
|
|
377
|
+
/** Interval in milliseconds for renewing distributed single-flight leases. */
|
|
243
378
|
singleFlightRenewIntervalMs?: number;
|
|
379
|
+
/** Base directory that constrains snapshot persistence paths, or `false` to disable path sandboxing. */
|
|
244
380
|
snapshotBaseDir?: string | false;
|
|
381
|
+
/** Maximum snapshot file size in bytes accepted during restore, or `false` to disable. */
|
|
245
382
|
snapshotMaxBytes?: number | false;
|
|
383
|
+
/** Maximum number of entries exported or imported in one snapshot, or `false` to disable. */
|
|
246
384
|
snapshotMaxEntries?: number | false;
|
|
385
|
+
/** Maximum keys one invalidation scan may affect, or `false` to disable the guard. */
|
|
247
386
|
invalidationMaxKeys?: number | false;
|
|
248
387
|
/**
|
|
249
388
|
* Maximum number of entries in `accessProfiles` and `circuitBreakers` maps
|
|
@@ -252,85 +391,140 @@ interface CacheStackOptions {
|
|
|
252
391
|
*/
|
|
253
392
|
maxProfileEntries?: number;
|
|
254
393
|
}
|
|
394
|
+
/** Adaptive TTL policy settings for hot keys. */
|
|
255
395
|
interface CacheAdaptiveTtlOptions {
|
|
396
|
+
/** Number of accesses after which a key is considered hot. */
|
|
256
397
|
hotAfter?: number;
|
|
398
|
+
/** TTL increment in milliseconds, uniform or per layer. */
|
|
257
399
|
step?: number | LayerTtlMap;
|
|
400
|
+
/** Maximum TTL in milliseconds after adaptive increases. */
|
|
258
401
|
maxTtl?: number | LayerTtlMap;
|
|
259
402
|
}
|
|
403
|
+
/** Options for pruning keys from older generations. */
|
|
260
404
|
interface CacheGenerationCleanupOptions {
|
|
405
|
+
/** Number of old-generation keys to remove per cleanup batch. */
|
|
261
406
|
batchSize?: number;
|
|
262
407
|
}
|
|
408
|
+
/** Built-in TTL policies or a function that returns a TTL in milliseconds. */
|
|
263
409
|
type CacheTtlPolicy = 'until-midnight' | 'next-hour' | {
|
|
264
410
|
alignTo: number;
|
|
265
411
|
} | ((context: CacheTtlPolicyContext) => number | undefined);
|
|
412
|
+
/** Context passed to a custom TTL policy. */
|
|
266
413
|
interface CacheTtlPolicyContext {
|
|
414
|
+
/** Fully qualified cache key being written. */
|
|
267
415
|
key: string;
|
|
416
|
+
/** Value being written. */
|
|
268
417
|
value: unknown;
|
|
269
418
|
}
|
|
419
|
+
/** Circuit breaker settings for protecting failing fetchers. */
|
|
270
420
|
interface CacheCircuitBreakerOptions {
|
|
421
|
+
/** Consecutive failures before the circuit opens. */
|
|
271
422
|
failureThreshold?: number;
|
|
423
|
+
/** Milliseconds before an open circuit allows another attempt. */
|
|
272
424
|
cooldownMs?: number;
|
|
273
425
|
}
|
|
426
|
+
/** Graceful degradation settings for temporarily unhealthy layers. */
|
|
274
427
|
interface CacheDegradationOptions {
|
|
428
|
+
/** Milliseconds to skip a failed layer before trying it again. */
|
|
275
429
|
retryAfterMs?: number;
|
|
276
430
|
}
|
|
431
|
+
/** Fetcher concurrency/rate limiting settings. */
|
|
277
432
|
interface CacheRateLimitOptions {
|
|
433
|
+
/** Maximum concurrent fetchers allowed in the selected scope. */
|
|
278
434
|
maxConcurrent?: number;
|
|
435
|
+
/** Window size in milliseconds for `maxPerInterval`. */
|
|
279
436
|
intervalMs?: number;
|
|
437
|
+
/** Maximum fetches allowed per interval window. */
|
|
280
438
|
maxPerInterval?: number;
|
|
439
|
+
/** Whether limits apply globally, per cache key, or per fetcher function. */
|
|
281
440
|
scope?: 'global' | 'key' | 'fetcher';
|
|
441
|
+
/** Custom bucket id used to group otherwise unrelated fetches. */
|
|
282
442
|
bucketKey?: string;
|
|
283
443
|
}
|
|
444
|
+
/** Queue controls for write-behind mode. */
|
|
284
445
|
interface CacheWriteBehindOptions {
|
|
446
|
+
/** Milliseconds between automatic queue flushes. */
|
|
285
447
|
flushIntervalMs?: number;
|
|
448
|
+
/** Maximum entries flushed in one batch. */
|
|
286
449
|
batchSize?: number;
|
|
450
|
+
/** Maximum queued writes before new writes are rejected. */
|
|
287
451
|
maxQueueSize?: number;
|
|
288
452
|
}
|
|
453
|
+
/** Entry used by `CacheStack.warm()` to pre-populate a key. */
|
|
289
454
|
interface CacheWarmEntry<T = unknown> {
|
|
455
|
+
/** Cache key to warm. */
|
|
290
456
|
key: string;
|
|
457
|
+
/** Fetcher used to produce the value. */
|
|
291
458
|
fetcher: CacheFetcher<T>;
|
|
459
|
+
/** Cache options applied while warming this entry. */
|
|
292
460
|
options?: CacheGetOptions;
|
|
461
|
+
/** Higher priority entries are warmed first. */
|
|
293
462
|
priority?: number;
|
|
294
463
|
}
|
|
295
464
|
/** Options controlling the cache warm-up process. */
|
|
296
465
|
interface CacheWarmOptions {
|
|
466
|
+
/** Number of warm fetchers to run concurrently. Defaults to 4. */
|
|
297
467
|
concurrency?: number;
|
|
468
|
+
/** Continue warming remaining entries after an entry fails. */
|
|
298
469
|
continueOnError?: boolean;
|
|
299
470
|
/** Called after each entry is processed (success or failure). */
|
|
300
471
|
onProgress?: (progress: CacheWarmProgress) => void;
|
|
301
472
|
}
|
|
302
473
|
/** Progress information delivered to `CacheWarmOptions.onProgress`. */
|
|
303
474
|
interface CacheWarmProgress {
|
|
475
|
+
/** Number of entries processed so far. */
|
|
304
476
|
completed: number;
|
|
477
|
+
/** Total entries scheduled for warming. */
|
|
305
478
|
total: number;
|
|
479
|
+
/** Key that just finished processing. */
|
|
306
480
|
key: string;
|
|
481
|
+
/** Whether the key was warmed successfully. */
|
|
307
482
|
success: boolean;
|
|
308
483
|
}
|
|
484
|
+
/** Options for `CacheStack.wrap()` and `CacheNamespace.wrap()`. */
|
|
309
485
|
interface CacheWrapOptions<TArgs extends unknown[] = unknown[]> extends CacheGetOptions {
|
|
486
|
+
/** Converts wrapper function arguments into a cache key suffix. */
|
|
310
487
|
keyResolver?: (...args: TArgs) => string;
|
|
311
488
|
}
|
|
489
|
+
/** Entry exported by snapshot APIs. */
|
|
312
490
|
interface CacheSnapshotEntry {
|
|
491
|
+
/** Cache key. */
|
|
313
492
|
key: string;
|
|
493
|
+
/** Stored value or envelope. */
|
|
314
494
|
value: unknown;
|
|
495
|
+
/** Remaining TTL in milliseconds, when available. */
|
|
315
496
|
ttl?: number;
|
|
316
497
|
}
|
|
498
|
+
/** Snapshot of metrics, layer health state, and active background work. */
|
|
317
499
|
interface CacheStatsSnapshot {
|
|
500
|
+
/** Current cumulative metrics. */
|
|
318
501
|
metrics: CacheMetricsSnapshot;
|
|
502
|
+
/** Layer-level runtime state. */
|
|
319
503
|
layers: Array<{
|
|
504
|
+
/** Layer name. */
|
|
320
505
|
name: string;
|
|
506
|
+
/** Whether the layer is local to this process. */
|
|
321
507
|
isLocal: boolean;
|
|
508
|
+
/** Timestamp in milliseconds until which the layer is degraded, or null. */
|
|
322
509
|
degradedUntil: number | null;
|
|
323
510
|
}>;
|
|
511
|
+
/** Number of background refreshes currently running. */
|
|
324
512
|
backgroundRefreshes: number;
|
|
325
513
|
}
|
|
514
|
+
/** Result returned by `CacheStack.healthCheck()`. */
|
|
326
515
|
interface CacheHealthCheckResult {
|
|
516
|
+
/** Layer name. */
|
|
327
517
|
layer: string;
|
|
518
|
+
/** Whether the layer responded successfully. */
|
|
328
519
|
healthy: boolean;
|
|
520
|
+
/** Time spent checking this layer in milliseconds. */
|
|
329
521
|
latencyMs: number;
|
|
522
|
+
/** Failure message when `healthy` is false. */
|
|
330
523
|
error?: string;
|
|
331
524
|
}
|
|
332
525
|
/** Detailed inspection result for a single cache key. */
|
|
333
526
|
interface CacheInspectResult {
|
|
527
|
+
/** User-facing cache key. */
|
|
334
528
|
key: string;
|
|
335
529
|
/** Layers in which the key is currently stored (not expired). */
|
|
336
530
|
foundInLayers: string[];
|
|
@@ -414,8 +608,11 @@ interface CacheStackEvents {
|
|
|
414
608
|
}
|
|
415
609
|
|
|
416
610
|
interface MemoryLayerSnapshotEntry {
|
|
611
|
+
/** Cache key stored in the snapshot. */
|
|
417
612
|
key: string;
|
|
613
|
+
/** Stored raw value or envelope. */
|
|
418
614
|
value: unknown;
|
|
615
|
+
/** Absolute expiry timestamp in milliseconds, or null for no expiry. */
|
|
419
616
|
expiresAt: number | null;
|
|
420
617
|
}
|
|
421
618
|
/**
|
|
@@ -426,13 +623,22 @@ interface MemoryLayerSnapshotEntry {
|
|
|
426
623
|
*/
|
|
427
624
|
type EvictionPolicy = 'lru' | 'lfu' | 'fifo';
|
|
428
625
|
interface MemoryLayerOptions {
|
|
626
|
+
/** Default TTL in milliseconds for entries written without an explicit TTL. */
|
|
429
627
|
ttl?: number;
|
|
628
|
+
/** Maximum number of entries retained in memory. Defaults to 1,000. */
|
|
430
629
|
maxSize?: number;
|
|
630
|
+
/** Layer name used for metrics and per-layer TTL maps. Defaults to `memory`. */
|
|
431
631
|
name?: string;
|
|
632
|
+
/** Eviction policy used when `maxSize` is exceeded. Defaults to `lru`. */
|
|
432
633
|
evictionPolicy?: EvictionPolicy;
|
|
634
|
+
/** Milliseconds between automatic expired-entry cleanup passes. Disabled when omitted. */
|
|
433
635
|
cleanupIntervalMs?: number;
|
|
636
|
+
/** Called with the key and unwrapped value whenever an entry is evicted. */
|
|
434
637
|
onEvict?: (key: string, value: unknown) => void;
|
|
435
638
|
}
|
|
639
|
+
/**
|
|
640
|
+
* In-process cache layer with TTL support and bounded-size eviction.
|
|
641
|
+
*/
|
|
436
642
|
declare class MemoryLayer implements CacheLayer {
|
|
437
643
|
readonly name: string;
|
|
438
644
|
readonly defaultTtl?: number;
|
|
@@ -442,23 +648,77 @@ declare class MemoryLayer implements CacheLayer {
|
|
|
442
648
|
private readonly onEvict?;
|
|
443
649
|
private readonly entries;
|
|
444
650
|
private cleanupTimer?;
|
|
651
|
+
/**
|
|
652
|
+
* Creates an in-memory cache layer.
|
|
653
|
+
*/
|
|
445
654
|
constructor(options?: MemoryLayerOptions);
|
|
655
|
+
/**
|
|
656
|
+
* Reads and unwraps a fresh value from memory.
|
|
657
|
+
*/
|
|
446
658
|
get<T>(key: string): Promise<T | null>;
|
|
659
|
+
/**
|
|
660
|
+
* Reads the raw stored value or envelope from memory.
|
|
661
|
+
*/
|
|
447
662
|
getEntry<T = unknown>(key: string): Promise<T | null>;
|
|
663
|
+
/**
|
|
664
|
+
* Reads many raw entries from memory.
|
|
665
|
+
*/
|
|
448
666
|
getMany<T>(keys: string[]): Promise<Array<T | null>>;
|
|
667
|
+
/**
|
|
668
|
+
* Writes many entries to memory.
|
|
669
|
+
*/
|
|
449
670
|
setMany(entries: CacheLayerSetManyEntry[]): Promise<void>;
|
|
671
|
+
/**
|
|
672
|
+
* Stores a value in memory using the provided TTL or layer default TTL.
|
|
673
|
+
*/
|
|
450
674
|
set(key: string, value: unknown, ttl?: number | undefined): Promise<void>;
|
|
675
|
+
/**
|
|
676
|
+
* Returns true when the key exists and has not expired.
|
|
677
|
+
*/
|
|
451
678
|
has(key: string): Promise<boolean>;
|
|
679
|
+
/**
|
|
680
|
+
* Returns remaining TTL in milliseconds, or null when absent or non-expiring.
|
|
681
|
+
*/
|
|
452
682
|
ttl(key: string): Promise<number | null>;
|
|
683
|
+
/**
|
|
684
|
+
* Returns the number of currently retained, non-expired entries.
|
|
685
|
+
*/
|
|
453
686
|
size(): Promise<number>;
|
|
687
|
+
/**
|
|
688
|
+
* Deletes a key from memory.
|
|
689
|
+
*/
|
|
454
690
|
delete(key: string): Promise<void>;
|
|
691
|
+
/**
|
|
692
|
+
* Deletes multiple keys from memory.
|
|
693
|
+
*/
|
|
455
694
|
deleteMany(keys: string[]): Promise<void>;
|
|
695
|
+
/**
|
|
696
|
+
* Removes all entries from memory.
|
|
697
|
+
*/
|
|
456
698
|
clear(): Promise<void>;
|
|
699
|
+
/**
|
|
700
|
+
* Health check hook that always succeeds for the in-process layer.
|
|
701
|
+
*/
|
|
457
702
|
ping(): Promise<boolean>;
|
|
703
|
+
/**
|
|
704
|
+
* Stops the cleanup timer, when one is active.
|
|
705
|
+
*/
|
|
458
706
|
dispose(): Promise<void>;
|
|
707
|
+
/**
|
|
708
|
+
* Returns all currently retained, non-expired keys.
|
|
709
|
+
*/
|
|
459
710
|
keys(): Promise<string[]>;
|
|
711
|
+
/**
|
|
712
|
+
* Visits all currently retained, non-expired keys.
|
|
713
|
+
*/
|
|
460
714
|
forEachKey(visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
715
|
+
/**
|
|
716
|
+
* Exports memory entries for process-local snapshots.
|
|
717
|
+
*/
|
|
461
718
|
exportState(): MemoryLayerSnapshotEntry[];
|
|
719
|
+
/**
|
|
720
|
+
* Imports entries previously produced by `exportState()`.
|
|
721
|
+
*/
|
|
462
722
|
importState(entries: MemoryLayerSnapshotEntry[]): void;
|
|
463
723
|
private evict;
|
|
464
724
|
private pruneExpired;
|
|
@@ -495,16 +755,49 @@ declare class TagIndex implements CacheTagIndex {
|
|
|
495
755
|
private nextNodeId;
|
|
496
756
|
private readonly root;
|
|
497
757
|
constructor(options?: TagIndexOptions);
|
|
758
|
+
/**
|
|
759
|
+
* Records a key as known without changing tag assignments.
|
|
760
|
+
*/
|
|
498
761
|
touch(key: string): Promise<void>;
|
|
762
|
+
/**
|
|
763
|
+
* Replaces the tags associated with a key and records the key as known.
|
|
764
|
+
*/
|
|
499
765
|
track(key: string, tags: string[]): Promise<void>;
|
|
766
|
+
/**
|
|
767
|
+
* Removes a key from all tag mappings and known-key tracking.
|
|
768
|
+
*/
|
|
500
769
|
remove(key: string): Promise<void>;
|
|
770
|
+
/**
|
|
771
|
+
* Returns keys currently associated with a tag.
|
|
772
|
+
*/
|
|
501
773
|
keysForTag(tag: string): Promise<string[]>;
|
|
774
|
+
/**
|
|
775
|
+
* Visits keys currently associated with a tag.
|
|
776
|
+
*/
|
|
502
777
|
forEachKeyForTag(tag: string, visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
778
|
+
/**
|
|
779
|
+
* Returns known keys that start with a prefix.
|
|
780
|
+
*/
|
|
503
781
|
keysForPrefix(prefix: string): Promise<string[]>;
|
|
782
|
+
/**
|
|
783
|
+
* Visits known keys that start with a prefix.
|
|
784
|
+
*/
|
|
504
785
|
forEachKeyForPrefix(prefix: string, visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
786
|
+
/**
|
|
787
|
+
* Returns the tags currently associated with a key.
|
|
788
|
+
*/
|
|
505
789
|
tagsForKey(key: string): Promise<string[]>;
|
|
790
|
+
/**
|
|
791
|
+
* Returns known keys matching a wildcard pattern.
|
|
792
|
+
*/
|
|
506
793
|
matchPattern(pattern: string): Promise<string[]>;
|
|
794
|
+
/**
|
|
795
|
+
* Visits known keys matching a wildcard pattern.
|
|
796
|
+
*/
|
|
507
797
|
forEachKeyMatchingPattern(pattern: string, visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
798
|
+
/**
|
|
799
|
+
* Clears all tag and known-key index state.
|
|
800
|
+
*/
|
|
508
801
|
clear(): Promise<void>;
|
|
509
802
|
private createTrieNode;
|
|
510
803
|
private insertKnownKey;
|
|
@@ -517,41 +810,133 @@ declare class TagIndex implements CacheTagIndex {
|
|
|
517
810
|
private removeKnownKey;
|
|
518
811
|
}
|
|
519
812
|
|
|
813
|
+
/**
|
|
814
|
+
* Prefix-scoped view over a `CacheStack`.
|
|
815
|
+
*
|
|
816
|
+
* All keys and tags passed through the namespace are qualified with the
|
|
817
|
+
* namespace prefix, while metrics are tracked separately for namespace usage.
|
|
818
|
+
*/
|
|
520
819
|
declare class CacheNamespace {
|
|
521
820
|
private readonly cache;
|
|
522
821
|
private readonly prefix;
|
|
523
822
|
private static readonly metricsMutexes;
|
|
524
823
|
private metrics;
|
|
824
|
+
/**
|
|
825
|
+
* Creates a namespace backed by an existing cache stack.
|
|
826
|
+
*/
|
|
525
827
|
constructor(cache: CacheStack, prefix: string);
|
|
828
|
+
/**
|
|
829
|
+
* Reads a key inside this namespace and optionally runs a read-through fetcher
|
|
830
|
+
* on miss or refresh.
|
|
831
|
+
*/
|
|
526
832
|
get<T>(key: string, fetcher?: CacheFetcher<T>, options?: CacheGetOptions): Promise<T | null>;
|
|
833
|
+
/**
|
|
834
|
+
* Alias for `get(key, fetcher, options)` that makes the get-or-set behavior explicit.
|
|
835
|
+
*/
|
|
527
836
|
getOrSet<T>(key: string, fetcher: CacheFetcher<T>, options?: CacheGetOptions): Promise<T | null>;
|
|
528
837
|
/**
|
|
529
838
|
* Like `get()`, but throws `CacheMissError` instead of returning `null`.
|
|
530
839
|
*/
|
|
531
840
|
getOrThrow<T>(key: string, fetcher?: CacheFetcher<T>, options?: CacheGetOptions): Promise<T>;
|
|
841
|
+
/**
|
|
842
|
+
* Returns true when the namespaced key exists and has not expired in any layer.
|
|
843
|
+
*/
|
|
532
844
|
has(key: string): Promise<boolean>;
|
|
845
|
+
/**
|
|
846
|
+
* Returns the remaining TTL in milliseconds for the namespaced key.
|
|
847
|
+
*/
|
|
533
848
|
ttl(key: string): Promise<number | null>;
|
|
849
|
+
/**
|
|
850
|
+
* Stores a value under a namespaced key.
|
|
851
|
+
*/
|
|
534
852
|
set<T>(key: string, value: T, options?: CacheWriteOptions): Promise<void>;
|
|
853
|
+
/**
|
|
854
|
+
* Deletes a namespaced key from all layers.
|
|
855
|
+
*/
|
|
535
856
|
delete(key: string): Promise<void>;
|
|
857
|
+
/**
|
|
858
|
+
* Deletes multiple namespaced keys from all layers.
|
|
859
|
+
*/
|
|
536
860
|
mdelete(keys: string[]): Promise<void>;
|
|
861
|
+
/**
|
|
862
|
+
* Alias for `delete(key)` scoped to this namespace.
|
|
863
|
+
*/
|
|
864
|
+
invalidateByKey(key: string): Promise<void>;
|
|
865
|
+
/**
|
|
866
|
+
* Alias for `mdelete(keys)` scoped to this namespace.
|
|
867
|
+
*/
|
|
868
|
+
invalidateByKeys(keys: string[]): Promise<void>;
|
|
869
|
+
/**
|
|
870
|
+
* Marks one exact namespaced key expired without deleting its stale value.
|
|
871
|
+
*/
|
|
872
|
+
expireByKey(key: string): Promise<void>;
|
|
873
|
+
/**
|
|
874
|
+
* Marks multiple exact namespaced keys expired without deleting their stale values.
|
|
875
|
+
*/
|
|
876
|
+
expireByKeys(keys: string[]): Promise<void>;
|
|
877
|
+
/**
|
|
878
|
+
* Clears all keys in this namespace by invalidating the namespace prefix.
|
|
879
|
+
*/
|
|
537
880
|
clear(): Promise<void>;
|
|
881
|
+
/**
|
|
882
|
+
* Reads many namespaced keys concurrently.
|
|
883
|
+
*/
|
|
538
884
|
mget<T>(entries: CacheMGetEntry<T>[]): Promise<Array<T | null>>;
|
|
885
|
+
/**
|
|
886
|
+
* Writes many namespaced entries concurrently.
|
|
887
|
+
*/
|
|
539
888
|
mset<T>(entries: CacheMSetEntry<T>[]): Promise<void>;
|
|
889
|
+
/**
|
|
890
|
+
* Deletes keys associated with a tag scoped to this namespace.
|
|
891
|
+
*/
|
|
540
892
|
invalidateByTag(tag: string): Promise<void>;
|
|
893
|
+
/**
|
|
894
|
+
* Expires keys associated with a tag scoped to this namespace while preserving stale windows.
|
|
895
|
+
*/
|
|
541
896
|
expireByTag(tag: string): Promise<void>;
|
|
897
|
+
/**
|
|
898
|
+
* Deletes keys associated with any or all namespace-scoped tags.
|
|
899
|
+
*/
|
|
542
900
|
invalidateByTags(tags: string[], mode?: 'any' | 'all'): Promise<void>;
|
|
901
|
+
/**
|
|
902
|
+
* Expires keys associated with any or all namespace-scoped tags while preserving stale windows.
|
|
903
|
+
*/
|
|
543
904
|
expireByTags(tags: string[], mode?: 'any' | 'all'): Promise<void>;
|
|
905
|
+
/**
|
|
906
|
+
* Deletes namespaced keys matching a wildcard pattern.
|
|
907
|
+
*/
|
|
544
908
|
invalidateByPattern(pattern: string): Promise<void>;
|
|
909
|
+
/**
|
|
910
|
+
* Expires namespaced keys matching a wildcard pattern while preserving stale windows.
|
|
911
|
+
*/
|
|
545
912
|
expireByPattern(pattern: string): Promise<void>;
|
|
913
|
+
/**
|
|
914
|
+
* Deletes namespaced keys with the provided prefix.
|
|
915
|
+
*/
|
|
546
916
|
invalidateByPrefix(prefix: string): Promise<void>;
|
|
917
|
+
/**
|
|
918
|
+
* Expires namespaced keys with the provided prefix while preserving stale windows.
|
|
919
|
+
*/
|
|
547
920
|
expireByPrefix(prefix: string): Promise<void>;
|
|
548
921
|
/**
|
|
549
922
|
* Returns detailed metadata about a single cache key within this namespace.
|
|
550
923
|
*/
|
|
551
924
|
inspect(key: string): Promise<CacheInspectResult | null>;
|
|
925
|
+
/**
|
|
926
|
+
* Returns a cached wrapper whose generated keys are scoped to this namespace.
|
|
927
|
+
*/
|
|
552
928
|
wrap<TArgs extends unknown[], TResult>(keyPrefix: string, fetcher: (...args: TArgs) => Promise<TResult>, options?: CacheWrapOptions<TArgs>): (...args: TArgs) => Promise<TResult | null>;
|
|
929
|
+
/**
|
|
930
|
+
* Warms entries after qualifying each key and tag with this namespace prefix.
|
|
931
|
+
*/
|
|
553
932
|
warm(entries: CacheWarmEntry[], options?: CacheWarmOptions): Promise<void>;
|
|
933
|
+
/**
|
|
934
|
+
* Returns metrics accumulated by operations performed through this namespace.
|
|
935
|
+
*/
|
|
554
936
|
getMetrics(): CacheMetricsSnapshot;
|
|
937
|
+
/**
|
|
938
|
+
* Returns hit-rate statistics for operations performed through this namespace.
|
|
939
|
+
*/
|
|
555
940
|
getHitRate(): CacheHitRateSnapshot;
|
|
556
941
|
/**
|
|
557
942
|
* Creates a nested namespace. Keys are prefixed with `parentPrefix:childPrefix:`.
|
|
@@ -563,6 +948,9 @@ declare class CacheNamespace {
|
|
|
563
948
|
* ```
|
|
564
949
|
*/
|
|
565
950
|
namespace(childPrefix: string): CacheNamespace;
|
|
951
|
+
/**
|
|
952
|
+
* Qualifies a raw key with this namespace prefix.
|
|
953
|
+
*/
|
|
566
954
|
qualify(key: string): string;
|
|
567
955
|
private qualifyTag;
|
|
568
956
|
private qualifyGetOptions;
|
|
@@ -574,14 +962,27 @@ declare class CacheNamespace {
|
|
|
574
962
|
|
|
575
963
|
/** Typed overloads for EventEmitter so callers get autocomplete on event names. */
|
|
576
964
|
interface CacheStack {
|
|
965
|
+
/** Register a typed CacheStack event listener. */
|
|
577
966
|
on<K extends keyof CacheStackEvents>(event: K, listener: (data: CacheStackEvents[K]) => void): this;
|
|
967
|
+
/** Register a typed CacheStack event listener that runs once. */
|
|
578
968
|
once<K extends keyof CacheStackEvents>(event: K, listener: (data: CacheStackEvents[K]) => void): this;
|
|
969
|
+
/** Remove a typed CacheStack event listener. */
|
|
579
970
|
off<K extends keyof CacheStackEvents>(event: K, listener: (data: CacheStackEvents[K]) => void): this;
|
|
971
|
+
/** Remove all listeners, optionally only for one typed CacheStack event. */
|
|
580
972
|
removeAllListeners<K extends keyof CacheStackEvents>(event?: K): this;
|
|
973
|
+
/** Return listeners registered for a typed CacheStack event. */
|
|
581
974
|
listeners<K extends keyof CacheStackEvents>(event: K): Array<(data: CacheStackEvents[K]) => void>;
|
|
975
|
+
/** Return the listener count for a typed CacheStack event. */
|
|
582
976
|
listenerCount<K extends keyof CacheStackEvents>(event: K): number;
|
|
977
|
+
/** Emit a typed CacheStack event. Mostly useful for custom integrations. */
|
|
583
978
|
emit<K extends keyof CacheStackEvents>(event: K, data: CacheStackEvents[K]): boolean;
|
|
584
979
|
}
|
|
980
|
+
/**
|
|
981
|
+
* Multi-layer read-through cache coordinator.
|
|
982
|
+
*
|
|
983
|
+
* Layers are checked from fastest to slowest, partial hits are backfilled into
|
|
984
|
+
* faster layers, and misses can be resolved by read-through fetchers.
|
|
985
|
+
*/
|
|
585
986
|
declare class CacheStack extends EventEmitter {
|
|
586
987
|
private readonly layers;
|
|
587
988
|
private readonly options;
|
|
@@ -607,6 +1008,9 @@ declare class CacheStack extends EventEmitter {
|
|
|
607
1008
|
private isDisconnecting;
|
|
608
1009
|
private readonly reader;
|
|
609
1010
|
private disconnectPromise?;
|
|
1011
|
+
/**
|
|
1012
|
+
* Creates a cache stack from ordered layers and optional global behavior settings.
|
|
1013
|
+
*/
|
|
610
1014
|
constructor(layers: CacheLayer[], options?: CacheStackOptions);
|
|
611
1015
|
/**
|
|
612
1016
|
* Read-through cache get.
|
|
@@ -643,13 +1047,45 @@ declare class CacheStack extends EventEmitter {
|
|
|
643
1047
|
* Deletes the key from all layers and publishes an invalidation message.
|
|
644
1048
|
*/
|
|
645
1049
|
delete(key: string): Promise<void>;
|
|
1050
|
+
/**
|
|
1051
|
+
* Clears every configured layer, removes tag metadata, resets internal TTL
|
|
1052
|
+
* profiles, and broadcasts a clear invalidation message.
|
|
1053
|
+
*/
|
|
646
1054
|
clear(): Promise<void>;
|
|
647
1055
|
/**
|
|
648
1056
|
* Deletes multiple keys at once. More efficient than calling `delete()` in a loop.
|
|
649
1057
|
*/
|
|
650
1058
|
mdelete(keys: string[]): Promise<void>;
|
|
1059
|
+
/**
|
|
1060
|
+
* Alias for `delete(key)` that matches the `invalidateBy*` API family.
|
|
1061
|
+
*/
|
|
1062
|
+
invalidateByKey(key: string): Promise<void>;
|
|
1063
|
+
/**
|
|
1064
|
+
* Alias for `mdelete(keys)` that matches the `invalidateBy*` API family.
|
|
1065
|
+
*/
|
|
1066
|
+
invalidateByKeys(keys: string[]): Promise<void>;
|
|
1067
|
+
/**
|
|
1068
|
+
* Marks one exact key expired without deleting its stale value.
|
|
1069
|
+
*/
|
|
1070
|
+
expireByKey(key: string): Promise<void>;
|
|
1071
|
+
/**
|
|
1072
|
+
* Marks multiple exact keys expired without deleting their stale values.
|
|
1073
|
+
*/
|
|
1074
|
+
expireByKeys(keys: string[]): Promise<void>;
|
|
1075
|
+
/**
|
|
1076
|
+
* Reads many keys concurrently. Simple reads use layer-level bulk fast paths;
|
|
1077
|
+
* entries with fetchers or options fall back to per-entry read-through logic.
|
|
1078
|
+
*/
|
|
651
1079
|
mget<T>(entries: CacheMGetEntry<T>[]): Promise<Array<T | null>>;
|
|
1080
|
+
/**
|
|
1081
|
+
* Writes many entries concurrently using each layer's bulk write fast path
|
|
1082
|
+
* when available.
|
|
1083
|
+
*/
|
|
652
1084
|
mset<T>(entries: CacheMSetEntry<T>[]): Promise<void>;
|
|
1085
|
+
/**
|
|
1086
|
+
* Pre-populates cache entries by running their fetchers with bounded
|
|
1087
|
+
* concurrency. Higher-priority entries run first.
|
|
1088
|
+
*/
|
|
653
1089
|
warm(entries: CacheWarmEntry[], options?: CacheWarmOptions): Promise<void>;
|
|
654
1090
|
/**
|
|
655
1091
|
* Returns a cached version of `fetcher`. The cache key is derived from
|
|
@@ -661,21 +1097,64 @@ declare class CacheStack extends EventEmitter {
|
|
|
661
1097
|
* `prefix:`. Useful for multi-tenant or module-level isolation.
|
|
662
1098
|
*/
|
|
663
1099
|
namespace(prefix: string): CacheNamespace;
|
|
1100
|
+
/**
|
|
1101
|
+
* Deletes every key currently associated with `tag` and broadcasts an
|
|
1102
|
+
* invalidation message.
|
|
1103
|
+
*/
|
|
664
1104
|
invalidateByTag(tag: string): Promise<void>;
|
|
1105
|
+
/**
|
|
1106
|
+
* Marks every key associated with `tag` as expired while preserving stale
|
|
1107
|
+
* windows for stale serving.
|
|
1108
|
+
*/
|
|
665
1109
|
expireByTag(tag: string): Promise<void>;
|
|
1110
|
+
/**
|
|
1111
|
+
* Deletes keys associated with any or all of the provided tags and broadcasts
|
|
1112
|
+
* an invalidation message.
|
|
1113
|
+
*/
|
|
666
1114
|
invalidateByTags(tags: string[], mode?: 'any' | 'all'): Promise<void>;
|
|
1115
|
+
/**
|
|
1116
|
+
* Marks keys associated with any or all of the provided tags as expired while
|
|
1117
|
+
* preserving stale windows for stale serving.
|
|
1118
|
+
*/
|
|
667
1119
|
expireByTags(tags: string[], mode?: 'any' | 'all'): Promise<void>;
|
|
1120
|
+
/**
|
|
1121
|
+
* Deletes keys matching a wildcard pattern such as `user:*`.
|
|
1122
|
+
*/
|
|
668
1123
|
invalidateByPattern(pattern: string): Promise<void>;
|
|
1124
|
+
/**
|
|
1125
|
+
* Marks keys matching a wildcard pattern as expired while preserving stale
|
|
1126
|
+
* windows for stale serving.
|
|
1127
|
+
*/
|
|
669
1128
|
expireByPattern(pattern: string): Promise<void>;
|
|
1129
|
+
/**
|
|
1130
|
+
* Deletes keys that start with the provided prefix.
|
|
1131
|
+
*/
|
|
670
1132
|
invalidateByPrefix(prefix: string): Promise<void>;
|
|
1133
|
+
/**
|
|
1134
|
+
* Marks keys that start with the provided prefix as expired while preserving
|
|
1135
|
+
* stale windows for stale serving.
|
|
1136
|
+
*/
|
|
671
1137
|
expireByPrefix(prefix: string): Promise<void>;
|
|
1138
|
+
/**
|
|
1139
|
+
* Returns cumulative cache metrics since startup or the last `resetMetrics()`.
|
|
1140
|
+
*/
|
|
672
1141
|
getMetrics(): CacheMetricsSnapshot;
|
|
1142
|
+
/**
|
|
1143
|
+
* Returns metrics plus layer degradation state and active background refresh count.
|
|
1144
|
+
*/
|
|
673
1145
|
getStats(): CacheStatsSnapshot;
|
|
1146
|
+
/**
|
|
1147
|
+
* Resets cumulative metrics counters.
|
|
1148
|
+
*/
|
|
674
1149
|
resetMetrics(): void;
|
|
675
1150
|
/**
|
|
676
1151
|
* Returns computed hit-rate statistics (overall and per-layer).
|
|
677
1152
|
*/
|
|
678
1153
|
getHitRate(): CacheHitRateSnapshot;
|
|
1154
|
+
/**
|
|
1155
|
+
* Runs each layer's `ping()` hook when available and returns per-layer health
|
|
1156
|
+
* and latency information.
|
|
1157
|
+
*/
|
|
679
1158
|
healthCheck(): Promise<CacheHealthCheckResult[]>;
|
|
680
1159
|
/**
|
|
681
1160
|
* Rotates the active generation prefix used for all future cache keys.
|
|
@@ -689,10 +1168,26 @@ declare class CacheStack extends EventEmitter {
|
|
|
689
1168
|
* Returns `null` if the key does not exist in any layer.
|
|
690
1169
|
*/
|
|
691
1170
|
inspect(key: string): Promise<CacheInspectResult | null>;
|
|
1171
|
+
/**
|
|
1172
|
+
* Exports cache entries from configured layers for process-local snapshots.
|
|
1173
|
+
*/
|
|
692
1174
|
exportState(): Promise<CacheSnapshotEntry[]>;
|
|
1175
|
+
/**
|
|
1176
|
+
* Imports entries produced by `exportState()` into the configured layers.
|
|
1177
|
+
*/
|
|
693
1178
|
importState(entries: CacheSnapshotEntry[]): Promise<void>;
|
|
1179
|
+
/**
|
|
1180
|
+
* Writes a snapshot file containing current cache entries.
|
|
1181
|
+
*/
|
|
694
1182
|
persistToFile(filePath: string): Promise<void>;
|
|
1183
|
+
/**
|
|
1184
|
+
* Restores cache entries from a snapshot file.
|
|
1185
|
+
*/
|
|
695
1186
|
restoreFromFile(filePath: string): Promise<void>;
|
|
1187
|
+
/**
|
|
1188
|
+
* Flushes background work, unsubscribes from buses, disposes timers, and then
|
|
1189
|
+
* disposes each layer that provides `dispose()`.
|
|
1190
|
+
*/
|
|
696
1191
|
disconnect(): Promise<void>;
|
|
697
1192
|
private initialize;
|
|
698
1193
|
private storeEntry;
|
|
@@ -754,10 +1249,19 @@ interface HonoLikeContext {
|
|
|
754
1249
|
json: (body: unknown, status?: number) => Response | Promise<Response> | unknown;
|
|
755
1250
|
}
|
|
756
1251
|
interface HonoCacheMiddlewareOptions extends CacheGetOptions {
|
|
1252
|
+
/**
|
|
1253
|
+
* Resolves a cache key from the incoming Hono request. Defaults to
|
|
1254
|
+
* `GET:<normalized path>` when `allowPrivateCaching` is enabled.
|
|
1255
|
+
*/
|
|
757
1256
|
keyResolver?: (request: HonoLikeRequest) => string;
|
|
1257
|
+
/** Only cache responses for these HTTP methods. Defaults to `['GET']`. */
|
|
758
1258
|
methods?: string[];
|
|
1259
|
+
/** Explicitly allow URL-only implicit cache keys. Disabled by default. */
|
|
759
1260
|
allowPrivateCaching?: boolean;
|
|
760
1261
|
}
|
|
1262
|
+
/**
|
|
1263
|
+
* Hono-compatible middleware that caches JSON responses for selected methods.
|
|
1264
|
+
*/
|
|
761
1265
|
declare function createHonoCacheMiddleware(cache: CacheStack, options?: HonoCacheMiddlewareOptions): (context: HonoLikeContext, next: () => Promise<void>) => Promise<unknown>;
|
|
762
1266
|
|
|
763
1267
|
export { CacheNamespace as A, type CacheRateLimitOptions as B, type CacheLogger as C, type CacheSnapshotEntry as D, type CacheStackEvents as E, type CacheStackOptions as F, type CacheStatsSnapshot as G, type CacheTtlPolicy as H, type InvalidationBus as I, type CacheTtlPolicyContext as J, type CacheWarmEntry as K, type CacheWarmOptions as L, type CacheWarmProgress as M, type CacheWriteBehindOptions as N, type CacheWriteOptions as O, type EvictionPolicy as P, type LayerTtlMap as Q, MemoryLayer as R, type MemoryLayerOptions as S, type MemoryLayerSnapshotEntry as T, PatternMatcher as U, TagIndex as V, createHonoCacheMiddleware as W, 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 CacheContextOptionsContext as m, type CacheDegradationOptions as n, type CacheEntryWriteKind as o, type CacheEntryWriteOptions as p, type CacheFetcher as q, type CacheFetcherContext as r, type CacheHealthCheckResult as s, type CacheHitRateSnapshot as t, type CacheInspectResult as u, type CacheLayerLatency as v, type CacheMGetEntry as w, type CacheMSetEntry as x, type CacheMetricsSnapshot as y, CacheMissError as z };
|