ioredis-toolkit 0.0.4 → 0.0.6
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/README.md +772 -316
- package/dist/cache.d.ts +502 -6
- package/dist/cache.js +501 -5
- package/dist/client.d.ts +107 -2
- package/dist/client.js +107 -3
- package/dist/health.d.ts +141 -0
- package/dist/health.js +133 -0
- package/dist/lock.d.ts +55 -1
- package/dist/lock.js +12 -22
- package/dist/pubsub.d.ts +259 -7
- package/dist/pubsub.js +259 -7
- package/dist/ratelimiter.d.ts +279 -1
- package/dist/ratelimiter.js +250 -0
- package/dist/session/session-manager.d.ts +14 -0
- package/dist/types.js +0 -2
- package/package.json +1 -1
package/dist/cache.d.ts
CHANGED
|
@@ -23,12 +23,21 @@ export declare class Cache {
|
|
|
23
23
|
/**
|
|
24
24
|
* Creates a cache bound to a Redis client.
|
|
25
25
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
26
|
+
* **Parameters:**
|
|
27
|
+
* - `client` - The underlying {@link RedisClientWrapper}. All cache operations
|
|
28
|
+
* delegate to this client.
|
|
29
|
+
* - `config` - Configuration object with the following fields:
|
|
30
|
+
* - `defaultTTL` (number, optional, default: `3600`) - Default TTL in seconds
|
|
31
|
+
* applied when no per-call TTL is specified.
|
|
32
|
+
* - `compressionThreshold` (number, optional, default: `1024`) - Byte threshold
|
|
33
|
+
* above which values are gzip-compressed transparently.
|
|
34
|
+
* - `logger` - Optional pino-compatible logger. Supports `trace/debug/info/warn/error/fatal`
|
|
35
|
+
* levels and `child()` for namespace logging. Defaults to `console`.
|
|
36
|
+
*
|
|
37
|
+
* **Type Parameters:**
|
|
38
|
+
* - `T` - The type of values stored/retrieved from the cache.
|
|
39
|
+
*
|
|
40
|
+
* **Example:**
|
|
32
41
|
* ```ts
|
|
33
42
|
* const cache = new Cache(client, { defaultTTL: 600, compressionThreshold: 2048 });
|
|
34
43
|
* ```
|
|
@@ -53,6 +62,44 @@ export declare class Cache {
|
|
|
53
62
|
* const token = await cache.get('token', 'auth');
|
|
54
63
|
* ```
|
|
55
64
|
*/
|
|
65
|
+
/**
|
|
66
|
+
* Reads a cached value.
|
|
67
|
+
*
|
|
68
|
+
* **Behavior:**
|
|
69
|
+
* - Objects are parsed from JSON when stored as JSON.
|
|
70
|
+
* - Values larger than `compressionThreshold` bytes are transparently
|
|
71
|
+
* decompressed (gzip) when reading.
|
|
72
|
+
* - Strings that are not JSON are returned as-is.
|
|
73
|
+
* - If the key does not exist, returns `null`.
|
|
74
|
+
*
|
|
75
|
+
* **Type Parameters:**
|
|
76
|
+
* - `T` - The expected return type. When the stored value is a JSON object/array,
|
|
77
|
+
* it will be parsed and returned as `T`. When it's a primitive (string/number/bool),
|
|
78
|
+
* it is returned as-is and typed as `T`.
|
|
79
|
+
*
|
|
80
|
+
* **Returns:**
|
|
81
|
+
* - The stored value, parsed as `T` when possible, or `null` when the key is missing.
|
|
82
|
+
*
|
|
83
|
+
* **Example:**
|
|
84
|
+
* ```ts
|
|
85
|
+
* // Store an object
|
|
86
|
+
* await cache.set('user:1', { name: 'alice', age: 30 });
|
|
87
|
+
*
|
|
88
|
+
* // Read it back with type coercion
|
|
89
|
+
* const user: { name: string; age: number } | null = await cache.get<UserProfile>('user:1');
|
|
90
|
+
* // user === { name: 'alice', age: 30 }
|
|
91
|
+
*
|
|
92
|
+
* // Read a string value
|
|
93
|
+
* const token = await cache.get('token'); // 'abc' | null
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* **Parameters:**
|
|
97
|
+
* - `key` - Cache key.
|
|
98
|
+
* - `namespace` - Optional namespace prefix (`namespace:key`). When provided,
|
|
99
|
+
* the key is internally transformed to `${namespace}:${key}`.
|
|
100
|
+
*
|
|
101
|
+
* @returns The stored value, or `null` when missing.
|
|
102
|
+
*/
|
|
56
103
|
get<T = any>(key: string, namespace?: string): Promise<T | null>;
|
|
57
104
|
/**
|
|
58
105
|
* Stores a value in the cache.
|
|
@@ -70,6 +117,47 @@ export declare class Cache {
|
|
|
70
117
|
* await cache.set('token', 'abc', { namespace: 'auth', compress: false });
|
|
71
118
|
* ```
|
|
72
119
|
*/
|
|
120
|
+
/**
|
|
121
|
+
* Stores a value in the cache.
|
|
122
|
+
*
|
|
123
|
+
* **Behavior:**
|
|
124
|
+
* - Values are JSON-serialized when they are objects, arrays, or booleans/strings/numbers
|
|
125
|
+
* are stored as-is.
|
|
126
|
+
* - Values larger than `compressionThreshold` bytes are gzip-compressed transparently.
|
|
127
|
+
* The compressed form is stored with metadata (`_compressed: true`, `_data: base64`) so
|
|
128
|
+
* it is transparently decompressed on read.
|
|
129
|
+
* - Set `compress: false` to disable compression for a single write, regardless of size.
|
|
130
|
+
* - Per-call TTL overrides the cache's `defaultTTL`.
|
|
131
|
+
* - Per-call `namespace` overrides the cache's configured namespace for that operation.
|
|
132
|
+
*
|
|
133
|
+
* **Type Parameters:**
|
|
134
|
+
* - `T` - The type of the value being stored. Can be any serializable JavaScript value.
|
|
135
|
+
*
|
|
136
|
+
* **Returns:**
|
|
137
|
+
* - `true` when the value was stored successfully (`result === 'OK'`).
|
|
138
|
+
*
|
|
139
|
+
* **Example:**
|
|
140
|
+
* ```ts
|
|
141
|
+
* // Store an object with a custom TTL
|
|
142
|
+
* await cache.set('user:1', { name: 'alice' }, { ttl: 300 });
|
|
143
|
+
*
|
|
144
|
+
* // Store with compression disabled
|
|
145
|
+
* await cache.set('token', 'abc123', { compress: false });
|
|
146
|
+
*
|
|
147
|
+
* // Store with a namespace
|
|
148
|
+
* await cache.set('token', 'abc', { namespace: 'auth' });
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* **Parameters:**
|
|
152
|
+
* - `key` - Cache key.
|
|
153
|
+
* - `value` - Any serializable value (string, number, boolean, Buffer, or object).
|
|
154
|
+
* - `options` - Optional configuration:
|
|
155
|
+
* - `ttl` (number, optional) - TTL in seconds. Falls back to `defaultTTL`.
|
|
156
|
+
* - `namespace` (string, optional) - Namespace prefix. Falls back to cache config.
|
|
157
|
+
* - `compress` (boolean, optional) - Force compression or disable it. Defaults to `true`.
|
|
158
|
+
*
|
|
159
|
+
* @returns `true` when stored successfully.
|
|
160
|
+
*/
|
|
73
161
|
set<T>(key: string, value: T, options?: CacheOptions): Promise<boolean>;
|
|
74
162
|
/**
|
|
75
163
|
* Stores a value only if the key does not exist yet (`SETNX`).
|
|
@@ -84,6 +172,36 @@ export declare class Cache {
|
|
|
84
172
|
* const claimed = await cache.setNX('job:1', 'worker-1', { ttl: 60 });
|
|
85
173
|
* ```
|
|
86
174
|
*/
|
|
175
|
+
/**
|
|
176
|
+
* Stores a value only if the key does not exist yet (`SETNX`).
|
|
177
|
+
*
|
|
178
|
+
* **Behavior:**
|
|
179
|
+
* - The value is stored atomically using Redis `SET key value EX ttl NX`.
|
|
180
|
+
* - Returns `true` only when the key did not exist and the value was set.
|
|
181
|
+
* - Per-call TTL overrides the cache's `defaultTTL`.
|
|
182
|
+
* - Per-call `namespace` is applied to the key.
|
|
183
|
+
*
|
|
184
|
+
* **Type Parameters:**
|
|
185
|
+
* - `T` - The type of the value being stored. Will be JSON-stringified if not a string.
|
|
186
|
+
*
|
|
187
|
+
* **Returns:**
|
|
188
|
+
* - `true` only when the value was actually stored (Redis SETNX returned `1`).
|
|
189
|
+
*
|
|
190
|
+
* **Example:**
|
|
191
|
+
* ```ts
|
|
192
|
+
* const claimed = await cache.setNX('job:1', 'worker-1', { ttl: 60 });
|
|
193
|
+
* // claimed === true (job was claimed by this worker)
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* **Parameters:**
|
|
197
|
+
* - `key` - Cache key.
|
|
198
|
+
* - `value` - The value to store. String stored as-is; objects are JSON-stringified.
|
|
199
|
+
* - `options` - Optional configuration:
|
|
200
|
+
* - `ttl` (number, optional) - TTL in seconds. Falls back to `defaultTTL`.
|
|
201
|
+
* - `namespace` (string, optional) - Namespace prefix.
|
|
202
|
+
*
|
|
203
|
+
* @returns `true` only when the value was actually stored.
|
|
204
|
+
*/
|
|
87
205
|
setNX<T>(key: string, value: T, options?: CacheOptions): Promise<boolean>;
|
|
88
206
|
/**
|
|
89
207
|
* Stores a value only if the key does not exist yet, atomically with the TTL
|
|
@@ -99,6 +217,39 @@ export declare class Cache {
|
|
|
99
217
|
* const locked = await cache.setEXNX('lock:order:42', 'txn-id', { ttl: 30 });
|
|
100
218
|
* ```
|
|
101
219
|
*/
|
|
220
|
+
/**
|
|
221
|
+
* Stores a value only if the key does not exist yet, atomically with the TTL
|
|
222
|
+
* (`SET ... EX NX`).
|
|
223
|
+
*
|
|
224
|
+
* **Behavior:**
|
|
225
|
+
* - The value is stored atomically using Redis `SET key value EX ttl NX`.
|
|
226
|
+
* - This is the atomic equivalent of calling `SET key value EX ttl` followed by
|
|
227
|
+
* `SET key value NX` - but done in a single Redis call.
|
|
228
|
+
* - Returns `true` only when the key did not exist and the value was set with TTL.
|
|
229
|
+
* - Per-call TTL overrides the cache's `defaultTTL`.
|
|
230
|
+
* - Per-call `namespace` is applied to the key.
|
|
231
|
+
*
|
|
232
|
+
* **Type Parameters:**
|
|
233
|
+
* - `T` - The type of the value being stored. Will be JSON-stringified if not a string.
|
|
234
|
+
*
|
|
235
|
+
* **Returns:**
|
|
236
|
+
* - `true` only when the value was actually stored (Redis SET returned `OK`).
|
|
237
|
+
*
|
|
238
|
+
* **Example:**
|
|
239
|
+
* ```ts
|
|
240
|
+
* const locked = await cache.setEXNX('lock:order:42', 'txn-id', { ttl: 30 });
|
|
241
|
+
* // locked === true (lock was acquired with 30s TTL)
|
|
242
|
+
* ```
|
|
243
|
+
*
|
|
244
|
+
* **Parameters:**
|
|
245
|
+
* - `key` - Cache key.
|
|
246
|
+
* - `value` - The value to store. String stored as-is; objects are JSON-stringified.
|
|
247
|
+
* - `options` - Optional configuration:
|
|
248
|
+
* - `ttl` (number, optional) - TTL in seconds. Falls back to `defaultTTL`.
|
|
249
|
+
* - `namespace` (string, optional) - Namespace prefix.
|
|
250
|
+
*
|
|
251
|
+
* @returns `true` only when the value was actually stored.
|
|
252
|
+
*/
|
|
102
253
|
setEXNX<T>(key: string, value: T, options?: CacheOptions): Promise<boolean>;
|
|
103
254
|
/**
|
|
104
255
|
* Reads multiple cache keys in one call.
|
|
@@ -114,6 +265,37 @@ export declare class Cache {
|
|
|
114
265
|
* const [a, b] = await cache.mget(['user:1', 'user:2']);
|
|
115
266
|
* ```
|
|
116
267
|
*/
|
|
268
|
+
/**
|
|
269
|
+
* Reads multiple cache keys in one call.
|
|
270
|
+
*
|
|
271
|
+
* **Behavior:**
|
|
272
|
+
* - Cluster-safe: keys are grouped by hash slot under the hood, avoiding CROSS-SLOT errors.
|
|
273
|
+
* - Values are deserialized from JSON when stored as JSON. Strings/numbers/buffers
|
|
274
|
+
* are returned as-is.
|
|
275
|
+
* - Missing keys return `null` in the corresponding position.
|
|
276
|
+
*
|
|
277
|
+
* **Type Parameters:**
|
|
278
|
+
* - `T` - The expected type of each returned value. When the stored value is JSON,
|
|
279
|
+
* it will be parsed and coerced to `T`.
|
|
280
|
+
*
|
|
281
|
+
* **Returns:**
|
|
282
|
+
* - An array of values in the same order as the input `keys`. Each element is `T | null`.
|
|
283
|
+
* `null` indicates the key did not exist.
|
|
284
|
+
*
|
|
285
|
+
* **Example:**
|
|
286
|
+
* ```ts
|
|
287
|
+
* const [a, b] = await cache.mget(['user:1', 'user:2']);
|
|
288
|
+
* // a === { name: 'alice' }, b === { name: 'bob' }
|
|
289
|
+
* ```
|
|
290
|
+
*
|
|
291
|
+
* **Parameters:**
|
|
292
|
+
* - `keys` - Cache keys to read. Will have the namespace prefix applied automatically
|
|
293
|
+
* if a namespace is configured.
|
|
294
|
+
* - `namespace` - Optional namespace prefix applied to every key. When provided,
|
|
295
|
+
* each key is internally transformed to `${namespace}:${key}`.
|
|
296
|
+
*
|
|
297
|
+
* @returns Values in input order; `null` for missing keys.
|
|
298
|
+
*/
|
|
117
299
|
mget<T = any>(keys: string[], namespace?: string): Promise<(T | null)[]>;
|
|
118
300
|
/**
|
|
119
301
|
* Stores multiple key/value entries in one call.
|
|
@@ -129,6 +311,39 @@ export declare class Cache {
|
|
|
129
311
|
* await cache.mset({ 'user:1': alice, 'user:2': bob }, { ttl: 300 });
|
|
130
312
|
* ```
|
|
131
313
|
*/
|
|
314
|
+
/**
|
|
315
|
+
* Stores multiple key/value entries in one call.
|
|
316
|
+
*
|
|
317
|
+
* **Behavior:**
|
|
318
|
+
* - Cluster-safe: entries are grouped by hash slot, one pipeline per slot.
|
|
319
|
+
* This avoids CROSS-SLOT errors that would occur if keys spanned multiple slots in a
|
|
320
|
+
* single pipeline.
|
|
321
|
+
* - Values are JSON-serialized when they are objects; strings/buffers are stored as-is.
|
|
322
|
+
* - Per-call TTL overrides the cache's `defaultTTL`.
|
|
323
|
+
* - Per-call `namespace` is applied to all keys.
|
|
324
|
+
*
|
|
325
|
+
* **Type Parameters:**
|
|
326
|
+
* - `T` - The type of values being stored. Objects are JSON-stringified; strings/buffers
|
|
327
|
+
* are stored as-is.
|
|
328
|
+
*
|
|
329
|
+
* **Returns:**
|
|
330
|
+
* - `true` when every entry was stored successfully.
|
|
331
|
+
* - `false` if any entry failed to store.
|
|
332
|
+
*
|
|
333
|
+
* **Example:**
|
|
334
|
+
* ```ts
|
|
335
|
+
* await cache.mset({ 'user:1': alice, 'user:2': bob }, { ttl: 300 });
|
|
336
|
+
* // Both entries stored with a 5-minute TTL
|
|
337
|
+
* ```
|
|
338
|
+
*
|
|
339
|
+
* **Parameters:**
|
|
340
|
+
* - `entries` - Object mapping cache keys to values.
|
|
341
|
+
* - `options` - Optional configuration:
|
|
342
|
+
* - `ttl` (number, optional) - TTL in seconds. Falls back to `defaultTTL`.
|
|
343
|
+
* - `namespace` (string, optional) - Namespace prefix. Applied to all keys.
|
|
344
|
+
*
|
|
345
|
+
* @returns `true` when every entry was stored.
|
|
346
|
+
*/
|
|
132
347
|
mset<T>(entries: Record<string, T>, options?: CacheOptions): Promise<boolean>;
|
|
133
348
|
/**
|
|
134
349
|
* Deletes a cache key.
|
|
@@ -142,6 +357,28 @@ export declare class Cache {
|
|
|
142
357
|
* const removed = await cache.delete('user:1');
|
|
143
358
|
* ```
|
|
144
359
|
*/
|
|
360
|
+
/**
|
|
361
|
+
* Deletes a cache key.
|
|
362
|
+
*
|
|
363
|
+
* **Behavior:**
|
|
364
|
+
* - Deletes the full key (including any namespace prefix).
|
|
365
|
+
* - Returns `true` only when the key existed and was deleted (Redis DEL returned `1`).
|
|
366
|
+
*
|
|
367
|
+
* **Returns:**
|
|
368
|
+
* - `true` if the key existed and was deleted.
|
|
369
|
+
*
|
|
370
|
+
* **Example:**
|
|
371
|
+
* ```ts
|
|
372
|
+
* const removed = await cache.delete('user:1');
|
|
373
|
+
* // removed === true
|
|
374
|
+
* ```
|
|
375
|
+
*
|
|
376
|
+
* **Parameters:**
|
|
377
|
+
* - `key` - Cache key.
|
|
378
|
+
* - `namespace` - Optional namespace prefix.
|
|
379
|
+
*
|
|
380
|
+
* @returns `true` if the key existed and was deleted.
|
|
381
|
+
*/
|
|
145
382
|
delete(key: string, namespace?: string): Promise<boolean>;
|
|
146
383
|
/**
|
|
147
384
|
* Checks whether a cache key exists.
|
|
@@ -155,6 +392,25 @@ export declare class Cache {
|
|
|
155
392
|
* const cached = await cache.exists('user:1');
|
|
156
393
|
* ```
|
|
157
394
|
*/
|
|
395
|
+
/**
|
|
396
|
+
* Checks whether a cache key exists.
|
|
397
|
+
*
|
|
398
|
+
* **Returns:**
|
|
399
|
+
* - `true` if the key exists in Redis.
|
|
400
|
+
* - `false` if the key does not exist.
|
|
401
|
+
*
|
|
402
|
+
* **Example:**
|
|
403
|
+
* ```ts
|
|
404
|
+
* const cached = await cache.exists('user:1');
|
|
405
|
+
* // cached === true when 'user:1' has been set
|
|
406
|
+
* ```
|
|
407
|
+
*
|
|
408
|
+
* **Parameters:**
|
|
409
|
+
* - `key` - Cache key.
|
|
410
|
+
* - `namespace` - Optional namespace prefix.
|
|
411
|
+
*
|
|
412
|
+
* @returns `true` if the key exists.
|
|
413
|
+
*/
|
|
158
414
|
exists(key: string, namespace?: string): Promise<boolean>;
|
|
159
415
|
/**
|
|
160
416
|
* Sets the TTL of an existing cache key.
|
|
@@ -169,6 +425,26 @@ export declare class Cache {
|
|
|
169
425
|
* const extended = await cache.expire('session:42', 3600);
|
|
170
426
|
* ```
|
|
171
427
|
*/
|
|
428
|
+
/**
|
|
429
|
+
* Sets the TTL of an existing cache key.
|
|
430
|
+
*
|
|
431
|
+
* **Returns:**
|
|
432
|
+
* - `true` if the TTL was applied (Redis EXPIRE returned `1`).
|
|
433
|
+
* - `false` if the key did not exist.
|
|
434
|
+
*
|
|
435
|
+
* **Example:**
|
|
436
|
+
* ```ts
|
|
437
|
+
* const extended = await cache.expire('session:42', 3600);
|
|
438
|
+
* // extended === true (TTL was set to 1 hour)
|
|
439
|
+
* ```
|
|
440
|
+
*
|
|
441
|
+
* **Parameters:**
|
|
442
|
+
* - `key` - Cache key.
|
|
443
|
+
* - `ttl` - TTL in seconds.
|
|
444
|
+
* - `namespace` - Optional namespace prefix.
|
|
445
|
+
*
|
|
446
|
+
* @returns `true` if the TTL was applied.
|
|
447
|
+
*/
|
|
172
448
|
expire(key: string, ttl: number, namespace?: string): Promise<boolean>;
|
|
173
449
|
/**
|
|
174
450
|
* Returns the remaining TTL of a cache key in seconds.
|
|
@@ -182,6 +458,26 @@ export declare class Cache {
|
|
|
182
458
|
* const secondsLeft = await cache.ttl('session:42');
|
|
183
459
|
* ```
|
|
184
460
|
*/
|
|
461
|
+
/**
|
|
462
|
+
* Returns the remaining TTL of a cache key in seconds.
|
|
463
|
+
*
|
|
464
|
+
* **Returns:**
|
|
465
|
+
* - The remaining TTL in seconds.
|
|
466
|
+
* - `-2` if the key does not exist.
|
|
467
|
+
* - `-1` if the key exists but has no TTL set.
|
|
468
|
+
*
|
|
469
|
+
* **Example:**
|
|
470
|
+
* ```ts
|
|
471
|
+
* const secondsLeft = await cache.ttl('session:42');
|
|
472
|
+
* // secondsLeft === 2500 (approximately 42 minutes remaining)
|
|
473
|
+
* ```
|
|
474
|
+
*
|
|
475
|
+
* **Parameters:**
|
|
476
|
+
* - `key` - Cache key.
|
|
477
|
+
* - `namespace` - Optional namespace prefix.
|
|
478
|
+
*
|
|
479
|
+
* @returns Remaining TTL in seconds (`-2` if missing, `-1` if no TTL).
|
|
480
|
+
*/
|
|
185
481
|
ttl(key: string, namespace?: string): Promise<number>;
|
|
186
482
|
/**
|
|
187
483
|
* Atomically increments a cache counter.
|
|
@@ -196,6 +492,35 @@ export declare class Cache {
|
|
|
196
492
|
* const visits = await cache.increment('stats:visits');
|
|
197
493
|
* ```
|
|
198
494
|
*/
|
|
495
|
+
/**
|
|
496
|
+
* Atomically increments a cache counter.
|
|
497
|
+
*
|
|
498
|
+
* **Behavior:**
|
|
499
|
+
* - Uses Redis `INCR` command on the full key (including namespace if set).
|
|
500
|
+
* - The counter starts at `0` if the key does not exist, then increments to `1`.
|
|
501
|
+
* - The `by` parameter is passed to Redis but note: Redis `INCR` always increments
|
|
502
|
+
* by `1`. The `by` parameter is kept for API parity with other cache implementations
|
|
503
|
+
* but has no effect on the actual Redis command result.
|
|
504
|
+
*
|
|
505
|
+
* **Returns:**
|
|
506
|
+
* - The new counter value (the value after incrementing).
|
|
507
|
+
*
|
|
508
|
+
* **Example:**
|
|
509
|
+
* ```ts
|
|
510
|
+
* const visits = await cache.increment('stats:visits');
|
|
511
|
+
* // visits === 1 (first increment)
|
|
512
|
+
* const more = await cache.increment('stats:visits', 5); // by parameter ignored
|
|
513
|
+
* // more === 2
|
|
514
|
+
* ```
|
|
515
|
+
*
|
|
516
|
+
* **Parameters:**
|
|
517
|
+
* - `key` - Counter key.
|
|
518
|
+
* - `by` - Amount to increment by (default `1`). Note: Redis `INCR` always increments
|
|
519
|
+
* by `1`; this parameter is kept for API parity.
|
|
520
|
+
* - `namespace` - Optional namespace prefix.
|
|
521
|
+
*
|
|
522
|
+
* @returns The new counter value.
|
|
523
|
+
*/
|
|
199
524
|
increment(key: string, by?: number, namespace?: string): Promise<number>;
|
|
200
525
|
/**
|
|
201
526
|
* Atomically decrements a cache counter.
|
|
@@ -210,6 +535,32 @@ export declare class Cache {
|
|
|
210
535
|
* const stock = await cache.decrement('inventory:sku-1');
|
|
211
536
|
* ```
|
|
212
537
|
*/
|
|
538
|
+
/**
|
|
539
|
+
* Atomically decrements a cache counter.
|
|
540
|
+
*
|
|
541
|
+
* **Behavior:**
|
|
542
|
+
* - Uses Redis `DECR` command on the full key (including namespace if set).
|
|
543
|
+
* - The `by` parameter is passed to Redis but note: Redis `DECR` always decrements
|
|
544
|
+
* by `1`. The `by` parameter is kept for API parity with other cache implementations
|
|
545
|
+
* but has no effect on the actual Redis command result.
|
|
546
|
+
*
|
|
547
|
+
* **Returns:**
|
|
548
|
+
* - The new counter value (the value after decrementing).
|
|
549
|
+
*
|
|
550
|
+
* **Example:**
|
|
551
|
+
* ```ts
|
|
552
|
+
* const stock = await cache.decrement('inventory:sku-1');
|
|
553
|
+
* // stock === 99 (started at 100, decremented by 1)
|
|
554
|
+
* ```
|
|
555
|
+
*
|
|
556
|
+
* **Parameters:**
|
|
557
|
+
* - `key` - Counter key.
|
|
558
|
+
* - `by` - Amount to decrement by (default `1`). Note: Redis `DECR` always decrements
|
|
559
|
+
* by `1`; this parameter is kept for API parity.
|
|
560
|
+
* - `namespace` - Optional namespace prefix.
|
|
561
|
+
*
|
|
562
|
+
* @returns The new counter value.
|
|
563
|
+
*/
|
|
213
564
|
decrement(key: string, by?: number, namespace?: string): Promise<number>;
|
|
214
565
|
/**
|
|
215
566
|
* Reads a field from a hash-style cache key.
|
|
@@ -224,6 +575,30 @@ export declare class Cache {
|
|
|
224
575
|
* const name = await cache.hget('user:1', 'name');
|
|
225
576
|
* ```
|
|
226
577
|
*/
|
|
578
|
+
/**
|
|
579
|
+
* Reads a field from a hash-style cache key.
|
|
580
|
+
*
|
|
581
|
+
* **Behavior:**
|
|
582
|
+
* - The field value is retrieved from the Redis hash.
|
|
583
|
+
* - When the stored value is a JSON string, it is parsed and returned as the typed result.
|
|
584
|
+
* - When the stored value is not JSON, it is returned as a raw string, typed as `T`.
|
|
585
|
+
*
|
|
586
|
+
* **Returns:**
|
|
587
|
+
* - The field value, parsed as `T` when possible, or `null` when the key or field does not exist.
|
|
588
|
+
*
|
|
589
|
+
* **Example:**
|
|
590
|
+
* ```ts
|
|
591
|
+
* const name = await cache.hget('user:1', 'name');
|
|
592
|
+
* // name === 'alice' | null
|
|
593
|
+
* ```
|
|
594
|
+
*
|
|
595
|
+
* **Parameters:**
|
|
596
|
+
* - `key` - Cache key (hash key in Redis).
|
|
597
|
+
* - `field` - Hash field to read.
|
|
598
|
+
* - `namespace` - Optional namespace prefix. Applied to the key.
|
|
599
|
+
*
|
|
600
|
+
* @returns The field value, or `null`.
|
|
601
|
+
*/
|
|
227
602
|
hget<T = any>(key: string, field: string, namespace?: string): Promise<T | null>;
|
|
228
603
|
/**
|
|
229
604
|
* Writes a field into a hash-style cache key.
|
|
@@ -239,6 +614,32 @@ export declare class Cache {
|
|
|
239
614
|
* await cache.hset('user:1', 'age', 30);
|
|
240
615
|
* ```
|
|
241
616
|
*/
|
|
617
|
+
/**
|
|
618
|
+
* Writes a field into a hash-style cache key.
|
|
619
|
+
*
|
|
620
|
+
* **Behavior:**
|
|
621
|
+
* - The value is JSON-stringified when it is not a string (objects, arrays, etc.).
|
|
622
|
+
* Strings are stored as-is.
|
|
623
|
+
* - Returns `true` only when a new field was created (Redis HSET returned `1`).
|
|
624
|
+
* If the field already exists, its value is overwritten and `true` is still returned.
|
|
625
|
+
*
|
|
626
|
+
* **Returns:**
|
|
627
|
+
* - `true` if a new field was created.
|
|
628
|
+
*
|
|
629
|
+
* **Example:**
|
|
630
|
+
* ```ts
|
|
631
|
+
* await cache.hset('user:1', 'age', 30);
|
|
632
|
+
* // Field 'age' set to '30' (stringified) in hash 'user:1'
|
|
633
|
+
* ```
|
|
634
|
+
*
|
|
635
|
+
* **Parameters:**
|
|
636
|
+
* - `key` - Cache key (hash key in Redis).
|
|
637
|
+
* - `field` - Hash field to write.
|
|
638
|
+
* - `value` - Any serializable value. Strings stored as-is; objects are JSON-stringified.
|
|
639
|
+
* - `namespace` - Optional namespace prefix. Applied to the key.
|
|
640
|
+
*
|
|
641
|
+
* @returns `true` if a new field was created.
|
|
642
|
+
*/
|
|
242
643
|
hset(key: string, field: string, value: any, namespace?: string): Promise<boolean>;
|
|
243
644
|
/**
|
|
244
645
|
* Returns every field of a hash-style cache key.
|
|
@@ -252,6 +653,29 @@ export declare class Cache {
|
|
|
252
653
|
* const profile = await cache.hgetall('user:1');
|
|
253
654
|
* ```
|
|
254
655
|
*/
|
|
656
|
+
/**
|
|
657
|
+
* Returns every field of a hash-style cache key.
|
|
658
|
+
*
|
|
659
|
+
* **Behavior:**
|
|
660
|
+
* - Retrieves all fields and values from the Redis hash.
|
|
661
|
+
* - Each value is parsed from JSON when possible. Non-JSON values are returned as raw strings.
|
|
662
|
+
* - Returns an empty object `{}` when the key does not exist or has no fields.
|
|
663
|
+
*
|
|
664
|
+
* **Returns:**
|
|
665
|
+
* - An object mapping field names to their values, with values parsed as `T` when possible.
|
|
666
|
+
*
|
|
667
|
+
* **Example:**
|
|
668
|
+
* ```ts
|
|
669
|
+
* const profile = await cache.hgetall('user:1');
|
|
670
|
+
* // profile === { age: 30, name: 'alice' }
|
|
671
|
+
* ```
|
|
672
|
+
*
|
|
673
|
+
* **Parameters:**
|
|
674
|
+
* - `key` - Cache key (hash key in Redis).
|
|
675
|
+
* - `namespace` - Optional namespace prefix. Applied to the key.
|
|
676
|
+
*
|
|
677
|
+
* @returns Object mapping fields to values (JSON-parsed when possible).
|
|
678
|
+
*/
|
|
255
679
|
hgetall<T = any>(key: string, namespace?: string): Promise<Record<string, T>>;
|
|
256
680
|
/**
|
|
257
681
|
* Deletes every cache key matching a glob pattern.
|
|
@@ -267,6 +691,31 @@ export declare class Cache {
|
|
|
267
691
|
* const removed = await cache.deletePattern('temp:*');
|
|
268
692
|
* ```
|
|
269
693
|
*/
|
|
694
|
+
/**
|
|
695
|
+
* Deletes every cache key matching a glob pattern.
|
|
696
|
+
*
|
|
697
|
+
* **Behavior:**
|
|
698
|
+
* - Cluster-safe: scans every node before deleting.
|
|
699
|
+
* - Uses `SCAN` iteratively to avoid blocking the Redis server on large datasets.
|
|
700
|
+
* - Each matching key is individually deleted via `DEL`.
|
|
701
|
+
* - The `batchSize` and `scanCount` options control the scan batching.
|
|
702
|
+
*
|
|
703
|
+
* **Returns:**
|
|
704
|
+
* - The number of deleted keys.
|
|
705
|
+
*
|
|
706
|
+
* **Example:**
|
|
707
|
+
* ```ts
|
|
708
|
+
* const removed = await cache.deletePattern('temp:*');
|
|
709
|
+
* // removed === number of keys matching 'temp:*' that were deleted
|
|
710
|
+
* ```
|
|
711
|
+
*
|
|
712
|
+
* **Parameters:**
|
|
713
|
+
* - `pattern` - Glob pattern, e.g. `'temp:*'`.
|
|
714
|
+
* - `namespace` - Optional namespace prefix. The pattern is transformed to
|
|
715
|
+
* `${namespace}:${pattern}` before scanning.
|
|
716
|
+
*
|
|
717
|
+
* @returns The number of deleted keys.
|
|
718
|
+
*/
|
|
270
719
|
deletePattern(pattern: string, namespace?: string): Promise<number>;
|
|
271
720
|
/**
|
|
272
721
|
* Lists every cache key matching a glob pattern.
|
|
@@ -282,6 +731,31 @@ export declare class Cache {
|
|
|
282
731
|
* const sessions = await cache.keys('session:*');
|
|
283
732
|
* ```
|
|
284
733
|
*/
|
|
734
|
+
/**
|
|
735
|
+
* Lists every cache key matching a glob pattern.
|
|
736
|
+
*
|
|
737
|
+
* **Behavior:**
|
|
738
|
+
* - Cluster-safe: scans every node.
|
|
739
|
+
* - Uses `SCAN` iteratively to avoid blocking the Redis server on large datasets.
|
|
740
|
+
* - Returns all keys matching the glob pattern across all nodes (in cluster mode).
|
|
741
|
+
*
|
|
742
|
+
* **Returns:**
|
|
743
|
+
* - An array of matching keys (relative format, without namespace prefix unless
|
|
744
|
+
* one was provided in the options).
|
|
745
|
+
*
|
|
746
|
+
* **Example:**
|
|
747
|
+
* ```ts
|
|
748
|
+
* const sessions = await cache.keys('session:*');
|
|
749
|
+
* // sessions === ['session:1', 'session:2', ...]
|
|
750
|
+
* ```
|
|
751
|
+
*
|
|
752
|
+
* **Parameters:**
|
|
753
|
+
* - `pattern` - Glob pattern, e.g. `'session:*'`.
|
|
754
|
+
* - `namespace` - Optional namespace prefix. The pattern is transformed to
|
|
755
|
+
* `${namespace}:${pattern}` before scanning.
|
|
756
|
+
*
|
|
757
|
+
* @returns Matching keys.
|
|
758
|
+
*/
|
|
285
759
|
keys(pattern: string, namespace?: string): Promise<string[]>;
|
|
286
760
|
/**
|
|
287
761
|
* Deletes every key inside a namespace.
|
|
@@ -294,5 +768,27 @@ export declare class Cache {
|
|
|
294
768
|
* const cleared = await cache.clearNamespace('sessions');
|
|
295
769
|
* ```
|
|
296
770
|
*/
|
|
771
|
+
/**
|
|
772
|
+
* Deletes every key inside a namespace.
|
|
773
|
+
*
|
|
774
|
+
* **Behavior:**
|
|
775
|
+
* - Deletes all keys matching the pattern `*` within the specified namespace.
|
|
776
|
+
* - Internally calls {@link deletePattern} with the pattern `*` and the given namespace.
|
|
777
|
+
*
|
|
778
|
+
* **Returns:**
|
|
779
|
+
* - The number of deleted keys.
|
|
780
|
+
*
|
|
781
|
+
* **Example:**
|
|
782
|
+
* ```ts
|
|
783
|
+
* const cleared = await cache.clearNamespace('sessions');
|
|
784
|
+
* // cleared === number of keys deleted in the 'sessions' namespace
|
|
785
|
+
* ```
|
|
786
|
+
*
|
|
787
|
+
* **Parameters:**
|
|
788
|
+
* - `namespace` - Namespace to wipe (e.g. `'sessions'`). Every key of the form
|
|
789
|
+
* `namespace:*` will be deleted.
|
|
790
|
+
*
|
|
791
|
+
* @returns The number of deleted keys.
|
|
792
|
+
*/
|
|
297
793
|
clearNamespace(namespace: string): Promise<number>;
|
|
298
794
|
}
|