@cacheable/node-cache 2.0.1 → 3.0.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.
- package/README.md +109 -51
- package/dist/index.cjs +149 -68
- package/dist/index.d.cts +56 -32
- package/dist/index.d.ts +56 -32
- package/dist/index.js +149 -68
- package/package.json +5 -12
package/dist/index.d.cts
CHANGED
|
@@ -6,24 +6,15 @@ type NodeCacheStoreOptions<T> = {
|
|
|
6
6
|
* Time to live in milliseconds. This is a breaking change from the original NodeCache.
|
|
7
7
|
*/
|
|
8
8
|
ttl?: number | string;
|
|
9
|
-
/**
|
|
10
|
-
* Maximum number of keys to store in the cache. If this is set to a value greater than 0, the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
11
|
-
*/
|
|
12
|
-
maxKeys?: number;
|
|
13
9
|
/**
|
|
14
10
|
* The Keyv store instance.
|
|
15
11
|
*/
|
|
16
12
|
store?: Keyv<T>;
|
|
17
|
-
/**
|
|
18
|
-
* Enable stats tracking. This is a breaking change from the original NodeCache.
|
|
19
|
-
*/
|
|
20
|
-
stats?: boolean;
|
|
21
13
|
};
|
|
22
14
|
declare class NodeCacheStore<T> extends Hookified {
|
|
23
|
-
private _maxKeys;
|
|
24
15
|
private _keyv;
|
|
25
|
-
private readonly _stats;
|
|
26
16
|
private _ttl?;
|
|
17
|
+
private _stats;
|
|
27
18
|
constructor(options?: NodeCacheStoreOptions<T>);
|
|
28
19
|
/**
|
|
29
20
|
* Time to live in milliseconds.
|
|
@@ -42,19 +33,6 @@ declare class NodeCacheStore<T> extends Hookified {
|
|
|
42
33
|
* @readonly
|
|
43
34
|
*/
|
|
44
35
|
get store(): Keyv<T>;
|
|
45
|
-
/**
|
|
46
|
-
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
|
|
47
|
-
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
48
|
-
* @returns {number}
|
|
49
|
-
* @readonly
|
|
50
|
-
*/
|
|
51
|
-
get maxKeys(): number;
|
|
52
|
-
/**
|
|
53
|
-
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
|
|
54
|
-
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
55
|
-
* @param {number} maxKeys
|
|
56
|
-
*/
|
|
57
|
-
set maxKeys(maxKeys: number);
|
|
58
36
|
/**
|
|
59
37
|
* Set a key/value pair in the cache.
|
|
60
38
|
* @param {string | number} key
|
|
@@ -74,13 +52,13 @@ declare class NodeCacheStore<T> extends Hookified {
|
|
|
74
52
|
* @param {string | number} key
|
|
75
53
|
* @returns {any | undefined}
|
|
76
54
|
*/
|
|
77
|
-
get<T>(key: string | number): Promise<
|
|
55
|
+
get<V = T>(key: string | number): Promise<V | undefined>;
|
|
78
56
|
/**
|
|
79
57
|
* Get multiple values from the cache.
|
|
80
58
|
* @param {Array<string | number>} keys
|
|
81
59
|
* @returns {Record<string, any | undefined>}
|
|
82
60
|
*/
|
|
83
|
-
mget<T>(keys: Array<string | number>): Promise<Record<string,
|
|
61
|
+
mget<V = T>(keys: Array<string | number>): Promise<Record<string, V | undefined>>;
|
|
84
62
|
/**
|
|
85
63
|
* Delete a key from the cache.
|
|
86
64
|
* @param {string | number} key
|
|
@@ -110,7 +88,17 @@ declare class NodeCacheStore<T> extends Hookified {
|
|
|
110
88
|
* @param {string | number} key
|
|
111
89
|
* @returns {T | undefined}
|
|
112
90
|
*/
|
|
113
|
-
take<T>(key: string | number): Promise<
|
|
91
|
+
take<V = T>(key: string | number): Promise<V | undefined>;
|
|
92
|
+
/**
|
|
93
|
+
* Gets the stats of the cache
|
|
94
|
+
* @returns {NodeCacheStats} the stats of the cache
|
|
95
|
+
*/
|
|
96
|
+
getStats(): NodeCacheStats;
|
|
97
|
+
/**
|
|
98
|
+
* Flush the stats.
|
|
99
|
+
* @returns {void}
|
|
100
|
+
*/
|
|
101
|
+
flushStats(): void;
|
|
114
102
|
/**
|
|
115
103
|
* Disconnect from the cache.
|
|
116
104
|
* @returns {void}
|
|
@@ -208,16 +196,33 @@ declare class NodeCache<T> extends Hookified {
|
|
|
208
196
|
constructor(options?: NodeCacheOptions);
|
|
209
197
|
/**
|
|
210
198
|
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
199
|
+
*
|
|
200
|
+
* TTL behavior:
|
|
201
|
+
* - `ttl > 0`: cache expires after the given number of seconds
|
|
202
|
+
* - `ttl === 0`: cache indefinitely (overrides stdTTL)
|
|
203
|
+
* - `ttl < 0`: store the value but it expires immediately on next access (matches original node-cache behavior)
|
|
204
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
205
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
206
|
+
*
|
|
211
207
|
* @param {string | number} key - it will convert the key to a string
|
|
212
208
|
* @param {T} value
|
|
213
|
-
* @param {number | string} [ttl] -
|
|
214
|
-
* @returns {boolean}
|
|
209
|
+
* @param {number | string} [ttl] - TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
210
|
+
* @returns {boolean} true on success
|
|
211
|
+
* @throws {Error} If the `key` or `ttl` is of an invalid type.
|
|
215
212
|
*/
|
|
216
213
|
set(key: string | number, value: T, ttl?: number | string): boolean;
|
|
217
214
|
/**
|
|
218
215
|
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
216
|
+
*
|
|
217
|
+
* Each item follows the same TTL behavior as `set()`:
|
|
218
|
+
* - Positive TTL: expires after the given seconds
|
|
219
|
+
* - `0`: cache indefinitely
|
|
220
|
+
* - Negative TTL: stored but expires immediately on next access
|
|
221
|
+
* - Omitted: uses `stdTTL` from options
|
|
222
|
+
*
|
|
219
223
|
* @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
|
|
220
|
-
* @returns {boolean}
|
|
224
|
+
* @returns {boolean} true on success
|
|
225
|
+
* @throws {Error} If `data` is not an array, or if any item has an invalid key or ttl type.
|
|
221
226
|
*/
|
|
222
227
|
mset(data: Array<PartialNodeCacheItem<T>>): boolean;
|
|
223
228
|
/**
|
|
@@ -232,14 +237,14 @@ declare class NodeCache<T> extends Hookified {
|
|
|
232
237
|
* @param {Array<string | number} keys an array of keys
|
|
233
238
|
* @returns {Record<string, T | undefined>} an object with the key as a property and the value as the value
|
|
234
239
|
*/
|
|
235
|
-
mget<T>(keys: Array<string | number>): Record<string,
|
|
240
|
+
mget<V = T>(keys: Array<string | number>): Record<string, V | undefined>;
|
|
236
241
|
/**
|
|
237
242
|
* Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key).
|
|
238
243
|
* Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
|
|
239
244
|
* @param {string | number} key
|
|
240
245
|
* @returns {T | undefined} the value or undefined
|
|
241
246
|
*/
|
|
242
|
-
take<T>(key: string | number):
|
|
247
|
+
take<V = T>(key: string | number): V | undefined;
|
|
243
248
|
/**
|
|
244
249
|
* Delete a key. Returns the number of deleted entries. A delete will never fail.
|
|
245
250
|
* @param {string | number | Array<string | number>} key if the key is a number it will convert it to a string. if an array is passed it will delete all keys in the array.
|
|
@@ -255,9 +260,18 @@ declare class NodeCache<T> extends Hookified {
|
|
|
255
260
|
/**
|
|
256
261
|
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
257
262
|
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
263
|
+
*
|
|
264
|
+
* TTL behavior:
|
|
265
|
+
* - `ttl > 0`: key expires after the given number of seconds
|
|
266
|
+
* - `ttl === 0`: key lives indefinitely (overrides stdTTL)
|
|
267
|
+
* - `ttl < 0`: key expires immediately on next access (matches original node-cache behavior)
|
|
268
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
269
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
270
|
+
*
|
|
258
271
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
259
|
-
* @param {number | string} [ttl]
|
|
272
|
+
* @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
260
273
|
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
274
|
+
* @throws {Error} If the `ttl` is of an invalid type (must be a number or string).
|
|
261
275
|
*/
|
|
262
276
|
ttl(key: string | number, ttl?: number | string): boolean;
|
|
263
277
|
/**
|
|
@@ -308,6 +322,16 @@ declare class NodeCache<T> extends Hookified {
|
|
|
308
322
|
private formatKey;
|
|
309
323
|
private clone;
|
|
310
324
|
private getExpirationTimestamp;
|
|
325
|
+
/**
|
|
326
|
+
* Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the
|
|
327
|
+
* resolved timestamp is not in the future (e.g. "0s" or a zero-duration string).
|
|
328
|
+
*/
|
|
329
|
+
private resolveExpiration;
|
|
330
|
+
/**
|
|
331
|
+
* Checks whether a TTL value is negative. Handles both numbers and
|
|
332
|
+
* purely numeric strings (e.g. "-1").
|
|
333
|
+
*/
|
|
334
|
+
private isNegativeTtl;
|
|
311
335
|
private checkData;
|
|
312
336
|
private createError;
|
|
313
337
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,24 +6,15 @@ type NodeCacheStoreOptions<T> = {
|
|
|
6
6
|
* Time to live in milliseconds. This is a breaking change from the original NodeCache.
|
|
7
7
|
*/
|
|
8
8
|
ttl?: number | string;
|
|
9
|
-
/**
|
|
10
|
-
* Maximum number of keys to store in the cache. If this is set to a value greater than 0, the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
11
|
-
*/
|
|
12
|
-
maxKeys?: number;
|
|
13
9
|
/**
|
|
14
10
|
* The Keyv store instance.
|
|
15
11
|
*/
|
|
16
12
|
store?: Keyv<T>;
|
|
17
|
-
/**
|
|
18
|
-
* Enable stats tracking. This is a breaking change from the original NodeCache.
|
|
19
|
-
*/
|
|
20
|
-
stats?: boolean;
|
|
21
13
|
};
|
|
22
14
|
declare class NodeCacheStore<T> extends Hookified {
|
|
23
|
-
private _maxKeys;
|
|
24
15
|
private _keyv;
|
|
25
|
-
private readonly _stats;
|
|
26
16
|
private _ttl?;
|
|
17
|
+
private _stats;
|
|
27
18
|
constructor(options?: NodeCacheStoreOptions<T>);
|
|
28
19
|
/**
|
|
29
20
|
* Time to live in milliseconds.
|
|
@@ -42,19 +33,6 @@ declare class NodeCacheStore<T> extends Hookified {
|
|
|
42
33
|
* @readonly
|
|
43
34
|
*/
|
|
44
35
|
get store(): Keyv<T>;
|
|
45
|
-
/**
|
|
46
|
-
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
|
|
47
|
-
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
48
|
-
* @returns {number}
|
|
49
|
-
* @readonly
|
|
50
|
-
*/
|
|
51
|
-
get maxKeys(): number;
|
|
52
|
-
/**
|
|
53
|
-
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
|
|
54
|
-
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
55
|
-
* @param {number} maxKeys
|
|
56
|
-
*/
|
|
57
|
-
set maxKeys(maxKeys: number);
|
|
58
36
|
/**
|
|
59
37
|
* Set a key/value pair in the cache.
|
|
60
38
|
* @param {string | number} key
|
|
@@ -74,13 +52,13 @@ declare class NodeCacheStore<T> extends Hookified {
|
|
|
74
52
|
* @param {string | number} key
|
|
75
53
|
* @returns {any | undefined}
|
|
76
54
|
*/
|
|
77
|
-
get<T>(key: string | number): Promise<
|
|
55
|
+
get<V = T>(key: string | number): Promise<V | undefined>;
|
|
78
56
|
/**
|
|
79
57
|
* Get multiple values from the cache.
|
|
80
58
|
* @param {Array<string | number>} keys
|
|
81
59
|
* @returns {Record<string, any | undefined>}
|
|
82
60
|
*/
|
|
83
|
-
mget<T>(keys: Array<string | number>): Promise<Record<string,
|
|
61
|
+
mget<V = T>(keys: Array<string | number>): Promise<Record<string, V | undefined>>;
|
|
84
62
|
/**
|
|
85
63
|
* Delete a key from the cache.
|
|
86
64
|
* @param {string | number} key
|
|
@@ -110,7 +88,17 @@ declare class NodeCacheStore<T> extends Hookified {
|
|
|
110
88
|
* @param {string | number} key
|
|
111
89
|
* @returns {T | undefined}
|
|
112
90
|
*/
|
|
113
|
-
take<T>(key: string | number): Promise<
|
|
91
|
+
take<V = T>(key: string | number): Promise<V | undefined>;
|
|
92
|
+
/**
|
|
93
|
+
* Gets the stats of the cache
|
|
94
|
+
* @returns {NodeCacheStats} the stats of the cache
|
|
95
|
+
*/
|
|
96
|
+
getStats(): NodeCacheStats;
|
|
97
|
+
/**
|
|
98
|
+
* Flush the stats.
|
|
99
|
+
* @returns {void}
|
|
100
|
+
*/
|
|
101
|
+
flushStats(): void;
|
|
114
102
|
/**
|
|
115
103
|
* Disconnect from the cache.
|
|
116
104
|
* @returns {void}
|
|
@@ -208,16 +196,33 @@ declare class NodeCache<T> extends Hookified {
|
|
|
208
196
|
constructor(options?: NodeCacheOptions);
|
|
209
197
|
/**
|
|
210
198
|
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
199
|
+
*
|
|
200
|
+
* TTL behavior:
|
|
201
|
+
* - `ttl > 0`: cache expires after the given number of seconds
|
|
202
|
+
* - `ttl === 0`: cache indefinitely (overrides stdTTL)
|
|
203
|
+
* - `ttl < 0`: store the value but it expires immediately on next access (matches original node-cache behavior)
|
|
204
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
205
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
206
|
+
*
|
|
211
207
|
* @param {string | number} key - it will convert the key to a string
|
|
212
208
|
* @param {T} value
|
|
213
|
-
* @param {number | string} [ttl] -
|
|
214
|
-
* @returns {boolean}
|
|
209
|
+
* @param {number | string} [ttl] - TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
210
|
+
* @returns {boolean} true on success
|
|
211
|
+
* @throws {Error} If the `key` or `ttl` is of an invalid type.
|
|
215
212
|
*/
|
|
216
213
|
set(key: string | number, value: T, ttl?: number | string): boolean;
|
|
217
214
|
/**
|
|
218
215
|
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
216
|
+
*
|
|
217
|
+
* Each item follows the same TTL behavior as `set()`:
|
|
218
|
+
* - Positive TTL: expires after the given seconds
|
|
219
|
+
* - `0`: cache indefinitely
|
|
220
|
+
* - Negative TTL: stored but expires immediately on next access
|
|
221
|
+
* - Omitted: uses `stdTTL` from options
|
|
222
|
+
*
|
|
219
223
|
* @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
|
|
220
|
-
* @returns {boolean}
|
|
224
|
+
* @returns {boolean} true on success
|
|
225
|
+
* @throws {Error} If `data` is not an array, or if any item has an invalid key or ttl type.
|
|
221
226
|
*/
|
|
222
227
|
mset(data: Array<PartialNodeCacheItem<T>>): boolean;
|
|
223
228
|
/**
|
|
@@ -232,14 +237,14 @@ declare class NodeCache<T> extends Hookified {
|
|
|
232
237
|
* @param {Array<string | number} keys an array of keys
|
|
233
238
|
* @returns {Record<string, T | undefined>} an object with the key as a property and the value as the value
|
|
234
239
|
*/
|
|
235
|
-
mget<T>(keys: Array<string | number>): Record<string,
|
|
240
|
+
mget<V = T>(keys: Array<string | number>): Record<string, V | undefined>;
|
|
236
241
|
/**
|
|
237
242
|
* Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key).
|
|
238
243
|
* Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
|
|
239
244
|
* @param {string | number} key
|
|
240
245
|
* @returns {T | undefined} the value or undefined
|
|
241
246
|
*/
|
|
242
|
-
take<T>(key: string | number):
|
|
247
|
+
take<V = T>(key: string | number): V | undefined;
|
|
243
248
|
/**
|
|
244
249
|
* Delete a key. Returns the number of deleted entries. A delete will never fail.
|
|
245
250
|
* @param {string | number | Array<string | number>} key if the key is a number it will convert it to a string. if an array is passed it will delete all keys in the array.
|
|
@@ -255,9 +260,18 @@ declare class NodeCache<T> extends Hookified {
|
|
|
255
260
|
/**
|
|
256
261
|
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
257
262
|
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
263
|
+
*
|
|
264
|
+
* TTL behavior:
|
|
265
|
+
* - `ttl > 0`: key expires after the given number of seconds
|
|
266
|
+
* - `ttl === 0`: key lives indefinitely (overrides stdTTL)
|
|
267
|
+
* - `ttl < 0`: key expires immediately on next access (matches original node-cache behavior)
|
|
268
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
269
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
270
|
+
*
|
|
258
271
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
259
|
-
* @param {number | string} [ttl]
|
|
272
|
+
* @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
260
273
|
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
274
|
+
* @throws {Error} If the `ttl` is of an invalid type (must be a number or string).
|
|
261
275
|
*/
|
|
262
276
|
ttl(key: string | number, ttl?: number | string): boolean;
|
|
263
277
|
/**
|
|
@@ -308,6 +322,16 @@ declare class NodeCache<T> extends Hookified {
|
|
|
308
322
|
private formatKey;
|
|
309
323
|
private clone;
|
|
310
324
|
private getExpirationTimestamp;
|
|
325
|
+
/**
|
|
326
|
+
* Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the
|
|
327
|
+
* resolved timestamp is not in the future (e.g. "0s" or a zero-duration string).
|
|
328
|
+
*/
|
|
329
|
+
private resolveExpiration;
|
|
330
|
+
/**
|
|
331
|
+
* Checks whether a TTL value is negative. Handles both numbers and
|
|
332
|
+
* purely numeric strings (e.g. "-1").
|
|
333
|
+
*/
|
|
334
|
+
private isNegativeTtl;
|
|
311
335
|
private checkData;
|
|
312
336
|
private createError;
|
|
313
337
|
}
|