@cacheable/node-cache 3.0.0 → 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/README.md +193 -11
- package/dist/index.cjs +789 -672
- package/dist/index.d.cts +418 -322
- package/dist/index.d.mts +435 -0
- package/dist/index.mjs +769 -0
- package/package.json +10 -10
- package/dist/index.d.ts +0 -339
- package/dist/index.js +0 -645
package/dist/index.d.cts
CHANGED
|
@@ -1,339 +1,435 @@
|
|
|
1
|
-
import { Hookified } from
|
|
2
|
-
import Keyv from
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
160
|
-
|
|
161
|
-
|
|
247
|
+
/**
|
|
248
|
+
* The ttl of the item in milliseconds. 0 = unlimited
|
|
249
|
+
*/
|
|
250
|
+
ttl: number;
|
|
162
251
|
};
|
|
163
252
|
declare enum NodeCacheErrors {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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;
|
|
337
433
|
}
|
|
338
|
-
|
|
339
|
-
export { NodeCache, NodeCacheErrors,
|
|
434
|
+
//#endregion
|
|
435
|
+
export { NodeCache, NodeCache as default, NodeCacheErrors, NodeCacheItem, NodeCacheOptions, NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, PartialNodeCacheItem };
|