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
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import { I as InvalidationBus, C as CacheLogger, a as InvalidationMessage, b as CacheTagIndex, c as CacheStack, d as CacheWrapOptions, e as CacheGetOptions, f as CacheLayer, g as CacheSerializer, h as CacheLayerSetManyEntry, i as CacheSingleFlightCoordinator, j as CacheSingleFlightExecutionOptions } from './edge-
|
|
2
|
-
export { k as CacheAdaptiveTtlOptions, l as CacheCircuitBreakerOptions, m as CacheContextOptionsContext, n as CacheDegradationOptions, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, q as CacheFetcher, r as CacheFetcherContext, s as CacheHealthCheckResult, t as CacheHitRateSnapshot, u as CacheInspectResult, v as CacheLayerLatency, w as CacheMGetEntry, x as CacheMSetEntry, y as CacheMetricsSnapshot, z as CacheMissError, A as CacheNamespace, B as CacheRateLimitOptions, D as CacheSnapshotEntry, E as CacheStackEvents, F as CacheStackOptions, G as CacheStatsSnapshot, H as CacheTtlPolicy, J as CacheTtlPolicyContext, K as CacheWarmEntry, L as CacheWarmOptions, M as CacheWarmProgress, N as CacheWriteBehindOptions, O as CacheWriteOptions, P as EvictionPolicy, Q as LayerTtlMap, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-
|
|
1
|
+
import { I as InvalidationBus, C as CacheLogger, a as InvalidationMessage, b as CacheTagIndex, c as CacheStack, d as CacheWrapOptions, e as CacheGetOptions, f as CacheLayer, g as CacheSerializer, h as CacheLayerSetManyEntry, i as CacheSingleFlightCoordinator, j as CacheSingleFlightExecutionOptions } from './edge-BCU8D-Yd.cjs';
|
|
2
|
+
export { k as CacheAdaptiveTtlOptions, l as CacheCircuitBreakerOptions, m as CacheContextOptionsContext, n as CacheDegradationOptions, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, q as CacheFetcher, r as CacheFetcherContext, s as CacheHealthCheckResult, t as CacheHitRateSnapshot, u as CacheInspectResult, v as CacheLayerLatency, w as CacheMGetEntry, x as CacheMSetEntry, y as CacheMetricsSnapshot, z as CacheMissError, A as CacheNamespace, B as CacheRateLimitOptions, D as CacheSnapshotEntry, E as CacheStackEvents, F as CacheStackOptions, G as CacheStatsSnapshot, H as CacheTtlPolicy, J as CacheTtlPolicyContext, K as CacheWarmEntry, L as CacheWarmOptions, M as CacheWarmProgress, N as CacheWriteBehindOptions, O as CacheWriteOptions, P as EvictionPolicy, Q as LayerTtlMap, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-BCU8D-Yd.cjs';
|
|
3
3
|
import Redis from 'ioredis';
|
|
4
4
|
import 'node:events';
|
|
5
5
|
|
|
6
6
|
interface RedisInvalidationBusOptions {
|
|
7
|
+
/** Redis client used to publish invalidation messages. */
|
|
7
8
|
publisher: Redis;
|
|
9
|
+
/** Redis client used for subscriptions. Defaults to `publisher.duplicate()`. */
|
|
8
10
|
subscriber?: Redis;
|
|
11
|
+
/** Pub/sub channel name. Defaults to `layercache:invalidation`. */
|
|
9
12
|
channel?: string;
|
|
13
|
+
/** Optional logger for invalid payloads or subscriber errors. */
|
|
10
14
|
logger?: CacheLogger;
|
|
11
15
|
}
|
|
12
16
|
/**
|
|
@@ -25,7 +29,13 @@ declare class RedisInvalidationBus implements InvalidationBus {
|
|
|
25
29
|
private sharedListener?;
|
|
26
30
|
private subscribePromise;
|
|
27
31
|
constructor(options: RedisInvalidationBusOptions);
|
|
32
|
+
/**
|
|
33
|
+
* Subscribes to invalidation messages and returns an unsubscribe function.
|
|
34
|
+
*/
|
|
28
35
|
subscribe(handler: (message: InvalidationMessage) => Promise<void> | void): Promise<() => Promise<void>>;
|
|
36
|
+
/**
|
|
37
|
+
* Publishes an invalidation message to other subscribers.
|
|
38
|
+
*/
|
|
29
39
|
publish(message: InvalidationMessage): Promise<void>;
|
|
30
40
|
private dispatchToHandlers;
|
|
31
41
|
private isInvalidationMessage;
|
|
@@ -33,9 +43,13 @@ declare class RedisInvalidationBus implements InvalidationBus {
|
|
|
33
43
|
}
|
|
34
44
|
|
|
35
45
|
interface RedisTagIndexOptions {
|
|
46
|
+
/** Redis client used for tag and known-key sets. */
|
|
36
47
|
client: Redis;
|
|
48
|
+
/** Redis key prefix for index data. Defaults to `layercache:tag-index`. */
|
|
37
49
|
prefix?: string;
|
|
50
|
+
/** Redis SCAN count hint used by pattern and prefix discovery. Defaults to 100. */
|
|
38
51
|
scanCount?: number;
|
|
52
|
+
/** Number of shards for known-key sets. Defaults to 16. */
|
|
39
53
|
knownKeysShards?: number;
|
|
40
54
|
}
|
|
41
55
|
declare class RedisTagIndex implements CacheTagIndex {
|
|
@@ -44,16 +58,49 @@ declare class RedisTagIndex implements CacheTagIndex {
|
|
|
44
58
|
private readonly scanCount;
|
|
45
59
|
private readonly knownKeysShards;
|
|
46
60
|
constructor(options: RedisTagIndexOptions);
|
|
61
|
+
/**
|
|
62
|
+
* Records a key as known without changing tag assignments.
|
|
63
|
+
*/
|
|
47
64
|
touch(key: string): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Replaces the tags associated with a key and records the key as known.
|
|
67
|
+
*/
|
|
48
68
|
track(key: string, tags: string[]): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Removes a key from all tag mappings and known-key tracking.
|
|
71
|
+
*/
|
|
49
72
|
remove(key: string): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Returns keys currently associated with a tag.
|
|
75
|
+
*/
|
|
50
76
|
keysForTag(tag: string): Promise<string[]>;
|
|
77
|
+
/**
|
|
78
|
+
* Visits keys currently associated with a tag.
|
|
79
|
+
*/
|
|
51
80
|
forEachKeyForTag(tag: string, visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Returns known keys that start with a prefix.
|
|
83
|
+
*/
|
|
52
84
|
keysForPrefix(prefix: string): Promise<string[]>;
|
|
85
|
+
/**
|
|
86
|
+
* Visits known keys that start with a prefix.
|
|
87
|
+
*/
|
|
53
88
|
forEachKeyForPrefix(prefix: string, visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Returns the tags currently associated with a key.
|
|
91
|
+
*/
|
|
54
92
|
tagsForKey(key: string): Promise<string[]>;
|
|
93
|
+
/**
|
|
94
|
+
* Returns known keys matching a wildcard pattern.
|
|
95
|
+
*/
|
|
55
96
|
matchPattern(pattern: string): Promise<string[]>;
|
|
97
|
+
/**
|
|
98
|
+
* Visits known keys matching a wildcard pattern.
|
|
99
|
+
*/
|
|
56
100
|
forEachKeyMatchingPattern(pattern: string, visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Clears all Redis tag-index state under this prefix.
|
|
103
|
+
*/
|
|
57
104
|
clear(): Promise<void>;
|
|
58
105
|
private scanIndexKeys;
|
|
59
106
|
private knownKeysKeyFor;
|
|
@@ -69,9 +116,14 @@ interface CacheStatsHandlerOptions {
|
|
|
69
116
|
* future release.
|
|
70
117
|
*/
|
|
71
118
|
allowPublicAccess?: boolean;
|
|
119
|
+
/** Authorize requests before returning cache stats. Required unless `allowPublicAccess` is true. */
|
|
72
120
|
authorize?: (request: unknown) => boolean | Promise<boolean>;
|
|
121
|
+
/** Status code returned when a request is unauthorized. Defaults to 403. */
|
|
73
122
|
unauthorizedStatusCode?: number;
|
|
74
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Creates a small Node HTTP handler that returns `cache.getStats()` as JSON.
|
|
126
|
+
*/
|
|
75
127
|
declare function createCacheStatsHandler(cache: CacheStack, options?: CacheStatsHandlerOptions): (request: unknown, response: {
|
|
76
128
|
setHeader?: (name: string, value: string) => void;
|
|
77
129
|
end: (body: string) => void;
|
|
@@ -79,9 +131,14 @@ declare function createCacheStatsHandler(cache: CacheStack, options?: CacheStats
|
|
|
79
131
|
}) => Promise<void>;
|
|
80
132
|
|
|
81
133
|
interface CachedMethodDecoratorOptions<TArgs extends unknown[]> extends CacheWrapOptions<TArgs> {
|
|
134
|
+
/** Returns the CacheStack instance used for the decorated object instance. */
|
|
82
135
|
cache: (instance: unknown) => CacheStack;
|
|
136
|
+
/** Cache key prefix for the method. Defaults to the decorated property name. */
|
|
83
137
|
prefix?: string;
|
|
84
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Creates a method decorator that caches async method results per instance.
|
|
141
|
+
*/
|
|
85
142
|
declare function createCachedMethodDecorator<TArgs extends unknown[] = unknown[]>(options: CachedMethodDecoratorOptions<TArgs>): MethodDecorator;
|
|
86
143
|
|
|
87
144
|
interface FastifyLike {
|
|
@@ -94,7 +151,9 @@ interface FastifyLikeReply {
|
|
|
94
151
|
statusCode?: number;
|
|
95
152
|
}
|
|
96
153
|
interface FastifyLayercachePluginOptions {
|
|
154
|
+
/** Register an HTTP route that returns `cache.getStats()`. Disabled by default. */
|
|
97
155
|
exposeStatsRoute?: boolean;
|
|
156
|
+
/** Path for the stats route when `exposeStatsRoute` is enabled. Defaults to `/cache/stats`. */
|
|
98
157
|
statsPath?: string;
|
|
99
158
|
/**
|
|
100
159
|
* @deprecated Exposing cache stats without authentication is a security risk.
|
|
@@ -102,9 +161,14 @@ interface FastifyLayercachePluginOptions {
|
|
|
102
161
|
* removed in a future release.
|
|
103
162
|
*/
|
|
104
163
|
allowPublicStatsRoute?: boolean;
|
|
164
|
+
/** Authorize requests to the stats route. Required unless `allowPublicStatsRoute` is true. */
|
|
105
165
|
authorizeStatsRoute?: (request: unknown) => boolean | Promise<boolean>;
|
|
166
|
+
/** Status code returned when a stats route request is unauthorized. Defaults to 403. */
|
|
106
167
|
unauthorizedStatusCode?: number;
|
|
107
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Fastify plugin that decorates the server with `cache` and can expose a protected stats route.
|
|
171
|
+
*/
|
|
108
172
|
declare function createFastifyLayercachePlugin(cache: CacheStack, options?: FastifyLayercachePluginOptions): (fastify: FastifyLike) => Promise<void>;
|
|
109
173
|
|
|
110
174
|
interface ExpressLikeRequest {
|
|
@@ -154,9 +218,14 @@ interface ExpressCacheMiddlewareOptions extends CacheGetOptions {
|
|
|
154
218
|
declare function createExpressCacheMiddleware(cache: CacheStack, options?: ExpressCacheMiddlewareOptions): (req: ExpressLikeRequest, res: ExpressLikeResponse, next: NextFunction) => Promise<void>;
|
|
155
219
|
|
|
156
220
|
interface GraphqlCacheOptions<TArgs extends unknown[]> extends CacheGetOptions {
|
|
221
|
+
/** Converts resolver arguments into a stable cache key suffix. */
|
|
157
222
|
keyResolver?: (...args: TArgs) => string;
|
|
223
|
+
/** Allow fallback key generation from resolver args when no `keyResolver` is provided. */
|
|
158
224
|
allowImplicitContextCaching?: boolean;
|
|
159
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Wraps a GraphQL resolver with read-through caching.
|
|
228
|
+
*/
|
|
160
229
|
declare function cacheGraphqlResolver<TArgs extends unknown[], TResult>(cache: CacheStack, prefix: string, resolver: (...args: TArgs) => Promise<TResult>, options?: GraphqlCacheOptions<TArgs>): (...args: TArgs) => Promise<TResult | null>;
|
|
161
230
|
|
|
162
231
|
interface OpenTelemetrySpan {
|
|
@@ -180,18 +249,27 @@ declare function createOpenTelemetryPlugin(cache: CacheStack, tracer: OpenTeleme
|
|
|
180
249
|
};
|
|
181
250
|
|
|
182
251
|
interface TrpcCacheMiddlewareContext<TInput = unknown, TResult = unknown> {
|
|
252
|
+
/** Procedure path, when supplied by the adapter. */
|
|
183
253
|
path?: string;
|
|
254
|
+
/** Procedure type, such as query or mutation. */
|
|
184
255
|
type?: string;
|
|
256
|
+
/** Raw procedure input used by the key resolver. */
|
|
185
257
|
rawInput?: TInput;
|
|
258
|
+
/** Runs the next tRPC middleware or resolver. */
|
|
186
259
|
next: () => Promise<{
|
|
187
260
|
ok: boolean;
|
|
188
261
|
data?: TResult;
|
|
189
262
|
}>;
|
|
190
263
|
}
|
|
191
264
|
interface TrpcCacheMiddlewareOptions<TInput> extends CacheGetOptions {
|
|
265
|
+
/** Converts procedure input and metadata into a stable cache key suffix. */
|
|
192
266
|
keyResolver?: (input: TInput, path?: string, type?: string) => string;
|
|
267
|
+
/** Allow fallback key generation from procedure path and raw input. */
|
|
193
268
|
allowImplicitContextCaching?: boolean;
|
|
194
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Creates a tRPC middleware that caches successful procedure results.
|
|
272
|
+
*/
|
|
195
273
|
declare function createTrpcCacheMiddleware<TInput = unknown, TResult = unknown>(cache: CacheStack, prefix: string, options?: TrpcCacheMiddlewareOptions<TInput>): (context: TrpcCacheMiddlewareContext<TInput, TResult>) => Promise<{
|
|
196
274
|
ok: boolean;
|
|
197
275
|
data?: TResult;
|
|
@@ -199,14 +277,23 @@ declare function createTrpcCacheMiddleware<TInput = unknown, TResult = unknown>(
|
|
|
199
277
|
|
|
200
278
|
type CompressionAlgorithm = 'gzip' | 'brotli';
|
|
201
279
|
interface RedisLayerOptions {
|
|
280
|
+
/** ioredis client used for all Redis commands. */
|
|
202
281
|
client: Redis;
|
|
282
|
+
/** Default TTL in milliseconds for writes that do not provide an explicit TTL. */
|
|
203
283
|
ttl?: number;
|
|
284
|
+
/** Layer name used for metrics and per-layer TTL maps. Defaults to `redis`. */
|
|
204
285
|
name?: string;
|
|
286
|
+
/** Serializer or serializer chain used to encode values before storage. */
|
|
205
287
|
serializer?: CacheSerializer | CacheSerializer[];
|
|
288
|
+
/** Prefix prepended to every Redis key. */
|
|
206
289
|
prefix?: string;
|
|
290
|
+
/** Allow `clear()` without a key prefix. Disabled by default to avoid deleting unrelated Redis keys. */
|
|
207
291
|
allowUnprefixedClear?: boolean;
|
|
292
|
+
/** Redis SCAN count hint used when discovering keys. Defaults to 100. */
|
|
208
293
|
scanCount?: number;
|
|
294
|
+
/** Optional compression algorithm applied to large serialized payloads. */
|
|
209
295
|
compression?: CompressionAlgorithm;
|
|
296
|
+
/** Minimum serialized payload size in bytes before compression is attempted. Defaults to 1 KiB. */
|
|
210
297
|
compressionThreshold?: number;
|
|
211
298
|
/**
|
|
212
299
|
* Maximum number of bytes allowed after decompression.
|
|
@@ -218,8 +305,12 @@ interface RedisLayerOptions {
|
|
|
218
305
|
* Slow commands reject so CacheStack can treat the layer as degraded.
|
|
219
306
|
*/
|
|
220
307
|
commandTimeoutMs?: number;
|
|
308
|
+
/** Disconnect the Redis client when this layer is disposed. Disabled by default. */
|
|
221
309
|
disconnectOnDispose?: boolean;
|
|
222
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* Redis-backed cache layer for shared cross-process cache state.
|
|
313
|
+
*/
|
|
223
314
|
declare class RedisLayer implements CacheLayer {
|
|
224
315
|
readonly name: string;
|
|
225
316
|
readonly defaultTtl?: number;
|
|
@@ -234,25 +325,70 @@ declare class RedisLayer implements CacheLayer {
|
|
|
234
325
|
private readonly decompressionMaxBytes;
|
|
235
326
|
private readonly commandTimeoutMs;
|
|
236
327
|
private readonly disconnectOnDispose;
|
|
328
|
+
/**
|
|
329
|
+
* Creates a Redis cache layer using an existing ioredis client.
|
|
330
|
+
*/
|
|
237
331
|
constructor(options: RedisLayerOptions);
|
|
332
|
+
/**
|
|
333
|
+
* Reads and unwraps a fresh value from Redis.
|
|
334
|
+
*/
|
|
238
335
|
get<T>(key: string): Promise<T | null>;
|
|
336
|
+
/**
|
|
337
|
+
* Reads the raw stored value or envelope from Redis.
|
|
338
|
+
*/
|
|
239
339
|
getEntry<T = unknown>(key: string): Promise<T | null>;
|
|
340
|
+
/**
|
|
341
|
+
* Reads many raw entries from Redis using a pipeline.
|
|
342
|
+
*/
|
|
240
343
|
getMany<T>(keys: string[]): Promise<Array<T | null>>;
|
|
344
|
+
/**
|
|
345
|
+
* Writes many entries to Redis using a pipeline.
|
|
346
|
+
*/
|
|
241
347
|
setMany(entries: CacheLayerSetManyEntry[]): Promise<void>;
|
|
348
|
+
/**
|
|
349
|
+
* Stores a value in Redis using the provided TTL or layer default TTL.
|
|
350
|
+
*/
|
|
242
351
|
set(key: string, value: unknown, ttl?: number | undefined): Promise<void>;
|
|
352
|
+
/**
|
|
353
|
+
* Deletes a key from Redis.
|
|
354
|
+
*/
|
|
243
355
|
delete(key: string): Promise<void>;
|
|
356
|
+
/**
|
|
357
|
+
* Deletes multiple keys from Redis in batches.
|
|
358
|
+
*/
|
|
244
359
|
deleteMany(keys: string[]): Promise<void>;
|
|
360
|
+
/**
|
|
361
|
+
* Returns true when the key exists in Redis.
|
|
362
|
+
*/
|
|
245
363
|
has(key: string): Promise<boolean>;
|
|
364
|
+
/**
|
|
365
|
+
* Returns remaining Redis TTL in milliseconds, or null when absent or non-expiring.
|
|
366
|
+
*/
|
|
246
367
|
ttl(key: string): Promise<number | null>;
|
|
368
|
+
/**
|
|
369
|
+
* Returns the number of keys under this layer's prefix.
|
|
370
|
+
*/
|
|
247
371
|
size(): Promise<number>;
|
|
372
|
+
/**
|
|
373
|
+
* Runs a Redis ping command.
|
|
374
|
+
*/
|
|
248
375
|
ping(): Promise<boolean>;
|
|
376
|
+
/**
|
|
377
|
+
* Disconnects the Redis client when `disconnectOnDispose` is enabled.
|
|
378
|
+
*/
|
|
249
379
|
dispose(): Promise<void>;
|
|
250
380
|
/**
|
|
251
381
|
* Deletes all keys matching the layer's prefix in batches to avoid
|
|
252
382
|
* loading millions of keys into memory at once.
|
|
253
383
|
*/
|
|
254
384
|
clear(): Promise<void>;
|
|
385
|
+
/**
|
|
386
|
+
* Returns keys under this layer's prefix without the prefix included.
|
|
387
|
+
*/
|
|
255
388
|
keys(): Promise<string[]>;
|
|
389
|
+
/**
|
|
390
|
+
* Visits keys under this layer's prefix without materializing all results.
|
|
391
|
+
*/
|
|
256
392
|
forEachKey(visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
257
393
|
private scanKeys;
|
|
258
394
|
private withPrefix;
|
|
@@ -279,9 +415,13 @@ declare class RedisLayer implements CacheLayer {
|
|
|
279
415
|
}
|
|
280
416
|
|
|
281
417
|
interface DiskLayerOptions {
|
|
418
|
+
/** Directory where cache entry files are stored. */
|
|
282
419
|
directory: string;
|
|
420
|
+
/** Default TTL in milliseconds for writes that do not provide an explicit TTL. */
|
|
283
421
|
ttl?: number;
|
|
422
|
+
/** Layer name used for metrics and per-layer TTL maps. Defaults to `disk`. */
|
|
284
423
|
name?: string;
|
|
424
|
+
/** Serializer used to encode values before writing them to disk. */
|
|
285
425
|
serializer?: CacheSerializer;
|
|
286
426
|
/**
|
|
287
427
|
* Maximum number of cache files to store on disk. When exceeded, the oldest
|
|
@@ -331,25 +471,70 @@ declare class DiskLayer implements CacheLayer {
|
|
|
331
471
|
private readonly maxEntryBytes;
|
|
332
472
|
private readonly protection;
|
|
333
473
|
private writeQueue;
|
|
474
|
+
/**
|
|
475
|
+
* Creates a disk-backed cache layer.
|
|
476
|
+
*/
|
|
334
477
|
constructor(options: DiskLayerOptions);
|
|
478
|
+
/**
|
|
479
|
+
* Reads and unwraps a fresh value from disk.
|
|
480
|
+
*/
|
|
335
481
|
get<T>(key: string): Promise<T | null>;
|
|
482
|
+
/**
|
|
483
|
+
* Reads the raw stored value or envelope from disk.
|
|
484
|
+
*/
|
|
336
485
|
getEntry<T = unknown>(key: string): Promise<T | null>;
|
|
486
|
+
/**
|
|
487
|
+
* Stores a value on disk using the provided TTL or layer default TTL.
|
|
488
|
+
*/
|
|
337
489
|
set(key: string, value: unknown, ttl?: number | undefined): Promise<void>;
|
|
490
|
+
/**
|
|
491
|
+
* Reads many raw entries from disk.
|
|
492
|
+
*/
|
|
338
493
|
getMany<T>(keys: string[]): Promise<Array<T | null>>;
|
|
494
|
+
/**
|
|
495
|
+
* Writes many entries to disk.
|
|
496
|
+
*/
|
|
339
497
|
setMany(entries: CacheLayerSetManyEntry[]): Promise<void>;
|
|
498
|
+
/**
|
|
499
|
+
* Returns true when the key exists and has not expired.
|
|
500
|
+
*/
|
|
340
501
|
has(key: string): Promise<boolean>;
|
|
502
|
+
/**
|
|
503
|
+
* Returns remaining TTL in milliseconds, or null when absent or non-expiring.
|
|
504
|
+
*/
|
|
341
505
|
ttl(key: string): Promise<number | null>;
|
|
506
|
+
/**
|
|
507
|
+
* Deletes a key from disk.
|
|
508
|
+
*/
|
|
342
509
|
delete(key: string): Promise<void>;
|
|
510
|
+
/**
|
|
511
|
+
* Deletes multiple keys from disk.
|
|
512
|
+
*/
|
|
343
513
|
deleteMany(keys: string[]): Promise<void>;
|
|
514
|
+
/**
|
|
515
|
+
* Removes all cache entry files from this layer's directory.
|
|
516
|
+
*/
|
|
344
517
|
clear(): Promise<void>;
|
|
345
518
|
/**
|
|
346
519
|
* Returns the original cache key strings stored on disk.
|
|
347
520
|
* Expired entries are skipped and cleaned up during the scan.
|
|
348
521
|
*/
|
|
349
522
|
keys(): Promise<string[]>;
|
|
523
|
+
/**
|
|
524
|
+
* Visits all non-expired keys stored on disk.
|
|
525
|
+
*/
|
|
350
526
|
forEachKey(visitor: (key: string) => void | Promise<void>): Promise<void>;
|
|
527
|
+
/**
|
|
528
|
+
* Returns the number of non-expired entries stored on disk.
|
|
529
|
+
*/
|
|
351
530
|
size(): Promise<number>;
|
|
531
|
+
/**
|
|
532
|
+
* Verifies the cache directory can be created.
|
|
533
|
+
*/
|
|
352
534
|
ping(): Promise<boolean>;
|
|
535
|
+
/**
|
|
536
|
+
* Reserved for interface compatibility; DiskLayer does not hold persistent handles.
|
|
537
|
+
*/
|
|
353
538
|
dispose(): Promise<void>;
|
|
354
539
|
private keyToPath;
|
|
355
540
|
private resolveDirectory;
|
|
@@ -377,19 +562,27 @@ declare class DiskLayer implements CacheLayer {
|
|
|
377
562
|
* npm install memcache-client
|
|
378
563
|
*/
|
|
379
564
|
interface MemcachedClient {
|
|
565
|
+
/** Read a raw value for `key`, returning null on miss. */
|
|
380
566
|
get(key: string): Promise<{
|
|
381
567
|
value: Buffer | null;
|
|
382
568
|
} | null>;
|
|
569
|
+
/** Store a raw value with optional expiration in seconds. */
|
|
383
570
|
set(key: string, value: string | Buffer, options?: {
|
|
384
571
|
expires?: number;
|
|
385
572
|
}): Promise<boolean | undefined>;
|
|
573
|
+
/** Delete a raw key. */
|
|
386
574
|
delete(key: string): Promise<boolean | undefined>;
|
|
387
575
|
}
|
|
388
576
|
interface MemcachedLayerOptions {
|
|
577
|
+
/** Memcached-compatible client implementation. */
|
|
389
578
|
client: MemcachedClient;
|
|
579
|
+
/** Default TTL in milliseconds for writes that do not provide an explicit TTL. */
|
|
390
580
|
ttl?: number;
|
|
581
|
+
/** Layer name used for metrics and per-layer TTL maps. Defaults to `memcached`. */
|
|
391
582
|
name?: string;
|
|
583
|
+
/** Prefix prepended to every Memcached key. */
|
|
392
584
|
keyPrefix?: string;
|
|
585
|
+
/** Serializer used to encode values before storing them in Memcached. */
|
|
393
586
|
serializer?: CacheSerializer;
|
|
394
587
|
}
|
|
395
588
|
/**
|
|
@@ -417,32 +610,74 @@ declare class MemcachedLayer implements CacheLayer {
|
|
|
417
610
|
private readonly client;
|
|
418
611
|
private readonly keyPrefix;
|
|
419
612
|
private readonly serializer;
|
|
613
|
+
/**
|
|
614
|
+
* Creates a Memcached cache layer using a compatible client.
|
|
615
|
+
*/
|
|
420
616
|
constructor(options: MemcachedLayerOptions);
|
|
617
|
+
/**
|
|
618
|
+
* Reads and unwraps a fresh value from Memcached.
|
|
619
|
+
*/
|
|
421
620
|
get<T>(key: string): Promise<T | null>;
|
|
621
|
+
/**
|
|
622
|
+
* Reads the raw stored value or envelope from Memcached.
|
|
623
|
+
*/
|
|
422
624
|
getEntry<T = unknown>(key: string): Promise<T | null>;
|
|
625
|
+
/**
|
|
626
|
+
* Reads many raw entries from Memcached.
|
|
627
|
+
*/
|
|
423
628
|
getMany<T>(keys: string[]): Promise<Array<T | null>>;
|
|
629
|
+
/**
|
|
630
|
+
* Stores a value in Memcached using the provided TTL or layer default TTL.
|
|
631
|
+
*/
|
|
424
632
|
set(key: string, value: unknown, ttl?: number | undefined): Promise<void>;
|
|
633
|
+
/**
|
|
634
|
+
* Returns true when the key exists in Memcached.
|
|
635
|
+
*/
|
|
425
636
|
has(key: string): Promise<boolean>;
|
|
637
|
+
/**
|
|
638
|
+
* Deletes a key from Memcached.
|
|
639
|
+
*/
|
|
426
640
|
delete(key: string): Promise<void>;
|
|
641
|
+
/**
|
|
642
|
+
* Deletes multiple keys from Memcached.
|
|
643
|
+
*/
|
|
427
644
|
deleteMany(keys: string[]): Promise<void>;
|
|
645
|
+
/**
|
|
646
|
+
* Always throws because Memcached has no safe prefix clear primitive.
|
|
647
|
+
*/
|
|
428
648
|
clear(): Promise<void>;
|
|
429
649
|
private withPrefix;
|
|
430
650
|
private validateKey;
|
|
431
651
|
}
|
|
432
652
|
|
|
433
653
|
declare class JsonSerializer implements CacheSerializer {
|
|
654
|
+
/**
|
|
655
|
+
* Serializes a value to JSON.
|
|
656
|
+
*/
|
|
434
657
|
serialize(value: unknown): string;
|
|
658
|
+
/**
|
|
659
|
+
* Parses JSON and sanitizes the result before returning it.
|
|
660
|
+
*/
|
|
435
661
|
deserialize<T>(payload: string | Buffer): T;
|
|
436
662
|
}
|
|
437
663
|
|
|
438
664
|
declare class MsgpackSerializer implements CacheSerializer {
|
|
665
|
+
/**
|
|
666
|
+
* Serializes a value to MessagePack bytes.
|
|
667
|
+
*/
|
|
439
668
|
serialize(value: unknown): Buffer;
|
|
669
|
+
/**
|
|
670
|
+
* Decodes MessagePack bytes and sanitizes the result before returning it.
|
|
671
|
+
*/
|
|
440
672
|
deserialize<T>(payload: string | Buffer): T;
|
|
441
673
|
}
|
|
442
674
|
|
|
443
675
|
interface RedisSingleFlightCoordinatorOptions {
|
|
676
|
+
/** Redis client used for lock acquisition, renewal, and release. */
|
|
444
677
|
client: Redis;
|
|
678
|
+
/** Redis key prefix for single-flight locks. Defaults to `layercache:singleflight`. */
|
|
445
679
|
prefix?: string;
|
|
680
|
+
/** Per-command timeout in milliseconds for Redis operations. */
|
|
446
681
|
commandTimeoutMs?: number;
|
|
447
682
|
}
|
|
448
683
|
declare class RedisSingleFlightCoordinator implements CacheSingleFlightCoordinator {
|
|
@@ -450,6 +685,10 @@ declare class RedisSingleFlightCoordinator implements CacheSingleFlightCoordinat
|
|
|
450
685
|
private readonly prefix;
|
|
451
686
|
private readonly commandTimeoutMs;
|
|
452
687
|
constructor(options: RedisSingleFlightCoordinatorOptions);
|
|
688
|
+
/**
|
|
689
|
+
* Executes `worker` when this process acquires the Redis lock; otherwise runs
|
|
690
|
+
* `waiter` while another process owns the work.
|
|
691
|
+
*/
|
|
453
692
|
execute<T>(key: string, options: CacheSingleFlightExecutionOptions, worker: () => Promise<T>, waiter: () => Promise<T>): Promise<T>;
|
|
454
693
|
private startLeaseRenewal;
|
|
455
694
|
private normalizeCommandTimeoutMs;
|
|
@@ -475,6 +714,9 @@ declare class StampedeGuard {
|
|
|
475
714
|
private readonly maxInFlight;
|
|
476
715
|
private readonly entryTimeoutMs;
|
|
477
716
|
constructor(options?: StampedeGuardOptions);
|
|
717
|
+
/**
|
|
718
|
+
* Deduplicates concurrent work for the same key in this process.
|
|
719
|
+
*/
|
|
478
720
|
execute<T>(key: string, task: () => Promise<T>): Promise<T>;
|
|
479
721
|
private withTimeout;
|
|
480
722
|
private releaseEntry;
|