layercache 1.2.2 → 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.
@@ -168,6 +168,7 @@ interface CacheStackOptions {
168
168
  invalidationBus?: InvalidationBus;
169
169
  tagIndex?: CacheTagIndex;
170
170
  generation?: number;
171
+ generationCleanup?: boolean | CacheGenerationCleanupOptions;
171
172
  broadcastL1Invalidation?: boolean;
172
173
  /**
173
174
  * @deprecated Use `broadcastL1Invalidation` instead.
@@ -186,11 +187,13 @@ interface CacheStackOptions {
186
187
  writeStrategy?: 'write-through' | 'write-behind';
187
188
  writeBehind?: CacheWriteBehindOptions;
188
189
  fetcherRateLimit?: CacheRateLimitOptions;
190
+ backgroundRefreshTimeoutMs?: number;
189
191
  singleFlightCoordinator?: CacheSingleFlightCoordinator;
190
192
  singleFlightLeaseMs?: number;
191
193
  singleFlightTimeoutMs?: number;
192
194
  singleFlightPollMs?: number;
193
195
  singleFlightRenewIntervalMs?: number;
196
+ snapshotBaseDir?: string | false;
194
197
  /**
195
198
  * Maximum number of entries in `accessProfiles` and `circuitBreakers` maps
196
199
  * before the oldest entries are pruned. Prevents unbounded memory growth.
@@ -203,6 +206,9 @@ interface CacheAdaptiveTtlOptions {
203
206
  step?: number | LayerTtlMap;
204
207
  maxTtl?: number | LayerTtlMap;
205
208
  }
209
+ interface CacheGenerationCleanupOptions {
210
+ batchSize?: number;
211
+ }
206
212
  type CacheTtlPolicy = 'until-midnight' | 'next-hour' | {
207
213
  alignTo: number;
208
214
  } | ((context: CacheTtlPolicyContext) => number | undefined);
@@ -405,6 +411,7 @@ declare class CacheStack extends EventEmitter {
405
411
  private readonly logger;
406
412
  private readonly tagIndex;
407
413
  private readonly fetchRateLimiter;
414
+ private readonly snapshotSerializer;
408
415
  private readonly backgroundRefreshes;
409
416
  private readonly layerDegradedUntil;
410
417
  private readonly ttlResolver;
@@ -413,6 +420,7 @@ declare class CacheStack extends EventEmitter {
413
420
  private readonly writeBehindQueue;
414
421
  private writeBehindTimer?;
415
422
  private writeBehindFlushPromise?;
423
+ private generationCleanupPromise?;
416
424
  private isDisconnecting;
417
425
  private disconnectPromise?;
418
426
  constructor(layers: CacheLayer[], options?: CacheStackOptions);
@@ -423,6 +431,7 @@ declare class CacheStack extends EventEmitter {
423
431
  * and no `fetcher` is provided.
424
432
  */
425
433
  get<T>(key: string, fetcher?: () => Promise<T>, options?: CacheGetOptions): Promise<T | null>;
434
+ private getPrepared;
426
435
  /**
427
436
  * Alias for `get(key, fetcher, options)` — explicit get-or-set pattern.
428
437
  * Fetches and caches the value if not already present.
@@ -481,6 +490,11 @@ declare class CacheStack extends EventEmitter {
481
490
  */
482
491
  getHitRate(): CacheHitRateSnapshot;
483
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
+ */
484
498
  bumpGeneration(nextGeneration?: number): number;
485
499
  /**
486
500
  * Returns detailed metadata about a single cache key: which layers contain it,
@@ -508,6 +522,7 @@ declare class CacheStack extends EventEmitter {
508
522
  private resolveLayerSeconds;
509
523
  private shouldNegativeCache;
510
524
  private scheduleBackgroundRefresh;
525
+ private runBackgroundRefresh;
511
526
  private resolveSingleFlightOptions;
512
527
  private deleteKeys;
513
528
  private publishInvalidation;
@@ -515,7 +530,14 @@ declare class CacheStack extends EventEmitter {
515
530
  private getTagsForKey;
516
531
  private formatError;
517
532
  private sleep;
533
+ private withTimeout;
518
534
  private shouldBroadcastL1Invalidation;
535
+ private collectKeysWithPrefix;
536
+ private collectKeysMatchingPattern;
537
+ private shouldCleanupGenerations;
538
+ private generationCleanupBatchSize;
539
+ private scheduleGenerationCleanup;
540
+ private cleanupGeneration;
519
541
  private initializeWriteBehind;
520
542
  private shouldWriteBehind;
521
543
  private enqueueWriteBehind;
@@ -543,12 +565,15 @@ declare class CacheStack extends EventEmitter {
543
565
  private applyFreshReadPolicies;
544
566
  private shouldSkipLayer;
545
567
  private handleLayerFailure;
568
+ private reportRecoverableLayerFailure;
546
569
  private isGracefulDegradationEnabled;
547
570
  private recordCircuitFailure;
548
571
  private isNegativeStoredValue;
549
572
  private emitError;
550
573
  private serializeKeyPart;
551
574
  private isCacheSnapshotEntries;
575
+ private sanitizeSnapshotValue;
576
+ private validateSnapshotFilePath;
552
577
  private normalizeForSerialization;
553
578
  }
554
579
 
@@ -168,6 +168,7 @@ interface CacheStackOptions {
168
168
  invalidationBus?: InvalidationBus;
169
169
  tagIndex?: CacheTagIndex;
170
170
  generation?: number;
171
+ generationCleanup?: boolean | CacheGenerationCleanupOptions;
171
172
  broadcastL1Invalidation?: boolean;
172
173
  /**
173
174
  * @deprecated Use `broadcastL1Invalidation` instead.
@@ -186,11 +187,13 @@ interface CacheStackOptions {
186
187
  writeStrategy?: 'write-through' | 'write-behind';
187
188
  writeBehind?: CacheWriteBehindOptions;
188
189
  fetcherRateLimit?: CacheRateLimitOptions;
190
+ backgroundRefreshTimeoutMs?: number;
189
191
  singleFlightCoordinator?: CacheSingleFlightCoordinator;
190
192
  singleFlightLeaseMs?: number;
191
193
  singleFlightTimeoutMs?: number;
192
194
  singleFlightPollMs?: number;
193
195
  singleFlightRenewIntervalMs?: number;
196
+ snapshotBaseDir?: string | false;
194
197
  /**
195
198
  * Maximum number of entries in `accessProfiles` and `circuitBreakers` maps
196
199
  * before the oldest entries are pruned. Prevents unbounded memory growth.
@@ -203,6 +206,9 @@ interface CacheAdaptiveTtlOptions {
203
206
  step?: number | LayerTtlMap;
204
207
  maxTtl?: number | LayerTtlMap;
205
208
  }
209
+ interface CacheGenerationCleanupOptions {
210
+ batchSize?: number;
211
+ }
206
212
  type CacheTtlPolicy = 'until-midnight' | 'next-hour' | {
207
213
  alignTo: number;
208
214
  } | ((context: CacheTtlPolicyContext) => number | undefined);
@@ -405,6 +411,7 @@ declare class CacheStack extends EventEmitter {
405
411
  private readonly logger;
406
412
  private readonly tagIndex;
407
413
  private readonly fetchRateLimiter;
414
+ private readonly snapshotSerializer;
408
415
  private readonly backgroundRefreshes;
409
416
  private readonly layerDegradedUntil;
410
417
  private readonly ttlResolver;
@@ -413,6 +420,7 @@ declare class CacheStack extends EventEmitter {
413
420
  private readonly writeBehindQueue;
414
421
  private writeBehindTimer?;
415
422
  private writeBehindFlushPromise?;
423
+ private generationCleanupPromise?;
416
424
  private isDisconnecting;
417
425
  private disconnectPromise?;
418
426
  constructor(layers: CacheLayer[], options?: CacheStackOptions);
@@ -423,6 +431,7 @@ declare class CacheStack extends EventEmitter {
423
431
  * and no `fetcher` is provided.
424
432
  */
425
433
  get<T>(key: string, fetcher?: () => Promise<T>, options?: CacheGetOptions): Promise<T | null>;
434
+ private getPrepared;
426
435
  /**
427
436
  * Alias for `get(key, fetcher, options)` — explicit get-or-set pattern.
428
437
  * Fetches and caches the value if not already present.
@@ -481,6 +490,11 @@ declare class CacheStack extends EventEmitter {
481
490
  */
482
491
  getHitRate(): CacheHitRateSnapshot;
483
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
+ */
484
498
  bumpGeneration(nextGeneration?: number): number;
485
499
  /**
486
500
  * Returns detailed metadata about a single cache key: which layers contain it,
@@ -508,6 +522,7 @@ declare class CacheStack extends EventEmitter {
508
522
  private resolveLayerSeconds;
509
523
  private shouldNegativeCache;
510
524
  private scheduleBackgroundRefresh;
525
+ private runBackgroundRefresh;
511
526
  private resolveSingleFlightOptions;
512
527
  private deleteKeys;
513
528
  private publishInvalidation;
@@ -515,7 +530,14 @@ declare class CacheStack extends EventEmitter {
515
530
  private getTagsForKey;
516
531
  private formatError;
517
532
  private sleep;
533
+ private withTimeout;
518
534
  private shouldBroadcastL1Invalidation;
535
+ private collectKeysWithPrefix;
536
+ private collectKeysMatchingPattern;
537
+ private shouldCleanupGenerations;
538
+ private generationCleanupBatchSize;
539
+ private scheduleGenerationCleanup;
540
+ private cleanupGeneration;
519
541
  private initializeWriteBehind;
520
542
  private shouldWriteBehind;
521
543
  private enqueueWriteBehind;
@@ -543,12 +565,15 @@ declare class CacheStack extends EventEmitter {
543
565
  private applyFreshReadPolicies;
544
566
  private shouldSkipLayer;
545
567
  private handleLayerFailure;
568
+ private reportRecoverableLayerFailure;
546
569
  private isGracefulDegradationEnabled;
547
570
  private recordCircuitFailure;
548
571
  private isNegativeStoredValue;
549
572
  private emitError;
550
573
  private serializeKeyPart;
551
574
  private isCacheSnapshotEntries;
575
+ private sanitizeSnapshotValue;
576
+ private validateSnapshotFilePath;
552
577
  private normalizeForSerialization;
553
578
  }
554
579