@cacheable/node-cache 1.4.0 → 1.4.2
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 +30 -29
- package/dist/index.cjs +175 -33
- package/dist/index.d.cts +229 -1
- package/dist/index.d.ts +229 -1
- package/dist/index.js +175 -33
- package/package.json +13 -9
package/dist/index.d.cts
CHANGED
|
@@ -3,46 +3,178 @@ import { Cacheable } from 'cacheable';
|
|
|
3
3
|
import { Keyv } from 'keyv';
|
|
4
4
|
|
|
5
5
|
type NodeCacheStoreOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Time to live in milliseconds. This is a breaking change from the original NodeCache.
|
|
8
|
+
*/
|
|
6
9
|
ttl?: number | string;
|
|
10
|
+
/**
|
|
11
|
+
* 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.
|
|
12
|
+
*/
|
|
7
13
|
maxKeys?: number;
|
|
14
|
+
/**
|
|
15
|
+
* Primary cache store.
|
|
16
|
+
*/
|
|
8
17
|
primary?: Keyv;
|
|
18
|
+
/**
|
|
19
|
+
* Secondary cache store. Learn more about the secondary cache store in the cacheable documentation.
|
|
20
|
+
* [storage-tiering-and-caching](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching)
|
|
21
|
+
*/
|
|
9
22
|
secondary?: Keyv;
|
|
10
23
|
};
|
|
11
24
|
declare class NodeCacheStore {
|
|
12
25
|
private _maxKeys;
|
|
13
26
|
private readonly _cache;
|
|
14
27
|
constructor(options?: NodeCacheStoreOptions);
|
|
28
|
+
/**
|
|
29
|
+
* Cacheable instance.
|
|
30
|
+
* @returns {Cacheable}
|
|
31
|
+
* @readonly
|
|
32
|
+
*/
|
|
15
33
|
get cache(): Cacheable;
|
|
34
|
+
/**
|
|
35
|
+
* Time to live in milliseconds.
|
|
36
|
+
* @returns {number | string | undefined}
|
|
37
|
+
* @readonly
|
|
38
|
+
*/
|
|
16
39
|
get ttl(): number | string | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Time to live in milliseconds.
|
|
42
|
+
* @param {number | string | undefined} ttl
|
|
43
|
+
*/
|
|
17
44
|
set ttl(ttl: number | string | undefined);
|
|
45
|
+
/**
|
|
46
|
+
* Primary cache store.
|
|
47
|
+
* @returns {Keyv}
|
|
48
|
+
* @readonly
|
|
49
|
+
*/
|
|
18
50
|
get primary(): Keyv;
|
|
51
|
+
/**
|
|
52
|
+
* Primary cache store.
|
|
53
|
+
* @param {Keyv} primary
|
|
54
|
+
*/
|
|
19
55
|
set primary(primary: Keyv);
|
|
56
|
+
/**
|
|
57
|
+
* Secondary cache store. Learn more about the secondary cache store in the
|
|
58
|
+
* [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
|
|
59
|
+
* @returns {Keyv | undefined}
|
|
60
|
+
*/
|
|
20
61
|
get secondary(): Keyv | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Secondary cache store. Learn more about the secondary cache store in the
|
|
64
|
+
* [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
|
|
65
|
+
* @param {Keyv | undefined} secondary
|
|
66
|
+
*/
|
|
21
67
|
set secondary(secondary: Keyv | undefined);
|
|
68
|
+
/**
|
|
69
|
+
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
|
|
70
|
+
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
71
|
+
* @returns {number}
|
|
72
|
+
* @readonly
|
|
73
|
+
*/
|
|
22
74
|
get maxKeys(): number;
|
|
75
|
+
/**
|
|
76
|
+
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
|
|
77
|
+
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
78
|
+
* @param {number} maxKeys
|
|
79
|
+
*/
|
|
23
80
|
set maxKeys(maxKeys: number);
|
|
81
|
+
/**
|
|
82
|
+
* Set a key/value pair in the cache.
|
|
83
|
+
* @param {string | number} key
|
|
84
|
+
* @param {any} value
|
|
85
|
+
* @param {number} [ttl]
|
|
86
|
+
* @returns {boolean}
|
|
87
|
+
*/
|
|
24
88
|
set(key: string | number, value: any, ttl?: number): Promise<boolean>;
|
|
89
|
+
/**
|
|
90
|
+
* Set multiple key/value pairs in the cache.
|
|
91
|
+
* @param {NodeCacheItem[]} list
|
|
92
|
+
* @returns {void}
|
|
93
|
+
*/
|
|
25
94
|
mset(list: NodeCacheItem[]): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Get a value from the cache.
|
|
97
|
+
* @param {string | number} key
|
|
98
|
+
* @returns {any | undefined}
|
|
99
|
+
*/
|
|
26
100
|
get<T>(key: string | number): Promise<T | undefined>;
|
|
101
|
+
/**
|
|
102
|
+
* Get multiple values from the cache.
|
|
103
|
+
* @param {Array<string | number>} keys
|
|
104
|
+
* @returns {Record<string, any | undefined>}
|
|
105
|
+
*/
|
|
27
106
|
mget<T>(keys: Array<string | number>): Promise<Record<string, T | undefined>>;
|
|
107
|
+
/**
|
|
108
|
+
* Delete a key from the cache.
|
|
109
|
+
* @param {string | number} key
|
|
110
|
+
* @returns {boolean}
|
|
111
|
+
*/
|
|
28
112
|
del(key: string | number): Promise<boolean>;
|
|
113
|
+
/**
|
|
114
|
+
* Delete multiple keys from the cache.
|
|
115
|
+
* @param {Array<string | number>} keys
|
|
116
|
+
* @returns {boolean}
|
|
117
|
+
*/
|
|
29
118
|
mdel(keys: Array<string | number>): Promise<boolean>;
|
|
119
|
+
/**
|
|
120
|
+
* Clear the cache.
|
|
121
|
+
* @returns {void}
|
|
122
|
+
*/
|
|
30
123
|
clear(): Promise<void>;
|
|
124
|
+
/**
|
|
125
|
+
* Check if a key exists in the cache.
|
|
126
|
+
* @param {string | number} key
|
|
127
|
+
* @returns {boolean}
|
|
128
|
+
*/
|
|
31
129
|
setTtl(key: string | number, ttl?: number): Promise<boolean>;
|
|
130
|
+
/**
|
|
131
|
+
* Check if a key exists in the cache. If it does exist it will get the value and delete the item from the cache.
|
|
132
|
+
* @param {string | number} key
|
|
133
|
+
* @returns {any | undefined}
|
|
134
|
+
*/
|
|
32
135
|
take<T>(key: string | number): Promise<T | undefined>;
|
|
136
|
+
/**
|
|
137
|
+
* Disconnect from the cache.
|
|
138
|
+
* @returns {void}
|
|
139
|
+
*/
|
|
33
140
|
disconnect(): Promise<void>;
|
|
34
141
|
}
|
|
35
142
|
|
|
36
143
|
type NodeCacheOptions = {
|
|
144
|
+
/**
|
|
145
|
+
* The standard ttl as number in seconds for every generated cache element. 0 = unlimited
|
|
146
|
+
*/
|
|
37
147
|
stdTTL?: number;
|
|
148
|
+
/**
|
|
149
|
+
* The interval to check for expired items in seconds. Default is 600 = 5 minutes
|
|
150
|
+
*/
|
|
38
151
|
checkperiod?: number;
|
|
152
|
+
/**
|
|
153
|
+
* Clones the returned items via get functions. Default is true.
|
|
154
|
+
*/
|
|
39
155
|
useClones?: boolean;
|
|
156
|
+
/**
|
|
157
|
+
* Delete all expired items at the set interval. Default is true.
|
|
158
|
+
*/
|
|
40
159
|
deleteOnExpire?: boolean;
|
|
160
|
+
/**
|
|
161
|
+
* The maximum number of keys that will be stored in the cache. Default is -1 = unlimited
|
|
162
|
+
* If the limit is reached, it will no longer add any new items until some expire.
|
|
163
|
+
*/
|
|
41
164
|
maxKeys?: number;
|
|
42
165
|
};
|
|
43
166
|
type NodeCacheItem = {
|
|
167
|
+
/**
|
|
168
|
+
* The key of the item
|
|
169
|
+
*/
|
|
44
170
|
key: string | number;
|
|
171
|
+
/**
|
|
172
|
+
* The value of the item
|
|
173
|
+
*/
|
|
45
174
|
value: unknown;
|
|
175
|
+
/**
|
|
176
|
+
* The ttl of the item in seconds. 0 = unlimited
|
|
177
|
+
*/
|
|
46
178
|
ttl?: number;
|
|
47
179
|
};
|
|
48
180
|
declare enum NodeCacheErrors {
|
|
@@ -52,10 +184,25 @@ declare enum NodeCacheErrors {
|
|
|
52
184
|
ETTLTYPE = "The ttl argument has to be a number."
|
|
53
185
|
}
|
|
54
186
|
type NodeCacheStats = {
|
|
187
|
+
/**
|
|
188
|
+
* The number of keys stored in the cache
|
|
189
|
+
*/
|
|
55
190
|
keys: number;
|
|
191
|
+
/**
|
|
192
|
+
* The number of hits
|
|
193
|
+
*/
|
|
56
194
|
hits: number;
|
|
195
|
+
/**
|
|
196
|
+
* The number of misses
|
|
197
|
+
*/
|
|
57
198
|
misses: number;
|
|
199
|
+
/**
|
|
200
|
+
* The global key size count in approximately bytes
|
|
201
|
+
*/
|
|
58
202
|
ksize: number;
|
|
203
|
+
/**
|
|
204
|
+
* The global value size count in approximately bytes
|
|
205
|
+
*/
|
|
59
206
|
vsize: number;
|
|
60
207
|
};
|
|
61
208
|
declare class NodeCache extends eventemitter {
|
|
@@ -65,21 +212,102 @@ declare class NodeCache extends eventemitter {
|
|
|
65
212
|
private readonly _cacheable;
|
|
66
213
|
private intervalId;
|
|
67
214
|
constructor(options?: NodeCacheOptions);
|
|
215
|
+
/**
|
|
216
|
+
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
217
|
+
* @param {string | number} key - it will convert the key to a string
|
|
218
|
+
* @param {any} value
|
|
219
|
+
* @param {number} [ttl] - this is in seconds and undefined will use the default ttl
|
|
220
|
+
* @returns {boolean}
|
|
221
|
+
*/
|
|
68
222
|
set(key: string | number, value: any, ttl?: number): boolean;
|
|
223
|
+
/**
|
|
224
|
+
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
225
|
+
* @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
|
|
226
|
+
* @returns {boolean}
|
|
227
|
+
*/
|
|
69
228
|
mset(data: NodeCacheItem[]): boolean;
|
|
229
|
+
/**
|
|
230
|
+
* Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
|
|
231
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
232
|
+
* @returns {T} the value or undefined
|
|
233
|
+
*/
|
|
70
234
|
get<T>(key: string | number): any;
|
|
235
|
+
/**
|
|
236
|
+
* Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
|
|
237
|
+
* If the value was found it returns an object with the key value pair.
|
|
238
|
+
* @param {Array<string | number} keys an array of keys
|
|
239
|
+
* @returns {Record<string, unknown>} an object with the key as a property and the value as the value
|
|
240
|
+
*/
|
|
71
241
|
mget<T>(keys: Array<string | number>): Record<string, unknown>;
|
|
72
|
-
|
|
242
|
+
/**
|
|
243
|
+
* Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key).
|
|
244
|
+
* Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
|
|
245
|
+
* @param {string | number} key
|
|
246
|
+
* @returns {T | undefined} the value or undefined
|
|
247
|
+
*/
|
|
248
|
+
take<T>(key: string | number): T | undefined;
|
|
249
|
+
/**
|
|
250
|
+
* Delete a key. Returns the number of deleted entries. A delete will never fail.
|
|
251
|
+
* @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.
|
|
252
|
+
* @returns {number} if it was successful it will return the count that was deleted
|
|
253
|
+
*/
|
|
73
254
|
del(key: string | number | Array<string | number>): number;
|
|
255
|
+
/**
|
|
256
|
+
* Delete all keys in Array that exist. Returns the number of deleted entries.
|
|
257
|
+
* @param {Array<string | number>} keys an array of keys
|
|
258
|
+
* @returns {number} the count of deleted keys
|
|
259
|
+
*/
|
|
74
260
|
mdel(keys: Array<string | number>): number;
|
|
261
|
+
/**
|
|
262
|
+
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
263
|
+
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
264
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
265
|
+
* @param {number} [ttl] the ttl in seconds
|
|
266
|
+
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
267
|
+
*/
|
|
75
268
|
ttl(key: string | number, ttl?: number): boolean;
|
|
269
|
+
/**
|
|
270
|
+
* Receive the ttl of a key.
|
|
271
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
272
|
+
* @returns {number | undefined} 0 if this key has no ttl, undefined if this key is not in the cache,
|
|
273
|
+
* a timestamp in ms representing the time at which this key will expire
|
|
274
|
+
*/
|
|
76
275
|
getTtl(key: string | number): number | undefined;
|
|
276
|
+
/**
|
|
277
|
+
* Returns an array of all existing keys. [ "all", "my", "keys", "foo", "bar" ]
|
|
278
|
+
* @returns {string[]} an array of all keys
|
|
279
|
+
*/
|
|
77
280
|
keys(): string[];
|
|
281
|
+
/**
|
|
282
|
+
* Returns boolean indicating if the key is cached.
|
|
283
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
284
|
+
* @returns {boolean} true if the key is cached
|
|
285
|
+
*/
|
|
78
286
|
has(key: string | number): boolean;
|
|
287
|
+
/**
|
|
288
|
+
* Gets the stats of the cache
|
|
289
|
+
* @returns {NodeCacheStats} the stats of the cache
|
|
290
|
+
*/
|
|
79
291
|
getStats(): NodeCacheStats;
|
|
292
|
+
/**
|
|
293
|
+
* Flush the whole data.
|
|
294
|
+
* @returns {void}
|
|
295
|
+
*/
|
|
80
296
|
flushAll(): void;
|
|
297
|
+
/**
|
|
298
|
+
* Flush the stats.
|
|
299
|
+
* @returns {void}
|
|
300
|
+
*/
|
|
81
301
|
flushStats(): void;
|
|
302
|
+
/**
|
|
303
|
+
* Close the cache. This will clear the interval timeout which is set on check period option.
|
|
304
|
+
* @returns {void}
|
|
305
|
+
*/
|
|
82
306
|
close(): void;
|
|
307
|
+
/**
|
|
308
|
+
* Get the interval id
|
|
309
|
+
* @returns {number | NodeJS.Timeout} the interval id
|
|
310
|
+
*/
|
|
83
311
|
getIntervalId(): number | NodeJS.Timeout;
|
|
84
312
|
private formatKey;
|
|
85
313
|
private getExpirationTimestamp;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,46 +3,178 @@ import { Cacheable } from 'cacheable';
|
|
|
3
3
|
import { Keyv } from 'keyv';
|
|
4
4
|
|
|
5
5
|
type NodeCacheStoreOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Time to live in milliseconds. This is a breaking change from the original NodeCache.
|
|
8
|
+
*/
|
|
6
9
|
ttl?: number | string;
|
|
10
|
+
/**
|
|
11
|
+
* 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.
|
|
12
|
+
*/
|
|
7
13
|
maxKeys?: number;
|
|
14
|
+
/**
|
|
15
|
+
* Primary cache store.
|
|
16
|
+
*/
|
|
8
17
|
primary?: Keyv;
|
|
18
|
+
/**
|
|
19
|
+
* Secondary cache store. Learn more about the secondary cache store in the cacheable documentation.
|
|
20
|
+
* [storage-tiering-and-caching](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching)
|
|
21
|
+
*/
|
|
9
22
|
secondary?: Keyv;
|
|
10
23
|
};
|
|
11
24
|
declare class NodeCacheStore {
|
|
12
25
|
private _maxKeys;
|
|
13
26
|
private readonly _cache;
|
|
14
27
|
constructor(options?: NodeCacheStoreOptions);
|
|
28
|
+
/**
|
|
29
|
+
* Cacheable instance.
|
|
30
|
+
* @returns {Cacheable}
|
|
31
|
+
* @readonly
|
|
32
|
+
*/
|
|
15
33
|
get cache(): Cacheable;
|
|
34
|
+
/**
|
|
35
|
+
* Time to live in milliseconds.
|
|
36
|
+
* @returns {number | string | undefined}
|
|
37
|
+
* @readonly
|
|
38
|
+
*/
|
|
16
39
|
get ttl(): number | string | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Time to live in milliseconds.
|
|
42
|
+
* @param {number | string | undefined} ttl
|
|
43
|
+
*/
|
|
17
44
|
set ttl(ttl: number | string | undefined);
|
|
45
|
+
/**
|
|
46
|
+
* Primary cache store.
|
|
47
|
+
* @returns {Keyv}
|
|
48
|
+
* @readonly
|
|
49
|
+
*/
|
|
18
50
|
get primary(): Keyv;
|
|
51
|
+
/**
|
|
52
|
+
* Primary cache store.
|
|
53
|
+
* @param {Keyv} primary
|
|
54
|
+
*/
|
|
19
55
|
set primary(primary: Keyv);
|
|
56
|
+
/**
|
|
57
|
+
* Secondary cache store. Learn more about the secondary cache store in the
|
|
58
|
+
* [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
|
|
59
|
+
* @returns {Keyv | undefined}
|
|
60
|
+
*/
|
|
20
61
|
get secondary(): Keyv | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Secondary cache store. Learn more about the secondary cache store in the
|
|
64
|
+
* [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
|
|
65
|
+
* @param {Keyv | undefined} secondary
|
|
66
|
+
*/
|
|
21
67
|
set secondary(secondary: Keyv | undefined);
|
|
68
|
+
/**
|
|
69
|
+
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
|
|
70
|
+
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
71
|
+
* @returns {number}
|
|
72
|
+
* @readonly
|
|
73
|
+
*/
|
|
22
74
|
get maxKeys(): number;
|
|
75
|
+
/**
|
|
76
|
+
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
|
|
77
|
+
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
78
|
+
* @param {number} maxKeys
|
|
79
|
+
*/
|
|
23
80
|
set maxKeys(maxKeys: number);
|
|
81
|
+
/**
|
|
82
|
+
* Set a key/value pair in the cache.
|
|
83
|
+
* @param {string | number} key
|
|
84
|
+
* @param {any} value
|
|
85
|
+
* @param {number} [ttl]
|
|
86
|
+
* @returns {boolean}
|
|
87
|
+
*/
|
|
24
88
|
set(key: string | number, value: any, ttl?: number): Promise<boolean>;
|
|
89
|
+
/**
|
|
90
|
+
* Set multiple key/value pairs in the cache.
|
|
91
|
+
* @param {NodeCacheItem[]} list
|
|
92
|
+
* @returns {void}
|
|
93
|
+
*/
|
|
25
94
|
mset(list: NodeCacheItem[]): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Get a value from the cache.
|
|
97
|
+
* @param {string | number} key
|
|
98
|
+
* @returns {any | undefined}
|
|
99
|
+
*/
|
|
26
100
|
get<T>(key: string | number): Promise<T | undefined>;
|
|
101
|
+
/**
|
|
102
|
+
* Get multiple values from the cache.
|
|
103
|
+
* @param {Array<string | number>} keys
|
|
104
|
+
* @returns {Record<string, any | undefined>}
|
|
105
|
+
*/
|
|
27
106
|
mget<T>(keys: Array<string | number>): Promise<Record<string, T | undefined>>;
|
|
107
|
+
/**
|
|
108
|
+
* Delete a key from the cache.
|
|
109
|
+
* @param {string | number} key
|
|
110
|
+
* @returns {boolean}
|
|
111
|
+
*/
|
|
28
112
|
del(key: string | number): Promise<boolean>;
|
|
113
|
+
/**
|
|
114
|
+
* Delete multiple keys from the cache.
|
|
115
|
+
* @param {Array<string | number>} keys
|
|
116
|
+
* @returns {boolean}
|
|
117
|
+
*/
|
|
29
118
|
mdel(keys: Array<string | number>): Promise<boolean>;
|
|
119
|
+
/**
|
|
120
|
+
* Clear the cache.
|
|
121
|
+
* @returns {void}
|
|
122
|
+
*/
|
|
30
123
|
clear(): Promise<void>;
|
|
124
|
+
/**
|
|
125
|
+
* Check if a key exists in the cache.
|
|
126
|
+
* @param {string | number} key
|
|
127
|
+
* @returns {boolean}
|
|
128
|
+
*/
|
|
31
129
|
setTtl(key: string | number, ttl?: number): Promise<boolean>;
|
|
130
|
+
/**
|
|
131
|
+
* Check if a key exists in the cache. If it does exist it will get the value and delete the item from the cache.
|
|
132
|
+
* @param {string | number} key
|
|
133
|
+
* @returns {any | undefined}
|
|
134
|
+
*/
|
|
32
135
|
take<T>(key: string | number): Promise<T | undefined>;
|
|
136
|
+
/**
|
|
137
|
+
* Disconnect from the cache.
|
|
138
|
+
* @returns {void}
|
|
139
|
+
*/
|
|
33
140
|
disconnect(): Promise<void>;
|
|
34
141
|
}
|
|
35
142
|
|
|
36
143
|
type NodeCacheOptions = {
|
|
144
|
+
/**
|
|
145
|
+
* The standard ttl as number in seconds for every generated cache element. 0 = unlimited
|
|
146
|
+
*/
|
|
37
147
|
stdTTL?: number;
|
|
148
|
+
/**
|
|
149
|
+
* The interval to check for expired items in seconds. Default is 600 = 5 minutes
|
|
150
|
+
*/
|
|
38
151
|
checkperiod?: number;
|
|
152
|
+
/**
|
|
153
|
+
* Clones the returned items via get functions. Default is true.
|
|
154
|
+
*/
|
|
39
155
|
useClones?: boolean;
|
|
156
|
+
/**
|
|
157
|
+
* Delete all expired items at the set interval. Default is true.
|
|
158
|
+
*/
|
|
40
159
|
deleteOnExpire?: boolean;
|
|
160
|
+
/**
|
|
161
|
+
* The maximum number of keys that will be stored in the cache. Default is -1 = unlimited
|
|
162
|
+
* If the limit is reached, it will no longer add any new items until some expire.
|
|
163
|
+
*/
|
|
41
164
|
maxKeys?: number;
|
|
42
165
|
};
|
|
43
166
|
type NodeCacheItem = {
|
|
167
|
+
/**
|
|
168
|
+
* The key of the item
|
|
169
|
+
*/
|
|
44
170
|
key: string | number;
|
|
171
|
+
/**
|
|
172
|
+
* The value of the item
|
|
173
|
+
*/
|
|
45
174
|
value: unknown;
|
|
175
|
+
/**
|
|
176
|
+
* The ttl of the item in seconds. 0 = unlimited
|
|
177
|
+
*/
|
|
46
178
|
ttl?: number;
|
|
47
179
|
};
|
|
48
180
|
declare enum NodeCacheErrors {
|
|
@@ -52,10 +184,25 @@ declare enum NodeCacheErrors {
|
|
|
52
184
|
ETTLTYPE = "The ttl argument has to be a number."
|
|
53
185
|
}
|
|
54
186
|
type NodeCacheStats = {
|
|
187
|
+
/**
|
|
188
|
+
* The number of keys stored in the cache
|
|
189
|
+
*/
|
|
55
190
|
keys: number;
|
|
191
|
+
/**
|
|
192
|
+
* The number of hits
|
|
193
|
+
*/
|
|
56
194
|
hits: number;
|
|
195
|
+
/**
|
|
196
|
+
* The number of misses
|
|
197
|
+
*/
|
|
57
198
|
misses: number;
|
|
199
|
+
/**
|
|
200
|
+
* The global key size count in approximately bytes
|
|
201
|
+
*/
|
|
58
202
|
ksize: number;
|
|
203
|
+
/**
|
|
204
|
+
* The global value size count in approximately bytes
|
|
205
|
+
*/
|
|
59
206
|
vsize: number;
|
|
60
207
|
};
|
|
61
208
|
declare class NodeCache extends eventemitter {
|
|
@@ -65,21 +212,102 @@ declare class NodeCache extends eventemitter {
|
|
|
65
212
|
private readonly _cacheable;
|
|
66
213
|
private intervalId;
|
|
67
214
|
constructor(options?: NodeCacheOptions);
|
|
215
|
+
/**
|
|
216
|
+
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
217
|
+
* @param {string | number} key - it will convert the key to a string
|
|
218
|
+
* @param {any} value
|
|
219
|
+
* @param {number} [ttl] - this is in seconds and undefined will use the default ttl
|
|
220
|
+
* @returns {boolean}
|
|
221
|
+
*/
|
|
68
222
|
set(key: string | number, value: any, ttl?: number): boolean;
|
|
223
|
+
/**
|
|
224
|
+
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
225
|
+
* @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
|
|
226
|
+
* @returns {boolean}
|
|
227
|
+
*/
|
|
69
228
|
mset(data: NodeCacheItem[]): boolean;
|
|
229
|
+
/**
|
|
230
|
+
* Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
|
|
231
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
232
|
+
* @returns {T} the value or undefined
|
|
233
|
+
*/
|
|
70
234
|
get<T>(key: string | number): any;
|
|
235
|
+
/**
|
|
236
|
+
* Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
|
|
237
|
+
* If the value was found it returns an object with the key value pair.
|
|
238
|
+
* @param {Array<string | number} keys an array of keys
|
|
239
|
+
* @returns {Record<string, unknown>} an object with the key as a property and the value as the value
|
|
240
|
+
*/
|
|
71
241
|
mget<T>(keys: Array<string | number>): Record<string, unknown>;
|
|
72
|
-
|
|
242
|
+
/**
|
|
243
|
+
* Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key).
|
|
244
|
+
* Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
|
|
245
|
+
* @param {string | number} key
|
|
246
|
+
* @returns {T | undefined} the value or undefined
|
|
247
|
+
*/
|
|
248
|
+
take<T>(key: string | number): T | undefined;
|
|
249
|
+
/**
|
|
250
|
+
* Delete a key. Returns the number of deleted entries. A delete will never fail.
|
|
251
|
+
* @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.
|
|
252
|
+
* @returns {number} if it was successful it will return the count that was deleted
|
|
253
|
+
*/
|
|
73
254
|
del(key: string | number | Array<string | number>): number;
|
|
255
|
+
/**
|
|
256
|
+
* Delete all keys in Array that exist. Returns the number of deleted entries.
|
|
257
|
+
* @param {Array<string | number>} keys an array of keys
|
|
258
|
+
* @returns {number} the count of deleted keys
|
|
259
|
+
*/
|
|
74
260
|
mdel(keys: Array<string | number>): number;
|
|
261
|
+
/**
|
|
262
|
+
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
263
|
+
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
264
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
265
|
+
* @param {number} [ttl] the ttl in seconds
|
|
266
|
+
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
267
|
+
*/
|
|
75
268
|
ttl(key: string | number, ttl?: number): boolean;
|
|
269
|
+
/**
|
|
270
|
+
* Receive the ttl of a key.
|
|
271
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
272
|
+
* @returns {number | undefined} 0 if this key has no ttl, undefined if this key is not in the cache,
|
|
273
|
+
* a timestamp in ms representing the time at which this key will expire
|
|
274
|
+
*/
|
|
76
275
|
getTtl(key: string | number): number | undefined;
|
|
276
|
+
/**
|
|
277
|
+
* Returns an array of all existing keys. [ "all", "my", "keys", "foo", "bar" ]
|
|
278
|
+
* @returns {string[]} an array of all keys
|
|
279
|
+
*/
|
|
77
280
|
keys(): string[];
|
|
281
|
+
/**
|
|
282
|
+
* Returns boolean indicating if the key is cached.
|
|
283
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
284
|
+
* @returns {boolean} true if the key is cached
|
|
285
|
+
*/
|
|
78
286
|
has(key: string | number): boolean;
|
|
287
|
+
/**
|
|
288
|
+
* Gets the stats of the cache
|
|
289
|
+
* @returns {NodeCacheStats} the stats of the cache
|
|
290
|
+
*/
|
|
79
291
|
getStats(): NodeCacheStats;
|
|
292
|
+
/**
|
|
293
|
+
* Flush the whole data.
|
|
294
|
+
* @returns {void}
|
|
295
|
+
*/
|
|
80
296
|
flushAll(): void;
|
|
297
|
+
/**
|
|
298
|
+
* Flush the stats.
|
|
299
|
+
* @returns {void}
|
|
300
|
+
*/
|
|
81
301
|
flushStats(): void;
|
|
302
|
+
/**
|
|
303
|
+
* Close the cache. This will clear the interval timeout which is set on check period option.
|
|
304
|
+
* @returns {void}
|
|
305
|
+
*/
|
|
82
306
|
close(): void;
|
|
307
|
+
/**
|
|
308
|
+
* Get the interval id
|
|
309
|
+
* @returns {number | NodeJS.Timeout} the interval id
|
|
310
|
+
*/
|
|
83
311
|
getIntervalId(): number | NodeJS.Timeout;
|
|
84
312
|
private formatKey;
|
|
85
313
|
private getExpirationTimestamp;
|