@cacheable/node-cache 3.0.1 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,346 +1,435 @@
1
- import { Hookified } from 'hookified';
2
- import Keyv from 'keyv';
1
+ import { Hookified } from "hookified";
2
+ import Keyv from "keyv";
3
3
 
4
+ //#region src/store.d.ts
4
5
  type NodeCacheStoreOptions<T> = {
5
- /**
6
- * Time to live in milliseconds. This is a breaking change from the original NodeCache.
7
- */
8
- ttl?: number | string;
9
- /**
10
- * The Keyv store instance.
11
- */
12
- store?: Keyv<T>;
6
+ /**
7
+ * Time to live in milliseconds. This is a breaking change from the original NodeCache.
8
+ */
9
+ ttl?: number | string;
10
+ /**
11
+ * The Keyv store instance.
12
+ */
13
+ store?: Keyv<T>;
14
+ /**
15
+ * If set to true, the cache will clone the returned items via get() functions using structuredClone.
16
+ * This means mutations to the returned object do not affect the cached copy and vice versa.
17
+ * Default is false.
18
+ */
19
+ useClones?: boolean;
20
+ /**
21
+ * The interval in seconds to check for expired items. 0 = disabled. Default is 0.
22
+ */
23
+ checkperiod?: number;
24
+ /**
25
+ * Whether to delete expired items when they are detected. Default is true.
26
+ */
27
+ deleteOnExpire?: boolean;
13
28
  };
14
29
  declare class NodeCacheStore<T> extends Hookified {
15
- private _keyv;
16
- private _ttl?;
17
- private _stats;
18
- constructor(options?: NodeCacheStoreOptions<T>);
19
- /**
20
- * Time to live in milliseconds.
21
- * @returns {number | string | undefined}
22
- * @readonly
23
- */
24
- get ttl(): number | string | undefined;
25
- /**
26
- * Time to live in milliseconds.
27
- * @param {number | string | undefined} ttl
28
- */
29
- set ttl(ttl: number | string | undefined);
30
- /**
31
- * The Keyv store instance.
32
- * @returns {Keyv<T>}
33
- * @readonly
34
- */
35
- get store(): Keyv<T>;
36
- /**
37
- * Set a key/value pair in the cache.
38
- * @param {string | number} key
39
- * @param {T} value
40
- * @param {number} [ttl]
41
- * @returns {boolean}
42
- */
43
- set(key: string | number, value: T, ttl?: number | string): Promise<boolean>;
44
- /**
45
- * Set multiple key/value pairs in the cache.
46
- * @param {PartialNodeCacheItem[]} list
47
- * @returns {void}
48
- */
49
- mset(list: Array<PartialNodeCacheItem<T>>): Promise<void>;
50
- /**
51
- * Get a value from the cache.
52
- * @param {string | number} key
53
- * @returns {any | undefined}
54
- */
55
- get<V = T>(key: string | number): Promise<V | undefined>;
56
- /**
57
- * Get multiple values from the cache.
58
- * @param {Array<string | number>} keys
59
- * @returns {Record<string, any | undefined>}
60
- */
61
- mget<V = T>(keys: Array<string | number>): Promise<Record<string, V | undefined>>;
62
- /**
63
- * Delete a key from the cache.
64
- * @param {string | number} key
65
- * @returns {boolean}
66
- */
67
- del(key: string | number): Promise<boolean>;
68
- /**
69
- * Delete multiple keys from the cache.
70
- * @param {Array<string | number>} keys
71
- * @returns {boolean}
72
- */
73
- mdel(keys: Array<string | number>): Promise<boolean>;
74
- /**
75
- * Clear the cache.
76
- * @returns {void}
77
- */
78
- clear(): Promise<void>;
79
- /**
80
- * Set the TTL of an existing key in the cache.
81
- * @param {string | number} key
82
- * @param {number | string} [ttl]
83
- * @returns {boolean}
84
- */
85
- setTtl(key: string | number, ttl?: number | string): Promise<boolean>;
86
- /**
87
- * Get a key from the cache and delete it. If it does exist it will get the value and delete the item from the cache.
88
- * @param {string | number} key
89
- * @returns {T | undefined}
90
- */
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;
102
- /**
103
- * Disconnect from the cache.
104
- * @returns {void}
105
- */
106
- disconnect(): Promise<void>;
107
- /**
108
- * Resolve the TTL to milliseconds.
109
- * @param {number | string | undefined} ttl
110
- * @returns {number | undefined}
111
- */
112
- private resolveTtl;
30
+ private _keyv;
31
+ private _ttl?;
32
+ private _stats;
33
+ private _useClones;
34
+ private _deleteOnExpire;
35
+ private _keys;
36
+ private _values;
37
+ private _ttls;
38
+ private _intervalId;
39
+ constructor(options?: NodeCacheStoreOptions<T>);
40
+ /**
41
+ * Time to live in milliseconds.
42
+ * @returns {number | string | undefined}
43
+ * @readonly
44
+ */
45
+ get ttl(): number | string | undefined;
46
+ /**
47
+ * Time to live in milliseconds.
48
+ * @param {number | string | undefined} ttl
49
+ */
50
+ set ttl(ttl: number | string | undefined);
51
+ /**
52
+ * Whether to clone values on get/set.
53
+ * @returns {boolean}
54
+ */
55
+ get useClones(): boolean;
56
+ /**
57
+ * Whether to clone values on get/set.
58
+ * @param {boolean} value
59
+ */
60
+ set useClones(value: boolean);
61
+ /**
62
+ * Whether to delete expired items when detected.
63
+ * @returns {boolean}
64
+ */
65
+ get deleteOnExpire(): boolean;
66
+ /**
67
+ * Whether to delete expired items when detected.
68
+ * @param {boolean} value
69
+ */
70
+ set deleteOnExpire(value: boolean);
71
+ /**
72
+ * The Keyv store instance.
73
+ * @returns {Keyv<T>}
74
+ * @readonly
75
+ */
76
+ get store(): Keyv<T>;
77
+ /**
78
+ * Set a key/value pair in the cache.
79
+ * @param {string | number} key
80
+ * @param {T} value
81
+ * @param {number} [ttl]
82
+ * @returns {boolean}
83
+ */
84
+ set(key: string | number, value: T, ttl?: number | string): Promise<boolean>;
85
+ /**
86
+ * Set multiple key/value pairs in the cache.
87
+ * @param {PartialNodeCacheItem[]} list
88
+ * @returns {void}
89
+ */
90
+ mset(list: Array<PartialNodeCacheItem<T>>): Promise<void>;
91
+ /**
92
+ * Get a value from the cache.
93
+ * @param {string | number} key
94
+ * @returns {any | undefined}
95
+ */
96
+ get<V = T>(key: string | number): Promise<V | undefined>;
97
+ /**
98
+ * Get multiple values from the cache.
99
+ * @param {Array<string | number>} keys
100
+ * @returns {Record<string, any | undefined>}
101
+ */
102
+ mget<V = T>(keys: Array<string | number>): Promise<Record<string, V | undefined>>;
103
+ /**
104
+ * Delete a key from the cache.
105
+ * @param {string | number} key
106
+ * @returns {boolean}
107
+ */
108
+ del(key: string | number): Promise<boolean>;
109
+ /**
110
+ * Delete multiple keys from the cache.
111
+ * @param {Array<string | number>} keys
112
+ * @returns {boolean}
113
+ */
114
+ mdel(keys: Array<string | number>): Promise<boolean>;
115
+ /**
116
+ * Clear the cache.
117
+ * @returns {void}
118
+ */
119
+ clear(): Promise<void>;
120
+ /**
121
+ * Flush all data and stats. Emits a "flush" event.
122
+ * @returns {void}
123
+ */
124
+ flushAll(): Promise<void>;
125
+ /**
126
+ * Set the TTL of an existing key in the cache.
127
+ * @param {string | number} key
128
+ * @param {number | string} [ttl]
129
+ * @returns {boolean}
130
+ */
131
+ setTtl(key: string | number, ttl?: number | string): Promise<boolean>;
132
+ /**
133
+ * Get the TTL of a key. Returns 0 if the key has no TTL (unlimited),
134
+ * undefined if the key does not exist, or a timestamp in ms of when the key will expire.
135
+ * @param {string | number} key
136
+ * @returns {number | undefined}
137
+ */
138
+ getTtl(key: string | number): Promise<number | undefined>;
139
+ /**
140
+ * Returns an array of all existing keys.
141
+ * @returns {string[]}
142
+ */
143
+ keys(): Promise<string[]>;
144
+ /**
145
+ * Returns boolean indicating if the key is cached and not expired.
146
+ * @param {string | number} key
147
+ * @returns {boolean}
148
+ */
149
+ has(key: string | number): Promise<boolean>;
150
+ /**
151
+ * Get a key from the cache and delete it. If it does exist it will get the value and delete the item from the cache.
152
+ * @param {string | number} key
153
+ * @returns {T | undefined}
154
+ */
155
+ take<V = T>(key: string | number): Promise<V | undefined>;
156
+ /**
157
+ * Gets the stats of the cache
158
+ * @returns {NodeCacheStats} the stats of the cache
159
+ */
160
+ getStats(): NodeCacheStats;
161
+ /**
162
+ * Flush the stats.
163
+ * @returns {void}
164
+ */
165
+ flushStats(): void;
166
+ /**
167
+ * Disconnect from the cache.
168
+ * @returns {void}
169
+ */
170
+ disconnect(): Promise<void>;
171
+ /**
172
+ * Close the cache. Stops the interval timer.
173
+ * @returns {void}
174
+ */
175
+ close(): void;
176
+ /**
177
+ * Get the interval id.
178
+ * @returns {number | NodeJS.Timeout}
179
+ */
180
+ getIntervalId(): number | NodeJS.Timeout;
181
+ /**
182
+ * Start the check interval for expired items.
183
+ * @param {number} [checkperiod] interval in seconds
184
+ */
185
+ startInterval(checkperiod?: number): void;
186
+ /**
187
+ * Stop the check interval.
188
+ */
189
+ stopInterval(): void;
190
+ private clone;
191
+ private isExpired;
192
+ private handleExpired;
193
+ private checkData;
194
+ private trackExpiration;
195
+ /**
196
+ * Resolve the TTL to milliseconds.
197
+ * @param {number | string | undefined} ttl
198
+ * @returns {number | undefined}
199
+ */
200
+ private resolveTtl;
113
201
  }
114
-
202
+ //#endregion
203
+ //#region src/index.d.ts
115
204
  type NodeCacheOptions = {
116
- /**
117
- * The standard ttl as number in seconds for every generated cache element. 0 = unlimited, If string, it will be in shorthand format like '1h' for 1 hour
118
- */
119
- stdTTL?: number | string;
120
- /**
121
- * The interval to check for expired items in seconds. Default is 600 = 5 minutes
122
- */
123
- checkperiod?: number;
124
- /**
125
- * If set to true (Default Setting), the cache will clone the returned items via get() functions.
126
- * This means that every time you set a value into the cache, node-cache makes a deep clone of it.
127
- * When you get that value back, you receive another deep clone.
128
- * This mimics the behavior of an external cache like Redis or Memcached, meaning mutations to the
129
- * returned object do not affect the cached copy (and vice versa). If set to false, the original
130
- * object will be returned, and mutations will affect the cached copy.
131
- */
132
- useClones?: boolean;
133
- /**
134
- * Delete expired items during an interval check or a single item on a get request. Default is true.
135
- */
136
- deleteOnExpire?: boolean;
137
- /**
138
- * The maximum number of keys that will be stored in the cache. Default is -1 = unlimited
139
- * If the limit is reached, it will no longer add any new items until some expire.
140
- */
141
- maxKeys?: number;
205
+ /**
206
+ * The standard ttl as number in seconds for every generated cache element. 0 = unlimited, If string, it will be in shorthand format like '1h' for 1 hour
207
+ */
208
+ stdTTL?: number | string;
209
+ /**
210
+ * The interval to check for expired items in seconds. Default is 600 = 5 minutes
211
+ */
212
+ checkperiod?: number;
213
+ /**
214
+ * If set to true (Default Setting), the cache will clone the returned items via get() functions.
215
+ * This means that every time you set a value into the cache, node-cache makes a deep clone of it.
216
+ * When you get that value back, you receive another deep clone.
217
+ * This mimics the behavior of an external cache like Redis or Memcached, meaning mutations to the
218
+ * returned object do not affect the cached copy (and vice versa). If set to false, the original
219
+ * object will be returned, and mutations will affect the cached copy.
220
+ */
221
+ useClones?: boolean;
222
+ /**
223
+ * Delete expired items during an interval check or a single item on a get request. Default is true.
224
+ */
225
+ deleteOnExpire?: boolean;
226
+ /**
227
+ * The maximum number of keys that will be stored in the cache. Default is -1 = unlimited
228
+ * If the limit is reached, it will no longer add any new items until some expire.
229
+ */
230
+ maxKeys?: number;
142
231
  };
143
232
  type PartialNodeCacheItem<T> = {
144
- /**
145
- * The key of the item
146
- */
147
- key: string | number;
148
- /**
149
- * The value of the item
150
- */
151
- value: T;
152
- /**
153
- * The ttl of the item in seconds. 0 = unlimited
154
- */
155
- ttl?: number;
233
+ /**
234
+ * The key of the item
235
+ */
236
+ key: string | number;
237
+ /**
238
+ * The value of the item
239
+ */
240
+ value: T;
241
+ /**
242
+ * The ttl of the item in seconds. 0 = unlimited
243
+ */
244
+ ttl?: number;
156
245
  };
157
246
  type NodeCacheItem<T> = PartialNodeCacheItem<T> & {
158
- /**
159
- * The ttl of the item in milliseconds. 0 = unlimited
160
- */
161
- ttl: number;
247
+ /**
248
+ * The ttl of the item in milliseconds. 0 = unlimited
249
+ */
250
+ ttl: number;
162
251
  };
163
252
  declare enum NodeCacheErrors {
164
- ECACHEFULL = "Cache max keys amount exceeded",
165
- EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
166
- EKEYSTYPE = "The keys argument has to be an array.",
167
- ETTLTYPE = "The ttl argument has to be a number or a string for shorthand ttl."
253
+ ECACHEFULL = "Cache max keys amount exceeded",
254
+ EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
255
+ EKEYSTYPE = "The keys argument has to be an array.",
256
+ ETTLTYPE = "The ttl argument has to be a number or a string for shorthand ttl."
168
257
  }
169
258
  type NodeCacheStats = {
170
- /**
171
- * The number of keys stored in the cache
172
- */
173
- keys: number;
174
- /**
175
- * The number of hits
176
- */
177
- hits: number;
178
- /**
179
- * The number of misses
180
- */
181
- misses: number;
182
- /**
183
- * The global key size count in approximately bytes
184
- */
185
- ksize: number;
186
- /**
187
- * The global value size count in approximately bytes
188
- */
189
- vsize: number;
259
+ /**
260
+ * The number of keys stored in the cache
261
+ */
262
+ keys: number;
263
+ /**
264
+ * The number of hits
265
+ */
266
+ hits: number;
267
+ /**
268
+ * The number of misses
269
+ */
270
+ misses: number;
271
+ /**
272
+ * The global key size count in approximately bytes
273
+ */
274
+ ksize: number;
275
+ /**
276
+ * The global value size count in approximately bytes
277
+ */
278
+ vsize: number;
190
279
  };
191
280
  declare class NodeCache<T> extends Hookified {
192
- readonly options: NodeCacheOptions;
193
- readonly store: Map<string, NodeCacheItem<T>>;
194
- private _stats;
195
- private intervalId;
196
- constructor(options?: NodeCacheOptions);
197
- /**
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
- *
207
- * @param {string | number} key - it will convert the key to a string
208
- * @param {T} value
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.
212
- */
213
- set(key: string | number, value: T, ttl?: number | string): boolean;
214
- /**
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
- *
223
- * @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
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.
226
- */
227
- mset(data: Array<PartialNodeCacheItem<T>>): boolean;
228
- /**
229
- * Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
230
- * @param {string | number} key if the key is a number it will convert it to a string
231
- * @returns {T} the value or undefined
232
- */
233
- get(key: string | number): T | undefined;
234
- /**
235
- * Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
236
- * If the value was found it returns an object with the key value pair.
237
- * @param {Array<string | number} keys an array of keys
238
- * @returns {Record<string, T | undefined>} an object with the key as a property and the value as the value
239
- */
240
- mget<V = T>(keys: Array<string | number>): Record<string, V | undefined>;
241
- /**
242
- * Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key).
243
- * Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
244
- * @param {string | number} key
245
- * @returns {T | undefined} the value or undefined
246
- */
247
- take<V = T>(key: string | number): V | undefined;
248
- /**
249
- * Delete a key. Returns the number of deleted entries. A delete will never fail.
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.
251
- * @returns {number} if it was successful it will return the count that was deleted
252
- */
253
- del(key: string | number | Array<string | number>): number;
254
- /**
255
- * Delete all keys in Array that exist. Returns the number of deleted entries.
256
- * @param {Array<string | number>} keys an array of keys
257
- * @returns {number} the count of deleted keys
258
- */
259
- mdel(keys: Array<string | number>): number;
260
- /**
261
- * Redefine the ttl of a key. Returns true if the key has been found and changed.
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
- *
271
- * @param {string | number} key if the key is a number it will convert it to a string
272
- * @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
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).
275
- */
276
- ttl(key: string | number, ttl?: number | string): boolean;
277
- /**
278
- * Receive the ttl of a key.
279
- * @param {string | number} key if the key is a number it will convert it to a string
280
- * @returns {number | undefined} 0 if this key has no ttl, undefined if this key is not in the cache,
281
- * a timestamp in ms representing the time at which this key will expire
282
- */
283
- getTtl(key: string | number): number | undefined;
284
- /**
285
- * Returns an array of all existing keys. [ "all", "my", "keys", "foo", "bar" ]
286
- * @returns {string[]} an array of all keys
287
- */
288
- keys(): string[];
289
- /**
290
- * Returns boolean indicating if the key is cached. If the key has expired, it
291
- * will return false and the key will be removed from the cache when
292
- * `deleteOnExpire` is enabled (matches the original node-cache behavior).
293
- * @param {string | number} key if the key is a number it will convert it to a string
294
- * @returns {boolean} true if the key is cached and not expired
295
- */
296
- has(key: string | number): boolean;
297
- /**
298
- * Gets the stats of the cache
299
- * @returns {NodeCacheStats} the stats of the cache
300
- */
301
- getStats(): NodeCacheStats;
302
- /**
303
- * Flush the whole data.
304
- * @returns {void}
305
- */
306
- flushAll(): void;
307
- /**
308
- * Flush the stats.
309
- * @returns {void}
310
- */
311
- flushStats(): void;
312
- /**
313
- * Close the cache. This will clear the interval timeout which is set on check period option.
314
- * @returns {void}
315
- */
316
- close(): void;
317
- /**
318
- * Get the interval id
319
- * @returns {number | NodeJS.Timeout} the interval id
320
- */
321
- getIntervalId(): number | NodeJS.Timeout;
322
- startInterval(): void;
323
- stopInterval(): void;
324
- private formatKey;
325
- private clone;
326
- private getExpirationTimestamp;
327
- /**
328
- * Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the
329
- * resolved timestamp is not in the future (e.g. "0s" or a zero-duration string).
330
- */
331
- private resolveExpiration;
332
- /**
333
- * Checks whether a TTL value is negative. Handles both numbers and
334
- * purely numeric strings (e.g. "-1").
335
- */
336
- private isNegativeTtl;
337
- private checkData;
338
- /**
339
- * Handles expiration for a cache entry. Deletes the entry when
340
- * `deleteOnExpire` is enabled and emits the "expired" event.
341
- */
342
- private handleExpired;
343
- private createError;
281
+ readonly options: NodeCacheOptions;
282
+ readonly store: Map<string, NodeCacheItem<T>>;
283
+ private _stats;
284
+ private intervalId;
285
+ constructor(options?: NodeCacheOptions);
286
+ /**
287
+ * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
288
+ *
289
+ * TTL behavior:
290
+ * - `ttl > 0`: cache expires after the given number of seconds
291
+ * - `ttl === 0`: cache indefinitely (overrides stdTTL)
292
+ * - `ttl < 0`: store the value but it expires immediately on next access (matches original node-cache behavior)
293
+ * - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
294
+ * - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
295
+ *
296
+ * @param {string | number} key - it will convert the key to a string
297
+ * @param {T} value
298
+ * @param {number | string} [ttl] - TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
299
+ * @returns {boolean} true on success
300
+ * @throws {Error} If the `key` or `ttl` is of an invalid type.
301
+ */
302
+ set(key: string | number, value: T, ttl?: number | string): boolean;
303
+ /**
304
+ * Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
305
+ *
306
+ * Each item follows the same TTL behavior as `set()`:
307
+ * - Positive TTL: expires after the given seconds
308
+ * - `0`: cache indefinitely
309
+ * - Negative TTL: stored but expires immediately on next access
310
+ * - Omitted: uses `stdTTL` from options
311
+ *
312
+ * @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
313
+ * @returns {boolean} true on success
314
+ * @throws {Error} If `data` is not an array, or if any item has an invalid key or ttl type.
315
+ */
316
+ mset(data: Array<PartialNodeCacheItem<T>>): boolean;
317
+ /**
318
+ * Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
319
+ * @param {string | number} key if the key is a number it will convert it to a string
320
+ * @returns {T} the value or undefined
321
+ */
322
+ get(key: string | number): T | undefined;
323
+ /**
324
+ * Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
325
+ * If the value was found it returns an object with the key value pair.
326
+ * @param {Array<string | number} keys an array of keys
327
+ * @returns {Record<string, T | undefined>} an object with the key as a property and the value as the value
328
+ */
329
+ mget<V = T>(keys: Array<string | number>): Record<string, V | undefined>;
330
+ /**
331
+ * Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key).
332
+ * Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
333
+ * @param {string | number} key
334
+ * @returns {T | undefined} the value or undefined
335
+ */
336
+ take<V = T>(key: string | number): V | undefined;
337
+ /**
338
+ * Delete a key. Returns the number of deleted entries. A delete will never fail.
339
+ * @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.
340
+ * @returns {number} if it was successful it will return the count that was deleted
341
+ */
342
+ del(key: string | number | Array<string | number>): number;
343
+ /**
344
+ * Delete all keys in Array that exist. Returns the number of deleted entries.
345
+ * @param {Array<string | number>} keys an array of keys
346
+ * @returns {number} the count of deleted keys
347
+ */
348
+ mdel(keys: Array<string | number>): number;
349
+ /**
350
+ * Redefine the ttl of a key. Returns true if the key has been found and changed.
351
+ * Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
352
+ *
353
+ * TTL behavior:
354
+ * - `ttl > 0`: key expires after the given number of seconds
355
+ * - `ttl === 0`: key lives indefinitely (overrides stdTTL)
356
+ * - `ttl < 0`: key expires immediately on next access (matches original node-cache behavior)
357
+ * - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
358
+ * - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
359
+ *
360
+ * @param {string | number} key if the key is a number it will convert it to a string
361
+ * @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
362
+ * @returns {boolean} true if the key has been found and changed. Otherwise returns false.
363
+ * @throws {Error} If the `ttl` is of an invalid type (must be a number or string).
364
+ */
365
+ ttl(key: string | number, ttl?: number | string): boolean;
366
+ /**
367
+ * Receive the ttl of a key.
368
+ * @param {string | number} key if the key is a number it will convert it to a string
369
+ * @returns {number | undefined} 0 if this key has no ttl, undefined if this key is not in the cache,
370
+ * a timestamp in ms representing the time at which this key will expire
371
+ */
372
+ getTtl(key: string | number): number | undefined;
373
+ /**
374
+ * Returns an array of all existing keys. [ "all", "my", "keys", "foo", "bar" ]
375
+ * @returns {string[]} an array of all keys
376
+ */
377
+ keys(): string[];
378
+ /**
379
+ * Returns boolean indicating if the key is cached. If the key has expired, it
380
+ * will return false and the key will be removed from the cache when
381
+ * `deleteOnExpire` is enabled (matches the original node-cache behavior).
382
+ * @param {string | number} key if the key is a number it will convert it to a string
383
+ * @returns {boolean} true if the key is cached and not expired
384
+ */
385
+ has(key: string | number): boolean;
386
+ /**
387
+ * Gets the stats of the cache
388
+ * @returns {NodeCacheStats} the stats of the cache
389
+ */
390
+ getStats(): NodeCacheStats;
391
+ /**
392
+ * Flush the whole data.
393
+ * @returns {void}
394
+ */
395
+ flushAll(): void;
396
+ /**
397
+ * Flush the stats.
398
+ * @returns {void}
399
+ */
400
+ flushStats(): void;
401
+ /**
402
+ * Close the cache. This will clear the interval timeout which is set on check period option.
403
+ * @returns {void}
404
+ */
405
+ close(): void;
406
+ /**
407
+ * Get the interval id
408
+ * @returns {number | NodeJS.Timeout} the interval id
409
+ */
410
+ getIntervalId(): number | NodeJS.Timeout;
411
+ startInterval(): void;
412
+ stopInterval(): void;
413
+ private formatKey;
414
+ private clone;
415
+ private getExpirationTimestamp;
416
+ /**
417
+ * Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the
418
+ * resolved timestamp is not in the future (e.g. "0s" or a zero-duration string).
419
+ */
420
+ private resolveExpiration;
421
+ /**
422
+ * Checks whether a TTL value is negative. Handles both numbers and
423
+ * purely numeric strings (e.g. "-1").
424
+ */
425
+ private isNegativeTtl;
426
+ private checkData;
427
+ /**
428
+ * Handles expiration for a cache entry. Deletes the entry when
429
+ * `deleteOnExpire` is enabled and emits the "expired" event.
430
+ */
431
+ private handleExpired;
432
+ private createError;
344
433
  }
345
-
346
- export { NodeCache, NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, type PartialNodeCacheItem, NodeCache as default };
434
+ //#endregion
435
+ export { NodeCache, NodeCache as default, NodeCacheErrors, NodeCacheItem, NodeCacheOptions, NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, PartialNodeCacheItem };