@cacheable/node-cache 1.4.1 → 1.5.0

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 CHANGED
@@ -42,6 +42,12 @@ import {NodeCache} from '@cacheable/node-cache';
42
42
  const cache = new NodeCache();
43
43
  cache.set('foo', 'bar');
44
44
  cache.get('foo'); // 'bar'
45
+
46
+ cache.set('foo', 'bar', 10); // 10 seconds
47
+
48
+ cache.del('foo'); // true
49
+
50
+ cache.set('bar', 'baz', '35m'); // 35 minutes using shorthand
45
51
  ```
46
52
 
47
53
  # Advanced Usage
@@ -121,7 +127,7 @@ Create a new cache instance. You can pass in options to set the configuration:
121
127
 
122
128
  ```javascript
123
129
  export type NodeCacheOptions = {
124
- stdTTL?: number; // The standard ttl as number in seconds for every generated cache element. 0 = unlimited
130
+ stdTTL?: number; // The standard ttl as number in seconds for every generated cache element. 0 = unlimited. If string, it will be parsed as shorthand and default to milliseconds if it is a number as a string.
125
131
  checkperiod?: number; // Default is 600, 0 means no periodic check
126
132
  useClones?: boolean; // Default is true
127
133
  deleteOnExpire?: boolean; // Default is true, if this is set to true it will delete the key when it expires.
package/dist/index.cjs CHANGED
@@ -240,7 +240,7 @@ var NodeCacheErrors = /* @__PURE__ */ ((NodeCacheErrors2) => {
240
240
  NodeCacheErrors2["ECACHEFULL"] = "Cache max keys amount exceeded";
241
241
  NodeCacheErrors2["EKEYTYPE"] = "The key argument has to be of type `string` or `number`. Found: `__key`";
242
242
  NodeCacheErrors2["EKEYSTYPE"] = "The keys argument has to be an array.";
243
- NodeCacheErrors2["ETTLTYPE"] = "The ttl argument has to be a number.";
243
+ NodeCacheErrors2["ETTLTYPE"] = "The ttl argument has to be a number or a string for shorthand ttl.";
244
244
  return NodeCacheErrors2;
245
245
  })(NodeCacheErrors || {});
246
246
  var NodeCache = class extends import_eventemitter3.default {
@@ -267,21 +267,27 @@ var NodeCache = class extends import_eventemitter3.default {
267
267
  * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
268
268
  * @param {string | number} key - it will convert the key to a string
269
269
  * @param {any} value
270
- * @param {number} [ttl] - this is in seconds and undefined will use the default ttl
270
+ * @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
271
271
  * @returns {boolean}
272
272
  */
273
273
  set(key, value, ttl) {
274
274
  if (typeof key !== "string" && typeof key !== "number") {
275
275
  throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
276
276
  }
277
- if (ttl && typeof ttl !== "number") {
278
- throw this.createError("The ttl argument has to be a number." /* ETTLTYPE */, this.formatKey(key));
277
+ if (ttl && typeof ttl !== "number" && typeof ttl !== "string") {
278
+ throw this.createError("The ttl argument has to be a number or a string for shorthand ttl." /* ETTLTYPE */, this.formatKey(key));
279
279
  }
280
280
  const keyValue = this.formatKey(key);
281
- const ttlValue = ttl ?? this.options.stdTTL;
281
+ let ttlValue = 0;
282
+ if (this.options.stdTTL) {
283
+ ttlValue = this.getExpirationTimestamp(this.options.stdTTL);
284
+ }
285
+ if (ttl) {
286
+ ttlValue = this.getExpirationTimestamp(ttl);
287
+ }
282
288
  let expirationTimestamp = 0;
283
289
  if (ttlValue && ttlValue > 0) {
284
- expirationTimestamp = this.getExpirationTimestamp(ttlValue);
290
+ expirationTimestamp = ttlValue;
285
291
  }
286
292
  if (this.options.maxKeys) {
287
293
  const maxKeys = this.options.maxKeys;
@@ -412,7 +418,7 @@ var NodeCache = class extends import_eventemitter3.default {
412
418
  * Redefine the ttl of a key. Returns true if the key has been found and changed.
413
419
  * Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
414
420
  * @param {string | number} key if the key is a number it will convert it to a string
415
- * @param {number} [ttl] the ttl in seconds
421
+ * @param {number | string} [ttl] the ttl in seconds if number, or a shorthand string like '1h' for 1 hour
416
422
  * @returns {boolean} true if the key has been found and changed. Otherwise returns false.
417
423
  */
418
424
  ttl(key, ttl) {
@@ -509,6 +515,9 @@ var NodeCache = class extends import_eventemitter3.default {
509
515
  return key.toString();
510
516
  }
511
517
  getExpirationTimestamp(ttlInSeconds) {
518
+ if (typeof ttlInSeconds === "string") {
519
+ return (0, import_cacheable2.shorthandToTime)(ttlInSeconds);
520
+ }
512
521
  const currentTimestamp = Date.now();
513
522
  const ttlInMilliseconds = ttlInSeconds * 1e3;
514
523
  const expirationTimestamp = currentTimestamp + ttlInMilliseconds;
package/dist/index.d.cts CHANGED
@@ -142,9 +142,9 @@ declare class NodeCacheStore {
142
142
 
143
143
  type NodeCacheOptions = {
144
144
  /**
145
- * The standard ttl as number in seconds for every generated cache element. 0 = unlimited
145
+ * The standard ttl as number in seconds for every generated cache element. 0 = unlimited, If string, it will be in shorthand format like '1h' for 1 hour
146
146
  */
147
- stdTTL?: number;
147
+ stdTTL?: number | string;
148
148
  /**
149
149
  * The interval to check for expired items in seconds. Default is 600 = 5 minutes
150
150
  */
@@ -181,7 +181,7 @@ declare enum NodeCacheErrors {
181
181
  ECACHEFULL = "Cache max keys amount exceeded",
182
182
  EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
183
183
  EKEYSTYPE = "The keys argument has to be an array.",
184
- ETTLTYPE = "The ttl argument has to be a number."
184
+ ETTLTYPE = "The ttl argument has to be a number or a string for shorthand ttl."
185
185
  }
186
186
  type NodeCacheStats = {
187
187
  /**
@@ -216,10 +216,10 @@ declare class NodeCache extends eventemitter {
216
216
  * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
217
217
  * @param {string | number} key - it will convert the key to a string
218
218
  * @param {any} value
219
- * @param {number} [ttl] - this is in seconds and undefined will use the default ttl
219
+ * @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
220
220
  * @returns {boolean}
221
221
  */
222
- set(key: string | number, value: any, ttl?: number): boolean;
222
+ set(key: string | number, value: any, ttl?: number | string): boolean;
223
223
  /**
224
224
  * Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
225
225
  * @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
@@ -262,10 +262,10 @@ declare class NodeCache extends eventemitter {
262
262
  * Redefine the ttl of a key. Returns true if the key has been found and changed.
263
263
  * Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
264
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
265
+ * @param {number | string} [ttl] the ttl in seconds if number, or a shorthand string like '1h' for 1 hour
266
266
  * @returns {boolean} true if the key has been found and changed. Otherwise returns false.
267
267
  */
268
- ttl(key: string | number, ttl?: number): boolean;
268
+ ttl(key: string | number, ttl?: number | string): boolean;
269
269
  /**
270
270
  * Receive the ttl of a key.
271
271
  * @param {string | number} key if the key is a number it will convert it to a string
package/dist/index.d.ts CHANGED
@@ -142,9 +142,9 @@ declare class NodeCacheStore {
142
142
 
143
143
  type NodeCacheOptions = {
144
144
  /**
145
- * The standard ttl as number in seconds for every generated cache element. 0 = unlimited
145
+ * The standard ttl as number in seconds for every generated cache element. 0 = unlimited, If string, it will be in shorthand format like '1h' for 1 hour
146
146
  */
147
- stdTTL?: number;
147
+ stdTTL?: number | string;
148
148
  /**
149
149
  * The interval to check for expired items in seconds. Default is 600 = 5 minutes
150
150
  */
@@ -181,7 +181,7 @@ declare enum NodeCacheErrors {
181
181
  ECACHEFULL = "Cache max keys amount exceeded",
182
182
  EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
183
183
  EKEYSTYPE = "The keys argument has to be an array.",
184
- ETTLTYPE = "The ttl argument has to be a number."
184
+ ETTLTYPE = "The ttl argument has to be a number or a string for shorthand ttl."
185
185
  }
186
186
  type NodeCacheStats = {
187
187
  /**
@@ -216,10 +216,10 @@ declare class NodeCache extends eventemitter {
216
216
  * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
217
217
  * @param {string | number} key - it will convert the key to a string
218
218
  * @param {any} value
219
- * @param {number} [ttl] - this is in seconds and undefined will use the default ttl
219
+ * @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
220
220
  * @returns {boolean}
221
221
  */
222
- set(key: string | number, value: any, ttl?: number): boolean;
222
+ set(key: string | number, value: any, ttl?: number | string): boolean;
223
223
  /**
224
224
  * Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
225
225
  * @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
@@ -262,10 +262,10 @@ declare class NodeCache extends eventemitter {
262
262
  * Redefine the ttl of a key. Returns true if the key has been found and changed.
263
263
  * Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
264
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
265
+ * @param {number | string} [ttl] the ttl in seconds if number, or a shorthand string like '1h' for 1 hour
266
266
  * @returns {boolean} true if the key has been found and changed. Otherwise returns false.
267
267
  */
268
- ttl(key: string | number, ttl?: number): boolean;
268
+ ttl(key: string | number, ttl?: number | string): boolean;
269
269
  /**
270
270
  * Receive the ttl of a key.
271
271
  * @param {string | number} key if the key is a number it will convert it to a string
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/index.ts
2
2
  import eventemitter from "eventemitter3";
3
- import { CacheableMemory as CacheableMemory2, CacheableStats } from "cacheable";
3
+ import { CacheableMemory as CacheableMemory2, CacheableStats, shorthandToTime } from "cacheable";
4
4
 
5
5
  // src/store.ts
6
6
  import { Cacheable, CacheableMemory } from "cacheable";
@@ -204,7 +204,7 @@ var NodeCacheErrors = /* @__PURE__ */ ((NodeCacheErrors2) => {
204
204
  NodeCacheErrors2["ECACHEFULL"] = "Cache max keys amount exceeded";
205
205
  NodeCacheErrors2["EKEYTYPE"] = "The key argument has to be of type `string` or `number`. Found: `__key`";
206
206
  NodeCacheErrors2["EKEYSTYPE"] = "The keys argument has to be an array.";
207
- NodeCacheErrors2["ETTLTYPE"] = "The ttl argument has to be a number.";
207
+ NodeCacheErrors2["ETTLTYPE"] = "The ttl argument has to be a number or a string for shorthand ttl.";
208
208
  return NodeCacheErrors2;
209
209
  })(NodeCacheErrors || {});
210
210
  var NodeCache = class extends eventemitter {
@@ -231,21 +231,27 @@ var NodeCache = class extends eventemitter {
231
231
  * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
232
232
  * @param {string | number} key - it will convert the key to a string
233
233
  * @param {any} value
234
- * @param {number} [ttl] - this is in seconds and undefined will use the default ttl
234
+ * @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
235
235
  * @returns {boolean}
236
236
  */
237
237
  set(key, value, ttl) {
238
238
  if (typeof key !== "string" && typeof key !== "number") {
239
239
  throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
240
240
  }
241
- if (ttl && typeof ttl !== "number") {
242
- throw this.createError("The ttl argument has to be a number." /* ETTLTYPE */, this.formatKey(key));
241
+ if (ttl && typeof ttl !== "number" && typeof ttl !== "string") {
242
+ throw this.createError("The ttl argument has to be a number or a string for shorthand ttl." /* ETTLTYPE */, this.formatKey(key));
243
243
  }
244
244
  const keyValue = this.formatKey(key);
245
- const ttlValue = ttl ?? this.options.stdTTL;
245
+ let ttlValue = 0;
246
+ if (this.options.stdTTL) {
247
+ ttlValue = this.getExpirationTimestamp(this.options.stdTTL);
248
+ }
249
+ if (ttl) {
250
+ ttlValue = this.getExpirationTimestamp(ttl);
251
+ }
246
252
  let expirationTimestamp = 0;
247
253
  if (ttlValue && ttlValue > 0) {
248
- expirationTimestamp = this.getExpirationTimestamp(ttlValue);
254
+ expirationTimestamp = ttlValue;
249
255
  }
250
256
  if (this.options.maxKeys) {
251
257
  const maxKeys = this.options.maxKeys;
@@ -376,7 +382,7 @@ var NodeCache = class extends eventemitter {
376
382
  * Redefine the ttl of a key. Returns true if the key has been found and changed.
377
383
  * Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
378
384
  * @param {string | number} key if the key is a number it will convert it to a string
379
- * @param {number} [ttl] the ttl in seconds
385
+ * @param {number | string} [ttl] the ttl in seconds if number, or a shorthand string like '1h' for 1 hour
380
386
  * @returns {boolean} true if the key has been found and changed. Otherwise returns false.
381
387
  */
382
388
  ttl(key, ttl) {
@@ -473,6 +479,9 @@ var NodeCache = class extends eventemitter {
473
479
  return key.toString();
474
480
  }
475
481
  getExpirationTimestamp(ttlInSeconds) {
482
+ if (typeof ttlInSeconds === "string") {
483
+ return shorthandToTime(ttlInSeconds);
484
+ }
476
485
  const currentTimestamp = Date.now();
477
486
  const ttlInMilliseconds = ttlInSeconds * 1e3;
478
487
  const expirationTimestamp = currentTimestamp + ttlInMilliseconds;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cacheable/node-cache",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "Simple and Maintained fast NodeJS internal caching",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -42,7 +42,7 @@
42
42
  "dependencies": {
43
43
  "cacheable": "^1.8.1",
44
44
  "eventemitter3": "^5.0.1",
45
- "keyv": "^5.1.2"
45
+ "keyv": "^5.2.1"
46
46
  },
47
47
  "files": [
48
48
  "dist",