@cacheable/node-cache 2.0.0 → 2.0.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 +32 -3
- package/dist/index.cjs +90 -18
- package/dist/index.d.cts +40 -4
- package/dist/index.d.ts +40 -4
- package/dist/index.js +90 -18
- package/package.json +7 -10
package/README.md
CHANGED
|
@@ -4,24 +4,25 @@
|
|
|
4
4
|
|
|
5
5
|
> Simple and Maintained fast Node.js caching
|
|
6
6
|
|
|
7
|
-
[](https://codecov.io/gh/jaredwray/cacheable)
|
|
7
|
+
[](https://codecov.io/gh/jaredwray/cacheable)
|
|
8
8
|
[](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
|
|
9
9
|
[](https://www.npmjs.com/package/@cacheable/node-cache)
|
|
10
10
|
[](https://www.npmjs.com/package/@cacheable/node-cache)
|
|
11
11
|
[](https://github.com/jaredwray/cacheable/blob/main/LICENSE)
|
|
12
12
|
|
|
13
|
-
`@cacheable/node-cache` is compatible with the [node-cache](https://www.npmjs.com/package/node-cache) package with regular maintenance and additional functionality (async/await and storage adapters)
|
|
13
|
+
`@cacheable/node-cache` is compatible with the [node-cache](https://www.npmjs.com/package/node-cache) package with regular maintenance and additional functionality (async/await and storage adapters) via `{NodeCacheStore}`. The only thing not implemented is the `enableLegacyCallbacks` option and functions. If you need them we are happy to take a PR to add them.
|
|
14
14
|
|
|
15
15
|
* Fully Compatible with `node-cache` using `{NodeCache}`
|
|
16
16
|
* Faster than the original `node-cache` package 🚀
|
|
17
|
+
* Storage Adapters via [Keyv](https://keyv.org)
|
|
17
18
|
* Async/Await functionality with `{NodeCacheStore}`
|
|
18
|
-
* Storage Adapters via [Keyv](https://keyv.org) with `{NodeCacheStore}`
|
|
19
19
|
* Lightweight - uses `@cacheable/utils` for utilities
|
|
20
20
|
* Maintained and Updated Regularly! 🎉
|
|
21
21
|
|
|
22
22
|
# Table of Contents
|
|
23
23
|
* [Getting Started](#getting-started)
|
|
24
24
|
* [Basic Usage](#basic-usage)
|
|
25
|
+
* [Breaking Changes from v1 to v2](#breaking-changes-from-v1-to-v2)
|
|
25
26
|
* [NodeCache Performance](#nodecache-performance)
|
|
26
27
|
* [NodeCache API](#nodecache-api)
|
|
27
28
|
* [NodeCacheStore](#nodecachestore)
|
|
@@ -71,6 +72,34 @@ cache.set('foo', 'bar');
|
|
|
71
72
|
cache.get('foo'); // 'bar'
|
|
72
73
|
```
|
|
73
74
|
|
|
75
|
+
# Breaking Changes from v1 to v2
|
|
76
|
+
|
|
77
|
+
The main `NodeCache` class API has not changed and remains fully compatible. The primary internal change is that it now uses Keyv as the underlying store.
|
|
78
|
+
|
|
79
|
+
## NodeCacheStore Changes
|
|
80
|
+
|
|
81
|
+
### Removed `cache` Property
|
|
82
|
+
- **V1**: `nodeCache.cache` returned a `Cacheable` instance
|
|
83
|
+
- **V2**: Use `nodeCache.store` which returns a `Keyv` instance
|
|
84
|
+
|
|
85
|
+
### Removed Storage Tiering (primary/secondary)
|
|
86
|
+
- **V1**: Supported `primary` and `secondary` store options for multi-tier caching
|
|
87
|
+
- **V2**: Uses single `store` option only
|
|
88
|
+
|
|
89
|
+
**Migration:**
|
|
90
|
+
```javascript
|
|
91
|
+
// V1
|
|
92
|
+
const cache = new NodeCacheStore({ primary: keyv1, secondary: keyv2 });
|
|
93
|
+
|
|
94
|
+
// V2 - use single store
|
|
95
|
+
const cache = new NodeCacheStore({ store: keyv });
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
If you need storage tiering functionality, use the `cacheable` package instead which supports primary and secondary stores.
|
|
99
|
+
|
|
100
|
+
### Internal Dependency Change
|
|
101
|
+
- V2 uses `@cacheable/utils` instead of the `cacheable` package for a lighter footprint
|
|
102
|
+
|
|
74
103
|
# NodeCache Performance
|
|
75
104
|
|
|
76
105
|
The performance is comparable if not faster to the original `node-cache` package, but with additional features and improvements.
|
package/dist/index.cjs
CHANGED
|
@@ -285,29 +285,40 @@ var NodeCache = class extends import_hookified2.Hookified {
|
|
|
285
285
|
}
|
|
286
286
|
/**
|
|
287
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
|
+
*
|
|
288
296
|
* @param {string | number} key - it will convert the key to a string
|
|
289
297
|
* @param {T} value
|
|
290
|
-
* @param {number | string} [ttl] -
|
|
291
|
-
* @returns {boolean}
|
|
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.
|
|
292
301
|
*/
|
|
293
|
-
set(key, value, ttl
|
|
302
|
+
set(key, value, ttl) {
|
|
294
303
|
if (typeof key !== "string" && typeof key !== "number") {
|
|
295
304
|
throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
|
|
296
305
|
}
|
|
297
|
-
if (ttl && typeof ttl !== "number" && typeof ttl !== "string") {
|
|
306
|
+
if (ttl !== void 0 && typeof ttl !== "number" && typeof ttl !== "string") {
|
|
298
307
|
throw this.createError("The ttl argument has to be a number or a string for shorthand ttl." /* ETTLTYPE */, this.formatKey(key));
|
|
299
308
|
}
|
|
300
309
|
const keyValue = this.formatKey(key);
|
|
301
|
-
let ttlValue = 0;
|
|
302
|
-
if (this.options.stdTTL) {
|
|
303
|
-
ttlValue = this.getExpirationTimestamp(this.options.stdTTL);
|
|
304
|
-
}
|
|
305
|
-
if (ttl) {
|
|
306
|
-
ttlValue = this.getExpirationTimestamp(ttl);
|
|
307
|
-
}
|
|
308
310
|
let expirationTimestamp = 0;
|
|
309
|
-
if (
|
|
310
|
-
expirationTimestamp =
|
|
311
|
+
if (this.isNegativeTtl(ttl)) {
|
|
312
|
+
expirationTimestamp = Math.max(
|
|
313
|
+
1,
|
|
314
|
+
this.getExpirationTimestamp(
|
|
315
|
+
typeof ttl === "string" ? Number(ttl) : ttl
|
|
316
|
+
)
|
|
317
|
+
);
|
|
318
|
+
} else if (ttl !== void 0 && (typeof ttl === "string" || ttl > 0)) {
|
|
319
|
+
expirationTimestamp = this.resolveExpiration(ttl);
|
|
320
|
+
} else if (ttl === void 0 && this.options.stdTTL !== void 0 && this.options.stdTTL !== 0) {
|
|
321
|
+
expirationTimestamp = this.resolveExpiration(this.options.stdTTL);
|
|
311
322
|
}
|
|
312
323
|
if (this.options.maxKeys) {
|
|
313
324
|
const { maxKeys } = this.options;
|
|
@@ -320,7 +331,7 @@ var NodeCache = class extends import_hookified2.Hookified {
|
|
|
320
331
|
value,
|
|
321
332
|
ttl: expirationTimestamp
|
|
322
333
|
});
|
|
323
|
-
this.emit("set", keyValue, value,
|
|
334
|
+
this.emit("set", keyValue, value, expirationTimestamp);
|
|
324
335
|
this._stats.incrementKSize(keyValue);
|
|
325
336
|
this._stats.incrementVSize(value);
|
|
326
337
|
this._stats.setCount(this.store.size);
|
|
@@ -328,8 +339,16 @@ var NodeCache = class extends import_hookified2.Hookified {
|
|
|
328
339
|
}
|
|
329
340
|
/**
|
|
330
341
|
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
342
|
+
*
|
|
343
|
+
* Each item follows the same TTL behavior as `set()`:
|
|
344
|
+
* - Positive TTL: expires after the given seconds
|
|
345
|
+
* - `0`: cache indefinitely
|
|
346
|
+
* - Negative TTL: stored but expires immediately on next access
|
|
347
|
+
* - Omitted: uses `stdTTL` from options
|
|
348
|
+
*
|
|
331
349
|
* @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
|
|
332
|
-
* @returns {boolean}
|
|
350
|
+
* @returns {boolean} true on success
|
|
351
|
+
* @throws {Error} If `data` is not an array, or if any item has an invalid key or ttl type.
|
|
333
352
|
*/
|
|
334
353
|
mset(data) {
|
|
335
354
|
if (!Array.isArray(data)) {
|
|
@@ -441,15 +460,41 @@ var NodeCache = class extends import_hookified2.Hookified {
|
|
|
441
460
|
/**
|
|
442
461
|
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
443
462
|
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
463
|
+
*
|
|
464
|
+
* TTL behavior:
|
|
465
|
+
* - `ttl > 0`: key expires after the given number of seconds
|
|
466
|
+
* - `ttl === 0`: key lives indefinitely (overrides stdTTL)
|
|
467
|
+
* - `ttl < 0`: key expires immediately on next access (matches original node-cache behavior)
|
|
468
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
469
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
470
|
+
*
|
|
444
471
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
445
|
-
* @param {number | string} [ttl]
|
|
472
|
+
* @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
446
473
|
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
474
|
+
* @throws {Error} If the `ttl` is of an invalid type (must be a number or string).
|
|
447
475
|
*/
|
|
448
476
|
ttl(key, ttl) {
|
|
477
|
+
if (ttl !== void 0 && typeof ttl !== "number" && typeof ttl !== "string") {
|
|
478
|
+
throw this.createError("The ttl argument has to be a number or a string for shorthand ttl." /* ETTLTYPE */, this.formatKey(key));
|
|
479
|
+
}
|
|
449
480
|
const result = this.store.get(this.formatKey(key));
|
|
450
481
|
if (result) {
|
|
451
|
-
|
|
452
|
-
|
|
482
|
+
if (this.isNegativeTtl(ttl)) {
|
|
483
|
+
result.ttl = Math.max(
|
|
484
|
+
1,
|
|
485
|
+
this.getExpirationTimestamp(
|
|
486
|
+
typeof ttl === "string" ? Number(ttl) : ttl
|
|
487
|
+
)
|
|
488
|
+
);
|
|
489
|
+
} else if (ttl !== void 0 && (typeof ttl === "string" || ttl > 0)) {
|
|
490
|
+
result.ttl = this.resolveExpiration(ttl);
|
|
491
|
+
} else if (ttl === 0) {
|
|
492
|
+
result.ttl = 0;
|
|
493
|
+
} else if (this.options.stdTTL !== void 0 && this.options.stdTTL !== 0) {
|
|
494
|
+
result.ttl = this.resolveExpiration(this.options.stdTTL);
|
|
495
|
+
} else {
|
|
496
|
+
result.ttl = 0;
|
|
497
|
+
}
|
|
453
498
|
this.store.set(this.formatKey(key), result);
|
|
454
499
|
return true;
|
|
455
500
|
}
|
|
@@ -573,6 +618,33 @@ var NodeCache = class extends import_hookified2.Hookified {
|
|
|
573
618
|
const expirationTimestamp = currentTimestamp + ttlInMilliseconds;
|
|
574
619
|
return expirationTimestamp;
|
|
575
620
|
}
|
|
621
|
+
/**
|
|
622
|
+
* Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the
|
|
623
|
+
* resolved timestamp is not in the future (e.g. "0s" or a zero-duration string).
|
|
624
|
+
*/
|
|
625
|
+
resolveExpiration(ttl) {
|
|
626
|
+
const timestamp = this.getExpirationTimestamp(ttl);
|
|
627
|
+
if (timestamp <= Date.now()) {
|
|
628
|
+
return 0;
|
|
629
|
+
}
|
|
630
|
+
return timestamp;
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Checks whether a TTL value is negative. Handles both numbers and
|
|
634
|
+
* purely numeric strings (e.g. "-1").
|
|
635
|
+
*/
|
|
636
|
+
isNegativeTtl(ttl) {
|
|
637
|
+
if (typeof ttl === "number") {
|
|
638
|
+
return ttl < 0;
|
|
639
|
+
}
|
|
640
|
+
if (typeof ttl === "string") {
|
|
641
|
+
const num = Number(ttl);
|
|
642
|
+
if (!Number.isNaN(num) && num < 0) {
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return false;
|
|
647
|
+
}
|
|
576
648
|
checkData() {
|
|
577
649
|
for (const [key, value] of this.store.entries()) {
|
|
578
650
|
if (value.ttl > 0 && value.ttl < Date.now()) {
|
package/dist/index.d.cts
CHANGED
|
@@ -208,16 +208,33 @@ declare class NodeCache<T> extends Hookified {
|
|
|
208
208
|
constructor(options?: NodeCacheOptions);
|
|
209
209
|
/**
|
|
210
210
|
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
211
|
+
*
|
|
212
|
+
* TTL behavior:
|
|
213
|
+
* - `ttl > 0`: cache expires after the given number of seconds
|
|
214
|
+
* - `ttl === 0`: cache indefinitely (overrides stdTTL)
|
|
215
|
+
* - `ttl < 0`: store the value but it expires immediately on next access (matches original node-cache behavior)
|
|
216
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
217
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
218
|
+
*
|
|
211
219
|
* @param {string | number} key - it will convert the key to a string
|
|
212
220
|
* @param {T} value
|
|
213
|
-
* @param {number | string} [ttl] -
|
|
214
|
-
* @returns {boolean}
|
|
221
|
+
* @param {number | string} [ttl] - TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
222
|
+
* @returns {boolean} true on success
|
|
223
|
+
* @throws {Error} If the `key` or `ttl` is of an invalid type.
|
|
215
224
|
*/
|
|
216
225
|
set(key: string | number, value: T, ttl?: number | string): boolean;
|
|
217
226
|
/**
|
|
218
227
|
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
228
|
+
*
|
|
229
|
+
* Each item follows the same TTL behavior as `set()`:
|
|
230
|
+
* - Positive TTL: expires after the given seconds
|
|
231
|
+
* - `0`: cache indefinitely
|
|
232
|
+
* - Negative TTL: stored but expires immediately on next access
|
|
233
|
+
* - Omitted: uses `stdTTL` from options
|
|
234
|
+
*
|
|
219
235
|
* @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
|
|
220
|
-
* @returns {boolean}
|
|
236
|
+
* @returns {boolean} true on success
|
|
237
|
+
* @throws {Error} If `data` is not an array, or if any item has an invalid key or ttl type.
|
|
221
238
|
*/
|
|
222
239
|
mset(data: Array<PartialNodeCacheItem<T>>): boolean;
|
|
223
240
|
/**
|
|
@@ -255,9 +272,18 @@ declare class NodeCache<T> extends Hookified {
|
|
|
255
272
|
/**
|
|
256
273
|
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
257
274
|
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
275
|
+
*
|
|
276
|
+
* TTL behavior:
|
|
277
|
+
* - `ttl > 0`: key expires after the given number of seconds
|
|
278
|
+
* - `ttl === 0`: key lives indefinitely (overrides stdTTL)
|
|
279
|
+
* - `ttl < 0`: key expires immediately on next access (matches original node-cache behavior)
|
|
280
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
281
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
282
|
+
*
|
|
258
283
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
259
|
-
* @param {number | string} [ttl]
|
|
284
|
+
* @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
260
285
|
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
286
|
+
* @throws {Error} If the `ttl` is of an invalid type (must be a number or string).
|
|
261
287
|
*/
|
|
262
288
|
ttl(key: string | number, ttl?: number | string): boolean;
|
|
263
289
|
/**
|
|
@@ -308,6 +334,16 @@ declare class NodeCache<T> extends Hookified {
|
|
|
308
334
|
private formatKey;
|
|
309
335
|
private clone;
|
|
310
336
|
private getExpirationTimestamp;
|
|
337
|
+
/**
|
|
338
|
+
* Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the
|
|
339
|
+
* resolved timestamp is not in the future (e.g. "0s" or a zero-duration string).
|
|
340
|
+
*/
|
|
341
|
+
private resolveExpiration;
|
|
342
|
+
/**
|
|
343
|
+
* Checks whether a TTL value is negative. Handles both numbers and
|
|
344
|
+
* purely numeric strings (e.g. "-1").
|
|
345
|
+
*/
|
|
346
|
+
private isNegativeTtl;
|
|
311
347
|
private checkData;
|
|
312
348
|
private createError;
|
|
313
349
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -208,16 +208,33 @@ declare class NodeCache<T> extends Hookified {
|
|
|
208
208
|
constructor(options?: NodeCacheOptions);
|
|
209
209
|
/**
|
|
210
210
|
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
211
|
+
*
|
|
212
|
+
* TTL behavior:
|
|
213
|
+
* - `ttl > 0`: cache expires after the given number of seconds
|
|
214
|
+
* - `ttl === 0`: cache indefinitely (overrides stdTTL)
|
|
215
|
+
* - `ttl < 0`: store the value but it expires immediately on next access (matches original node-cache behavior)
|
|
216
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
217
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
218
|
+
*
|
|
211
219
|
* @param {string | number} key - it will convert the key to a string
|
|
212
220
|
* @param {T} value
|
|
213
|
-
* @param {number | string} [ttl] -
|
|
214
|
-
* @returns {boolean}
|
|
221
|
+
* @param {number | string} [ttl] - TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
222
|
+
* @returns {boolean} true on success
|
|
223
|
+
* @throws {Error} If the `key` or `ttl` is of an invalid type.
|
|
215
224
|
*/
|
|
216
225
|
set(key: string | number, value: T, ttl?: number | string): boolean;
|
|
217
226
|
/**
|
|
218
227
|
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
228
|
+
*
|
|
229
|
+
* Each item follows the same TTL behavior as `set()`:
|
|
230
|
+
* - Positive TTL: expires after the given seconds
|
|
231
|
+
* - `0`: cache indefinitely
|
|
232
|
+
* - Negative TTL: stored but expires immediately on next access
|
|
233
|
+
* - Omitted: uses `stdTTL` from options
|
|
234
|
+
*
|
|
219
235
|
* @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
|
|
220
|
-
* @returns {boolean}
|
|
236
|
+
* @returns {boolean} true on success
|
|
237
|
+
* @throws {Error} If `data` is not an array, or if any item has an invalid key or ttl type.
|
|
221
238
|
*/
|
|
222
239
|
mset(data: Array<PartialNodeCacheItem<T>>): boolean;
|
|
223
240
|
/**
|
|
@@ -255,9 +272,18 @@ declare class NodeCache<T> extends Hookified {
|
|
|
255
272
|
/**
|
|
256
273
|
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
257
274
|
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
275
|
+
*
|
|
276
|
+
* TTL behavior:
|
|
277
|
+
* - `ttl > 0`: key expires after the given number of seconds
|
|
278
|
+
* - `ttl === 0`: key lives indefinitely (overrides stdTTL)
|
|
279
|
+
* - `ttl < 0`: key expires immediately on next access (matches original node-cache behavior)
|
|
280
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
281
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
282
|
+
*
|
|
258
283
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
259
|
-
* @param {number | string} [ttl]
|
|
284
|
+
* @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
260
285
|
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
286
|
+
* @throws {Error} If the `ttl` is of an invalid type (must be a number or string).
|
|
261
287
|
*/
|
|
262
288
|
ttl(key: string | number, ttl?: number | string): boolean;
|
|
263
289
|
/**
|
|
@@ -308,6 +334,16 @@ declare class NodeCache<T> extends Hookified {
|
|
|
308
334
|
private formatKey;
|
|
309
335
|
private clone;
|
|
310
336
|
private getExpirationTimestamp;
|
|
337
|
+
/**
|
|
338
|
+
* Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the
|
|
339
|
+
* resolved timestamp is not in the future (e.g. "0s" or a zero-duration string).
|
|
340
|
+
*/
|
|
341
|
+
private resolveExpiration;
|
|
342
|
+
/**
|
|
343
|
+
* Checks whether a TTL value is negative. Handles both numbers and
|
|
344
|
+
* purely numeric strings (e.g. "-1").
|
|
345
|
+
*/
|
|
346
|
+
private isNegativeTtl;
|
|
311
347
|
private checkData;
|
|
312
348
|
private createError;
|
|
313
349
|
}
|
package/dist/index.js
CHANGED
|
@@ -248,29 +248,40 @@ var NodeCache = class extends Hookified2 {
|
|
|
248
248
|
}
|
|
249
249
|
/**
|
|
250
250
|
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
251
|
+
*
|
|
252
|
+
* TTL behavior:
|
|
253
|
+
* - `ttl > 0`: cache expires after the given number of seconds
|
|
254
|
+
* - `ttl === 0`: cache indefinitely (overrides stdTTL)
|
|
255
|
+
* - `ttl < 0`: store the value but it expires immediately on next access (matches original node-cache behavior)
|
|
256
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
257
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
258
|
+
*
|
|
251
259
|
* @param {string | number} key - it will convert the key to a string
|
|
252
260
|
* @param {T} value
|
|
253
|
-
* @param {number | string} [ttl] -
|
|
254
|
-
* @returns {boolean}
|
|
261
|
+
* @param {number | string} [ttl] - TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
262
|
+
* @returns {boolean} true on success
|
|
263
|
+
* @throws {Error} If the `key` or `ttl` is of an invalid type.
|
|
255
264
|
*/
|
|
256
|
-
set(key, value, ttl
|
|
265
|
+
set(key, value, ttl) {
|
|
257
266
|
if (typeof key !== "string" && typeof key !== "number") {
|
|
258
267
|
throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
|
|
259
268
|
}
|
|
260
|
-
if (ttl && typeof ttl !== "number" && typeof ttl !== "string") {
|
|
269
|
+
if (ttl !== void 0 && typeof ttl !== "number" && typeof ttl !== "string") {
|
|
261
270
|
throw this.createError("The ttl argument has to be a number or a string for shorthand ttl." /* ETTLTYPE */, this.formatKey(key));
|
|
262
271
|
}
|
|
263
272
|
const keyValue = this.formatKey(key);
|
|
264
|
-
let ttlValue = 0;
|
|
265
|
-
if (this.options.stdTTL) {
|
|
266
|
-
ttlValue = this.getExpirationTimestamp(this.options.stdTTL);
|
|
267
|
-
}
|
|
268
|
-
if (ttl) {
|
|
269
|
-
ttlValue = this.getExpirationTimestamp(ttl);
|
|
270
|
-
}
|
|
271
273
|
let expirationTimestamp = 0;
|
|
272
|
-
if (
|
|
273
|
-
expirationTimestamp =
|
|
274
|
+
if (this.isNegativeTtl(ttl)) {
|
|
275
|
+
expirationTimestamp = Math.max(
|
|
276
|
+
1,
|
|
277
|
+
this.getExpirationTimestamp(
|
|
278
|
+
typeof ttl === "string" ? Number(ttl) : ttl
|
|
279
|
+
)
|
|
280
|
+
);
|
|
281
|
+
} else if (ttl !== void 0 && (typeof ttl === "string" || ttl > 0)) {
|
|
282
|
+
expirationTimestamp = this.resolveExpiration(ttl);
|
|
283
|
+
} else if (ttl === void 0 && this.options.stdTTL !== void 0 && this.options.stdTTL !== 0) {
|
|
284
|
+
expirationTimestamp = this.resolveExpiration(this.options.stdTTL);
|
|
274
285
|
}
|
|
275
286
|
if (this.options.maxKeys) {
|
|
276
287
|
const { maxKeys } = this.options;
|
|
@@ -283,7 +294,7 @@ var NodeCache = class extends Hookified2 {
|
|
|
283
294
|
value,
|
|
284
295
|
ttl: expirationTimestamp
|
|
285
296
|
});
|
|
286
|
-
this.emit("set", keyValue, value,
|
|
297
|
+
this.emit("set", keyValue, value, expirationTimestamp);
|
|
287
298
|
this._stats.incrementKSize(keyValue);
|
|
288
299
|
this._stats.incrementVSize(value);
|
|
289
300
|
this._stats.setCount(this.store.size);
|
|
@@ -291,8 +302,16 @@ var NodeCache = class extends Hookified2 {
|
|
|
291
302
|
}
|
|
292
303
|
/**
|
|
293
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
|
+
*
|
|
294
312
|
* @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
|
|
295
|
-
* @returns {boolean}
|
|
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.
|
|
296
315
|
*/
|
|
297
316
|
mset(data) {
|
|
298
317
|
if (!Array.isArray(data)) {
|
|
@@ -404,15 +423,41 @@ var NodeCache = class extends Hookified2 {
|
|
|
404
423
|
/**
|
|
405
424
|
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
406
425
|
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
426
|
+
*
|
|
427
|
+
* TTL behavior:
|
|
428
|
+
* - `ttl > 0`: key expires after the given number of seconds
|
|
429
|
+
* - `ttl === 0`: key lives indefinitely (overrides stdTTL)
|
|
430
|
+
* - `ttl < 0`: key expires immediately on next access (matches original node-cache behavior)
|
|
431
|
+
* - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
|
|
432
|
+
* - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
|
|
433
|
+
*
|
|
407
434
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
408
|
-
* @param {number | string} [ttl]
|
|
435
|
+
* @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
|
|
409
436
|
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
437
|
+
* @throws {Error} If the `ttl` is of an invalid type (must be a number or string).
|
|
410
438
|
*/
|
|
411
439
|
ttl(key, ttl) {
|
|
440
|
+
if (ttl !== void 0 && typeof ttl !== "number" && typeof ttl !== "string") {
|
|
441
|
+
throw this.createError("The ttl argument has to be a number or a string for shorthand ttl." /* ETTLTYPE */, this.formatKey(key));
|
|
442
|
+
}
|
|
412
443
|
const result = this.store.get(this.formatKey(key));
|
|
413
444
|
if (result) {
|
|
414
|
-
|
|
415
|
-
|
|
445
|
+
if (this.isNegativeTtl(ttl)) {
|
|
446
|
+
result.ttl = Math.max(
|
|
447
|
+
1,
|
|
448
|
+
this.getExpirationTimestamp(
|
|
449
|
+
typeof ttl === "string" ? Number(ttl) : ttl
|
|
450
|
+
)
|
|
451
|
+
);
|
|
452
|
+
} else if (ttl !== void 0 && (typeof ttl === "string" || ttl > 0)) {
|
|
453
|
+
result.ttl = this.resolveExpiration(ttl);
|
|
454
|
+
} else if (ttl === 0) {
|
|
455
|
+
result.ttl = 0;
|
|
456
|
+
} else if (this.options.stdTTL !== void 0 && this.options.stdTTL !== 0) {
|
|
457
|
+
result.ttl = this.resolveExpiration(this.options.stdTTL);
|
|
458
|
+
} else {
|
|
459
|
+
result.ttl = 0;
|
|
460
|
+
}
|
|
416
461
|
this.store.set(this.formatKey(key), result);
|
|
417
462
|
return true;
|
|
418
463
|
}
|
|
@@ -536,6 +581,33 @@ var NodeCache = class extends Hookified2 {
|
|
|
536
581
|
const expirationTimestamp = currentTimestamp + ttlInMilliseconds;
|
|
537
582
|
return expirationTimestamp;
|
|
538
583
|
}
|
|
584
|
+
/**
|
|
585
|
+
* Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the
|
|
586
|
+
* resolved timestamp is not in the future (e.g. "0s" or a zero-duration string).
|
|
587
|
+
*/
|
|
588
|
+
resolveExpiration(ttl) {
|
|
589
|
+
const timestamp = this.getExpirationTimestamp(ttl);
|
|
590
|
+
if (timestamp <= Date.now()) {
|
|
591
|
+
return 0;
|
|
592
|
+
}
|
|
593
|
+
return timestamp;
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Checks whether a TTL value is negative. Handles both numbers and
|
|
597
|
+
* purely numeric strings (e.g. "-1").
|
|
598
|
+
*/
|
|
599
|
+
isNegativeTtl(ttl) {
|
|
600
|
+
if (typeof ttl === "number") {
|
|
601
|
+
return ttl < 0;
|
|
602
|
+
}
|
|
603
|
+
if (typeof ttl === "string") {
|
|
604
|
+
const num = Number(ttl);
|
|
605
|
+
if (!Number.isNaN(num) && num < 0) {
|
|
606
|
+
return true;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
return false;
|
|
610
|
+
}
|
|
539
611
|
checkData() {
|
|
540
612
|
for (const [key, value] of this.store.entries()) {
|
|
541
613
|
if (value.ttl > 0 && value.ttl < Date.now()) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cacheable/node-cache",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Simple and Maintained fast NodeJS internal caching",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -40,19 +40,16 @@
|
|
|
40
40
|
"cacheable-node"
|
|
41
41
|
],
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@
|
|
44
|
-
"@
|
|
45
|
-
"@types/node": "^25.0.2",
|
|
46
|
-
"@vitest/coverage-v8": "^4.0.15",
|
|
43
|
+
"@faker-js/faker": "^10.2.0",
|
|
44
|
+
"@types/node": "^24.10.10",
|
|
47
45
|
"rimraf": "^6.1.2",
|
|
48
46
|
"tsup": "^8.5.1",
|
|
49
|
-
"typescript": "^5.9.3"
|
|
50
|
-
"vitest": "^4.0.15"
|
|
47
|
+
"typescript": "^5.9.3"
|
|
51
48
|
},
|
|
52
49
|
"dependencies": {
|
|
53
|
-
"hookified": "^1.
|
|
54
|
-
"keyv": "^5.
|
|
55
|
-
"@cacheable/utils": "^2.
|
|
50
|
+
"hookified": "^1.15.0",
|
|
51
|
+
"keyv": "^5.6.0",
|
|
52
|
+
"@cacheable/utils": "^2.4.0"
|
|
56
53
|
},
|
|
57
54
|
"files": [
|
|
58
55
|
"dist",
|