@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/README.md +193 -11
- package/dist/index.cjs +789 -687
- package/dist/index.d.cts +418 -329
- package/dist/index.d.mts +435 -0
- package/dist/index.mjs +769 -0
- package/package.json +10 -10
- package/dist/index.d.ts +0 -346
- package/dist/index.js +0 -660
package/dist/index.js
DELETED
|
@@ -1,660 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import { Stats as Stats2, shorthandToTime } from "@cacheable/utils";
|
|
3
|
-
import { Hookified as Hookified2 } from "hookified";
|
|
4
|
-
|
|
5
|
-
// src/store.ts
|
|
6
|
-
import { Stats, shorthandToMilliseconds } from "@cacheable/utils";
|
|
7
|
-
import { Hookified } from "hookified";
|
|
8
|
-
import Keyv from "keyv";
|
|
9
|
-
var NodeCacheStore = class extends Hookified {
|
|
10
|
-
_keyv;
|
|
11
|
-
_ttl;
|
|
12
|
-
_stats = new Stats({ enabled: true });
|
|
13
|
-
constructor(options) {
|
|
14
|
-
super();
|
|
15
|
-
this._ttl = options?.ttl;
|
|
16
|
-
this._keyv = options?.store ?? new Keyv();
|
|
17
|
-
this._keyv.on("error", (error) => {
|
|
18
|
-
this.emit("error", error);
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Time to live in milliseconds.
|
|
23
|
-
* @returns {number | string | undefined}
|
|
24
|
-
* @readonly
|
|
25
|
-
*/
|
|
26
|
-
get ttl() {
|
|
27
|
-
return this._ttl;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Time to live in milliseconds.
|
|
31
|
-
* @param {number | string | undefined} ttl
|
|
32
|
-
*/
|
|
33
|
-
set ttl(ttl) {
|
|
34
|
-
this._ttl = ttl;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* The Keyv store instance.
|
|
38
|
-
* @returns {Keyv<T>}
|
|
39
|
-
* @readonly
|
|
40
|
-
*/
|
|
41
|
-
get store() {
|
|
42
|
-
return this._keyv;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Set a key/value pair in the cache.
|
|
46
|
-
* @param {string | number} key
|
|
47
|
-
* @param {T} value
|
|
48
|
-
* @param {number} [ttl]
|
|
49
|
-
* @returns {boolean}
|
|
50
|
-
*/
|
|
51
|
-
async set(key, value, ttl) {
|
|
52
|
-
const keyValue = key.toString();
|
|
53
|
-
const finalTtl = this.resolveTtl(ttl);
|
|
54
|
-
await this._keyv.set(keyValue, value, finalTtl);
|
|
55
|
-
this._stats.incrementKSize(keyValue);
|
|
56
|
-
this._stats.incrementVSize(value);
|
|
57
|
-
this._stats.incrementCount();
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Set multiple key/value pairs in the cache.
|
|
62
|
-
* @param {PartialNodeCacheItem[]} list
|
|
63
|
-
* @returns {void}
|
|
64
|
-
*/
|
|
65
|
-
async mset(list) {
|
|
66
|
-
for (const item of list) {
|
|
67
|
-
const keyValue = item.key.toString();
|
|
68
|
-
const finalTtl = this.resolveTtl(item.ttl);
|
|
69
|
-
await this._keyv.set(keyValue, item.value, finalTtl);
|
|
70
|
-
this._stats.incrementKSize(keyValue);
|
|
71
|
-
this._stats.incrementVSize(item.value);
|
|
72
|
-
this._stats.incrementCount();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Get a value from the cache.
|
|
77
|
-
* @param {string | number} key
|
|
78
|
-
* @returns {any | undefined}
|
|
79
|
-
*/
|
|
80
|
-
async get(key) {
|
|
81
|
-
const result = await this._keyv.get(key.toString());
|
|
82
|
-
if (result === void 0) {
|
|
83
|
-
this._stats.incrementMisses();
|
|
84
|
-
} else {
|
|
85
|
-
this._stats.incrementHits();
|
|
86
|
-
}
|
|
87
|
-
return result;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Get multiple values from the cache.
|
|
91
|
-
* @param {Array<string | number>} keys
|
|
92
|
-
* @returns {Record<string, any | undefined>}
|
|
93
|
-
*/
|
|
94
|
-
async mget(keys) {
|
|
95
|
-
const result = /* @__PURE__ */ Object.create(null);
|
|
96
|
-
for (const key of keys) {
|
|
97
|
-
const value = await this._keyv.get(key.toString());
|
|
98
|
-
if (value === void 0) {
|
|
99
|
-
this._stats.incrementMisses();
|
|
100
|
-
} else {
|
|
101
|
-
this._stats.incrementHits();
|
|
102
|
-
}
|
|
103
|
-
result[key.toString()] = value;
|
|
104
|
-
}
|
|
105
|
-
return result;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Delete a key from the cache.
|
|
109
|
-
* @param {string | number} key
|
|
110
|
-
* @returns {boolean}
|
|
111
|
-
*/
|
|
112
|
-
async del(key) {
|
|
113
|
-
const keyValue = key.toString();
|
|
114
|
-
const value = await this._keyv.get(keyValue);
|
|
115
|
-
const result = await this._keyv.delete(keyValue);
|
|
116
|
-
if (result) {
|
|
117
|
-
this._stats.decreaseKSize(keyValue);
|
|
118
|
-
if (value !== void 0) {
|
|
119
|
-
this._stats.decreaseVSize(value);
|
|
120
|
-
}
|
|
121
|
-
this._stats.decreaseCount();
|
|
122
|
-
}
|
|
123
|
-
return result;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Delete multiple keys from the cache.
|
|
127
|
-
* @param {Array<string | number>} keys
|
|
128
|
-
* @returns {boolean}
|
|
129
|
-
*/
|
|
130
|
-
async mdel(keys) {
|
|
131
|
-
for (const key of keys) {
|
|
132
|
-
const keyValue = key.toString();
|
|
133
|
-
const value = await this._keyv.get(keyValue);
|
|
134
|
-
this._stats.decreaseKSize(keyValue);
|
|
135
|
-
if (value !== void 0) {
|
|
136
|
-
this._stats.decreaseVSize(value);
|
|
137
|
-
}
|
|
138
|
-
this._stats.decreaseCount();
|
|
139
|
-
}
|
|
140
|
-
return this._keyv.delete(keys.map((key) => key.toString()));
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Clear the cache.
|
|
144
|
-
* @returns {void}
|
|
145
|
-
*/
|
|
146
|
-
async clear() {
|
|
147
|
-
await this._keyv.clear();
|
|
148
|
-
this._stats.resetStoreValues();
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Set the TTL of an existing key in the cache.
|
|
152
|
-
* @param {string | number} key
|
|
153
|
-
* @param {number | string} [ttl]
|
|
154
|
-
* @returns {boolean}
|
|
155
|
-
*/
|
|
156
|
-
async setTtl(key, ttl) {
|
|
157
|
-
const item = await this._keyv.get(key.toString());
|
|
158
|
-
if (item) {
|
|
159
|
-
const finalTtl = this.resolveTtl(ttl);
|
|
160
|
-
await this._keyv.set(key.toString(), item, finalTtl);
|
|
161
|
-
return true;
|
|
162
|
-
}
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* 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.
|
|
167
|
-
* @param {string | number} key
|
|
168
|
-
* @returns {T | undefined}
|
|
169
|
-
*/
|
|
170
|
-
async take(key) {
|
|
171
|
-
const keyValue = key.toString();
|
|
172
|
-
const result = await this._keyv.get(keyValue);
|
|
173
|
-
if (result !== void 0) {
|
|
174
|
-
await this._keyv.delete(keyValue);
|
|
175
|
-
this._stats.incrementHits();
|
|
176
|
-
this._stats.decreaseKSize(keyValue);
|
|
177
|
-
this._stats.decreaseVSize(result);
|
|
178
|
-
this._stats.decreaseCount();
|
|
179
|
-
} else {
|
|
180
|
-
this._stats.incrementMisses();
|
|
181
|
-
}
|
|
182
|
-
return result;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Gets the stats of the cache
|
|
186
|
-
* @returns {NodeCacheStats} the stats of the cache
|
|
187
|
-
*/
|
|
188
|
-
getStats() {
|
|
189
|
-
return {
|
|
190
|
-
keys: this._stats.count,
|
|
191
|
-
hits: this._stats.hits,
|
|
192
|
-
misses: this._stats.misses,
|
|
193
|
-
ksize: this._stats.ksize,
|
|
194
|
-
vsize: this._stats.vsize
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Flush the stats.
|
|
199
|
-
* @returns {void}
|
|
200
|
-
*/
|
|
201
|
-
flushStats() {
|
|
202
|
-
this._stats = new Stats({ enabled: true });
|
|
203
|
-
this.emit("flush_stats");
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Disconnect from the cache.
|
|
207
|
-
* @returns {void}
|
|
208
|
-
*/
|
|
209
|
-
async disconnect() {
|
|
210
|
-
await this._keyv.disconnect();
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Resolve the TTL to milliseconds.
|
|
214
|
-
* @param {number | string | undefined} ttl
|
|
215
|
-
* @returns {number | undefined}
|
|
216
|
-
*/
|
|
217
|
-
resolveTtl(ttl) {
|
|
218
|
-
const effectiveTtl = ttl ?? this._ttl;
|
|
219
|
-
if (effectiveTtl === void 0) {
|
|
220
|
-
return void 0;
|
|
221
|
-
}
|
|
222
|
-
if (typeof effectiveTtl === "string") {
|
|
223
|
-
return shorthandToMilliseconds(effectiveTtl);
|
|
224
|
-
}
|
|
225
|
-
if (effectiveTtl === 0) {
|
|
226
|
-
return void 0;
|
|
227
|
-
}
|
|
228
|
-
return effectiveTtl;
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
// src/index.ts
|
|
233
|
-
var NodeCacheErrors = /* @__PURE__ */ ((NodeCacheErrors2) => {
|
|
234
|
-
NodeCacheErrors2["ECACHEFULL"] = "Cache max keys amount exceeded";
|
|
235
|
-
NodeCacheErrors2["EKEYTYPE"] = "The key argument has to be of type `string` or `number`. Found: `__key`";
|
|
236
|
-
NodeCacheErrors2["EKEYSTYPE"] = "The keys argument has to be an array.";
|
|
237
|
-
NodeCacheErrors2["ETTLTYPE"] = "The ttl argument has to be a number or a string for shorthand ttl.";
|
|
238
|
-
return NodeCacheErrors2;
|
|
239
|
-
})(NodeCacheErrors || {});
|
|
240
|
-
var NodeCache = class extends Hookified2 {
|
|
241
|
-
options = {
|
|
242
|
-
stdTTL: 0,
|
|
243
|
-
checkperiod: 600,
|
|
244
|
-
useClones: true,
|
|
245
|
-
deleteOnExpire: true,
|
|
246
|
-
maxKeys: -1
|
|
247
|
-
};
|
|
248
|
-
store = /* @__PURE__ */ new Map();
|
|
249
|
-
_stats = new Stats2({ enabled: true });
|
|
250
|
-
intervalId = 0;
|
|
251
|
-
constructor(options) {
|
|
252
|
-
super({ throwOnHookError: false });
|
|
253
|
-
if (options) {
|
|
254
|
-
this.options = { ...this.options, ...options };
|
|
255
|
-
}
|
|
256
|
-
this.startInterval();
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
260
|
-
*
|
|
261
|
-
* TTL behavior:
|
|
262
|
-
* - `ttl > 0`: cache expires after the given number of seconds
|
|
263
|
-
* - `ttl === 0`: cache indefinitely (overrides stdTTL)
|
|
264
|
-
* - `ttl < 0`: store the value but it expires immediately on next access (matches original node-cache behavior)
|
|
265
|
-
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
266
|
-
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
267
|
-
*
|
|
268
|
-
* @param {string | number} key - it will convert the key to a string
|
|
269
|
-
* @param {T} value
|
|
270
|
-
* @param {number | string} [ttl] - TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
271
|
-
* @returns {boolean} true on success
|
|
272
|
-
* @throws {Error} If the `key` or `ttl` is of an invalid type.
|
|
273
|
-
*/
|
|
274
|
-
set(key, value, ttl) {
|
|
275
|
-
if (typeof key !== "string" && typeof key !== "number") {
|
|
276
|
-
throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
|
|
277
|
-
}
|
|
278
|
-
if (ttl !== void 0 && typeof ttl !== "number" && typeof ttl !== "string") {
|
|
279
|
-
throw this.createError("The ttl argument has to be a number or a string for shorthand ttl." /* ETTLTYPE */, this.formatKey(key));
|
|
280
|
-
}
|
|
281
|
-
const keyValue = this.formatKey(key);
|
|
282
|
-
let expirationTimestamp = 0;
|
|
283
|
-
if (this.isNegativeTtl(ttl)) {
|
|
284
|
-
expirationTimestamp = Math.max(
|
|
285
|
-
1,
|
|
286
|
-
this.getExpirationTimestamp(
|
|
287
|
-
typeof ttl === "string" ? Number(ttl) : ttl
|
|
288
|
-
)
|
|
289
|
-
);
|
|
290
|
-
} else if (ttl !== void 0 && (typeof ttl === "string" || ttl > 0)) {
|
|
291
|
-
expirationTimestamp = this.resolveExpiration(ttl);
|
|
292
|
-
} else if (ttl === void 0 && this.options.stdTTL !== void 0 && this.options.stdTTL !== 0) {
|
|
293
|
-
expirationTimestamp = this.resolveExpiration(this.options.stdTTL);
|
|
294
|
-
}
|
|
295
|
-
if (this.options.maxKeys) {
|
|
296
|
-
const { maxKeys } = this.options;
|
|
297
|
-
if (maxKeys > -1 && this.store.size >= maxKeys) {
|
|
298
|
-
throw this.createError("Cache max keys amount exceeded" /* ECACHEFULL */, this.formatKey(key));
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
this.store.set(keyValue, {
|
|
302
|
-
key: keyValue,
|
|
303
|
-
value,
|
|
304
|
-
ttl: expirationTimestamp
|
|
305
|
-
});
|
|
306
|
-
this.emit("set", keyValue, value, expirationTimestamp);
|
|
307
|
-
this._stats.incrementKSize(keyValue);
|
|
308
|
-
this._stats.incrementVSize(value);
|
|
309
|
-
this._stats.setCount(this.store.size);
|
|
310
|
-
return true;
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
314
|
-
*
|
|
315
|
-
* Each item follows the same TTL behavior as `set()`:
|
|
316
|
-
* - Positive TTL: expires after the given seconds
|
|
317
|
-
* - `0`: cache indefinitely
|
|
318
|
-
* - Negative TTL: stored but expires immediately on next access
|
|
319
|
-
* - Omitted: uses `stdTTL` from options
|
|
320
|
-
*
|
|
321
|
-
* @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
|
|
322
|
-
* @returns {boolean} true on success
|
|
323
|
-
* @throws {Error} If `data` is not an array, or if any item has an invalid key or ttl type.
|
|
324
|
-
*/
|
|
325
|
-
mset(data) {
|
|
326
|
-
if (!Array.isArray(data)) {
|
|
327
|
-
throw this.createError("The keys argument has to be an array." /* EKEYSTYPE */);
|
|
328
|
-
}
|
|
329
|
-
for (const item of data) {
|
|
330
|
-
this.set(item.key, item.value, item.ttl);
|
|
331
|
-
}
|
|
332
|
-
return true;
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
|
|
336
|
-
* @param {string | number} key if the key is a number it will convert it to a string
|
|
337
|
-
* @returns {T} the value or undefined
|
|
338
|
-
*/
|
|
339
|
-
get(key) {
|
|
340
|
-
const result = this.store.get(this.formatKey(key));
|
|
341
|
-
if (result) {
|
|
342
|
-
if (result.ttl > 0) {
|
|
343
|
-
if (result.ttl < Date.now()) {
|
|
344
|
-
this._stats.incrementMisses();
|
|
345
|
-
this.handleExpired(key, result);
|
|
346
|
-
return void 0;
|
|
347
|
-
}
|
|
348
|
-
this._stats.incrementHits();
|
|
349
|
-
if (this.options.useClones) {
|
|
350
|
-
return this.clone(result.value);
|
|
351
|
-
}
|
|
352
|
-
return result.value;
|
|
353
|
-
}
|
|
354
|
-
this._stats.incrementHits();
|
|
355
|
-
if (this.options.useClones) {
|
|
356
|
-
return this.clone(result.value);
|
|
357
|
-
}
|
|
358
|
-
return result.value;
|
|
359
|
-
}
|
|
360
|
-
this._stats.incrementMisses();
|
|
361
|
-
return void 0;
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
|
|
365
|
-
* If the value was found it returns an object with the key value pair.
|
|
366
|
-
* @param {Array<string | number} keys an array of keys
|
|
367
|
-
* @returns {Record<string, T | undefined>} an object with the key as a property and the value as the value
|
|
368
|
-
*/
|
|
369
|
-
mget(keys) {
|
|
370
|
-
const result = /* @__PURE__ */ Object.create(null);
|
|
371
|
-
for (const key of keys) {
|
|
372
|
-
const value = this.get(key);
|
|
373
|
-
if (value) {
|
|
374
|
-
result[this.formatKey(key)] = value;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
return result;
|
|
378
|
-
}
|
|
379
|
-
/**
|
|
380
|
-
* Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key).
|
|
381
|
-
* Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
|
|
382
|
-
* @param {string | number} key
|
|
383
|
-
* @returns {T | undefined} the value or undefined
|
|
384
|
-
*/
|
|
385
|
-
take(key) {
|
|
386
|
-
const result = this.get(key);
|
|
387
|
-
if (result) {
|
|
388
|
-
this.del(key);
|
|
389
|
-
if (this.options.useClones) {
|
|
390
|
-
return this.clone(result);
|
|
391
|
-
}
|
|
392
|
-
return result;
|
|
393
|
-
}
|
|
394
|
-
return void 0;
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* Delete a key. Returns the number of deleted entries. A delete will never fail.
|
|
398
|
-
* @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.
|
|
399
|
-
* @returns {number} if it was successful it will return the count that was deleted
|
|
400
|
-
*/
|
|
401
|
-
del(key) {
|
|
402
|
-
if (Array.isArray(key)) {
|
|
403
|
-
return this.mdel(key);
|
|
404
|
-
}
|
|
405
|
-
const result = this.store.get(this.formatKey(key));
|
|
406
|
-
if (result) {
|
|
407
|
-
const keyValue = this.formatKey(key);
|
|
408
|
-
this.store.delete(keyValue);
|
|
409
|
-
this.emit("del", keyValue, result.value);
|
|
410
|
-
this._stats.decreaseKSize(keyValue);
|
|
411
|
-
this._stats.decreaseVSize(result.value);
|
|
412
|
-
this._stats.setCount(this.store.size);
|
|
413
|
-
return 1;
|
|
414
|
-
}
|
|
415
|
-
return 0;
|
|
416
|
-
}
|
|
417
|
-
/**
|
|
418
|
-
* Delete all keys in Array that exist. Returns the number of deleted entries.
|
|
419
|
-
* @param {Array<string | number>} keys an array of keys
|
|
420
|
-
* @returns {number} the count of deleted keys
|
|
421
|
-
*/
|
|
422
|
-
mdel(keys) {
|
|
423
|
-
let result = 0;
|
|
424
|
-
for (const key of keys) {
|
|
425
|
-
result += this.del(key);
|
|
426
|
-
}
|
|
427
|
-
return result;
|
|
428
|
-
}
|
|
429
|
-
/**
|
|
430
|
-
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
431
|
-
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
432
|
-
*
|
|
433
|
-
* TTL behavior:
|
|
434
|
-
* - `ttl > 0`: key expires after the given number of seconds
|
|
435
|
-
* - `ttl === 0`: key lives indefinitely (overrides stdTTL)
|
|
436
|
-
* - `ttl < 0`: key expires immediately on next access (matches original node-cache behavior)
|
|
437
|
-
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
438
|
-
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
439
|
-
*
|
|
440
|
-
* @param {string | number} key if the key is a number it will convert it to a string
|
|
441
|
-
* @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
442
|
-
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
443
|
-
* @throws {Error} If the `ttl` is of an invalid type (must be a number or string).
|
|
444
|
-
*/
|
|
445
|
-
ttl(key, ttl) {
|
|
446
|
-
if (ttl !== void 0 && typeof ttl !== "number" && typeof ttl !== "string") {
|
|
447
|
-
throw this.createError("The ttl argument has to be a number or a string for shorthand ttl." /* ETTLTYPE */, this.formatKey(key));
|
|
448
|
-
}
|
|
449
|
-
const result = this.store.get(this.formatKey(key));
|
|
450
|
-
if (result) {
|
|
451
|
-
if (this.isNegativeTtl(ttl)) {
|
|
452
|
-
result.ttl = Math.max(
|
|
453
|
-
1,
|
|
454
|
-
this.getExpirationTimestamp(
|
|
455
|
-
typeof ttl === "string" ? Number(ttl) : ttl
|
|
456
|
-
)
|
|
457
|
-
);
|
|
458
|
-
} else if (ttl !== void 0 && (typeof ttl === "string" || ttl > 0)) {
|
|
459
|
-
result.ttl = this.resolveExpiration(ttl);
|
|
460
|
-
} else if (ttl === 0) {
|
|
461
|
-
result.ttl = 0;
|
|
462
|
-
} else if (this.options.stdTTL !== void 0 && this.options.stdTTL !== 0) {
|
|
463
|
-
result.ttl = this.resolveExpiration(this.options.stdTTL);
|
|
464
|
-
} else {
|
|
465
|
-
result.ttl = 0;
|
|
466
|
-
}
|
|
467
|
-
this.store.set(this.formatKey(key), result);
|
|
468
|
-
return true;
|
|
469
|
-
}
|
|
470
|
-
return false;
|
|
471
|
-
}
|
|
472
|
-
/**
|
|
473
|
-
* Receive the ttl of a key.
|
|
474
|
-
* @param {string | number} key if the key is a number it will convert it to a string
|
|
475
|
-
* @returns {number | undefined} 0 if this key has no ttl, undefined if this key is not in the cache,
|
|
476
|
-
* a timestamp in ms representing the time at which this key will expire
|
|
477
|
-
*/
|
|
478
|
-
getTtl(key) {
|
|
479
|
-
const result = this.store.get(this.formatKey(key));
|
|
480
|
-
if (result) {
|
|
481
|
-
if (result.ttl === 0) {
|
|
482
|
-
return 0;
|
|
483
|
-
}
|
|
484
|
-
return result.ttl;
|
|
485
|
-
}
|
|
486
|
-
return void 0;
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* Returns an array of all existing keys. [ "all", "my", "keys", "foo", "bar" ]
|
|
490
|
-
* @returns {string[]} an array of all keys
|
|
491
|
-
*/
|
|
492
|
-
keys() {
|
|
493
|
-
const result = [];
|
|
494
|
-
for (const key of this.store.keys()) {
|
|
495
|
-
result.push(key);
|
|
496
|
-
}
|
|
497
|
-
return result;
|
|
498
|
-
}
|
|
499
|
-
/**
|
|
500
|
-
* Returns boolean indicating if the key is cached. If the key has expired, it
|
|
501
|
-
* will return false and the key will be removed from the cache when
|
|
502
|
-
* `deleteOnExpire` is enabled (matches the original node-cache behavior).
|
|
503
|
-
* @param {string | number} key if the key is a number it will convert it to a string
|
|
504
|
-
* @returns {boolean} true if the key is cached and not expired
|
|
505
|
-
*/
|
|
506
|
-
has(key) {
|
|
507
|
-
const result = this.store.get(this.formatKey(key));
|
|
508
|
-
if (!result) {
|
|
509
|
-
return false;
|
|
510
|
-
}
|
|
511
|
-
if (result.ttl > 0 && result.ttl < Date.now()) {
|
|
512
|
-
this.handleExpired(key, result);
|
|
513
|
-
return false;
|
|
514
|
-
}
|
|
515
|
-
return true;
|
|
516
|
-
}
|
|
517
|
-
/**
|
|
518
|
-
* Gets the stats of the cache
|
|
519
|
-
* @returns {NodeCacheStats} the stats of the cache
|
|
520
|
-
*/
|
|
521
|
-
getStats() {
|
|
522
|
-
const stats = {
|
|
523
|
-
keys: this._stats.count,
|
|
524
|
-
hits: this._stats.hits,
|
|
525
|
-
misses: this._stats.misses,
|
|
526
|
-
ksize: this._stats.ksize,
|
|
527
|
-
vsize: this._stats.vsize
|
|
528
|
-
};
|
|
529
|
-
return stats;
|
|
530
|
-
}
|
|
531
|
-
/**
|
|
532
|
-
* Flush the whole data.
|
|
533
|
-
* @returns {void}
|
|
534
|
-
*/
|
|
535
|
-
flushAll() {
|
|
536
|
-
this.store.clear();
|
|
537
|
-
this.flushStats();
|
|
538
|
-
this.emit("flush");
|
|
539
|
-
}
|
|
540
|
-
/**
|
|
541
|
-
* Flush the stats.
|
|
542
|
-
* @returns {void}
|
|
543
|
-
*/
|
|
544
|
-
flushStats() {
|
|
545
|
-
this._stats = new Stats2({ enabled: true });
|
|
546
|
-
this.emit("flush_stats");
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* Close the cache. This will clear the interval timeout which is set on check period option.
|
|
550
|
-
* @returns {void}
|
|
551
|
-
*/
|
|
552
|
-
close() {
|
|
553
|
-
this.stopInterval();
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
|
-
* Get the interval id
|
|
557
|
-
* @returns {number | NodeJS.Timeout} the interval id
|
|
558
|
-
*/
|
|
559
|
-
getIntervalId() {
|
|
560
|
-
return this.intervalId;
|
|
561
|
-
}
|
|
562
|
-
startInterval() {
|
|
563
|
-
if (this.options.checkperiod && this.options.checkperiod > 0) {
|
|
564
|
-
const checkPeriodinSeconds = this.options.checkperiod * 1e3;
|
|
565
|
-
this.intervalId = setInterval(() => {
|
|
566
|
-
this.checkData();
|
|
567
|
-
}, checkPeriodinSeconds).unref();
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
|
-
this.intervalId = 0;
|
|
571
|
-
}
|
|
572
|
-
stopInterval() {
|
|
573
|
-
if (this.intervalId !== 0) {
|
|
574
|
-
clearInterval(this.intervalId);
|
|
575
|
-
this.intervalId = 0;
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
formatKey(key) {
|
|
579
|
-
return key.toString();
|
|
580
|
-
}
|
|
581
|
-
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
582
|
-
clone(value) {
|
|
583
|
-
if (value === null || value === void 0) {
|
|
584
|
-
return value;
|
|
585
|
-
}
|
|
586
|
-
if (typeof value !== "object") {
|
|
587
|
-
return value;
|
|
588
|
-
}
|
|
589
|
-
return structuredClone(value);
|
|
590
|
-
}
|
|
591
|
-
getExpirationTimestamp(ttlInSeconds) {
|
|
592
|
-
if (typeof ttlInSeconds === "string") {
|
|
593
|
-
return shorthandToTime(ttlInSeconds);
|
|
594
|
-
}
|
|
595
|
-
const currentTimestamp = Date.now();
|
|
596
|
-
const ttlInMilliseconds = ttlInSeconds * 1e3;
|
|
597
|
-
const expirationTimestamp = currentTimestamp + ttlInMilliseconds;
|
|
598
|
-
return expirationTimestamp;
|
|
599
|
-
}
|
|
600
|
-
/**
|
|
601
|
-
* Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the
|
|
602
|
-
* resolved timestamp is not in the future (e.g. "0s" or a zero-duration string).
|
|
603
|
-
*/
|
|
604
|
-
resolveExpiration(ttl) {
|
|
605
|
-
const timestamp = this.getExpirationTimestamp(ttl);
|
|
606
|
-
if (timestamp <= Date.now()) {
|
|
607
|
-
return 0;
|
|
608
|
-
}
|
|
609
|
-
return timestamp;
|
|
610
|
-
}
|
|
611
|
-
/**
|
|
612
|
-
* Checks whether a TTL value is negative. Handles both numbers and
|
|
613
|
-
* purely numeric strings (e.g. "-1").
|
|
614
|
-
*/
|
|
615
|
-
isNegativeTtl(ttl) {
|
|
616
|
-
if (typeof ttl === "number") {
|
|
617
|
-
return ttl < 0;
|
|
618
|
-
}
|
|
619
|
-
if (typeof ttl === "string") {
|
|
620
|
-
const num = Number(ttl);
|
|
621
|
-
if (!Number.isNaN(num) && num < 0) {
|
|
622
|
-
return true;
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
return false;
|
|
626
|
-
}
|
|
627
|
-
checkData() {
|
|
628
|
-
for (const [key, value] of this.store.entries()) {
|
|
629
|
-
if (value.ttl > 0 && value.ttl < Date.now()) {
|
|
630
|
-
this.handleExpired(key, value);
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
/**
|
|
635
|
-
* Handles expiration for a cache entry. Deletes the entry when
|
|
636
|
-
* `deleteOnExpire` is enabled and emits the "expired" event.
|
|
637
|
-
*/
|
|
638
|
-
handleExpired(key, entry) {
|
|
639
|
-
const keyValue = this.formatKey(key);
|
|
640
|
-
if (this.options.deleteOnExpire) {
|
|
641
|
-
this.del(key);
|
|
642
|
-
}
|
|
643
|
-
this.emit("expired", keyValue, entry.value);
|
|
644
|
-
}
|
|
645
|
-
createError(errorCode, key) {
|
|
646
|
-
let error = errorCode;
|
|
647
|
-
if (key) {
|
|
648
|
-
error = error.replace("__key", key);
|
|
649
|
-
}
|
|
650
|
-
return new Error(error);
|
|
651
|
-
}
|
|
652
|
-
};
|
|
653
|
-
var index_default = NodeCache;
|
|
654
|
-
export {
|
|
655
|
-
NodeCache,
|
|
656
|
-
NodeCacheErrors,
|
|
657
|
-
NodeCacheStore,
|
|
658
|
-
index_default as default
|
|
659
|
-
};
|
|
660
|
-
/* v8 ignore next -- @preserve */
|