@cacheable/node-cache 3.0.0 → 3.0.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/dist/index.cjs CHANGED
@@ -129,7 +129,7 @@ var NodeCacheStore = class extends import_hookified.Hookified {
129
129
  * @returns {Record<string, any | undefined>}
130
130
  */
131
131
  async mget(keys) {
132
- const result = {};
132
+ const result = /* @__PURE__ */ Object.create(null);
133
133
  for (const key of keys) {
134
134
  const value = await this._keyv.get(key.toString());
135
135
  if (value === void 0) {
@@ -378,11 +378,8 @@ var NodeCache = class extends import_hookified2.Hookified {
378
378
  if (result) {
379
379
  if (result.ttl > 0) {
380
380
  if (result.ttl < Date.now()) {
381
- if (this.options.deleteOnExpire) {
382
- this.del(key);
383
- }
384
381
  this._stats.incrementMisses();
385
- this.emit("expired", this.formatKey(key), result.value);
382
+ this.handleExpired(key, result);
386
383
  return void 0;
387
384
  }
388
385
  this._stats.incrementHits();
@@ -407,7 +404,7 @@ var NodeCache = class extends import_hookified2.Hookified {
407
404
  * @returns {Record<string, T | undefined>} an object with the key as a property and the value as the value
408
405
  */
409
406
  mget(keys) {
410
- const result = {};
407
+ const result = /* @__PURE__ */ Object.create(null);
411
408
  for (const key of keys) {
412
409
  const value = this.get(key);
413
410
  if (value) {
@@ -537,12 +534,22 @@ var NodeCache = class extends import_hookified2.Hookified {
537
534
  return result;
538
535
  }
539
536
  /**
540
- * Returns boolean indicating if the key is cached.
537
+ * Returns boolean indicating if the key is cached. If the key has expired, it
538
+ * will return false and the key will be removed from the cache when
539
+ * `deleteOnExpire` is enabled (matches the original node-cache behavior).
541
540
  * @param {string | number} key if the key is a number it will convert it to a string
542
- * @returns {boolean} true if the key is cached
541
+ * @returns {boolean} true if the key is cached and not expired
543
542
  */
544
543
  has(key) {
545
- return this.store.has(this.formatKey(key));
544
+ const result = this.store.get(this.formatKey(key));
545
+ if (!result) {
546
+ return false;
547
+ }
548
+ if (result.ttl > 0 && result.ttl < Date.now()) {
549
+ this.handleExpired(key, result);
550
+ return false;
551
+ }
552
+ return true;
546
553
  }
547
554
  /**
548
555
  * Gets the stats of the cache
@@ -657,13 +664,21 @@ var NodeCache = class extends import_hookified2.Hookified {
657
664
  checkData() {
658
665
  for (const [key, value] of this.store.entries()) {
659
666
  if (value.ttl > 0 && value.ttl < Date.now()) {
660
- if (this.options.deleteOnExpire) {
661
- this.del(key);
662
- }
663
- this.emit("expired", this.formatKey(key), value.value);
667
+ this.handleExpired(key, value);
664
668
  }
665
669
  }
666
670
  }
671
+ /**
672
+ * Handles expiration for a cache entry. Deletes the entry when
673
+ * `deleteOnExpire` is enabled and emits the "expired" event.
674
+ */
675
+ handleExpired(key, entry) {
676
+ const keyValue = this.formatKey(key);
677
+ if (this.options.deleteOnExpire) {
678
+ this.del(key);
679
+ }
680
+ this.emit("expired", keyValue, entry.value);
681
+ }
667
682
  createError(errorCode, key) {
668
683
  let error = errorCode;
669
684
  if (key) {
package/dist/index.d.cts CHANGED
@@ -287,9 +287,11 @@ declare class NodeCache<T> extends Hookified {
287
287
  */
288
288
  keys(): string[];
289
289
  /**
290
- * Returns boolean indicating if the key is cached.
290
+ * Returns boolean indicating if the key is cached. If the key has expired, it
291
+ * will return false and the key will be removed from the cache when
292
+ * `deleteOnExpire` is enabled (matches the original node-cache behavior).
291
293
  * @param {string | number} key if the key is a number it will convert it to a string
292
- * @returns {boolean} true if the key is cached
294
+ * @returns {boolean} true if the key is cached and not expired
293
295
  */
294
296
  has(key: string | number): boolean;
295
297
  /**
@@ -333,6 +335,11 @@ declare class NodeCache<T> extends Hookified {
333
335
  */
334
336
  private isNegativeTtl;
335
337
  private checkData;
338
+ /**
339
+ * Handles expiration for a cache entry. Deletes the entry when
340
+ * `deleteOnExpire` is enabled and emits the "expired" event.
341
+ */
342
+ private handleExpired;
336
343
  private createError;
337
344
  }
338
345
 
package/dist/index.d.ts CHANGED
@@ -287,9 +287,11 @@ declare class NodeCache<T> extends Hookified {
287
287
  */
288
288
  keys(): string[];
289
289
  /**
290
- * Returns boolean indicating if the key is cached.
290
+ * Returns boolean indicating if the key is cached. If the key has expired, it
291
+ * will return false and the key will be removed from the cache when
292
+ * `deleteOnExpire` is enabled (matches the original node-cache behavior).
291
293
  * @param {string | number} key if the key is a number it will convert it to a string
292
- * @returns {boolean} true if the key is cached
294
+ * @returns {boolean} true if the key is cached and not expired
293
295
  */
294
296
  has(key: string | number): boolean;
295
297
  /**
@@ -333,6 +335,11 @@ declare class NodeCache<T> extends Hookified {
333
335
  */
334
336
  private isNegativeTtl;
335
337
  private checkData;
338
+ /**
339
+ * Handles expiration for a cache entry. Deletes the entry when
340
+ * `deleteOnExpire` is enabled and emits the "expired" event.
341
+ */
342
+ private handleExpired;
336
343
  private createError;
337
344
  }
338
345
 
package/dist/index.js CHANGED
@@ -92,7 +92,7 @@ var NodeCacheStore = class extends Hookified {
92
92
  * @returns {Record<string, any | undefined>}
93
93
  */
94
94
  async mget(keys) {
95
- const result = {};
95
+ const result = /* @__PURE__ */ Object.create(null);
96
96
  for (const key of keys) {
97
97
  const value = await this._keyv.get(key.toString());
98
98
  if (value === void 0) {
@@ -341,11 +341,8 @@ var NodeCache = class extends Hookified2 {
341
341
  if (result) {
342
342
  if (result.ttl > 0) {
343
343
  if (result.ttl < Date.now()) {
344
- if (this.options.deleteOnExpire) {
345
- this.del(key);
346
- }
347
344
  this._stats.incrementMisses();
348
- this.emit("expired", this.formatKey(key), result.value);
345
+ this.handleExpired(key, result);
349
346
  return void 0;
350
347
  }
351
348
  this._stats.incrementHits();
@@ -370,7 +367,7 @@ var NodeCache = class extends Hookified2 {
370
367
  * @returns {Record<string, T | undefined>} an object with the key as a property and the value as the value
371
368
  */
372
369
  mget(keys) {
373
- const result = {};
370
+ const result = /* @__PURE__ */ Object.create(null);
374
371
  for (const key of keys) {
375
372
  const value = this.get(key);
376
373
  if (value) {
@@ -500,12 +497,22 @@ var NodeCache = class extends Hookified2 {
500
497
  return result;
501
498
  }
502
499
  /**
503
- * Returns boolean indicating if the key is cached.
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).
504
503
  * @param {string | number} key if the key is a number it will convert it to a string
505
- * @returns {boolean} true if the key is cached
504
+ * @returns {boolean} true if the key is cached and not expired
506
505
  */
507
506
  has(key) {
508
- return this.store.has(this.formatKey(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;
509
516
  }
510
517
  /**
511
518
  * Gets the stats of the cache
@@ -620,13 +627,21 @@ var NodeCache = class extends Hookified2 {
620
627
  checkData() {
621
628
  for (const [key, value] of this.store.entries()) {
622
629
  if (value.ttl > 0 && value.ttl < Date.now()) {
623
- if (this.options.deleteOnExpire) {
624
- this.del(key);
625
- }
626
- this.emit("expired", this.formatKey(key), value.value);
630
+ this.handleExpired(key, value);
627
631
  }
628
632
  }
629
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
+ }
630
645
  createError(errorCode, key) {
631
646
  let error = errorCode;
632
647
  if (key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cacheable/node-cache",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "Simple and Maintained fast NodeJS internal caching",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",