@gravito/stasis 2.0.0 → 3.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.cjs +5 -6
- package/dist/index.d.cts +533 -19
- package/dist/index.d.ts +533 -19
- package/dist/index.js +5 -6
- package/package.json +3 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,67 +1,295 @@
|
|
|
1
1
|
import { PlanetCore, GravitoOrbit } from '@gravito/core';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Error thrown when a lock cannot be acquired within the specified timeout.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
* @since 3.0.0
|
|
8
|
+
*/
|
|
3
9
|
declare class LockTimeoutError extends Error {
|
|
4
10
|
name: string;
|
|
5
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Interface for a cache-backed distributed lock.
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
* @since 3.0.0
|
|
17
|
+
*/
|
|
6
18
|
interface CacheLock {
|
|
19
|
+
/**
|
|
20
|
+
* Attempt to acquire the lock.
|
|
21
|
+
*
|
|
22
|
+
* @returns `true` if acquired, `false` otherwise.
|
|
23
|
+
*/
|
|
7
24
|
acquire(): Promise<boolean>;
|
|
25
|
+
/**
|
|
26
|
+
* Release the lock.
|
|
27
|
+
*/
|
|
8
28
|
release(): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Attempt to acquire the lock and execute a callback.
|
|
31
|
+
* If the lock is held by someone else, it will wait (poll) until the timeout.
|
|
32
|
+
*
|
|
33
|
+
* @param seconds - Maximum time to wait for the lock in seconds.
|
|
34
|
+
* @param callback - Function to execute once the lock is acquired.
|
|
35
|
+
* @param options - Polling options.
|
|
36
|
+
* @returns The result of the callback.
|
|
37
|
+
* @throws {LockTimeoutError} If the lock could not be acquired within the timeout.
|
|
38
|
+
*/
|
|
9
39
|
block<T>(seconds: number, callback: () => Promise<T> | T, options?: {
|
|
10
40
|
sleepMillis?: number;
|
|
11
41
|
}): Promise<T>;
|
|
12
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Pause execution for a specified number of milliseconds.
|
|
45
|
+
*
|
|
46
|
+
* @param ms - Milliseconds to sleep.
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
13
49
|
declare function sleep(ms: number): Promise<void>;
|
|
14
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Represents a unique identifier for a cached item.
|
|
53
|
+
*
|
|
54
|
+
* @public
|
|
55
|
+
* @since 3.0.0
|
|
56
|
+
*/
|
|
15
57
|
type CacheKey = string;
|
|
16
58
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* - `
|
|
20
|
-
* - `
|
|
21
|
-
* - `
|
|
59
|
+
* Time-to-live (TTL) configuration for cache entries.
|
|
60
|
+
*
|
|
61
|
+
* - `number`: Seconds from now.
|
|
62
|
+
* - `Date`: Absolute expiry time.
|
|
63
|
+
* - `null`: Cache forever.
|
|
64
|
+
* - `undefined`: Use the store/repository default.
|
|
65
|
+
*
|
|
66
|
+
* @public
|
|
67
|
+
* @since 3.0.0
|
|
22
68
|
*/
|
|
23
69
|
type CacheTtl = number | Date | null | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Result of a cache retrieval.
|
|
72
|
+
* Returns the value of type `T` or `null` if not found or expired.
|
|
73
|
+
*
|
|
74
|
+
* @public
|
|
75
|
+
* @since 3.0.0
|
|
76
|
+
*/
|
|
24
77
|
type CacheValue<T = unknown> = T | null;
|
|
78
|
+
/**
|
|
79
|
+
* Normalize a cache key to ensure it is valid.
|
|
80
|
+
*
|
|
81
|
+
* @param key - The raw cache key.
|
|
82
|
+
* @returns The normalized cache key.
|
|
83
|
+
* @throws {Error} If the key is empty.
|
|
84
|
+
*
|
|
85
|
+
* @public
|
|
86
|
+
* @since 3.0.0
|
|
87
|
+
*/
|
|
25
88
|
declare function normalizeCacheKey(key: string): string;
|
|
89
|
+
/**
|
|
90
|
+
* Convert a CacheTtl value to an absolute epoch timestamp.
|
|
91
|
+
*
|
|
92
|
+
* @param ttl - The TTL value to convert.
|
|
93
|
+
* @param now - Optional current timestamp in milliseconds.
|
|
94
|
+
* @returns The expiration timestamp in milliseconds, null for forever, or undefined.
|
|
95
|
+
*
|
|
96
|
+
* @public
|
|
97
|
+
* @since 3.0.0
|
|
98
|
+
*/
|
|
26
99
|
declare function ttlToExpiresAt(ttl: CacheTtl, now?: number): number | null | undefined;
|
|
100
|
+
/**
|
|
101
|
+
* Determine if a specific expiration timestamp has passed.
|
|
102
|
+
*
|
|
103
|
+
* @param expiresAt - The expiration timestamp in milliseconds (or null for forever).
|
|
104
|
+
* @param now - Optional current timestamp in milliseconds.
|
|
105
|
+
* @returns `true` if expired, `false` otherwise.
|
|
106
|
+
*
|
|
107
|
+
* @public
|
|
108
|
+
* @since 3.0.0
|
|
109
|
+
*/
|
|
27
110
|
declare function isExpired(expiresAt: number | null | undefined, now?: number): boolean;
|
|
28
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Interface for a low-level cache storage backend.
|
|
114
|
+
*
|
|
115
|
+
* All cache drivers (Memory, Redis, etc.) must implement this interface.
|
|
116
|
+
*
|
|
117
|
+
* @public
|
|
118
|
+
* @since 3.0.0
|
|
119
|
+
*/
|
|
29
120
|
interface CacheStore {
|
|
121
|
+
/**
|
|
122
|
+
* Retrieve an item from the cache by its key.
|
|
123
|
+
*
|
|
124
|
+
* @param key - The unique cache key.
|
|
125
|
+
* @returns A promise that resolves to the cached value, or null if not found or expired.
|
|
126
|
+
*/
|
|
30
127
|
get<T = unknown>(key: CacheKey): Promise<CacheValue<T>>;
|
|
128
|
+
/**
|
|
129
|
+
* Store an item in the cache for a specific duration.
|
|
130
|
+
*
|
|
131
|
+
* @param key - The unique cache key.
|
|
132
|
+
* @param value - The value to store.
|
|
133
|
+
* @param ttl - Time-to-live.
|
|
134
|
+
*/
|
|
31
135
|
put(key: CacheKey, value: unknown, ttl: CacheTtl): Promise<void>;
|
|
136
|
+
/**
|
|
137
|
+
* Store an item in the cache only if the key does not already exist.
|
|
138
|
+
*
|
|
139
|
+
* @param key - The unique cache key.
|
|
140
|
+
* @param value - The value to store.
|
|
141
|
+
* @param ttl - Time-to-live.
|
|
142
|
+
* @returns A promise that resolves to true if the item was added, false otherwise.
|
|
143
|
+
*/
|
|
32
144
|
add(key: CacheKey, value: unknown, ttl: CacheTtl): Promise<boolean>;
|
|
145
|
+
/**
|
|
146
|
+
* Remove an item from the cache by its key.
|
|
147
|
+
*
|
|
148
|
+
* @param key - The cache key to remove.
|
|
149
|
+
* @returns A promise that resolves to true if the item existed and was removed.
|
|
150
|
+
*/
|
|
33
151
|
forget(key: CacheKey): Promise<boolean>;
|
|
152
|
+
/**
|
|
153
|
+
* Remove all items from the cache storage.
|
|
154
|
+
*/
|
|
34
155
|
flush(): Promise<void>;
|
|
156
|
+
/**
|
|
157
|
+
* Increment the value of a numeric item in the cache.
|
|
158
|
+
*
|
|
159
|
+
* @param key - The cache key.
|
|
160
|
+
* @param value - The amount to increment by.
|
|
161
|
+
* @returns The new value.
|
|
162
|
+
*/
|
|
35
163
|
increment(key: CacheKey, value?: number): Promise<number>;
|
|
164
|
+
/**
|
|
165
|
+
* Decrement the value of a numeric item in the cache.
|
|
166
|
+
*
|
|
167
|
+
* @param key - The cache key.
|
|
168
|
+
* @param value - The amount to decrement by.
|
|
169
|
+
* @returns The new value.
|
|
170
|
+
*/
|
|
36
171
|
decrement(key: CacheKey, value?: number): Promise<number>;
|
|
172
|
+
/**
|
|
173
|
+
* Get a distributed lock instance for the given name.
|
|
174
|
+
*
|
|
175
|
+
* @param name - The lock name.
|
|
176
|
+
* @param seconds - Optional default duration for the lock.
|
|
177
|
+
* @returns A `CacheLock` instance if supported, otherwise undefined.
|
|
178
|
+
*/
|
|
37
179
|
lock?(name: string, seconds?: number): CacheLock | undefined;
|
|
38
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Interface for cache stores that support grouping entries by tags.
|
|
183
|
+
*
|
|
184
|
+
* @public
|
|
185
|
+
* @since 3.0.0
|
|
186
|
+
*/
|
|
39
187
|
interface TaggableStore {
|
|
188
|
+
/**
|
|
189
|
+
* Remove all items associated with the given tags.
|
|
190
|
+
*
|
|
191
|
+
* @param tags - An array of tag names.
|
|
192
|
+
*/
|
|
40
193
|
flushTags(tags: readonly string[]): Promise<void>;
|
|
194
|
+
/**
|
|
195
|
+
* Generate a tagged key for storage based on the provided tags.
|
|
196
|
+
*
|
|
197
|
+
* @param key - The original cache key.
|
|
198
|
+
* @param tags - An array of tag names.
|
|
199
|
+
* @returns The tagged key string.
|
|
200
|
+
*/
|
|
41
201
|
tagKey(key: string, tags: readonly string[]): string;
|
|
202
|
+
/**
|
|
203
|
+
* Add a key to the index for specific tags.
|
|
204
|
+
*
|
|
205
|
+
* @param tags - An array of tag names.
|
|
206
|
+
* @param taggedKey - The key to associate with the tags.
|
|
207
|
+
*/
|
|
42
208
|
tagIndexAdd(tags: readonly string[], taggedKey: string): void;
|
|
209
|
+
/**
|
|
210
|
+
* Remove a key from the tag index.
|
|
211
|
+
*
|
|
212
|
+
* @param taggedKey - The key to remove from all tag indexes.
|
|
213
|
+
*/
|
|
43
214
|
tagIndexRemove(taggedKey: string): void;
|
|
44
215
|
}
|
|
216
|
+
/**
|
|
217
|
+
* Type guard to check if a cache store supports tagging.
|
|
218
|
+
*
|
|
219
|
+
* @param store - The cache store instance to check.
|
|
220
|
+
* @returns `true` if the store implements `TaggableStore`.
|
|
221
|
+
*
|
|
222
|
+
* @public
|
|
223
|
+
* @since 3.0.0
|
|
224
|
+
*/
|
|
45
225
|
declare function isTaggableStore(store: CacheStore): store is CacheStore & TaggableStore;
|
|
46
226
|
|
|
227
|
+
/**
|
|
228
|
+
* Supported modes for emitting cache events.
|
|
229
|
+
*
|
|
230
|
+
* @public
|
|
231
|
+
* @since 3.0.0
|
|
232
|
+
*/
|
|
47
233
|
type CacheEventMode = 'sync' | 'async' | 'off';
|
|
234
|
+
/**
|
|
235
|
+
* Event handlers for cache lifecycle events.
|
|
236
|
+
*
|
|
237
|
+
* @public
|
|
238
|
+
* @since 3.0.0
|
|
239
|
+
*/
|
|
48
240
|
type CacheEvents = {
|
|
241
|
+
/** Triggered on a cache hit. */
|
|
49
242
|
hit?: (key: string) => void | Promise<void>;
|
|
243
|
+
/** Triggered on a cache miss. */
|
|
50
244
|
miss?: (key: string) => void | Promise<void>;
|
|
245
|
+
/** Triggered when a value is written to the cache. */
|
|
51
246
|
write?: (key: string) => void | Promise<void>;
|
|
247
|
+
/** Triggered when a value is removed from the cache. */
|
|
52
248
|
forget?: (key: string) => void | Promise<void>;
|
|
249
|
+
/** Triggered when the entire cache is flushed. */
|
|
53
250
|
flush?: () => void | Promise<void>;
|
|
54
251
|
};
|
|
252
|
+
/**
|
|
253
|
+
* Options for configuring the `CacheRepository`.
|
|
254
|
+
*
|
|
255
|
+
* @public
|
|
256
|
+
* @since 3.0.0
|
|
257
|
+
*/
|
|
55
258
|
type CacheRepositoryOptions = {
|
|
259
|
+
/** Optional prefix for all cache keys. */
|
|
56
260
|
prefix?: string;
|
|
261
|
+
/** Default time-to-live for cache entries. */
|
|
57
262
|
defaultTtl?: CacheTtl;
|
|
263
|
+
/** Event handlers for cache operations. */
|
|
58
264
|
events?: CacheEvents;
|
|
265
|
+
/** Mode for emitting events (sync, async, or off). @default 'async' */
|
|
59
266
|
eventsMode?: CacheEventMode;
|
|
267
|
+
/** Whether to throw an error if an event handler fails. @default false */
|
|
60
268
|
throwOnEventError?: boolean;
|
|
269
|
+
/** Callback triggered when an event handler encounters an error. */
|
|
61
270
|
onEventError?: (error: unknown, event: keyof CacheEvents, payload: {
|
|
62
271
|
key?: string;
|
|
63
272
|
}) => void;
|
|
64
273
|
};
|
|
274
|
+
/**
|
|
275
|
+
* CacheRepository provides a high-level, developer-friendly API for cache operations.
|
|
276
|
+
*
|
|
277
|
+
* It wraps a low-level `CacheStore` and adds features like:
|
|
278
|
+
* - Key prefixing.
|
|
279
|
+
* - Event emission (hit, miss, write, etc.).
|
|
280
|
+
* - Automatic serialization/deserialization.
|
|
281
|
+
* - Higher-level helpers like `remember`, `flexible`, and `pull`.
|
|
282
|
+
* - Support for tagged cache entries (if the store supports it).
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* const cache = new CacheRepository(redisStore, { prefix: 'app:' });
|
|
287
|
+
* const user = await cache.remember('user:1', 3600, () => fetchUser(1));
|
|
288
|
+
* ```
|
|
289
|
+
*
|
|
290
|
+
* @public
|
|
291
|
+
* @since 3.0.0
|
|
292
|
+
*/
|
|
65
293
|
declare class CacheRepository {
|
|
66
294
|
protected readonly store: CacheStore;
|
|
67
295
|
protected readonly options: CacheRepositoryOptions;
|
|
@@ -105,11 +333,38 @@ declare class CacheRepository {
|
|
|
105
333
|
getStore(): CacheStore;
|
|
106
334
|
}
|
|
107
335
|
|
|
336
|
+
/**
|
|
337
|
+
* Represents the response from a rate limiting attempt.
|
|
338
|
+
*
|
|
339
|
+
* @public
|
|
340
|
+
* @since 3.0.0
|
|
341
|
+
*/
|
|
108
342
|
interface RateLimiterResponse {
|
|
343
|
+
/** Whether the request is allowed. */
|
|
109
344
|
allowed: boolean;
|
|
345
|
+
/** Number of attempts remaining within the current window. */
|
|
110
346
|
remaining: number;
|
|
347
|
+
/** Epoch timestamp in seconds when the rate limit will reset. */
|
|
111
348
|
reset: number;
|
|
112
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* RateLimiter provides a simple mechanism for limiting request frequency.
|
|
352
|
+
*
|
|
353
|
+
* It uses a `CacheStore` backend to track attempt counts and handle
|
|
354
|
+
* expiration (sliding or fixed window depending on store capability).
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```typescript
|
|
358
|
+
* const limiter = new RateLimiter(cacheStore);
|
|
359
|
+
* const status = await limiter.attempt('login:127.0.0.1', 5, 60);
|
|
360
|
+
* if (!status.allowed) {
|
|
361
|
+
* throw new Error('Too many attempts');
|
|
362
|
+
* }
|
|
363
|
+
* ```
|
|
364
|
+
*
|
|
365
|
+
* @public
|
|
366
|
+
* @since 3.0.0
|
|
367
|
+
*/
|
|
113
368
|
declare class RateLimiter {
|
|
114
369
|
private store;
|
|
115
370
|
constructor(store: CacheStore);
|
|
@@ -126,6 +381,12 @@ declare class RateLimiter {
|
|
|
126
381
|
clear(key: string): Promise<void>;
|
|
127
382
|
}
|
|
128
383
|
|
|
384
|
+
/**
|
|
385
|
+
* Configuration for a specific cache store.
|
|
386
|
+
*
|
|
387
|
+
* @public
|
|
388
|
+
* @since 3.0.0
|
|
389
|
+
*/
|
|
129
390
|
type StoreConfig = {
|
|
130
391
|
driver: 'memory';
|
|
131
392
|
maxItems?: number;
|
|
@@ -141,14 +402,47 @@ type StoreConfig = {
|
|
|
141
402
|
} | {
|
|
142
403
|
driver: 'provider';
|
|
143
404
|
};
|
|
405
|
+
/**
|
|
406
|
+
* Global cache configuration for multiple stores.
|
|
407
|
+
*
|
|
408
|
+
* @public
|
|
409
|
+
* @since 3.0.0
|
|
410
|
+
*/
|
|
144
411
|
type CacheConfig = {
|
|
412
|
+
/** The name of the default store to use. @default 'memory' */
|
|
145
413
|
default?: string;
|
|
414
|
+
/** Global prefix for all cache keys across all stores. */
|
|
146
415
|
prefix?: string;
|
|
416
|
+
/** Global default time-to-live for cache entries. */
|
|
147
417
|
defaultTtl?: CacheTtl;
|
|
418
|
+
/** Map of named store configurations. */
|
|
148
419
|
stores?: Record<string, StoreConfig & {
|
|
149
420
|
provider?: CacheStore;
|
|
150
421
|
}>;
|
|
151
422
|
};
|
|
423
|
+
/**
|
|
424
|
+
* CacheManager orchestrates multiple cache stores and provides a unified API.
|
|
425
|
+
*
|
|
426
|
+
* It supports various drivers (Memory, File, Redis) and provides both
|
|
427
|
+
* a multi-store API (`cache.store('redis').get(...)`) and a default store
|
|
428
|
+
* proxy API (`cache.get(...)`).
|
|
429
|
+
*
|
|
430
|
+
* @example
|
|
431
|
+
* ```typescript
|
|
432
|
+
* const cache = new CacheManager(factory, {
|
|
433
|
+
* default: 'redis',
|
|
434
|
+
* stores: {
|
|
435
|
+
* redis: { driver: 'redis', prefix: 'myapp:' }
|
|
436
|
+
* }
|
|
437
|
+
* });
|
|
438
|
+
*
|
|
439
|
+
* await cache.set('key', 'value', 3600);
|
|
440
|
+
* const val = await cache.get('key');
|
|
441
|
+
* ```
|
|
442
|
+
*
|
|
443
|
+
* @public
|
|
444
|
+
* @since 3.0.0
|
|
445
|
+
*/
|
|
152
446
|
declare class CacheManager {
|
|
153
447
|
private readonly storeFactory;
|
|
154
448
|
private readonly config;
|
|
@@ -295,9 +589,26 @@ declare class CacheManager {
|
|
|
295
589
|
tags(tags: readonly string[]): CacheRepository;
|
|
296
590
|
}
|
|
297
591
|
|
|
592
|
+
/**
|
|
593
|
+
* Options for configuring the `FileStore`.
|
|
594
|
+
*
|
|
595
|
+
* @public
|
|
596
|
+
* @since 3.0.0
|
|
597
|
+
*/
|
|
298
598
|
type FileStoreOptions = {
|
|
599
|
+
/** The directory where cache files will be stored. */
|
|
299
600
|
directory: string;
|
|
300
601
|
};
|
|
602
|
+
/**
|
|
603
|
+
* FileStore implements the `CacheStore` interface using the local file system.
|
|
604
|
+
*
|
|
605
|
+
* It stores cache entries as individual JSON files in the specified directory.
|
|
606
|
+
* While not as fast as memory or Redis, it provides persistent caching across
|
|
607
|
+
* application restarts without external dependencies.
|
|
608
|
+
*
|
|
609
|
+
* @public
|
|
610
|
+
* @since 3.0.0
|
|
611
|
+
*/
|
|
301
612
|
declare class FileStore implements CacheStore {
|
|
302
613
|
private options;
|
|
303
614
|
constructor(options: FileStoreOptions);
|
|
@@ -313,9 +624,25 @@ declare class FileStore implements CacheStore {
|
|
|
313
624
|
lock(name: string, seconds?: number): CacheLock;
|
|
314
625
|
}
|
|
315
626
|
|
|
627
|
+
/**
|
|
628
|
+
* Options for configuring the `MemoryStore`.
|
|
629
|
+
*
|
|
630
|
+
* @public
|
|
631
|
+
* @since 3.0.0
|
|
632
|
+
*/
|
|
316
633
|
type MemoryStoreOptions = {
|
|
634
|
+
/** Maximum number of items to keep in memory. Uses LRU eviction. */
|
|
317
635
|
maxItems?: number;
|
|
318
636
|
};
|
|
637
|
+
/**
|
|
638
|
+
* MemoryStore implements the `CacheStore` interface using a Map.
|
|
639
|
+
*
|
|
640
|
+
* It provides a fast, non-persistent cache backend with support for
|
|
641
|
+
* TTL expiration, basic tagging, and local locking.
|
|
642
|
+
*
|
|
643
|
+
* @public
|
|
644
|
+
* @since 3.0.0
|
|
645
|
+
*/
|
|
319
646
|
declare class MemoryStore implements CacheStore, TaggableStore {
|
|
320
647
|
private options;
|
|
321
648
|
private entries;
|
|
@@ -340,6 +667,16 @@ declare class MemoryStore implements CacheStore, TaggableStore {
|
|
|
340
667
|
flushTags(tags: readonly string[]): Promise<void>;
|
|
341
668
|
}
|
|
342
669
|
|
|
670
|
+
/**
|
|
671
|
+
* NullStore is a black-hole cache implementation.
|
|
672
|
+
*
|
|
673
|
+
* It implements the `CacheStore` interface but does not actually store or
|
|
674
|
+
* retrieve any data. This is useful for disabling caching entirely or for
|
|
675
|
+
* testing.
|
|
676
|
+
*
|
|
677
|
+
* @public
|
|
678
|
+
* @since 3.0.0
|
|
679
|
+
*/
|
|
343
680
|
declare class NullStore implements CacheStore {
|
|
344
681
|
get<T = unknown>(_key: CacheKey): Promise<CacheValue<T>>;
|
|
345
682
|
put(_key: CacheKey, _value: unknown, _ttl: CacheTtl): Promise<void>;
|
|
@@ -350,10 +687,27 @@ declare class NullStore implements CacheStore {
|
|
|
350
687
|
decrement(_key: CacheKey, _value?: number): Promise<number>;
|
|
351
688
|
}
|
|
352
689
|
|
|
690
|
+
/**
|
|
691
|
+
* Options for configuring the `RedisStore`.
|
|
692
|
+
*
|
|
693
|
+
* @public
|
|
694
|
+
* @since 3.0.0
|
|
695
|
+
*/
|
|
353
696
|
type RedisStoreOptions = {
|
|
697
|
+
/** The name of the Redis connection to use. */
|
|
354
698
|
connection?: string;
|
|
699
|
+
/** Optional Redis-level prefix for keys. */
|
|
355
700
|
prefix?: string;
|
|
356
701
|
};
|
|
702
|
+
/**
|
|
703
|
+
* RedisStore implements the `CacheStore` interface using Redis.
|
|
704
|
+
*
|
|
705
|
+
* It provides a distributed, persistent cache backend with support for
|
|
706
|
+
* atomic increments/decrements, tagging, and distributed locking.
|
|
707
|
+
*
|
|
708
|
+
* @public
|
|
709
|
+
* @since 3.0.0
|
|
710
|
+
*/
|
|
357
711
|
declare class RedisStore implements CacheStore, TaggableStore {
|
|
358
712
|
private connectionName?;
|
|
359
713
|
constructor(options?: RedisStoreOptions);
|
|
@@ -372,63 +726,223 @@ declare class RedisStore implements CacheStore, TaggableStore {
|
|
|
372
726
|
lock(name: string, seconds?: number): CacheLock;
|
|
373
727
|
}
|
|
374
728
|
|
|
375
|
-
|
|
729
|
+
/**
|
|
730
|
+
* Interface for a low-level cache storage provider.
|
|
731
|
+
*
|
|
732
|
+
* Use this to implement custom storage backends that can be plugged into Stasis.
|
|
733
|
+
*
|
|
734
|
+
* @public
|
|
735
|
+
* @since 3.0.0
|
|
736
|
+
*/
|
|
737
|
+
interface CacheStorageProvider {
|
|
738
|
+
/**
|
|
739
|
+
* Retrieve an item from the cache.
|
|
740
|
+
*
|
|
741
|
+
* @param key - The unique cache key.
|
|
742
|
+
* @returns The cached value, or null if not found.
|
|
743
|
+
*/
|
|
376
744
|
get<T = unknown>(key: string): Promise<T | null>;
|
|
745
|
+
/**
|
|
746
|
+
* Store an item in the cache.
|
|
747
|
+
*
|
|
748
|
+
* @param key - The unique cache key.
|
|
749
|
+
* @param value - The value to store.
|
|
750
|
+
* @param ttl - Time-to-live in seconds.
|
|
751
|
+
*/
|
|
377
752
|
set(key: string, value: unknown, ttl?: number): Promise<void>;
|
|
753
|
+
/**
|
|
754
|
+
* Delete an item from the cache.
|
|
755
|
+
*
|
|
756
|
+
* @param key - The cache key to remove.
|
|
757
|
+
*/
|
|
378
758
|
delete(key: string): Promise<void>;
|
|
759
|
+
/**
|
|
760
|
+
* Clear all items from the cache storage.
|
|
761
|
+
*/
|
|
379
762
|
clear(): Promise<void>;
|
|
380
763
|
}
|
|
764
|
+
/** @deprecated Use CacheStorageProvider instead */
|
|
765
|
+
type CacheProvider = CacheStorageProvider;
|
|
766
|
+
/**
|
|
767
|
+
* High-level application contract for caching operations.
|
|
768
|
+
*
|
|
769
|
+
* This is the public API exposed to the rest of the application via
|
|
770
|
+
* the request context or service container.
|
|
771
|
+
*
|
|
772
|
+
* @public
|
|
773
|
+
* @since 3.0.0
|
|
774
|
+
*/
|
|
381
775
|
interface CacheService {
|
|
776
|
+
/**
|
|
777
|
+
* Retrieve an item from the cache.
|
|
778
|
+
*
|
|
779
|
+
* @param key - The unique cache key.
|
|
780
|
+
* @returns The cached value, or null if not found or expired.
|
|
781
|
+
*/
|
|
382
782
|
get<T = unknown>(key: string): Promise<T | null>;
|
|
783
|
+
/**
|
|
784
|
+
* Store an item in the cache with an optional TTL.
|
|
785
|
+
*
|
|
786
|
+
* @param key - The unique cache key.
|
|
787
|
+
* @param value - The value to store.
|
|
788
|
+
* @param ttl - Time-to-live in seconds or an absolute Date.
|
|
789
|
+
*/
|
|
383
790
|
set(key: string, value: unknown, ttl?: CacheTtl): Promise<void>;
|
|
791
|
+
/**
|
|
792
|
+
* Determine if an item exists in the cache and is not expired.
|
|
793
|
+
*
|
|
794
|
+
* @param key - The cache key to check.
|
|
795
|
+
* @returns True if the item exists and is valid.
|
|
796
|
+
*/
|
|
797
|
+
has(key: string): Promise<boolean>;
|
|
798
|
+
/**
|
|
799
|
+
* Store an item in the cache only if it doesn't already exist.
|
|
800
|
+
*
|
|
801
|
+
* @param key - The unique cache key.
|
|
802
|
+
* @param value - The value to store.
|
|
803
|
+
* @param ttl - Time-to-live.
|
|
804
|
+
* @returns True if the item was added, false otherwise.
|
|
805
|
+
*/
|
|
806
|
+
add(key: string, value: unknown, ttl?: CacheTtl): Promise<boolean>;
|
|
807
|
+
/**
|
|
808
|
+
* Remove an item from the cache.
|
|
809
|
+
*
|
|
810
|
+
* @param key - The cache key to remove.
|
|
811
|
+
* @returns True if the item existed and was removed.
|
|
812
|
+
*/
|
|
384
813
|
delete(key: string): Promise<boolean>;
|
|
385
|
-
|
|
814
|
+
/**
|
|
815
|
+
* Retrieve an item from the cache and then delete it.
|
|
816
|
+
*
|
|
817
|
+
* @param key - The cache key.
|
|
818
|
+
* @param defaultValue - Optional value to return if not found.
|
|
819
|
+
* @returns The cached value or the default.
|
|
820
|
+
*/
|
|
821
|
+
pull<T = unknown>(key: string, defaultValue?: T): Promise<T | null>;
|
|
822
|
+
/**
|
|
823
|
+
* Get an item from the cache, or execute the given callback and store the result.
|
|
824
|
+
*
|
|
825
|
+
* @param key - The cache key.
|
|
826
|
+
* @param ttl - Time-to-live if the item needs to be fetched.
|
|
827
|
+
* @param callback - Closure to execute if the item is missing.
|
|
828
|
+
* @returns The cached or fetched value.
|
|
829
|
+
*/
|
|
386
830
|
remember<T>(key: string, ttl: CacheTtl, callback: () => Promise<T> | T): Promise<T>;
|
|
831
|
+
/**
|
|
832
|
+
* Store an item in the cache indefinitely.
|
|
833
|
+
*
|
|
834
|
+
* @param key - The unique cache key.
|
|
835
|
+
* @param callback - Closure to execute if the item is missing.
|
|
836
|
+
* @returns The cached or fetched value.
|
|
837
|
+
*/
|
|
838
|
+
rememberForever<T>(key: string, callback: () => Promise<T> | T): Promise<T>;
|
|
839
|
+
/**
|
|
840
|
+
* Clear the entire cache.
|
|
841
|
+
*/
|
|
842
|
+
clear(): Promise<void>;
|
|
387
843
|
}
|
|
388
|
-
|
|
844
|
+
/**
|
|
845
|
+
* A standard in-memory implementation of `CacheStorageProvider`.
|
|
846
|
+
*
|
|
847
|
+
* @public
|
|
848
|
+
* @since 3.0.0
|
|
849
|
+
*/
|
|
850
|
+
declare class MemoryCacheProvider implements CacheStorageProvider {
|
|
389
851
|
private store;
|
|
390
852
|
get<T = unknown>(key: string): Promise<T | null>;
|
|
391
853
|
set(key: string, value: unknown, ttl?: number): Promise<void>;
|
|
392
854
|
delete(key: string): Promise<void>;
|
|
393
855
|
clear(): Promise<void>;
|
|
394
856
|
}
|
|
395
|
-
|
|
857
|
+
/**
|
|
858
|
+
* Configuration for an individual cache store instance.
|
|
859
|
+
*
|
|
860
|
+
* @public
|
|
861
|
+
* @since 3.0.0
|
|
862
|
+
*/
|
|
863
|
+
type OrbitCacheStoreConfig =
|
|
864
|
+
/** Simple in-memory cache using a Map. */
|
|
865
|
+
{
|
|
396
866
|
driver: 'memory';
|
|
397
867
|
maxItems?: number;
|
|
398
|
-
}
|
|
868
|
+
}
|
|
869
|
+
/** Local file system based cache. */
|
|
870
|
+
| {
|
|
399
871
|
driver: 'file';
|
|
400
872
|
directory: string;
|
|
401
|
-
}
|
|
873
|
+
}
|
|
874
|
+
/** Redis-backed distributed cache. */
|
|
875
|
+
| {
|
|
402
876
|
driver: 'redis';
|
|
403
877
|
connection?: string;
|
|
404
878
|
prefix?: string;
|
|
405
|
-
}
|
|
879
|
+
}
|
|
880
|
+
/** No-op cache that stores nothing. */
|
|
881
|
+
| {
|
|
406
882
|
driver: 'null';
|
|
407
|
-
}
|
|
883
|
+
}
|
|
884
|
+
/** Use a custom implementation of the low-level `CacheStore` interface. */
|
|
885
|
+
| {
|
|
408
886
|
driver: 'custom';
|
|
409
887
|
store: CacheStore;
|
|
410
|
-
}
|
|
888
|
+
}
|
|
889
|
+
/** Use an implementation of the `CacheStorageProvider` interface. */
|
|
890
|
+
| {
|
|
411
891
|
driver: 'provider';
|
|
412
|
-
provider:
|
|
892
|
+
provider: CacheStorageProvider;
|
|
413
893
|
};
|
|
894
|
+
/**
|
|
895
|
+
* Options for configuring the `OrbitStasis` cache orbit.
|
|
896
|
+
*
|
|
897
|
+
* @public
|
|
898
|
+
* @since 3.0.0
|
|
899
|
+
*/
|
|
414
900
|
interface OrbitCacheOptions {
|
|
901
|
+
/** The key used to expose the cache manager in the request context. @default 'cache' */
|
|
415
902
|
exposeAs?: string;
|
|
903
|
+
/** The name of the default store to use for proxy methods. @default 'memory' */
|
|
416
904
|
default?: string;
|
|
905
|
+
/** Global prefix for all cache keys across all stores. */
|
|
417
906
|
prefix?: string;
|
|
907
|
+
/** Default time-to-live for cache entries if not specified. @default 60 */
|
|
418
908
|
defaultTtl?: CacheTtl;
|
|
909
|
+
/** Map of named cache stores and their configurations. */
|
|
419
910
|
stores?: Record<string, OrbitCacheStoreConfig>;
|
|
911
|
+
/** How to handle cache events (hit/miss/etc) */
|
|
420
912
|
eventsMode?: CacheEventMode;
|
|
913
|
+
/** Whether to throw if an event listener fails. @default false */
|
|
421
914
|
throwOnEventError?: boolean;
|
|
915
|
+
/** Custom error handler for cache events. */
|
|
422
916
|
onEventError?: (error: unknown, event: keyof CacheEvents, payload: {
|
|
423
917
|
key?: string;
|
|
424
918
|
}) => void;
|
|
425
|
-
provider
|
|
919
|
+
/** @deprecated Use stores mapping with 'provider' driver. */
|
|
920
|
+
provider?: CacheStorageProvider;
|
|
921
|
+
/** @deprecated Use defaultTtl. */
|
|
426
922
|
defaultTTL?: number;
|
|
427
923
|
}
|
|
428
924
|
/**
|
|
429
|
-
* OrbitStasis
|
|
925
|
+
* OrbitStasis is the core caching module for Gravito.
|
|
926
|
+
*
|
|
927
|
+
* It provides a robust, multi-store cache management system with support for:
|
|
928
|
+
* - Pluggable backends (Memory, File, Redis).
|
|
929
|
+
* - Advanced features like tags, distributed locks, and atomic increments.
|
|
930
|
+
* - Stale-while-revalidate (flexible) caching strategy.
|
|
931
|
+
* - Integrated rate limiting.
|
|
932
|
+
*
|
|
933
|
+
* @example
|
|
934
|
+
* ```typescript
|
|
935
|
+
* const stasis = new OrbitStasis({
|
|
936
|
+
* default: 'redis',
|
|
937
|
+
* stores: {
|
|
938
|
+
* redis: { driver: 'redis', connection: 'default' }
|
|
939
|
+
* }
|
|
940
|
+
* });
|
|
941
|
+
* core.addOrbit(stasis);
|
|
942
|
+
* ```
|
|
430
943
|
*
|
|
431
|
-
*
|
|
944
|
+
* @public
|
|
945
|
+
* @since 3.0.0
|
|
432
946
|
*/
|
|
433
947
|
declare class OrbitStasis implements GravitoOrbit {
|
|
434
948
|
private options?;
|
|
@@ -447,4 +961,4 @@ declare module '@gravito/core' {
|
|
|
447
961
|
}
|
|
448
962
|
}
|
|
449
963
|
|
|
450
|
-
export { type CacheConfig, type CacheEventMode, type CacheEvents, type CacheKey, type CacheLock, CacheManager, type CacheProvider, CacheRepository, type CacheRepositoryOptions, type CacheService, type CacheStore, type CacheTtl, type CacheValue, FileStore, type FileStoreOptions, LockTimeoutError, MemoryCacheProvider, MemoryStore, type MemoryStoreOptions, NullStore, OrbitCache, type OrbitCacheOptions, type OrbitCacheStoreConfig, OrbitStasis, RateLimiter, type RateLimiterResponse, RedisStore, type RedisStoreOptions, type StoreConfig, type TaggableStore, orbitCache as default, isExpired, isTaggableStore, normalizeCacheKey, sleep, ttlToExpiresAt };
|
|
964
|
+
export { type CacheConfig, type CacheEventMode, type CacheEvents, type CacheKey, type CacheLock, CacheManager, type CacheProvider, CacheRepository, type CacheRepositoryOptions, type CacheService, type CacheStorageProvider, type CacheStore, type CacheTtl, type CacheValue, FileStore, type FileStoreOptions, LockTimeoutError, MemoryCacheProvider, MemoryStore, type MemoryStoreOptions, NullStore, OrbitCache, type OrbitCacheOptions, type OrbitCacheStoreConfig, OrbitStasis, RateLimiter, type RateLimiterResponse, RedisStore, type RedisStoreOptions, type StoreConfig, type TaggableStore, orbitCache as default, isExpired, isTaggableStore, normalizeCacheKey, sleep, ttlToExpiresAt };
|