layercache 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -6,6 +6,11 @@ interface LayerTtlMap {
6
6
  interface CacheWriteOptions {
7
7
  tags?: string[];
8
8
  ttl?: number | LayerTtlMap;
9
+ negativeCache?: boolean;
10
+ negativeTtl?: number | LayerTtlMap;
11
+ staleWhileRevalidate?: number | LayerTtlMap;
12
+ staleIfError?: number | LayerTtlMap;
13
+ ttlJitter?: number | LayerTtlMap;
9
14
  }
10
15
  interface CacheGetOptions extends CacheWriteOptions {
11
16
  }
@@ -24,6 +29,8 @@ interface CacheLayer {
24
29
  readonly defaultTtl?: number;
25
30
  readonly isLocal?: boolean;
26
31
  get<T>(key: string): Promise<T | null>;
32
+ getEntry?<T = unknown>(key: string): Promise<T | null>;
33
+ getMany?<T>(keys: string[]): Promise<Array<T | null>>;
27
34
  set(key: string, value: unknown, ttl?: number): Promise<void>;
28
35
  delete(key: string): Promise<void>;
29
36
  clear(): Promise<void>;
@@ -42,6 +49,11 @@ interface CacheMetricsSnapshot {
42
49
  deletes: number;
43
50
  backfills: number;
44
51
  invalidations: number;
52
+ staleHits: number;
53
+ refreshes: number;
54
+ refreshErrors: number;
55
+ writeFailures: number;
56
+ singleFlightWaits: number;
45
57
  }
46
58
  interface CacheLogger {
47
59
  debug(message: string, context?: Record<string, unknown>): void;
@@ -64,6 +76,14 @@ interface InvalidationBus {
64
76
  subscribe(handler: (message: InvalidationMessage) => Promise<void> | void): Promise<() => Promise<void> | void>;
65
77
  publish(message: InvalidationMessage): Promise<void>;
66
78
  }
79
+ interface CacheSingleFlightExecutionOptions {
80
+ leaseMs: number;
81
+ waitTimeoutMs: number;
82
+ pollIntervalMs: number;
83
+ }
84
+ interface CacheSingleFlightCoordinator {
85
+ execute<T>(key: string, options: CacheSingleFlightExecutionOptions, worker: () => Promise<T>, waiter: () => Promise<T>): Promise<T>;
86
+ }
67
87
  interface CacheStackOptions {
68
88
  logger?: CacheLogger | boolean;
69
89
  metrics?: boolean;
@@ -71,6 +91,16 @@ interface CacheStackOptions {
71
91
  invalidationBus?: InvalidationBus;
72
92
  tagIndex?: CacheTagIndex;
73
93
  publishSetInvalidation?: boolean;
94
+ negativeCaching?: boolean;
95
+ negativeTtl?: number | LayerTtlMap;
96
+ staleWhileRevalidate?: number | LayerTtlMap;
97
+ staleIfError?: number | LayerTtlMap;
98
+ ttlJitter?: number | LayerTtlMap;
99
+ writePolicy?: 'strict' | 'best-effort';
100
+ singleFlightCoordinator?: CacheSingleFlightCoordinator;
101
+ singleFlightLeaseMs?: number;
102
+ singleFlightTimeoutMs?: number;
103
+ singleFlightPollMs?: number;
74
104
  }
75
105
 
76
106
  declare class CacheStack {
@@ -83,6 +113,7 @@ declare class CacheStack {
83
113
  private unsubscribeInvalidation?;
84
114
  private readonly logger;
85
115
  private readonly tagIndex;
116
+ private readonly backgroundRefreshes;
86
117
  constructor(layers: CacheLayer[], options?: CacheStackOptions);
87
118
  get<T>(key: string, fetcher?: () => Promise<T>, options?: CacheGetOptions): Promise<T | null>;
88
119
  set<T>(key: string, value: T, options?: CacheWriteOptions): Promise<void>;
@@ -96,13 +127,27 @@ declare class CacheStack {
96
127
  resetMetrics(): void;
97
128
  disconnect(): Promise<void>;
98
129
  private initialize;
99
- private getFromLayers;
130
+ private fetchWithGuards;
131
+ private waitForFreshValue;
132
+ private fetchAndPopulate;
133
+ private storeEntry;
134
+ private readFromLayers;
135
+ private readLayerEntry;
100
136
  private backfill;
101
- private setAcrossLayers;
102
- private resolveTtl;
137
+ private writeAcrossLayers;
138
+ private executeLayerOperations;
139
+ private resolveFreshTtl;
140
+ private resolveLayerSeconds;
141
+ private readLayerNumber;
142
+ private applyJitter;
143
+ private shouldNegativeCache;
144
+ private scheduleBackgroundRefresh;
145
+ private resolveSingleFlightOptions;
103
146
  private deleteKeys;
104
147
  private publishInvalidation;
105
148
  private handleInvalidationMessage;
149
+ private formatError;
150
+ private sleep;
106
151
  }
107
152
 
108
153
  declare class PatternMatcher {
@@ -170,6 +215,8 @@ declare class MemoryLayer implements CacheLayer {
170
215
  private readonly entries;
171
216
  constructor(options?: MemoryLayerOptions);
172
217
  get<T>(key: string): Promise<T | null>;
218
+ getEntry<T = unknown>(key: string): Promise<T | null>;
219
+ getMany<T>(keys: string[]): Promise<Array<T | null>>;
173
220
  set(key: string, value: unknown, ttl?: number | undefined): Promise<void>;
174
221
  delete(key: string): Promise<void>;
175
222
  deleteMany(keys: string[]): Promise<void>;
@@ -199,6 +246,8 @@ declare class RedisLayer implements CacheLayer {
199
246
  private readonly scanCount;
200
247
  constructor(options: RedisLayerOptions);
201
248
  get<T>(key: string): Promise<T | null>;
249
+ getEntry<T = unknown>(key: string): Promise<T | null>;
250
+ getMany<T>(keys: string[]): Promise<Array<T | null>>;
202
251
  set(key: string, value: unknown, ttl?: number | undefined): Promise<void>;
203
252
  delete(key: string): Promise<void>;
204
253
  deleteMany(keys: string[]): Promise<void>;
@@ -218,10 +267,21 @@ declare class MsgpackSerializer implements CacheSerializer {
218
267
  deserialize<T>(payload: string | Buffer): T;
219
268
  }
220
269
 
270
+ interface RedisSingleFlightCoordinatorOptions {
271
+ client: Redis;
272
+ prefix?: string;
273
+ }
274
+ declare class RedisSingleFlightCoordinator implements CacheSingleFlightCoordinator {
275
+ private readonly client;
276
+ private readonly prefix;
277
+ constructor(options: RedisSingleFlightCoordinatorOptions);
278
+ execute<T>(key: string, options: CacheSingleFlightExecutionOptions, worker: () => Promise<T>, waiter: () => Promise<T>): Promise<T>;
279
+ }
280
+
221
281
  declare class StampedeGuard {
222
282
  private readonly mutexes;
223
283
  execute<T>(key: string, task: () => Promise<T>): Promise<T>;
224
284
  private getMutex;
225
285
  }
226
286
 
227
- export { type CacheGetOptions, type CacheLayer, type CacheLogger, type CacheMGetEntry, type CacheMSetEntry, type CacheMetricsSnapshot, type CacheSerializer, CacheStack, type CacheStackOptions, type CacheTagIndex, type CacheWriteOptions, type InvalidationBus, type InvalidationMessage, JsonSerializer, type LayerTtlMap, MemoryLayer, MsgpackSerializer, PatternMatcher, RedisInvalidationBus, RedisLayer, RedisTagIndex, StampedeGuard, TagIndex };
287
+ export { type CacheGetOptions, type CacheLayer, type CacheLogger, type CacheMGetEntry, type CacheMSetEntry, type CacheMetricsSnapshot, type CacheSerializer, type CacheSingleFlightCoordinator, type CacheSingleFlightExecutionOptions, CacheStack, type CacheStackOptions, type CacheTagIndex, type CacheWriteOptions, type InvalidationBus, type InvalidationMessage, JsonSerializer, type LayerTtlMap, MemoryLayer, MsgpackSerializer, PatternMatcher, RedisInvalidationBus, RedisLayer, RedisSingleFlightCoordinator, RedisTagIndex, StampedeGuard, TagIndex };
package/dist/index.d.ts CHANGED
@@ -6,6 +6,11 @@ interface LayerTtlMap {
6
6
  interface CacheWriteOptions {
7
7
  tags?: string[];
8
8
  ttl?: number | LayerTtlMap;
9
+ negativeCache?: boolean;
10
+ negativeTtl?: number | LayerTtlMap;
11
+ staleWhileRevalidate?: number | LayerTtlMap;
12
+ staleIfError?: number | LayerTtlMap;
13
+ ttlJitter?: number | LayerTtlMap;
9
14
  }
10
15
  interface CacheGetOptions extends CacheWriteOptions {
11
16
  }
@@ -24,6 +29,8 @@ interface CacheLayer {
24
29
  readonly defaultTtl?: number;
25
30
  readonly isLocal?: boolean;
26
31
  get<T>(key: string): Promise<T | null>;
32
+ getEntry?<T = unknown>(key: string): Promise<T | null>;
33
+ getMany?<T>(keys: string[]): Promise<Array<T | null>>;
27
34
  set(key: string, value: unknown, ttl?: number): Promise<void>;
28
35
  delete(key: string): Promise<void>;
29
36
  clear(): Promise<void>;
@@ -42,6 +49,11 @@ interface CacheMetricsSnapshot {
42
49
  deletes: number;
43
50
  backfills: number;
44
51
  invalidations: number;
52
+ staleHits: number;
53
+ refreshes: number;
54
+ refreshErrors: number;
55
+ writeFailures: number;
56
+ singleFlightWaits: number;
45
57
  }
46
58
  interface CacheLogger {
47
59
  debug(message: string, context?: Record<string, unknown>): void;
@@ -64,6 +76,14 @@ interface InvalidationBus {
64
76
  subscribe(handler: (message: InvalidationMessage) => Promise<void> | void): Promise<() => Promise<void> | void>;
65
77
  publish(message: InvalidationMessage): Promise<void>;
66
78
  }
79
+ interface CacheSingleFlightExecutionOptions {
80
+ leaseMs: number;
81
+ waitTimeoutMs: number;
82
+ pollIntervalMs: number;
83
+ }
84
+ interface CacheSingleFlightCoordinator {
85
+ execute<T>(key: string, options: CacheSingleFlightExecutionOptions, worker: () => Promise<T>, waiter: () => Promise<T>): Promise<T>;
86
+ }
67
87
  interface CacheStackOptions {
68
88
  logger?: CacheLogger | boolean;
69
89
  metrics?: boolean;
@@ -71,6 +91,16 @@ interface CacheStackOptions {
71
91
  invalidationBus?: InvalidationBus;
72
92
  tagIndex?: CacheTagIndex;
73
93
  publishSetInvalidation?: boolean;
94
+ negativeCaching?: boolean;
95
+ negativeTtl?: number | LayerTtlMap;
96
+ staleWhileRevalidate?: number | LayerTtlMap;
97
+ staleIfError?: number | LayerTtlMap;
98
+ ttlJitter?: number | LayerTtlMap;
99
+ writePolicy?: 'strict' | 'best-effort';
100
+ singleFlightCoordinator?: CacheSingleFlightCoordinator;
101
+ singleFlightLeaseMs?: number;
102
+ singleFlightTimeoutMs?: number;
103
+ singleFlightPollMs?: number;
74
104
  }
75
105
 
76
106
  declare class CacheStack {
@@ -83,6 +113,7 @@ declare class CacheStack {
83
113
  private unsubscribeInvalidation?;
84
114
  private readonly logger;
85
115
  private readonly tagIndex;
116
+ private readonly backgroundRefreshes;
86
117
  constructor(layers: CacheLayer[], options?: CacheStackOptions);
87
118
  get<T>(key: string, fetcher?: () => Promise<T>, options?: CacheGetOptions): Promise<T | null>;
88
119
  set<T>(key: string, value: T, options?: CacheWriteOptions): Promise<void>;
@@ -96,13 +127,27 @@ declare class CacheStack {
96
127
  resetMetrics(): void;
97
128
  disconnect(): Promise<void>;
98
129
  private initialize;
99
- private getFromLayers;
130
+ private fetchWithGuards;
131
+ private waitForFreshValue;
132
+ private fetchAndPopulate;
133
+ private storeEntry;
134
+ private readFromLayers;
135
+ private readLayerEntry;
100
136
  private backfill;
101
- private setAcrossLayers;
102
- private resolveTtl;
137
+ private writeAcrossLayers;
138
+ private executeLayerOperations;
139
+ private resolveFreshTtl;
140
+ private resolveLayerSeconds;
141
+ private readLayerNumber;
142
+ private applyJitter;
143
+ private shouldNegativeCache;
144
+ private scheduleBackgroundRefresh;
145
+ private resolveSingleFlightOptions;
103
146
  private deleteKeys;
104
147
  private publishInvalidation;
105
148
  private handleInvalidationMessage;
149
+ private formatError;
150
+ private sleep;
106
151
  }
107
152
 
108
153
  declare class PatternMatcher {
@@ -170,6 +215,8 @@ declare class MemoryLayer implements CacheLayer {
170
215
  private readonly entries;
171
216
  constructor(options?: MemoryLayerOptions);
172
217
  get<T>(key: string): Promise<T | null>;
218
+ getEntry<T = unknown>(key: string): Promise<T | null>;
219
+ getMany<T>(keys: string[]): Promise<Array<T | null>>;
173
220
  set(key: string, value: unknown, ttl?: number | undefined): Promise<void>;
174
221
  delete(key: string): Promise<void>;
175
222
  deleteMany(keys: string[]): Promise<void>;
@@ -199,6 +246,8 @@ declare class RedisLayer implements CacheLayer {
199
246
  private readonly scanCount;
200
247
  constructor(options: RedisLayerOptions);
201
248
  get<T>(key: string): Promise<T | null>;
249
+ getEntry<T = unknown>(key: string): Promise<T | null>;
250
+ getMany<T>(keys: string[]): Promise<Array<T | null>>;
202
251
  set(key: string, value: unknown, ttl?: number | undefined): Promise<void>;
203
252
  delete(key: string): Promise<void>;
204
253
  deleteMany(keys: string[]): Promise<void>;
@@ -218,10 +267,21 @@ declare class MsgpackSerializer implements CacheSerializer {
218
267
  deserialize<T>(payload: string | Buffer): T;
219
268
  }
220
269
 
270
+ interface RedisSingleFlightCoordinatorOptions {
271
+ client: Redis;
272
+ prefix?: string;
273
+ }
274
+ declare class RedisSingleFlightCoordinator implements CacheSingleFlightCoordinator {
275
+ private readonly client;
276
+ private readonly prefix;
277
+ constructor(options: RedisSingleFlightCoordinatorOptions);
278
+ execute<T>(key: string, options: CacheSingleFlightExecutionOptions, worker: () => Promise<T>, waiter: () => Promise<T>): Promise<T>;
279
+ }
280
+
221
281
  declare class StampedeGuard {
222
282
  private readonly mutexes;
223
283
  execute<T>(key: string, task: () => Promise<T>): Promise<T>;
224
284
  private getMutex;
225
285
  }
226
286
 
227
- export { type CacheGetOptions, type CacheLayer, type CacheLogger, type CacheMGetEntry, type CacheMSetEntry, type CacheMetricsSnapshot, type CacheSerializer, CacheStack, type CacheStackOptions, type CacheTagIndex, type CacheWriteOptions, type InvalidationBus, type InvalidationMessage, JsonSerializer, type LayerTtlMap, MemoryLayer, MsgpackSerializer, PatternMatcher, RedisInvalidationBus, RedisLayer, RedisTagIndex, StampedeGuard, TagIndex };
287
+ export { type CacheGetOptions, type CacheLayer, type CacheLogger, type CacheMGetEntry, type CacheMSetEntry, type CacheMetricsSnapshot, type CacheSerializer, type CacheSingleFlightCoordinator, type CacheSingleFlightExecutionOptions, CacheStack, type CacheStackOptions, type CacheTagIndex, type CacheWriteOptions, type InvalidationBus, type InvalidationMessage, JsonSerializer, type LayerTtlMap, MemoryLayer, MsgpackSerializer, PatternMatcher, RedisInvalidationBus, RedisLayer, RedisSingleFlightCoordinator, RedisTagIndex, StampedeGuard, TagIndex };