@sebspark/promise-cache 6.1.2 → 6.2.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.
@@ -0,0 +1,940 @@
1
+ import { RedisClientOptions, RedisClientOptions as RedisClientOptions$1, SetOptions, createClient } from "redis";
2
+ import { UUID } from "node:crypto";
3
+ import { add, sub } from "date-fns";
4
+
5
+ //#region src/types.d.ts
6
+ type MultiExecReturnTypes = string[] | string | {
7
+ [x: string]: string;
8
+ } | number | boolean | null | undefined;
9
+ /**
10
+ * Defines the expiration strategy for cached values.
11
+ *
12
+ * - A `number` is interpreted as a TTL (time-to-live) in **milliseconds**.
13
+ * - A `Date` represents an **exact expiration timestamp**.
14
+ * - If omitted, the cache entry **never expires**.
15
+ */
16
+ type Expiry = number | Date;
17
+ /**
18
+ * Options for caching a function's result.
19
+ * @template A - Argument types of the function.
20
+ */
21
+ type CachingOptions<A extends unknown[], R> = {
22
+ /**
23
+ * A fixed key or a function that generates a key based on function arguments.
24
+ */
25
+ key: string | ((...args: A) => string);
26
+ /**
27
+ * Defines how long the cached value remains valid before expiring.
28
+ *
29
+ * - A **number** is treated as a TTL (time-to-live) in **milliseconds**.
30
+ * - A **Date** sets an **exact expiration timestamp**.
31
+ * - A **function** dynamically determines the expiration based on:
32
+ * - `args` - The function arguments.
33
+ * - `response` - The function result.
34
+ * - If omitted, the cache entry **does not expire**.
35
+ */
36
+ expiry?: Expiry | ((args: A, response: R) => Expiry);
37
+ };
38
+ /**
39
+ * Represents a caching system that wraps asynchronous functions.
40
+ */
41
+ type Cache = {
42
+ /**
43
+ * The underlying persistor used for caching.
44
+ */
45
+ persistor: IPersistor;
46
+ /**
47
+ * Wraps an asynchronous function to enable caching.
48
+ * @template A - Argument types.
49
+ * @template R - Return type.
50
+ * @param {Delegate<A, R>} delegate - The function to wrap.
51
+ * @param {CachingOptions<A>} options - Caching options, including key strategy.
52
+ * @returns {Delegate<A, R>} A new function that caches results.
53
+ */
54
+ wrap<A extends unknown[], R>(delegate: (...args: A) => Promise<R>, options: CachingOptions<A, R>): (...args: A) => Promise<R>;
55
+ };
56
+ type HashTypes = string | number;
57
+ type HashValue = {
58
+ [x: string]: HashTypes;
59
+ [x: number]: HashTypes;
60
+ } | Map<HashTypes, HashTypes>;
61
+ /**
62
+ * Interface for a key-value storage system that supports Redis-like commands.
63
+ * Provides methods for storing, retrieving, and managing data with optional expiration.
64
+ */
65
+ interface IPersistor {
66
+ /**
67
+ * Stores a value in Redis or Memory with an optional expiration.
68
+ * @param key - The storage key.
69
+ * @param value - The string value to store.
70
+ * @param options - Expiration options (TTL, absolute expiration, etc.).
71
+ * @returns Resolves to `"OK"` if successful, otherwise `null` if the operation fails.
72
+ */
73
+ set(key: string, value: HashTypes, options?: SetOptions): Promise<string | null>;
74
+ /**
75
+ * Retrieves a value from Redis or Memory.
76
+ * @param key - The storage key.
77
+ * @returns Resolves to the stored value as a string, or `null` if the key does not exist.
78
+ */
79
+ get(key: string): Promise<string | null>;
80
+ /**
81
+ * Deletes a key from Redis or Memory.
82
+ * @param key - The storage key.
83
+ * @returns Resolves to the number of keys removed (`1` if deleted, `0` if the key was not found).
84
+ */
85
+ del(key: string): Promise<number>;
86
+ /**
87
+ * Sets a time-to-live (TTL) in seconds for a key.
88
+ * @param key - The storage key.
89
+ * @param seconds - TTL in seconds.
90
+ * @returns Resolves to `true` if the expiration was successfully set, `false` if the key does not exist.
91
+ */
92
+ expire(key: string, seconds: number): Promise<number>;
93
+ /**
94
+ * Gets the remaining TTL (time-to-live) of a key.
95
+ * @param key - The storage key.
96
+ * @returns
97
+ * - The remaining TTL in seconds.
98
+ * - `-1` if the key exists but has no expiration.
99
+ * - `-2` if the key does not exist.
100
+ */
101
+ ttl(key: string): Promise<number>;
102
+ /**
103
+ * Clears all keys from the storage system.
104
+ * @returns Resolves to `"OK"` when the operation completes successfully.
105
+ */
106
+ flushAll(): Promise<string>;
107
+ /**
108
+ * Stores a value and sets an expiration time in seconds.
109
+ * @param key - The storage key.
110
+ * @param seconds - Expiration time in seconds.
111
+ * @param value - The string value to store.
112
+ * @returns Resolves to `"OK"` if successful, otherwise `null` if the operation fails.
113
+ */
114
+ setEx(key: string, seconds: number, value: string): Promise<string | null>;
115
+ /**
116
+ * Stores a value and sets an expiration time in milliseconds.
117
+ * @param key - The storage key.
118
+ * @param milliseconds - Expiration time in milliseconds.
119
+ * @param value - The string value to store.
120
+ * @returns Resolves to `"OK"` if successful, otherwise `null` if the operation fails.
121
+ */
122
+ pSetEx(key: string, milliseconds: number, value: string): Promise<string | null>;
123
+ /**
124
+ * Stores a value **only if the key does not already exist**.
125
+ * @param key - The storage key.
126
+ * @param value - The string value to store.
127
+ * @returns Resolves to `true` if the key was set, or `false` if the key already exists.
128
+ */
129
+ setNX(key: string, value: string): Promise<number>;
130
+ /**
131
+ * Creates a multi-command batch operation.
132
+ * This allows multiple commands to be executed in a batch, improving performance.
133
+ * @returns An instance of `IPersistorMulti` to queue multiple commands.
134
+ */
135
+ multi(): IPersistorMulti;
136
+ /**
137
+ * Checks if keys exist in storage.
138
+ * @param keys - One or more keys to check.
139
+ * @returns Resolves to the number of keys that exist.
140
+ */
141
+ exists(key: string | string[]): Promise<number>;
142
+ /**
143
+ * Increments a key by 1.
144
+ * @param key - The key to increment.
145
+ * @returns Resolves to the new value after increment.
146
+ */
147
+ incr(key: string): Promise<number>;
148
+ /**
149
+ * Decrements a key by 1.
150
+ * @param key - The key to decrement.
151
+ * @returns Resolves to the new value after decrement.
152
+ */
153
+ decr(key: string): Promise<number>;
154
+ /**
155
+ * Increments a key by a specified amount.
156
+ * @param key - The key to increment.
157
+ * @param increment - The amount to increase by.
158
+ * @returns Resolves to the new value after increment.
159
+ */
160
+ incrBy(key: string, increment: number): Promise<number>;
161
+ /**
162
+ * Decrements a key by a specified amount.
163
+ * @param key - The key to decrement.
164
+ * @param decrement - The amount to decrease by.
165
+ * @returns Resolves to the new value after decrement.
166
+ */
167
+ decrBy(key: string, decrement: number): Promise<number>;
168
+ /**
169
+ * Sets a field in a hash.
170
+ * @param key - The hash key.
171
+ * @param field - The field name.
172
+ * @param value - The value to store.
173
+ * @returns Resolves to `1` if the field was added, or `0` if updated.
174
+ */
175
+ hSet(key: string, field: string, value: HashTypes): Promise<number>;
176
+ /**
177
+ * Sets a (partial) hash.
178
+ * @param key - The hash key.
179
+ * @param value - The (partial) value to store.
180
+ * @returns Resolves to `1` if a field was added, or `0` if updated.
181
+ */
182
+ hSet(key: string, value: HashValue): Promise<number>;
183
+ /**
184
+ * Retrieves a field from a hash.
185
+ * @param key - The hash key.
186
+ * @param field - The field name.
187
+ * @returns Resolves to the value, or null if the field does not exist.
188
+ */
189
+ hGet(key: string, field: string): Promise<string | null>;
190
+ /**
191
+ * Retrieves a hash value.
192
+ * @param key - The hash key.
193
+ * @returns Resolves to the value, or null if the hash does not exist.
194
+ */
195
+ hGetAll(key: string): Promise<{
196
+ [x: string]: string;
197
+ }>;
198
+ /**
199
+ * Pushes values to the left (head) of a list.
200
+ * @param key - The list key.
201
+ * @param values - The values to add.
202
+ * @returns Resolves to the length of the list after the operation.
203
+ */
204
+ lPush(key: string, values: string | string[]): Promise<number>;
205
+ /**
206
+ * Pushes values to the right (tail) of a list.
207
+ * @param key - The list key.
208
+ * @param values - The values to add.
209
+ * @returns Resolves to the length of the list after the operation.
210
+ */
211
+ rPush(key: string, values: string | string[]): Promise<number>;
212
+ /**
213
+ * Removes and returns the first element from a list.
214
+ * @param key - The list key.
215
+ * @returns Resolves to the removed element, or `null` if the list is empty.
216
+ */
217
+ lPop(key: string): Promise<string | null>;
218
+ /**
219
+ * Removes and returns the last element from a list.
220
+ * @param key - The list key.
221
+ * @returns Resolves to the removed element, or `null` if the list is empty.
222
+ */
223
+ rPop(key: string): Promise<string | null>;
224
+ /**
225
+ * Retrieves a range of elements from a list.
226
+ * @param key - The list key.
227
+ * @param start - The start index.
228
+ * @param stop - The stop index (inclusive).
229
+ * @returns Resolves to an array of elements in the range.
230
+ */
231
+ lRange(key: string, start: number, stop: number): Promise<string[]>;
232
+ /**
233
+ * Adds members to a set.
234
+ * @param key - The set key.
235
+ * @param values - The values to add.
236
+ * @returns Resolves to the number of elements successfully added.
237
+ */
238
+ sAdd(key: string, values: string | string[]): Promise<number>;
239
+ /**
240
+ * Removes members from a set.
241
+ * @param key - The set key.
242
+ * @param values - The values to remove.
243
+ * @returns Resolves to the number of elements removed.
244
+ */
245
+ sRem(key: string, values: string | string[]): Promise<number>;
246
+ /**
247
+ * Retrieves all members of a set.
248
+ * @param key - The set key.
249
+ * @returns Resolves to an array of all members in the set.
250
+ */
251
+ sMembers(key: string): Promise<string[]>;
252
+ /**
253
+ * Adds members to a sorted set with scores.
254
+ * @param key - The sorted set key.
255
+ * @param members - An array of objects with `score` and `value`.
256
+ * @returns Resolves to the number of elements successfully added.
257
+ */
258
+ zAdd(key: string, members: {
259
+ score: number;
260
+ value: string;
261
+ }[]): Promise<number>;
262
+ /**
263
+ * Retrieves a range of members from a sorted set.
264
+ * @param key - The sorted set key.
265
+ * @param start - The start index.
266
+ * @param stop - The stop index (inclusive).
267
+ * @returns Resolves to an array of member values in the range.
268
+ */
269
+ zRange(key: string, start: number, stop: number): Promise<string[]>;
270
+ /**
271
+ * Removes members from a sorted set.
272
+ * @param key - The sorted set key.
273
+ * @param members - The members to remove.
274
+ * @returns Resolves to the number of elements removed.
275
+ */
276
+ zRem(key: string, members: string | string[]): Promise<number>;
277
+ isReady: boolean;
278
+ isOpen: boolean;
279
+ connect(): Promise<IPersistor>;
280
+ once(event: IPersistorEvent, cb: EventCallback): IPersistor;
281
+ }
282
+ type IPersistorEvent = 'ready' | 'connect' | 'reconnecting';
283
+ type EventCallback = () => void;
284
+ /**
285
+ * Interface for executing multiple storage commands in a batch operation.
286
+ * All commands are queued and executed when `exec()` is called.
287
+ */
288
+ interface IPersistorMulti {
289
+ /**
290
+ * Stores a value in Redis or Memory with an optional expiration.
291
+ * @param key - The storage key.
292
+ * @param value - The string value to store.
293
+ * @param options - Expiration options (TTL, absolute expiration, etc.).
294
+ * @returns The same `IPersistorMulti` instance, enabling method chaining.
295
+ */
296
+ set(key: string, value: HashTypes, options?: SetOptions): IPersistorMulti;
297
+ /**
298
+ * Stores a value and sets an expiration time in seconds.
299
+ * @param key - The storage key.
300
+ * @param seconds - Expiration time in seconds.
301
+ * @param value - The string value to store.
302
+ * @returns The same `IPersistorMulti` instance, enabling method chaining.
303
+ */
304
+ setEx(key: string, seconds: number, value: string): IPersistorMulti;
305
+ /**
306
+ * Stores a value and sets an expiration time in milliseconds.
307
+ * @param key - The storage key.
308
+ * @param milliseconds - Expiration time in milliseconds.
309
+ * @param value - The string value to store.
310
+ * @returns The same `IPersistorMulti` instance, enabling method chaining.
311
+ */
312
+ pSetEx(key: string, milliseconds: number, value: string): IPersistorMulti;
313
+ /**
314
+ * Stores a value **only if the key does not already exist**.
315
+ * @param key - The storage key.
316
+ * @param value - The string value to store.
317
+ * @returns The same `IPersistorMulti` instance, enabling method chaining.
318
+ */
319
+ setNX(key: string, value: string): IPersistorMulti;
320
+ /**
321
+ * Retrieves a value from Redis or Memory.
322
+ * @param key - The storage key.
323
+ * @returns The same `IPersistorMulti` instance, enabling method chaining.
324
+ */
325
+ get(key: string): IPersistorMulti;
326
+ /**
327
+ * Deletes a key from Redis or Memory.
328
+ * @param key - The storage key.
329
+ * @returns The same `IPersistorMulti` instance, enabling method chaining.
330
+ */
331
+ del(key: string): IPersistorMulti;
332
+ /**
333
+ * Sets a time-to-live (TTL) in seconds for a key.
334
+ * @param key - The storage key.
335
+ * @param seconds - TTL in seconds.
336
+ * @returns The same `IPersistorMulti` instance, enabling method chaining.
337
+ */
338
+ expire(key: string, seconds: number): IPersistorMulti;
339
+ /**
340
+ * Gets the remaining TTL (time-to-live) of a key.
341
+ * @param key - The storage key.
342
+ * @returns The same `IPersistorMulti` instance, enabling method chaining.
343
+ */
344
+ ttl(key: string): IPersistorMulti;
345
+ /**
346
+ * Clears all keys from the storage system.
347
+ * @returns The same `IPersistorMulti` instance, enabling method chaining.
348
+ */
349
+ flushAll(): IPersistorMulti;
350
+ /**
351
+ * Queues an `exists` operation in the transaction.
352
+ * @param key - The storage key or an array of keys.
353
+ * @returns The `IPersistorMulti` instance for method chaining.
354
+ */
355
+ exists(key: string | string[]): IPersistorMulti;
356
+ /**
357
+ * Queues an `incr` operation in the transaction.
358
+ * @param key - The storage key.
359
+ * @returns The `IPersistorMulti` instance for method chaining.
360
+ */
361
+ incr(key: string): IPersistorMulti;
362
+ /**
363
+ * Queues an `incrBy` operation in the transaction.
364
+ * @param key - The storage key.
365
+ * @param increment - The amount to increment by.
366
+ * @returns The `IPersistorMulti` instance for method chaining.
367
+ */
368
+ incrBy(key: string, increment: number): IPersistorMulti;
369
+ /**
370
+ * Queues a `decr` operation in the transaction.
371
+ * @param key - The storage key.
372
+ * @returns The `IPersistorMulti` instance for method chaining.
373
+ */
374
+ decr(key: string): IPersistorMulti;
375
+ /**
376
+ * Queues a `decrBy` operation in the transaction.
377
+ * @param key - The storage key.
378
+ * @param decrement - The amount to decrement by.
379
+ * @returns The `IPersistorMulti` instance for method chaining.
380
+ */
381
+ decrBy(key: string, decrement: number): IPersistorMulti;
382
+ /**
383
+ * Sets a field in a hash.
384
+ * @param key - The hash key.
385
+ * @param field - The field name.
386
+ * @param value - The value to store.
387
+ * @returns The multi-instance for chaining.
388
+ */
389
+ hSet(key: string, field: string, value: HashTypes): IPersistorMulti;
390
+ /**
391
+ * Sets a (partial) hash.
392
+ * @param key - The hash key.
393
+ * @param value - The (partial) value to store.
394
+ * @returns Resolves to `1` if a field was added, or `0` if updated.
395
+ */
396
+ hSet(key: string, value: HashValue): IPersistorMulti;
397
+ /**
398
+ * Retrieves a field from a hash.
399
+ * @param key - The hash key.
400
+ * @param field - The field name.
401
+ * @returns Resolves to the value, or null if the field does not exist.
402
+ */
403
+ hGet(key: string, field: string): IPersistorMulti;
404
+ /**
405
+ * Retrieves a hash value.
406
+ * @param key - The hash key.
407
+ * @returns Resolves to the value, or null if the hash does not exist.
408
+ */
409
+ hGetAll(key: string): IPersistorMulti;
410
+ /**
411
+ * Pushes values to the left (head) of a list.
412
+ * @param key - The list key.
413
+ * @param values - The values to add.
414
+ * @returns The multi-instance for chaining.
415
+ */
416
+ lPush(key: string, values: string | string[]): IPersistorMulti;
417
+ /**
418
+ * Pushes values to the right (tail) of a list.
419
+ * @param key - The list key.
420
+ * @param values - The values to add.
421
+ * @returns The multi-instance for chaining.
422
+ */
423
+ rPush(key: string, values: string | string[]): IPersistorMulti;
424
+ /**
425
+ * Removes and returns the first element from a list.
426
+ * @param key - The list key.
427
+ * @returns The multi-instance for chaining.
428
+ */
429
+ lPop(key: string): IPersistorMulti;
430
+ /**
431
+ * Removes and returns the last element from a list.
432
+ * @param key - The list key.
433
+ * @returns The multi-instance for chaining.
434
+ */
435
+ rPop(key: string): IPersistorMulti;
436
+ /**
437
+ * Retrieves a range of elements from a list.
438
+ * @param key - The list key.
439
+ * @param start - The start index.
440
+ * @param stop - The stop index (inclusive).
441
+ * @returns The multi-instance for chaining.
442
+ */
443
+ lRange(key: string, start: number, stop: number): IPersistorMulti;
444
+ /**
445
+ * Adds members to a set.
446
+ * @param key - The set key.
447
+ * @param values - The values to add.
448
+ * @returns The multi-instance for chaining.
449
+ */
450
+ sAdd(key: string, values: string | string[]): IPersistorMulti;
451
+ /**
452
+ * Removes members from a set.
453
+ * @param key - The set key.
454
+ * @param values - The values to remove.
455
+ * @returns The multi-instance for chaining.
456
+ */
457
+ sRem(key: string, values: string | string[]): IPersistorMulti;
458
+ /**
459
+ * Retrieves all members of a set.
460
+ * @param key - The set key.
461
+ * @returns The multi-instance for chaining.
462
+ */
463
+ sMembers(key: string): IPersistorMulti;
464
+ /**
465
+ * Adds members to a sorted set with scores.
466
+ * @param key - The sorted set key.
467
+ * @param members - An array of objects with `score` and `value`.
468
+ * @returns The multi-instance for chaining.
469
+ */
470
+ zAdd(key: string, members: {
471
+ score: number;
472
+ value: string;
473
+ }[]): IPersistorMulti;
474
+ /**
475
+ * Retrieves a range of members from a sorted set.
476
+ * @param key - The sorted set key.
477
+ * @param start - The start index.
478
+ * @param stop - The stop index (inclusive).
479
+ * @returns The multi-instance for chaining.
480
+ */
481
+ zRange(key: string, start: number, stop: number): IPersistorMulti;
482
+ /**
483
+ * Removes members from a sorted set.
484
+ * @param key - The sorted set key.
485
+ * @param members - The members to remove.
486
+ * @returns The multi-instance for chaining.
487
+ */
488
+ zRem(key: string, members: string | string[]): IPersistorMulti;
489
+ /**
490
+ * Executes all queued commands and returns their results.
491
+ * @returns A promise resolving to an array of results for each command.
492
+ * The result type can be `string | number | boolean | null`, depending on the command.
493
+ */
494
+ exec(): Promise<MultiExecReturnTypes[] | unknown>;
495
+ }
496
+ //#endregion
497
+ //#region src/cache.d.ts
498
+ /**
499
+ * Creates a cache instance using a given persistor.
500
+ * @param {IPersistor} persistor - The underlying storage for caching.
501
+ * @param {string?} prefix - An optional prefix that will be prepended to the key, formatted as `prefix:key`.
502
+ * @returns {Cache} A cache instance.
503
+ */
504
+ declare const createCache: (persistor: IPersistor, prefix?: string) => Cache;
505
+ //#endregion
506
+ //#region src/inMemoryPersistor.d.ts
507
+ /**
508
+ * An in-memory key-value store with Redis-like behavior.
509
+ * Supports basic operations like `set`, `get`, `del`, `expire`, `ttl`, and `flushAll`.
510
+ * Implements expiration using `setTimeout` for automatic key deletion.
511
+ */
512
+ declare class InMemoryPersistor implements IPersistor {
513
+ /**
514
+ * Internal key-value store for caching string values.
515
+ * @private
516
+ */
517
+ private readonly store;
518
+ /**
519
+ * Tracks active timeouts for expiring keys.
520
+ * Each key maps to a `setTimeout` reference that deletes the key when triggered.
521
+ * @private
522
+ */
523
+ private readonly expirations;
524
+ /**
525
+ * Stores absolute expiration timestamps (in milliseconds since epoch) for each key.
526
+ * Used to compute remaining TTL.
527
+ * @private
528
+ */
529
+ private readonly expiryTimestamps;
530
+ /**
531
+ * Creates a new instance of `InMemoryPersistor`.
532
+ * Initializes an empty store, expiration map, and TTL tracker.
533
+ */
534
+ constructor();
535
+ connect(): Promise<this>;
536
+ get isReady(): boolean;
537
+ get isOpen(): boolean;
538
+ once(): this;
539
+ /**
540
+ * Stores a key-value pair with optional expiration settings.
541
+ * If an expiration is provided (`EX`, `PX`, `EXAT`, `PXAT`), the key is automatically removed when TTL expires.
542
+ *
543
+ * @param {string} key - The key to store.
544
+ * @param {string} value - The string value to associate with the key.
545
+ * @param {SetOptions} [options] - Optional Redis-style expiration settings.
546
+ * @returns {Promise<'OK' | null>} Resolves to `'OK'` on success, or `null` if a conditional set (`NX`) fails.
547
+ */
548
+ set(key: string, value: HashTypes, options?: SetOptions): Promise<'OK' | null>;
549
+ /**
550
+ * Stores a key-value pair with an expiration time in seconds.
551
+ * If the key already exists, it will be overwritten.
552
+ *
553
+ * @param key - The storage key.
554
+ * @param seconds - Expiration time in seconds.
555
+ * @param value - The string value to store.
556
+ * @returns Resolves to `'OK'` on success.
557
+ */
558
+ setEx(key: string, seconds: number, value: string): Promise<string | null>;
559
+ /**
560
+ * Stores a key-value pair with an expiration time in milliseconds.
561
+ * If the key already exists, it will be overwritten.
562
+ *
563
+ * @param key - The storage key.
564
+ * @param milliseconds - Expiration time in milliseconds.
565
+ * @param value - The string value to store.
566
+ * @returns Resolves to `'OK'` on success.
567
+ */
568
+ pSetEx(key: string, milliseconds: number, value: string): Promise<string | null>;
569
+ /**
570
+ * Stores a key-value pair **only if the key does not already exist**.
571
+ * If the key exists, the operation fails and returns `false`.
572
+ *
573
+ * @param key - The storage key.
574
+ * @param value - The string value to store.
575
+ * @returns Resolves to `true` if the key was set, or `false` if the key already exists.
576
+ */
577
+ setNX(key: string, value: string): Promise<number>;
578
+ /**
579
+ * Retrieves the value associated with a key.
580
+ *
581
+ * @param {string} key - The key to retrieve.
582
+ * @returns {Promise<string | null>} Resolves to the string value, or `null` if the key does not exist.
583
+ */
584
+ get(key: string): Promise<string | null>;
585
+ /**
586
+ * Deletes a key from the store.
587
+ * If the key exists, it is removed along with any associated expiration.
588
+ *
589
+ * @param {string} key - The key to delete.
590
+ * @returns {Promise<number>} Resolves to `1` if the key was deleted, or `0` if the key did not exist.
591
+ */
592
+ del(key: string): Promise<number>;
593
+ /**
594
+ * Sets a time-to-live (TTL) in seconds for a key.
595
+ * If the key exists, it will be deleted after the specified duration.
596
+ *
597
+ * @param {string} key - The key to set an expiration on.
598
+ * @param {number} seconds - The TTL in seconds.
599
+ * @returns {Promise<number>} Resolves to `1` if the TTL was set, or `0` if the key does not exist.
600
+ */
601
+ expire(key: string, seconds: number): Promise<number>;
602
+ /**
603
+ * Retrieves the remaining time-to-live (TTL) of a key in seconds.
604
+ *
605
+ * @param {string} key - The key to check.
606
+ * @returns {Promise<number>} Resolves to:
607
+ * - Remaining TTL in **seconds** if the key exists and has an expiration.
608
+ * - `-1` if the key exists but has no expiration.
609
+ * - `-2` if the key does not exist.
610
+ */
611
+ ttl(key: string): Promise<number>;
612
+ /**
613
+ * Checks if one or more keys exist in the store.
614
+ *
615
+ * @param {string | string[]} keys - A single key or an array of keys to check.
616
+ * @returns {Promise<number>} Resolves to the number of keys that exist.
617
+ */
618
+ exists(keys: string | string[]): Promise<number>;
619
+ /**
620
+ * Increments a numeric value stored at a key by 1.
621
+ * If the key does not exist, it is set to `1`.
622
+ *
623
+ * @param {string} key - The key to increment.
624
+ * @returns {Promise<number>} Resolves to the new value after increment.
625
+ */
626
+ incr(key: string): Promise<number>;
627
+ /**
628
+ * Increments a numeric value stored at a key by a specified amount.
629
+ * If the key does not exist, it is set to the increment value.
630
+ *
631
+ * @param {string} key - The key to increment.
632
+ * @param {number} increment - The amount to increase by.
633
+ * @returns {Promise<number>} Resolves to the new value after increment.
634
+ */
635
+ incrBy(key: string, increment: number): Promise<number>;
636
+ /**
637
+ * Decrements a numeric value stored at a key by 1.
638
+ * If the key does not exist, it is set to `-1`.
639
+ *
640
+ * @param {string} key - The key to decrement.
641
+ * @returns {Promise<number>} Resolves to the new value after decrement.
642
+ */
643
+ decr(key: string): Promise<number>;
644
+ /**
645
+ * Decrements a numeric value stored at a key by a specified amount.
646
+ * If the key does not exist, it is set to the negative decrement value.
647
+ *
648
+ * @param {string} key - The key to decrement.
649
+ * @param {number} decrement - The amount to decrease by.
650
+ * @returns {Promise<number>} Resolves to the new value after decrement.
651
+ */
652
+ decrBy(key: string, decrement: number): Promise<number>;
653
+ /**
654
+ * Sets a field in a hash.
655
+ * If the field already exists, its value is updated.
656
+ *
657
+ * @param key - The hash key.
658
+ * @param field - The field name.
659
+ * @param value - The value to store.
660
+ * @returns Resolves to `1` if a new field was added, `0` if an existing field was updated.
661
+ */
662
+ hSet(key: string, fieldOrValue: string | HashValue, value?: HashTypes): Promise<number>;
663
+ /**
664
+ * Retrieves a field from a hash.
665
+ *
666
+ * @param key - The hash key.
667
+ * @param field - The field name to retrieve.
668
+ * @returns Resolves to the field value, or `null` if the field does not exist.
669
+ */
670
+ hGet(key: string, field: string): Promise<string | null>;
671
+ /**
672
+ * Retrieves a hash value.
673
+ * @param key - The hash key.
674
+ * @returns Resolves to the value, or null if the hash does not exist.
675
+ */
676
+ hGetAll(key: string): Promise<{
677
+ [x: string]: string;
678
+ }>;
679
+ /**
680
+ * Pushes elements to the left (head) of a list.
681
+ *
682
+ * @param key - The list key.
683
+ * @param values - One or more values to add.
684
+ * @returns Resolves to the length of the list after the operation.
685
+ */
686
+ lPush(key: string, values: string | string[]): Promise<number>;
687
+ /**
688
+ * Pushes elements to the right (tail) of a list.
689
+ *
690
+ * @param key - The list key.
691
+ * @param values - One or more values to add.
692
+ * @returns Resolves to the length of the list after the operation.
693
+ */
694
+ rPush(key: string, values: string | string[]): Promise<number>;
695
+ /**
696
+ * Removes and returns the first element from a list.
697
+ *
698
+ * @param key - The list key.
699
+ * @returns Resolves to the removed element, or `null` if the list is empty.
700
+ */
701
+ lPop(key: string): Promise<string | null>;
702
+ /**
703
+ * Removes and returns the last element from a list.
704
+ *
705
+ * @param key - The list key.
706
+ * @returns Resolves to the removed element, or `null` if the list is empty.
707
+ */
708
+ rPop(key: string): Promise<string | null>;
709
+ /**
710
+ * Retrieves a range of elements from a list.
711
+ *
712
+ * @param key - The list key.
713
+ * @param start - The starting index.
714
+ * @param stop - The stopping index.
715
+ * @returns Resolves to an array containing the requested range.
716
+ */
717
+ lRange(key: string, start: number, stop: number): Promise<string[]>;
718
+ /**
719
+ * Adds elements to a set.
720
+ *
721
+ * @param key - The set key.
722
+ * @param values - One or more values to add.
723
+ * @returns Resolves to the number of new elements added.
724
+ */
725
+ sAdd(key: string, values: string | string[]): Promise<number>;
726
+ /**
727
+ * Removes elements from a set.
728
+ *
729
+ * @param key - The set key.
730
+ * @param values - One or more values to remove.
731
+ * @returns Resolves to the number of elements removed.
732
+ */
733
+ sRem(key: string, values: string | string[]): Promise<number>;
734
+ /**
735
+ * Retrieves all elements from a set.
736
+ *
737
+ * @param key - The set key.
738
+ * @returns Resolves to an array of all set members.
739
+ */
740
+ sMembers(key: string): Promise<string[]>;
741
+ /**
742
+ * Adds members to a sorted set with scores.
743
+ *
744
+ * @param key - The sorted set key.
745
+ * @param members - An array of objects containing `{ score, value }`.
746
+ * @returns Resolves to the number of new elements added.
747
+ */
748
+ zAdd(key: string, members: {
749
+ score: number;
750
+ value: string;
751
+ }[]): Promise<number>;
752
+ /**
753
+ * Retrieves a range of elements from a sorted set.
754
+ *
755
+ * @param key - The sorted set key.
756
+ * @param start - The starting index.
757
+ * @param stop - The stopping index.
758
+ * @returns Resolves to an array of sorted set values in the range.
759
+ */
760
+ zRange(key: string, start: number, stop: number): Promise<string[]>;
761
+ /**
762
+ * Removes elements from a sorted set.
763
+ *
764
+ * @param key - The sorted set key.
765
+ * @param members - One or more values to remove.
766
+ * @returns Resolves to the number of elements removed.
767
+ */
768
+ zRem(key: string, members: string | string[]): Promise<number>;
769
+ /**
770
+ * Removes all keys from the store and clears all active expirations.
771
+ *
772
+ * @returns {Promise<'OK'>} Resolves to `'OK'` after all data is cleared.
773
+ */
774
+ flushAll(): Promise<'OK'>;
775
+ /**
776
+ * Creates a new multi-command batch instance.
777
+ * Commands queued in this batch will be executed together when `exec()` is called.
778
+ *
779
+ * @returns A new `IPersistorMulti` instance for batching commands.
780
+ */
781
+ multi(): IPersistorMulti;
782
+ /**
783
+ * Sets an expiration timeout for a key.
784
+ * Cancels any existing expiration before setting a new one.
785
+ *
786
+ * @private
787
+ * @param {string} key - The key to expire.
788
+ * @param {number} ttlMs - Time-to-live in milliseconds.
789
+ */
790
+ private setExpiration;
791
+ /**
792
+ * Cancels an active expiration timeout for a key and removes its TTL record.
793
+ *
794
+ * @private
795
+ * @param {string} key - The key whose expiration should be cleared.
796
+ */
797
+ private clearExpiration;
798
+ }
799
+ //#endregion
800
+ //#region src/persistor.d.ts
801
+ type GetType<T> = {
802
+ value: T;
803
+ ttl?: number;
804
+ timestamp: number;
805
+ };
806
+ type SetParams<T> = {
807
+ value: T;
808
+ timestamp?: number;
809
+ ttl?: number;
810
+ };
811
+ type PersistorConstructorType = {
812
+ redis?: RedisClientOptions$1;
813
+ redisClient?: ReturnType<typeof createClient>;
814
+ clientId?: UUID;
815
+ onError?: (error: string) => void;
816
+ onSuccess?: () => void;
817
+ };
818
+ declare class Persistor {
819
+ client: ReturnType<typeof createClient> | null;
820
+ private readonly clientId?;
821
+ private readonly onError;
822
+ private readonly onSuccess;
823
+ private readonly logger;
824
+ private readonly redis?;
825
+ constructor({
826
+ redis,
827
+ redisClient,
828
+ clientId,
829
+ onSuccess,
830
+ onError
831
+ }: PersistorConstructorType);
832
+ startConnection(): Promise<void>;
833
+ size(): Promise<number>;
834
+ getClientId(): UUID | undefined;
835
+ getIsClientConnected(): boolean;
836
+ private createOptions;
837
+ /**
838
+ * Set a value in the cache.
839
+ * @param key Cache key.
840
+ * @param object.value Value to set in the cache.
841
+ * @param object.ttl Time to live in seconds.
842
+ * @param object.timestamp Timestamp
843
+ */
844
+ set<T>(key: string, {
845
+ value,
846
+ timestamp,
847
+ ttl
848
+ }: SetParams<T>): Promise<void>;
849
+ /**
850
+ * Get a value from the cache.
851
+ * @param key Cache key.
852
+ * @returns GetType<T> value
853
+ */
854
+ get<T>(key: string): Promise<GetType<T> | null>;
855
+ /**
856
+ * Delete a value from the cache.
857
+ * @param key Cache key
858
+ */
859
+ delete(key: string): Promise<void>;
860
+ }
861
+ //#endregion
862
+ //#region src/promiseCache.d.ts
863
+ type PromiseCacheOptions = {
864
+ ttlInSeconds?: number;
865
+ caseSensitive?: boolean;
866
+ redis?: RedisClientOptions;
867
+ fallbackToFunction?: boolean;
868
+ onError?: (error: string) => void;
869
+ onSuccess?: () => void;
870
+ };
871
+ declare class PromiseCache<U> {
872
+ persistor: Persistor;
873
+ private readonly clientId;
874
+ private readonly caseSensitive;
875
+ private readonly fallbackToFunction;
876
+ private readonly ttl?;
877
+ private readonly logger;
878
+ /**
879
+ * Initialize a new PromiseCache.
880
+ * @param ttlInSeconds Default cache TTL.
881
+ * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
882
+ */
883
+ constructor({
884
+ ttlInSeconds,
885
+ caseSensitive,
886
+ redis,
887
+ fallbackToFunction,
888
+ onSuccess,
889
+ onError
890
+ }: PromiseCacheOptions);
891
+ /**
892
+ * Cache size.
893
+ * @returns The number of entries in the cache.
894
+ */
895
+ size(): Promise<number>;
896
+ /**
897
+ * Override a value in the cache.
898
+ * @param key Cache key.
899
+ * @param value Cache value.
900
+ * @param ttlInSeconds Time to live in seconds.
901
+ */
902
+ override<U>(key: string, value: U, ttlInSeconds?: number): Promise<void>;
903
+ /**
904
+ * Get a value from the cache.
905
+ * @param key Cache key.
906
+ */
907
+ find<U>(key: string): Promise<U | null>;
908
+ /**
909
+ * A simple promise cache wrapper.
910
+ * @param key Cache key.
911
+ * @param delegate The function to execute if the key is not in the cache.
912
+ * @param ttlInSeconds Time to live in seconds.
913
+ * @param ttlKeyInSeconds The key in the response object that contains the TTL.
914
+ * @returns The result of the delegate function.
915
+ */
916
+ wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKeyInSeconds?: string): Promise<U>;
917
+ }
918
+ declare namespace serializer_d_exports {
919
+ export { deserialize, serialize };
920
+ }
921
+ declare const serialize: <T>(data: T) => string;
922
+ declare const deserialize: <T>(serialized: string | null) => T | null;
923
+ declare namespace time_d_exports {
924
+ export { DAY, HOUR, MINUTE, SECOND, WEEK, add, days, hours, minutes, seconds, sub, today, tomorrow, weeks };
925
+ }
926
+ declare const SECOND = 1000;
927
+ declare const MINUTE: number;
928
+ declare const HOUR: number;
929
+ declare const DAY: number;
930
+ declare const WEEK: number;
931
+ declare const seconds: (num: number) => number;
932
+ declare const minutes: (num: number) => number;
933
+ declare const hours: (num: number) => number;
934
+ declare const days: (num: number) => number;
935
+ declare const weeks: (num: number) => number;
936
+ declare const today: (hours?: number, minutes?: number, seconds?: number) => Date;
937
+ declare const tomorrow: (hours?: number, minutes?: number, seconds?: number) => Date;
938
+ //#endregion
939
+ export { type Cache, type CachingOptions, type IPersistor, InMemoryPersistor, Persistor, PersistorConstructorType, PromiseCache, PromiseCacheOptions, type RedisClientOptions, createCache, serializer_d_exports as serializer, time_d_exports as time };
940
+ //# sourceMappingURL=index.d.mts.map