layercache 1.2.1 → 1.2.3

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.
@@ -156,6 +156,7 @@ interface CacheSingleFlightExecutionOptions {
156
156
  leaseMs: number;
157
157
  waitTimeoutMs: number;
158
158
  pollIntervalMs: number;
159
+ renewIntervalMs?: number;
159
160
  }
160
161
  interface CacheSingleFlightCoordinator {
161
162
  execute<T>(key: string, options: CacheSingleFlightExecutionOptions, worker: () => Promise<T>, waiter: () => Promise<T>): Promise<T>;
@@ -167,6 +168,7 @@ interface CacheStackOptions {
167
168
  invalidationBus?: InvalidationBus;
168
169
  tagIndex?: CacheTagIndex;
169
170
  generation?: number;
171
+ generationCleanup?: boolean | CacheGenerationCleanupOptions;
170
172
  broadcastL1Invalidation?: boolean;
171
173
  /**
172
174
  * @deprecated Use `broadcastL1Invalidation` instead.
@@ -185,10 +187,13 @@ interface CacheStackOptions {
185
187
  writeStrategy?: 'write-through' | 'write-behind';
186
188
  writeBehind?: CacheWriteBehindOptions;
187
189
  fetcherRateLimit?: CacheRateLimitOptions;
190
+ backgroundRefreshTimeoutMs?: number;
188
191
  singleFlightCoordinator?: CacheSingleFlightCoordinator;
189
192
  singleFlightLeaseMs?: number;
190
193
  singleFlightTimeoutMs?: number;
191
194
  singleFlightPollMs?: number;
195
+ singleFlightRenewIntervalMs?: number;
196
+ snapshotBaseDir?: string | false;
192
197
  /**
193
198
  * Maximum number of entries in `accessProfiles` and `circuitBreakers` maps
194
199
  * before the oldest entries are pruned. Prevents unbounded memory growth.
@@ -201,6 +206,9 @@ interface CacheAdaptiveTtlOptions {
201
206
  step?: number | LayerTtlMap;
202
207
  maxTtl?: number | LayerTtlMap;
203
208
  }
209
+ interface CacheGenerationCleanupOptions {
210
+ batchSize?: number;
211
+ }
204
212
  type CacheTtlPolicy = 'until-midnight' | 'next-hour' | {
205
213
  alignTo: number;
206
214
  } | ((context: CacheTtlPolicyContext) => number | undefined);
@@ -219,6 +227,8 @@ interface CacheRateLimitOptions {
219
227
  maxConcurrent?: number;
220
228
  intervalMs?: number;
221
229
  maxPerInterval?: number;
230
+ scope?: 'global' | 'key' | 'fetcher';
231
+ bucketKey?: string;
222
232
  }
223
233
  interface CacheWriteBehindOptions {
224
234
  flushIntervalMs?: number;
@@ -401,6 +411,7 @@ declare class CacheStack extends EventEmitter {
401
411
  private readonly logger;
402
412
  private readonly tagIndex;
403
413
  private readonly fetchRateLimiter;
414
+ private readonly snapshotSerializer;
404
415
  private readonly backgroundRefreshes;
405
416
  private readonly layerDegradedUntil;
406
417
  private readonly ttlResolver;
@@ -409,6 +420,7 @@ declare class CacheStack extends EventEmitter {
409
420
  private readonly writeBehindQueue;
410
421
  private writeBehindTimer?;
411
422
  private writeBehindFlushPromise?;
423
+ private generationCleanupPromise?;
412
424
  private isDisconnecting;
413
425
  private disconnectPromise?;
414
426
  constructor(layers: CacheLayer[], options?: CacheStackOptions);
@@ -419,6 +431,7 @@ declare class CacheStack extends EventEmitter {
419
431
  * and no `fetcher` is provided.
420
432
  */
421
433
  get<T>(key: string, fetcher?: () => Promise<T>, options?: CacheGetOptions): Promise<T | null>;
434
+ private getPrepared;
422
435
  /**
423
436
  * Alias for `get(key, fetcher, options)` — explicit get-or-set pattern.
424
437
  * Fetches and caches the value if not already present.
@@ -477,6 +490,11 @@ declare class CacheStack extends EventEmitter {
477
490
  */
478
491
  getHitRate(): CacheHitRateSnapshot;
479
492
  healthCheck(): Promise<CacheHealthCheckResult[]>;
493
+ /**
494
+ * Rotates the active generation prefix used for all future cache keys.
495
+ * Previous-generation keys remain in the underlying layers until they expire,
496
+ * unless `generationCleanup` is enabled to prune them in the background.
497
+ */
480
498
  bumpGeneration(nextGeneration?: number): number;
481
499
  /**
482
500
  * Returns detailed metadata about a single cache key: which layers contain it,
@@ -504,6 +522,7 @@ declare class CacheStack extends EventEmitter {
504
522
  private resolveLayerSeconds;
505
523
  private shouldNegativeCache;
506
524
  private scheduleBackgroundRefresh;
525
+ private runBackgroundRefresh;
507
526
  private resolveSingleFlightOptions;
508
527
  private deleteKeys;
509
528
  private publishInvalidation;
@@ -511,7 +530,14 @@ declare class CacheStack extends EventEmitter {
511
530
  private getTagsForKey;
512
531
  private formatError;
513
532
  private sleep;
533
+ private withTimeout;
514
534
  private shouldBroadcastL1Invalidation;
535
+ private collectKeysWithPrefix;
536
+ private collectKeysMatchingPattern;
537
+ private shouldCleanupGenerations;
538
+ private generationCleanupBatchSize;
539
+ private scheduleGenerationCleanup;
540
+ private cleanupGeneration;
515
541
  private initializeWriteBehind;
516
542
  private shouldWriteBehind;
517
543
  private enqueueWriteBehind;
@@ -527,6 +553,7 @@ declare class CacheStack extends EventEmitter {
527
553
  private validateWriteOptions;
528
554
  private validateLayerNumberOption;
529
555
  private validatePositiveNumber;
556
+ private validateRateLimitOptions;
530
557
  private validateNonNegativeNumber;
531
558
  private validateCacheKey;
532
559
  private validateTtlPolicy;
@@ -538,12 +565,15 @@ declare class CacheStack extends EventEmitter {
538
565
  private applyFreshReadPolicies;
539
566
  private shouldSkipLayer;
540
567
  private handleLayerFailure;
568
+ private reportRecoverableLayerFailure;
541
569
  private isGracefulDegradationEnabled;
542
570
  private recordCircuitFailure;
543
571
  private isNegativeStoredValue;
544
572
  private emitError;
545
573
  private serializeKeyPart;
546
574
  private isCacheSnapshotEntries;
575
+ private sanitizeSnapshotValue;
576
+ private validateSnapshotFilePath;
547
577
  private normalizeForSerialization;
548
578
  }
549
579
 
@@ -156,6 +156,7 @@ interface CacheSingleFlightExecutionOptions {
156
156
  leaseMs: number;
157
157
  waitTimeoutMs: number;
158
158
  pollIntervalMs: number;
159
+ renewIntervalMs?: number;
159
160
  }
160
161
  interface CacheSingleFlightCoordinator {
161
162
  execute<T>(key: string, options: CacheSingleFlightExecutionOptions, worker: () => Promise<T>, waiter: () => Promise<T>): Promise<T>;
@@ -167,6 +168,7 @@ interface CacheStackOptions {
167
168
  invalidationBus?: InvalidationBus;
168
169
  tagIndex?: CacheTagIndex;
169
170
  generation?: number;
171
+ generationCleanup?: boolean | CacheGenerationCleanupOptions;
170
172
  broadcastL1Invalidation?: boolean;
171
173
  /**
172
174
  * @deprecated Use `broadcastL1Invalidation` instead.
@@ -185,10 +187,13 @@ interface CacheStackOptions {
185
187
  writeStrategy?: 'write-through' | 'write-behind';
186
188
  writeBehind?: CacheWriteBehindOptions;
187
189
  fetcherRateLimit?: CacheRateLimitOptions;
190
+ backgroundRefreshTimeoutMs?: number;
188
191
  singleFlightCoordinator?: CacheSingleFlightCoordinator;
189
192
  singleFlightLeaseMs?: number;
190
193
  singleFlightTimeoutMs?: number;
191
194
  singleFlightPollMs?: number;
195
+ singleFlightRenewIntervalMs?: number;
196
+ snapshotBaseDir?: string | false;
192
197
  /**
193
198
  * Maximum number of entries in `accessProfiles` and `circuitBreakers` maps
194
199
  * before the oldest entries are pruned. Prevents unbounded memory growth.
@@ -201,6 +206,9 @@ interface CacheAdaptiveTtlOptions {
201
206
  step?: number | LayerTtlMap;
202
207
  maxTtl?: number | LayerTtlMap;
203
208
  }
209
+ interface CacheGenerationCleanupOptions {
210
+ batchSize?: number;
211
+ }
204
212
  type CacheTtlPolicy = 'until-midnight' | 'next-hour' | {
205
213
  alignTo: number;
206
214
  } | ((context: CacheTtlPolicyContext) => number | undefined);
@@ -219,6 +227,8 @@ interface CacheRateLimitOptions {
219
227
  maxConcurrent?: number;
220
228
  intervalMs?: number;
221
229
  maxPerInterval?: number;
230
+ scope?: 'global' | 'key' | 'fetcher';
231
+ bucketKey?: string;
222
232
  }
223
233
  interface CacheWriteBehindOptions {
224
234
  flushIntervalMs?: number;
@@ -401,6 +411,7 @@ declare class CacheStack extends EventEmitter {
401
411
  private readonly logger;
402
412
  private readonly tagIndex;
403
413
  private readonly fetchRateLimiter;
414
+ private readonly snapshotSerializer;
404
415
  private readonly backgroundRefreshes;
405
416
  private readonly layerDegradedUntil;
406
417
  private readonly ttlResolver;
@@ -409,6 +420,7 @@ declare class CacheStack extends EventEmitter {
409
420
  private readonly writeBehindQueue;
410
421
  private writeBehindTimer?;
411
422
  private writeBehindFlushPromise?;
423
+ private generationCleanupPromise?;
412
424
  private isDisconnecting;
413
425
  private disconnectPromise?;
414
426
  constructor(layers: CacheLayer[], options?: CacheStackOptions);
@@ -419,6 +431,7 @@ declare class CacheStack extends EventEmitter {
419
431
  * and no `fetcher` is provided.
420
432
  */
421
433
  get<T>(key: string, fetcher?: () => Promise<T>, options?: CacheGetOptions): Promise<T | null>;
434
+ private getPrepared;
422
435
  /**
423
436
  * Alias for `get(key, fetcher, options)` — explicit get-or-set pattern.
424
437
  * Fetches and caches the value if not already present.
@@ -477,6 +490,11 @@ declare class CacheStack extends EventEmitter {
477
490
  */
478
491
  getHitRate(): CacheHitRateSnapshot;
479
492
  healthCheck(): Promise<CacheHealthCheckResult[]>;
493
+ /**
494
+ * Rotates the active generation prefix used for all future cache keys.
495
+ * Previous-generation keys remain in the underlying layers until they expire,
496
+ * unless `generationCleanup` is enabled to prune them in the background.
497
+ */
480
498
  bumpGeneration(nextGeneration?: number): number;
481
499
  /**
482
500
  * Returns detailed metadata about a single cache key: which layers contain it,
@@ -504,6 +522,7 @@ declare class CacheStack extends EventEmitter {
504
522
  private resolveLayerSeconds;
505
523
  private shouldNegativeCache;
506
524
  private scheduleBackgroundRefresh;
525
+ private runBackgroundRefresh;
507
526
  private resolveSingleFlightOptions;
508
527
  private deleteKeys;
509
528
  private publishInvalidation;
@@ -511,7 +530,14 @@ declare class CacheStack extends EventEmitter {
511
530
  private getTagsForKey;
512
531
  private formatError;
513
532
  private sleep;
533
+ private withTimeout;
514
534
  private shouldBroadcastL1Invalidation;
535
+ private collectKeysWithPrefix;
536
+ private collectKeysMatchingPattern;
537
+ private shouldCleanupGenerations;
538
+ private generationCleanupBatchSize;
539
+ private scheduleGenerationCleanup;
540
+ private cleanupGeneration;
515
541
  private initializeWriteBehind;
516
542
  private shouldWriteBehind;
517
543
  private enqueueWriteBehind;
@@ -527,6 +553,7 @@ declare class CacheStack extends EventEmitter {
527
553
  private validateWriteOptions;
528
554
  private validateLayerNumberOption;
529
555
  private validatePositiveNumber;
556
+ private validateRateLimitOptions;
530
557
  private validateNonNegativeNumber;
531
558
  private validateCacheKey;
532
559
  private validateTtlPolicy;
@@ -538,12 +565,15 @@ declare class CacheStack extends EventEmitter {
538
565
  private applyFreshReadPolicies;
539
566
  private shouldSkipLayer;
540
567
  private handleLayerFailure;
568
+ private reportRecoverableLayerFailure;
541
569
  private isGracefulDegradationEnabled;
542
570
  private recordCircuitFailure;
543
571
  private isNegativeStoredValue;
544
572
  private emitError;
545
573
  private serializeKeyPart;
546
574
  private isCacheSnapshotEntries;
575
+ private sanitizeSnapshotValue;
576
+ private validateSnapshotFilePath;
547
577
  private normalizeForSerialization;
548
578
  }
549
579