@cacheable/node-cache 1.5.8 → 1.6.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 CHANGED
@@ -60,6 +60,16 @@ cache.set('foo', 'bar');
60
60
  cache.get('foo'); // 'bar'
61
61
  ```
62
62
 
63
+ `NodeCache` also offers the ability to set the type of values that can be cached in Typescript environments.
64
+
65
+ ```typescript
66
+ import {NodeCache} from '@cacheable/node-cache';
67
+
68
+ const cache = new NodeCache<string>();
69
+ cache.set('foo', 'bar');
70
+ cache.get('foo'); // 'bar'
71
+ ```
72
+
63
73
  # NodeCache Performance
64
74
 
65
75
  The performance is comparable if not faster to the original `node-cache` package, but with additional features and improvements.
package/dist/index.cjs CHANGED
@@ -79,7 +79,7 @@ var NodeCacheStore = class extends import_hookified.Hookified {
79
79
  }
80
80
  /**
81
81
  * Primary cache store.
82
- * @returns {Keyv}
82
+ * @returns {Keyv<T>}
83
83
  * @readonly
84
84
  */
85
85
  get primary() {
@@ -87,7 +87,7 @@ var NodeCacheStore = class extends import_hookified.Hookified {
87
87
  }
88
88
  /**
89
89
  * Primary cache store.
90
- * @param {Keyv} primary
90
+ * @param {Keyv<T>} primary
91
91
  */
92
92
  set primary(primary) {
93
93
  this._cache.primary = primary;
@@ -95,7 +95,7 @@ var NodeCacheStore = class extends import_hookified.Hookified {
95
95
  /**
96
96
  * Secondary cache store. Learn more about the secondary cache store in the
97
97
  * [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
98
- * @returns {Keyv | undefined}
98
+ * @returns {Keyv<T> | undefined}
99
99
  */
100
100
  get secondary() {
101
101
  return this._cache.secondary;
@@ -131,7 +131,7 @@ var NodeCacheStore = class extends import_hookified.Hookified {
131
131
  /**
132
132
  * Set a key/value pair in the cache.
133
133
  * @param {string | number} key
134
- * @param {any} value
134
+ * @param {T} value
135
135
  * @param {number} [ttl]
136
136
  * @returns {boolean}
137
137
  */
@@ -146,7 +146,7 @@ var NodeCacheStore = class extends import_hookified.Hookified {
146
146
  }
147
147
  /**
148
148
  * Set multiple key/value pairs in the cache.
149
- * @param {NodeCacheItem[]} list
149
+ * @param {PartialNodeCacheItem[]} list
150
150
  * @returns {void}
151
151
  */
152
152
  async mset(list) {
@@ -260,11 +260,11 @@ var NodeCache = class extends import_hookified2.Hookified {
260
260
  /**
261
261
  * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
262
262
  * @param {string | number} key - it will convert the key to a string
263
- * @param {any} value
263
+ * @param {T} value
264
264
  * @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
265
265
  * @returns {boolean}
266
266
  */
267
- set(key, value, ttl) {
267
+ set(key, value, ttl = 0) {
268
268
  if (typeof key !== "string" && typeof key !== "number") {
269
269
  throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
270
270
  }
@@ -298,7 +298,7 @@ var NodeCache = class extends import_hookified2.Hookified {
298
298
  }
299
299
  /**
300
300
  * Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
301
- * @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
301
+ * @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
302
302
  * @returns {boolean}
303
303
  */
304
304
  mset(data) {
package/dist/index.d.cts CHANGED
@@ -2,7 +2,7 @@ import { Hookified } from 'hookified';
2
2
  import { Cacheable } from 'cacheable';
3
3
  import { Keyv } from 'keyv';
4
4
 
5
- type NodeCacheStoreOptions = {
5
+ type NodeCacheStoreOptions<T> = {
6
6
  /**
7
7
  * Time to live in milliseconds. This is a breaking change from the original NodeCache.
8
8
  */
@@ -14,21 +14,21 @@ type NodeCacheStoreOptions = {
14
14
  /**
15
15
  * Primary cache store.
16
16
  */
17
- primary?: Keyv;
17
+ primary?: Keyv<T>;
18
18
  /**
19
19
  * Secondary cache store. Learn more about the secondary cache store in the cacheable documentation.
20
20
  * [storage-tiering-and-caching](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching)
21
21
  */
22
- secondary?: Keyv;
22
+ secondary?: Keyv<T>;
23
23
  /**
24
24
  * Enable stats tracking. This is a breaking change from the original NodeCache.
25
25
  */
26
26
  stats?: boolean;
27
27
  };
28
- declare class NodeCacheStore extends Hookified {
28
+ declare class NodeCacheStore<T> extends Hookified {
29
29
  private _maxKeys;
30
30
  private readonly _cache;
31
- constructor(options?: NodeCacheStoreOptions);
31
+ constructor(options?: NodeCacheStoreOptions<T>);
32
32
  /**
33
33
  * Cacheable instance.
34
34
  * @returns {Cacheable}
@@ -48,21 +48,21 @@ declare class NodeCacheStore extends Hookified {
48
48
  set ttl(ttl: number | string | undefined);
49
49
  /**
50
50
  * Primary cache store.
51
- * @returns {Keyv}
51
+ * @returns {Keyv<T>}
52
52
  * @readonly
53
53
  */
54
- get primary(): Keyv;
54
+ get primary(): Keyv<T>;
55
55
  /**
56
56
  * Primary cache store.
57
- * @param {Keyv} primary
57
+ * @param {Keyv<T>} primary
58
58
  */
59
- set primary(primary: Keyv);
59
+ set primary(primary: Keyv<T>);
60
60
  /**
61
61
  * Secondary cache store. Learn more about the secondary cache store in the
62
62
  * [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
63
- * @returns {Keyv | undefined}
63
+ * @returns {Keyv<T> | undefined}
64
64
  */
65
- get secondary(): Keyv | undefined;
65
+ get secondary(): Keyv<T> | undefined;
66
66
  /**
67
67
  * Secondary cache store. Learn more about the secondary cache store in the
68
68
  * [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
@@ -85,17 +85,17 @@ declare class NodeCacheStore extends Hookified {
85
85
  /**
86
86
  * Set a key/value pair in the cache.
87
87
  * @param {string | number} key
88
- * @param {any} value
88
+ * @param {T} value
89
89
  * @param {number} [ttl]
90
90
  * @returns {boolean}
91
91
  */
92
- set(key: string | number, value: any, ttl?: number): Promise<boolean>;
92
+ set(key: string | number, value: T, ttl?: number): Promise<boolean>;
93
93
  /**
94
94
  * Set multiple key/value pairs in the cache.
95
- * @param {NodeCacheItem[]} list
95
+ * @param {PartialNodeCacheItem[]} list
96
96
  * @returns {void}
97
97
  */
98
- mset(list: NodeCacheItem[]): Promise<void>;
98
+ mset(list: Array<PartialNodeCacheItem<T>>): Promise<void>;
99
99
  /**
100
100
  * Get a value from the cache.
101
101
  * @param {string | number} key
@@ -172,7 +172,7 @@ type NodeCacheOptions = {
172
172
  */
173
173
  maxKeys?: number;
174
174
  };
175
- type NodeCacheItem = {
175
+ type PartialNodeCacheItem<T> = {
176
176
  /**
177
177
  * The key of the item
178
178
  */
@@ -180,12 +180,18 @@ type NodeCacheItem = {
180
180
  /**
181
181
  * The value of the item
182
182
  */
183
- value: unknown;
183
+ value: T;
184
184
  /**
185
185
  * The ttl of the item in seconds. 0 = unlimited
186
186
  */
187
187
  ttl?: number;
188
188
  };
189
+ type NodeCacheItem<T> = PartialNodeCacheItem<T> & {
190
+ /**
191
+ * The ttl of the item in milliseconds. 0 = unlimited
192
+ */
193
+ ttl: number;
194
+ };
189
195
  declare enum NodeCacheErrors {
190
196
  ECACHEFULL = "Cache max keys amount exceeded",
191
197
  EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
@@ -214,9 +220,9 @@ type NodeCacheStats = {
214
220
  */
215
221
  vsize: number;
216
222
  };
217
- declare class NodeCache extends Hookified {
223
+ declare class NodeCache<T> extends Hookified {
218
224
  readonly options: NodeCacheOptions;
219
- readonly store: Map<string, any>;
225
+ readonly store: Map<string, NodeCacheItem<T>>;
220
226
  private _stats;
221
227
  private readonly _cacheable;
222
228
  private intervalId;
@@ -224,23 +230,23 @@ declare class NodeCache extends Hookified {
224
230
  /**
225
231
  * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
226
232
  * @param {string | number} key - it will convert the key to a string
227
- * @param {any} value
233
+ * @param {T} value
228
234
  * @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
229
235
  * @returns {boolean}
230
236
  */
231
- set(key: string | number, value: any, ttl?: number | string): boolean;
237
+ set(key: string | number, value: T, ttl?: number | string): boolean;
232
238
  /**
233
239
  * Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
234
- * @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
240
+ * @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
235
241
  * @returns {boolean}
236
242
  */
237
- mset(data: NodeCacheItem[]): boolean;
243
+ mset(data: Array<PartialNodeCacheItem<T>>): boolean;
238
244
  /**
239
245
  * Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
240
246
  * @param {string | number} key if the key is a number it will convert it to a string
241
247
  * @returns {T} the value or undefined
242
248
  */
243
- get<T>(key: string | number): T | undefined;
249
+ get(key: string | number): T | undefined;
244
250
  /**
245
251
  * Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
246
252
  * If the value was found it returns an object with the key value pair.
@@ -326,4 +332,4 @@ declare class NodeCache extends Hookified {
326
332
  private createError;
327
333
  }
328
334
 
329
- export { NodeCache, NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, NodeCache as default };
335
+ export { NodeCache, NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, type PartialNodeCacheItem, NodeCache as default };
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Hookified } from 'hookified';
2
2
  import { Cacheable } from 'cacheable';
3
3
  import { Keyv } from 'keyv';
4
4
 
5
- type NodeCacheStoreOptions = {
5
+ type NodeCacheStoreOptions<T> = {
6
6
  /**
7
7
  * Time to live in milliseconds. This is a breaking change from the original NodeCache.
8
8
  */
@@ -14,21 +14,21 @@ type NodeCacheStoreOptions = {
14
14
  /**
15
15
  * Primary cache store.
16
16
  */
17
- primary?: Keyv;
17
+ primary?: Keyv<T>;
18
18
  /**
19
19
  * Secondary cache store. Learn more about the secondary cache store in the cacheable documentation.
20
20
  * [storage-tiering-and-caching](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching)
21
21
  */
22
- secondary?: Keyv;
22
+ secondary?: Keyv<T>;
23
23
  /**
24
24
  * Enable stats tracking. This is a breaking change from the original NodeCache.
25
25
  */
26
26
  stats?: boolean;
27
27
  };
28
- declare class NodeCacheStore extends Hookified {
28
+ declare class NodeCacheStore<T> extends Hookified {
29
29
  private _maxKeys;
30
30
  private readonly _cache;
31
- constructor(options?: NodeCacheStoreOptions);
31
+ constructor(options?: NodeCacheStoreOptions<T>);
32
32
  /**
33
33
  * Cacheable instance.
34
34
  * @returns {Cacheable}
@@ -48,21 +48,21 @@ declare class NodeCacheStore extends Hookified {
48
48
  set ttl(ttl: number | string | undefined);
49
49
  /**
50
50
  * Primary cache store.
51
- * @returns {Keyv}
51
+ * @returns {Keyv<T>}
52
52
  * @readonly
53
53
  */
54
- get primary(): Keyv;
54
+ get primary(): Keyv<T>;
55
55
  /**
56
56
  * Primary cache store.
57
- * @param {Keyv} primary
57
+ * @param {Keyv<T>} primary
58
58
  */
59
- set primary(primary: Keyv);
59
+ set primary(primary: Keyv<T>);
60
60
  /**
61
61
  * Secondary cache store. Learn more about the secondary cache store in the
62
62
  * [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
63
- * @returns {Keyv | undefined}
63
+ * @returns {Keyv<T> | undefined}
64
64
  */
65
- get secondary(): Keyv | undefined;
65
+ get secondary(): Keyv<T> | undefined;
66
66
  /**
67
67
  * Secondary cache store. Learn more about the secondary cache store in the
68
68
  * [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
@@ -85,17 +85,17 @@ declare class NodeCacheStore extends Hookified {
85
85
  /**
86
86
  * Set a key/value pair in the cache.
87
87
  * @param {string | number} key
88
- * @param {any} value
88
+ * @param {T} value
89
89
  * @param {number} [ttl]
90
90
  * @returns {boolean}
91
91
  */
92
- set(key: string | number, value: any, ttl?: number): Promise<boolean>;
92
+ set(key: string | number, value: T, ttl?: number): Promise<boolean>;
93
93
  /**
94
94
  * Set multiple key/value pairs in the cache.
95
- * @param {NodeCacheItem[]} list
95
+ * @param {PartialNodeCacheItem[]} list
96
96
  * @returns {void}
97
97
  */
98
- mset(list: NodeCacheItem[]): Promise<void>;
98
+ mset(list: Array<PartialNodeCacheItem<T>>): Promise<void>;
99
99
  /**
100
100
  * Get a value from the cache.
101
101
  * @param {string | number} key
@@ -172,7 +172,7 @@ type NodeCacheOptions = {
172
172
  */
173
173
  maxKeys?: number;
174
174
  };
175
- type NodeCacheItem = {
175
+ type PartialNodeCacheItem<T> = {
176
176
  /**
177
177
  * The key of the item
178
178
  */
@@ -180,12 +180,18 @@ type NodeCacheItem = {
180
180
  /**
181
181
  * The value of the item
182
182
  */
183
- value: unknown;
183
+ value: T;
184
184
  /**
185
185
  * The ttl of the item in seconds. 0 = unlimited
186
186
  */
187
187
  ttl?: number;
188
188
  };
189
+ type NodeCacheItem<T> = PartialNodeCacheItem<T> & {
190
+ /**
191
+ * The ttl of the item in milliseconds. 0 = unlimited
192
+ */
193
+ ttl: number;
194
+ };
189
195
  declare enum NodeCacheErrors {
190
196
  ECACHEFULL = "Cache max keys amount exceeded",
191
197
  EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
@@ -214,9 +220,9 @@ type NodeCacheStats = {
214
220
  */
215
221
  vsize: number;
216
222
  };
217
- declare class NodeCache extends Hookified {
223
+ declare class NodeCache<T> extends Hookified {
218
224
  readonly options: NodeCacheOptions;
219
- readonly store: Map<string, any>;
225
+ readonly store: Map<string, NodeCacheItem<T>>;
220
226
  private _stats;
221
227
  private readonly _cacheable;
222
228
  private intervalId;
@@ -224,23 +230,23 @@ declare class NodeCache extends Hookified {
224
230
  /**
225
231
  * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
226
232
  * @param {string | number} key - it will convert the key to a string
227
- * @param {any} value
233
+ * @param {T} value
228
234
  * @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
229
235
  * @returns {boolean}
230
236
  */
231
- set(key: string | number, value: any, ttl?: number | string): boolean;
237
+ set(key: string | number, value: T, ttl?: number | string): boolean;
232
238
  /**
233
239
  * Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
234
- * @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
240
+ * @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
235
241
  * @returns {boolean}
236
242
  */
237
- mset(data: NodeCacheItem[]): boolean;
243
+ mset(data: Array<PartialNodeCacheItem<T>>): boolean;
238
244
  /**
239
245
  * Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
240
246
  * @param {string | number} key if the key is a number it will convert it to a string
241
247
  * @returns {T} the value or undefined
242
248
  */
243
- get<T>(key: string | number): T | undefined;
249
+ get(key: string | number): T | undefined;
244
250
  /**
245
251
  * Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
246
252
  * If the value was found it returns an object with the key value pair.
@@ -326,4 +332,4 @@ declare class NodeCache extends Hookified {
326
332
  private createError;
327
333
  }
328
334
 
329
- export { NodeCache, NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, NodeCache as default };
335
+ export { NodeCache, NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, type PartialNodeCacheItem, NodeCache as default };
package/dist/index.js CHANGED
@@ -52,7 +52,7 @@ var NodeCacheStore = class extends Hookified {
52
52
  }
53
53
  /**
54
54
  * Primary cache store.
55
- * @returns {Keyv}
55
+ * @returns {Keyv<T>}
56
56
  * @readonly
57
57
  */
58
58
  get primary() {
@@ -60,7 +60,7 @@ var NodeCacheStore = class extends Hookified {
60
60
  }
61
61
  /**
62
62
  * Primary cache store.
63
- * @param {Keyv} primary
63
+ * @param {Keyv<T>} primary
64
64
  */
65
65
  set primary(primary) {
66
66
  this._cache.primary = primary;
@@ -68,7 +68,7 @@ var NodeCacheStore = class extends Hookified {
68
68
  /**
69
69
  * Secondary cache store. Learn more about the secondary cache store in the
70
70
  * [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
71
- * @returns {Keyv | undefined}
71
+ * @returns {Keyv<T> | undefined}
72
72
  */
73
73
  get secondary() {
74
74
  return this._cache.secondary;
@@ -104,7 +104,7 @@ var NodeCacheStore = class extends Hookified {
104
104
  /**
105
105
  * Set a key/value pair in the cache.
106
106
  * @param {string | number} key
107
- * @param {any} value
107
+ * @param {T} value
108
108
  * @param {number} [ttl]
109
109
  * @returns {boolean}
110
110
  */
@@ -119,7 +119,7 @@ var NodeCacheStore = class extends Hookified {
119
119
  }
120
120
  /**
121
121
  * Set multiple key/value pairs in the cache.
122
- * @param {NodeCacheItem[]} list
122
+ * @param {PartialNodeCacheItem[]} list
123
123
  * @returns {void}
124
124
  */
125
125
  async mset(list) {
@@ -233,11 +233,11 @@ var NodeCache = class extends Hookified2 {
233
233
  /**
234
234
  * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
235
235
  * @param {string | number} key - it will convert the key to a string
236
- * @param {any} value
236
+ * @param {T} value
237
237
  * @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
238
238
  * @returns {boolean}
239
239
  */
240
- set(key, value, ttl) {
240
+ set(key, value, ttl = 0) {
241
241
  if (typeof key !== "string" && typeof key !== "number") {
242
242
  throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
243
243
  }
@@ -271,7 +271,7 @@ var NodeCache = class extends Hookified2 {
271
271
  }
272
272
  /**
273
273
  * Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
274
- * @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
274
+ * @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
275
275
  * @returns {boolean}
276
276
  */
277
277
  mset(data) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cacheable/node-cache",
3
- "version": "1.5.8",
3
+ "version": "1.6.1",
4
4
  "description": "Simple and Maintained fast NodeJS internal caching",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -32,18 +32,18 @@
32
32
  ],
33
33
  "devDependencies": {
34
34
  "@types/eslint": "^9.6.1",
35
- "@types/node": "^24.0.7",
35
+ "@types/node": "^24.1.0",
36
36
  "@vitest/coverage-v8": "^3.2.4",
37
37
  "rimraf": "^6.0.1",
38
38
  "tsup": "^8.5.0",
39
39
  "typescript": "^5.8.3",
40
40
  "vitest": "^3.2.4",
41
- "xo": "^1.1.1"
41
+ "xo": "^1.2.1"
42
42
  },
43
43
  "dependencies": {
44
44
  "hookified": "^1.10.0",
45
- "keyv": "^5.3.4",
46
- "cacheable": "^1.10.1"
45
+ "keyv": "^5.4.0",
46
+ "cacheable": "^1.10.3"
47
47
  },
48
48
  "files": [
49
49
  "dist",