@cacheable/node-cache 1.3.0 → 1.4.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.js CHANGED
@@ -24,36 +24,87 @@ var NodeCacheStore = class {
24
24
  }
25
25
  }
26
26
  }
27
+ /**
28
+ * Cacheable instance.
29
+ * @returns {Cacheable}
30
+ * @readonly
31
+ */
27
32
  get cache() {
28
33
  return this._cache;
29
34
  }
35
+ /**
36
+ * Time to live in milliseconds.
37
+ * @returns {number | string | undefined}
38
+ * @readonly
39
+ */
30
40
  get ttl() {
31
41
  return this._cache.ttl;
32
42
  }
43
+ /**
44
+ * Time to live in milliseconds.
45
+ * @param {number | string | undefined} ttl
46
+ */
33
47
  set ttl(ttl) {
34
48
  this._cache.ttl = ttl;
35
49
  }
50
+ /**
51
+ * Primary cache store.
52
+ * @returns {Keyv}
53
+ * @readonly
54
+ */
36
55
  get primary() {
37
56
  return this._cache.primary;
38
57
  }
58
+ /**
59
+ * Primary cache store.
60
+ * @param {Keyv} primary
61
+ */
39
62
  set primary(primary) {
40
63
  this._cache.primary = primary;
41
64
  }
65
+ /**
66
+ * Secondary cache store. Learn more about the secondary cache store in the
67
+ * [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
68
+ * @returns {Keyv | undefined}
69
+ */
42
70
  get secondary() {
43
71
  return this._cache.secondary;
44
72
  }
73
+ /**
74
+ * Secondary cache store. Learn more about the secondary cache store in the
75
+ * [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
76
+ * @param {Keyv | undefined} secondary
77
+ */
45
78
  set secondary(secondary) {
46
79
  this._cache.secondary = secondary;
47
80
  }
81
+ /**
82
+ * Maximum number of keys to store in the cache. if this is set to a value greater than 0,
83
+ * the cache will keep track of the number of keys and will not store more than the specified number of keys.
84
+ * @returns {number}
85
+ * @readonly
86
+ */
48
87
  get maxKeys() {
49
88
  return this._maxKeys;
50
89
  }
90
+ /**
91
+ * Maximum number of keys to store in the cache. if this is set to a value greater than 0,
92
+ * the cache will keep track of the number of keys and will not store more than the specified number of keys.
93
+ * @param {number} maxKeys
94
+ */
51
95
  set maxKeys(maxKeys) {
52
96
  this._maxKeys = maxKeys;
53
97
  if (this._maxKeys > 0) {
54
98
  this._cache.stats.enabled = true;
55
99
  }
56
100
  }
101
+ /**
102
+ * Set a key/value pair in the cache.
103
+ * @param {string | number} key
104
+ * @param {any} value
105
+ * @param {number} [ttl]
106
+ * @returns {boolean}
107
+ */
57
108
  async set(key, value, ttl) {
58
109
  if (this._maxKeys > 0) {
59
110
  if (this._cache.stats.count >= this._maxKeys) {
@@ -63,6 +114,11 @@ var NodeCacheStore = class {
63
114
  await this._cache.set(key.toString(), value, ttl);
64
115
  return true;
65
116
  }
117
+ /**
118
+ * Set multiple key/value pairs in the cache.
119
+ * @param {NodeCacheItem[]} list
120
+ * @returns {void}
121
+ */
66
122
  async mset(list) {
67
123
  const items = new Array();
68
124
  for (const item of list) {
@@ -70,9 +126,19 @@ var NodeCacheStore = class {
70
126
  }
71
127
  await this._cache.setMany(items);
72
128
  }
129
+ /**
130
+ * Get a value from the cache.
131
+ * @param {string | number} key
132
+ * @returns {any | undefined}
133
+ */
73
134
  async get(key) {
74
135
  return this._cache.get(key.toString());
75
136
  }
137
+ /**
138
+ * Get multiple values from the cache.
139
+ * @param {Array<string | number>} keys
140
+ * @returns {Record<string, any | undefined>}
141
+ */
76
142
  async mget(keys) {
77
143
  const result = {};
78
144
  for (const key of keys) {
@@ -80,15 +146,34 @@ var NodeCacheStore = class {
80
146
  }
81
147
  return result;
82
148
  }
149
+ /**
150
+ * Delete a key from the cache.
151
+ * @param {string | number} key
152
+ * @returns {boolean}
153
+ */
83
154
  async del(key) {
84
155
  return this._cache.delete(key.toString());
85
156
  }
157
+ /**
158
+ * Delete multiple keys from the cache.
159
+ * @param {Array<string | number>} keys
160
+ * @returns {boolean}
161
+ */
86
162
  async mdel(keys) {
87
163
  return this._cache.deleteMany(keys.map((key) => key.toString()));
88
164
  }
165
+ /**
166
+ * Clear the cache.
167
+ * @returns {void}
168
+ */
89
169
  async clear() {
90
170
  return this._cache.clear();
91
171
  }
172
+ /**
173
+ * Check if a key exists in the cache.
174
+ * @param {string | number} key
175
+ * @returns {boolean}
176
+ */
92
177
  async setTtl(key, ttl) {
93
178
  const item = await this._cache.get(key.toString());
94
179
  if (item) {
@@ -97,9 +182,18 @@ var NodeCacheStore = class {
97
182
  }
98
183
  return false;
99
184
  }
185
+ /**
186
+ * Check if a key exists in the cache. If it does exist it will get the value and delete the item from the cache.
187
+ * @param {string | number} key
188
+ * @returns {any | undefined}
189
+ */
100
190
  async take(key) {
101
191
  return this._cache.take(key.toString());
102
192
  }
193
+ /**
194
+ * Disconnect from the cache.
195
+ * @returns {void}
196
+ */
103
197
  async disconnect() {
104
198
  await this._cache.disconnect();
105
199
  }
@@ -133,7 +227,13 @@ var NodeCache = class extends eventemitter {
133
227
  }
134
228
  this.startInterval();
135
229
  }
136
- // Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
230
+ /**
231
+ * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
232
+ * @param {string | number} key - it will convert the key to a string
233
+ * @param {any} value
234
+ * @param {number} [ttl] - this is in seconds and undefined will use the default ttl
235
+ * @returns {boolean}
236
+ */
137
237
  set(key, value, ttl) {
138
238
  if (typeof key !== "string" && typeof key !== "number") {
139
239
  throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
@@ -160,7 +260,11 @@ var NodeCache = class extends eventemitter {
160
260
  this._stats.setCount(this.store.size);
161
261
  return true;
162
262
  }
163
- // Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
263
+ /**
264
+ * Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
265
+ * @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
266
+ * @returns {boolean}
267
+ */
164
268
  mset(data) {
165
269
  if (!Array.isArray(data)) {
166
270
  throw this.createError("The keys argument has to be an array." /* EKEYSTYPE */);
@@ -170,7 +274,11 @@ var NodeCache = class extends eventemitter {
170
274
  }
171
275
  return true;
172
276
  }
173
- // Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
277
+ /**
278
+ * Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
279
+ * @param {string | number} key if the key is a number it will convert it to a string
280
+ * @returns {T} the value or undefined
281
+ */
174
282
  get(key) {
175
283
  const result = this.store.get(this.formatKey(key));
176
284
  if (result) {
@@ -198,10 +306,12 @@ var NodeCache = class extends eventemitter {
198
306
  this._stats.incrementMisses();
199
307
  return void 0;
200
308
  }
201
- /*
202
- Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
203
- If the value was found it returns an object with the key value pair.
204
- */
309
+ /**
310
+ * Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
311
+ * If the value was found it returns an object with the key value pair.
312
+ * @param {Array<string | number} keys an array of keys
313
+ * @returns {Record<string, unknown>} an object with the key as a property and the value as the value
314
+ */
205
315
  mget(keys) {
206
316
  const result = {};
207
317
  for (const key of keys) {
@@ -212,11 +322,12 @@ var NodeCache = class extends eventemitter {
212
322
  }
213
323
  return result;
214
324
  }
215
- /*
216
- Get the cached value and remove the key from the cache.
217
- Equivalent to calling get(key) + del(key).
218
- Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
219
- */
325
+ /**
326
+ * Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key).
327
+ * Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
328
+ * @param {string | number} key
329
+ * @returns {T | undefined} the value or undefined
330
+ */
220
331
  take(key) {
221
332
  const result = this.get(key);
222
333
  if (result) {
@@ -228,7 +339,11 @@ var NodeCache = class extends eventemitter {
228
339
  }
229
340
  return void 0;
230
341
  }
231
- // Delete a key. Returns the number of deleted entries. A delete will never fail.
342
+ /**
343
+ * Delete a key. Returns the number of deleted entries. A delete will never fail.
344
+ * @param {string | number | Array<string | number>} key if the key is a number it will convert it to a string. if an array is passed it will delete all keys in the array.
345
+ * @returns {number} if it was successful it will return the count that was deleted
346
+ */
232
347
  del(key) {
233
348
  if (Array.isArray(key)) {
234
349
  return this.mdel(key);
@@ -245,7 +360,11 @@ var NodeCache = class extends eventemitter {
245
360
  }
246
361
  return 0;
247
362
  }
248
- // Delete all keys in Array that exist. Returns the number of deleted entries.
363
+ /**
364
+ * Delete all keys in Array that exist. Returns the number of deleted entries.
365
+ * @param {Array<string | number>} keys an array of keys
366
+ * @returns {number} the count of deleted keys
367
+ */
249
368
  mdel(keys) {
250
369
  let result = 0;
251
370
  for (const key of keys) {
@@ -253,8 +372,13 @@ var NodeCache = class extends eventemitter {
253
372
  }
254
373
  return result;
255
374
  }
256
- // Redefine the ttl of a key. Returns true if the key has been found and changed.
257
- // Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
375
+ /**
376
+ * Redefine the ttl of a key. Returns true if the key has been found and changed.
377
+ * Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
378
+ * @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
380
+ * @returns {boolean} true if the key has been found and changed. Otherwise returns false.
381
+ */
258
382
  ttl(key, ttl) {
259
383
  const result = this.store.get(this.formatKey(key));
260
384
  if (result) {
@@ -265,13 +389,12 @@ var NodeCache = class extends eventemitter {
265
389
  }
266
390
  return false;
267
391
  }
268
- /*
269
- Receive the ttl of a key. You will get:
270
-
271
- undefined if the key does not exist
272
- 0 if this key has no ttl
273
- a timestamp in ms representing the time at which the key will expire
274
- */
392
+ /**
393
+ * Receive the ttl of a key.
394
+ * @param {string | number} key if the key is a number it will convert it to a string
395
+ * @returns {number | undefined} 0 if this key has no ttl, undefined if this key is not in the cache,
396
+ * a timestamp in ms representing the time at which this key will expire
397
+ */
275
398
  getTtl(key) {
276
399
  const result = this.store.get(this.formatKey(key));
277
400
  if (result) {
@@ -282,10 +405,10 @@ var NodeCache = class extends eventemitter {
282
405
  }
283
406
  return void 0;
284
407
  }
285
- /*
286
- Returns an array of all existing keys.
287
- [ "all", "my", "keys", "foo", "bar" ]
288
- */
408
+ /**
409
+ * Returns an array of all existing keys. [ "all", "my", "keys", "foo", "bar" ]
410
+ * @returns {string[]} an array of all keys
411
+ */
289
412
  keys() {
290
413
  const result = [];
291
414
  for (const key of this.store.keys()) {
@@ -293,11 +416,18 @@ var NodeCache = class extends eventemitter {
293
416
  }
294
417
  return result;
295
418
  }
296
- // Returns boolean indicating if the key is cached.
419
+ /**
420
+ * Returns boolean indicating if the key is cached.
421
+ * @param {string | number} key if the key is a number it will convert it to a string
422
+ * @returns {boolean} true if the key is cached
423
+ */
297
424
  has(key) {
298
425
  return this.store.has(this.formatKey(key));
299
426
  }
300
- // Gets the stats of the cache.
427
+ /**
428
+ * Gets the stats of the cache
429
+ * @returns {NodeCacheStats} the stats of the cache
430
+ */
301
431
  getStats() {
302
432
  const stats = {
303
433
  keys: this._stats.count,
@@ -308,22 +438,34 @@ var NodeCache = class extends eventemitter {
308
438
  };
309
439
  return stats;
310
440
  }
311
- // Flush the whole data.
441
+ /**
442
+ * Flush the whole data.
443
+ * @returns {void}
444
+ */
312
445
  flushAll() {
313
446
  this.store.clear();
314
447
  this.flushStats();
315
448
  this.emit("flush");
316
449
  }
317
- // Flush the stats
450
+ /**
451
+ * Flush the stats.
452
+ * @returns {void}
453
+ */
318
454
  flushStats() {
319
455
  this._stats = new CacheableStats({ enabled: true });
320
456
  this.emit("flush_stats");
321
457
  }
322
- // Close the cache. This will clear the interval timeout which is set on check period option.
458
+ /**
459
+ * Close the cache. This will clear the interval timeout which is set on check period option.
460
+ * @returns {void}
461
+ */
323
462
  close() {
324
463
  this.stopInterval();
325
464
  }
326
- // Get the interval id
465
+ /**
466
+ * Get the interval id
467
+ * @returns {number | NodeJS.Timeout} the interval id
468
+ */
327
469
  getIntervalId() {
328
470
  return this.intervalId;
329
471
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cacheable/node-cache",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "description": "Simple and Maintained fast NodeJS internal caching",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -12,7 +12,11 @@
12
12
  "import": "./dist/index.js"
13
13
  }
14
14
  },
15
- "repository": "https://github.com/jaredwray/cacheable.git",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/jaredwray/cacheable.git",
18
+ "directory": "packages/node-cache"
19
+ },
16
20
  "author": "Jared Wray <me@jaredwray.com>",
17
21
  "license": "MIT",
18
22
  "private": false,
@@ -27,18 +31,18 @@
27
31
  "cacheable-node"
28
32
  ],
29
33
  "devDependencies": {
30
- "@types/node": "^22.7.4",
31
- "@vitest/coverage-v8": "^2.1.1",
34
+ "@types/node": "^22.8.1",
35
+ "@vitest/coverage-v8": "^2.1.3",
32
36
  "rimraf": "^6.0.1",
33
- "tsup": "^8.3.0",
34
- "typescript": "^5.6.2",
35
- "vitest": "^2.1.1",
37
+ "tsup": "^8.3.5",
38
+ "typescript": "^5.6.3",
39
+ "vitest": "^2.1.3",
36
40
  "xo": "^0.59.3"
37
41
  },
38
42
  "dependencies": {
39
- "cacheable": "^1.7.0",
43
+ "cacheable": "^1.8.1",
40
44
  "eventemitter3": "^5.0.1",
41
- "keyv": "^5.0.3"
45
+ "keyv": "^5.1.2"
42
46
  },
43
47
  "files": [
44
48
  "dist",