@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/README.md
CHANGED
|
@@ -333,11 +333,22 @@ When initializing the cache you can pass in the options below:
|
|
|
333
333
|
|
|
334
334
|
```javascript
|
|
335
335
|
export type NodeCacheStoreOptions = {
|
|
336
|
-
ttl?: number | string; // The standard ttl as number in milliseconds
|
|
336
|
+
ttl?: number | string; // The standard ttl as number in milliseconds. 0 = unlimited. Supports shorthand like '1h'.
|
|
337
337
|
store?: Keyv; // The storage adapter (defaults to in-memory Keyv)
|
|
338
|
+
useClones?: boolean; // Clone values on get/set via structuredClone. Default: false
|
|
339
|
+
checkperiod?: number; // Interval in seconds to check for expired items. 0 = disabled. Default: 0
|
|
340
|
+
deleteOnExpire?: boolean; // Delete expired items when detected. Default: true
|
|
338
341
|
};
|
|
339
342
|
```
|
|
340
343
|
|
|
344
|
+
| Option | Default | Description |
|
|
345
|
+
|--------|---------|-------------|
|
|
346
|
+
| `ttl` | `undefined` | The standard TTL in milliseconds for every cache element. `undefined` or `0` = unlimited. Supports shorthand strings like `'1h'`, `'30m'`, `'5s'`. |
|
|
347
|
+
| `store` | `new Keyv()` | The Keyv storage adapter. Pass any Keyv-compatible store (Redis, MongoDB, etc.). |
|
|
348
|
+
| `useClones` | `false` | If `true`, values are deep-cloned via `structuredClone` on both `set()` and `get()`. This prevents mutations to the returned object from affecting the cached copy and vice versa. |
|
|
349
|
+
| `checkperiod` | `0` | The interval in seconds to check for expired items. `0` = disabled (expiration is checked lazily on access). |
|
|
350
|
+
| `deleteOnExpire` | `true` | If `true`, expired keys are automatically deleted. If `false`, expired keys remain in the store but `get()` returns `undefined` and `has()` returns `false`. You can handle them via the `expired` event. |
|
|
351
|
+
|
|
341
352
|
Note: the `ttl` is now in milliseconds and not seconds like `stdTTL` in `NodeCache`. You can also use shorthand notation for TTL values. Here is an example:
|
|
342
353
|
|
|
343
354
|
```javascript
|
|
@@ -348,18 +359,189 @@ await cache.set('longfoo', 'bar', '1d'); // 1 day
|
|
|
348
359
|
|
|
349
360
|
## NodeCacheStore API
|
|
350
361
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
362
|
+
### `set(key, value, ttl?): Promise<boolean>`
|
|
363
|
+
|
|
364
|
+
Set a key value pair with an optional ttl (in milliseconds or shorthand string). Returns `true` on success.
|
|
365
|
+
|
|
366
|
+
```javascript
|
|
367
|
+
await cache.set('foo', 'bar');
|
|
368
|
+
await cache.set('foo', 'bar', 5000); // 5 seconds in milliseconds
|
|
369
|
+
await cache.set('foo', 'bar', '1h'); // 1 hour using shorthand
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
### `mset(data): Promise<void>`
|
|
373
|
+
|
|
374
|
+
Set multiple key value pairs at once.
|
|
375
|
+
|
|
376
|
+
```javascript
|
|
377
|
+
await cache.mset([{key: 'foo', value: 'bar'}, {key: 'baz', value: 'qux', ttl: 5000}]);
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### `get<T>(key): Promise<T | undefined>`
|
|
381
|
+
|
|
382
|
+
Get a value from the cache by key. Returns `undefined` if the key does not exist or has expired.
|
|
383
|
+
|
|
384
|
+
```javascript
|
|
385
|
+
await cache.get('foo'); // 'bar'
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### `mget<T>(keys): Promise<Record<string, T | undefined>>`
|
|
389
|
+
|
|
390
|
+
Get multiple values from the cache by keys.
|
|
391
|
+
|
|
392
|
+
```javascript
|
|
393
|
+
await cache.mget(['foo', 'bar']); // { foo: 'value1', bar: 'value2' }
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### `take<T>(key): Promise<T | undefined>`
|
|
397
|
+
|
|
398
|
+
Get a value from the cache by key and delete it. Useful for single-use values like OTPs.
|
|
399
|
+
|
|
400
|
+
```javascript
|
|
401
|
+
await cache.take('foo'); // 'bar'
|
|
402
|
+
await cache.get('foo'); // undefined
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### `del(key): Promise<boolean>`
|
|
406
|
+
|
|
407
|
+
Delete a key from the cache. Returns `true` if the key was deleted.
|
|
408
|
+
|
|
409
|
+
```javascript
|
|
410
|
+
await cache.del('foo'); // true
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### `mdel(keys): Promise<boolean>`
|
|
414
|
+
|
|
415
|
+
Delete multiple keys from the cache.
|
|
416
|
+
|
|
417
|
+
```javascript
|
|
418
|
+
await cache.mdel(['foo', 'bar']); // true
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
### `has(key): Promise<boolean>`
|
|
422
|
+
|
|
423
|
+
Check if a key exists in the cache and is not expired.
|
|
424
|
+
|
|
425
|
+
```javascript
|
|
426
|
+
await cache.set('foo', 'bar');
|
|
427
|
+
await cache.has('foo'); // true
|
|
428
|
+
await cache.has('missing'); // false
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
### `keys(): Promise<string[]>`
|
|
432
|
+
|
|
433
|
+
Get all keys from the cache.
|
|
434
|
+
|
|
435
|
+
```javascript
|
|
436
|
+
await cache.keys(); // ['foo', 'bar']
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
### `getTtl(key): Promise<number | undefined>`
|
|
440
|
+
|
|
441
|
+
Get the TTL expiration timestamp of a key. Returns `0` if the key has no TTL (unlimited), `undefined` if the key does not exist, or a timestamp in milliseconds of when the key will expire.
|
|
442
|
+
|
|
443
|
+
```javascript
|
|
444
|
+
await cache.set('foo', 'bar', 5000);
|
|
445
|
+
await cache.getTtl('foo'); // 1725993344859 (timestamp)
|
|
446
|
+
await cache.set('bar', 'baz');
|
|
447
|
+
await cache.getTtl('bar'); // 0 (unlimited)
|
|
448
|
+
await cache.getTtl('missing'); // undefined
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
### `setTtl(key, ttl?): Promise<boolean>`
|
|
452
|
+
|
|
453
|
+
Set the TTL of an existing key. Returns `true` if the key was found and updated.
|
|
454
|
+
|
|
455
|
+
```javascript
|
|
456
|
+
await cache.setTtl('foo', 10000); // true
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
### `clear(): Promise<void>`
|
|
460
|
+
|
|
461
|
+
Clear the cache. Removes all keys and resets store-related stats (keys, ksize, vsize) but preserves hit/miss counts.
|
|
462
|
+
|
|
463
|
+
```javascript
|
|
464
|
+
await cache.clear();
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
### `flushAll(): Promise<void>`
|
|
468
|
+
|
|
469
|
+
Flush the entire cache. Removes all keys and resets all stats. Emits a `flush` event.
|
|
470
|
+
|
|
471
|
+
```javascript
|
|
472
|
+
await cache.flushAll();
|
|
473
|
+
await cache.keys(); // []
|
|
474
|
+
cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
### `getStats(): NodeCacheStats`
|
|
478
|
+
|
|
479
|
+
Get the stats of the cache.
|
|
480
|
+
|
|
481
|
+
```javascript
|
|
482
|
+
cache.getStats(); // {hits: 1, misses: 0, keys: 2, ksize: 12, vsize: 24}
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
### `flushStats(): void`
|
|
486
|
+
|
|
487
|
+
Flush the stats. Resets all stats but keeps the cached data.
|
|
488
|
+
|
|
489
|
+
### `close(): void`
|
|
490
|
+
|
|
491
|
+
Stop the check interval timer. Use this to clean up when you're done with the cache.
|
|
492
|
+
|
|
493
|
+
```javascript
|
|
494
|
+
cache.close();
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
### `disconnect(): Promise<void>`
|
|
498
|
+
|
|
499
|
+
Disconnect the storage adapter and stop the check interval.
|
|
500
|
+
|
|
501
|
+
```javascript
|
|
502
|
+
await cache.disconnect();
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
### Events
|
|
506
|
+
|
|
507
|
+
Listen to events using `on()`:
|
|
508
|
+
|
|
509
|
+
```javascript
|
|
510
|
+
cache.on('set', (key, value) => {
|
|
511
|
+
console.log(`Key ${key} has been set with value ${value}`);
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
cache.on('del', (key, value) => {
|
|
515
|
+
console.log(`Key ${key} has been deleted`);
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
cache.on('expired', (key, value) => {
|
|
519
|
+
console.log(`Key ${key} has expired`);
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
cache.on('flush', () => {
|
|
523
|
+
console.log('Cache has been flushed');
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
cache.on('flush_stats', () => {
|
|
527
|
+
console.log('Stats have been flushed');
|
|
528
|
+
});
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
| Event | Arguments | Description |
|
|
532
|
+
|-------|-----------|-------------|
|
|
533
|
+
| `set` | `key, value` | Emitted when a key is set |
|
|
534
|
+
| `del` | `key, value` | Emitted when a key is deleted (including via `take()` and `mdel()`) |
|
|
535
|
+
| `expired` | `key, value` | Emitted when an expired key is detected |
|
|
536
|
+
| `flush` | — | Emitted when `flushAll()` is called |
|
|
537
|
+
| `flush_stats` | — | Emitted when `flushStats()` is called |
|
|
538
|
+
|
|
539
|
+
### Properties
|
|
540
|
+
|
|
361
541
|
* `ttl`: `number | string | undefined` - The standard ttl for every generated cache element. `undefined` = unlimited
|
|
362
542
|
* `store`: `Keyv` - The storage adapter (read-only)
|
|
543
|
+
* `useClones`: `boolean` - Whether values are cloned on get/set
|
|
544
|
+
* `deleteOnExpire`: `boolean` - Whether expired items are automatically deleted
|
|
363
545
|
|
|
364
546
|
|
|
365
547
|
# Migrating to v2
|