cacheable 1.8.0 → 1.8.2

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
@@ -114,11 +114,23 @@ var shorthandToTime = (shorthand, fromDate) => {
114
114
  return fromDate.getTime() + milliseconds;
115
115
  };
116
116
 
117
+ // src/hash.ts
118
+ var crypto = __toESM(require("crypto"), 1);
119
+ function hash(object, algorithm = "sha256") {
120
+ const objectString = JSON.stringify(object);
121
+ if (!crypto.getHashes().includes(algorithm)) {
122
+ throw new Error(`Unsupported hash algorithm: '${algorithm}'`);
123
+ }
124
+ const hasher = crypto.createHash(algorithm);
125
+ hasher.update(objectString);
126
+ return hasher.digest("hex");
127
+ }
128
+
117
129
  // src/wrap.ts
118
130
  function wrapSync(function_, options) {
119
- const { ttl, key, cache } = options;
131
+ const { ttl, keyPrefix, cache } = options;
120
132
  return function(...arguments_) {
121
- const cacheKey = key ?? cache.hash(arguments_);
133
+ const cacheKey = createWrapKey(function_, arguments_, keyPrefix);
122
134
  let value = cache.get(cacheKey);
123
135
  if (value === void 0) {
124
136
  value = function_(...arguments_);
@@ -128,9 +140,9 @@ function wrapSync(function_, options) {
128
140
  };
129
141
  }
130
142
  function wrap(function_, options) {
131
- const { ttl, key, cache } = options;
143
+ const { ttl, keyPrefix, cache } = options;
132
144
  return async function(...arguments_) {
133
- const cacheKey = key ?? cache.hash(arguments_);
145
+ const cacheKey = createWrapKey(function_, arguments_, keyPrefix);
134
146
  let value = await cache.get(cacheKey);
135
147
  if (value === void 0) {
136
148
  value = await function_(...arguments_);
@@ -139,6 +151,12 @@ function wrap(function_, options) {
139
151
  return value;
140
152
  };
141
153
  }
154
+ function createWrapKey(function_, arguments_, keyPrefix) {
155
+ if (!keyPrefix) {
156
+ return `${function_.name}::${hash(arguments_)}`;
157
+ }
158
+ return `${keyPrefix}::${function_.name}::${hash(arguments_)}`;
159
+ }
142
160
 
143
161
  // src/memory-lru.ts
144
162
  var ListNode = class {
@@ -213,20 +231,9 @@ var DoublyLinkedList = class {
213
231
  }
214
232
  };
215
233
 
216
- // src/hash.ts
217
- var crypto = __toESM(require("crypto"), 1);
218
- function hash(object, algorithm = "sha256") {
219
- const objectString = JSON.stringify(object);
220
- if (!crypto.getHashes().includes(algorithm)) {
221
- throw new Error(`Unsupported hash algorithm: '${algorithm}'`);
222
- }
223
- const hasher = crypto.createHash(algorithm);
224
- hasher.update(objectString);
225
- return hasher.digest("hex");
226
- }
227
-
228
234
  // src/memory.ts
229
235
  var CacheableMemory = class {
236
+ _lru = new DoublyLinkedList();
230
237
  _hashCache = /* @__PURE__ */ new Map();
231
238
  _hash0 = /* @__PURE__ */ new Map();
232
239
  _hash1 = /* @__PURE__ */ new Map();
@@ -238,7 +245,6 @@ var CacheableMemory = class {
238
245
  _hash7 = /* @__PURE__ */ new Map();
239
246
  _hash8 = /* @__PURE__ */ new Map();
240
247
  _hash9 = /* @__PURE__ */ new Map();
241
- _lru = new DoublyLinkedList();
242
248
  _ttl;
243
249
  // Turned off by default
244
250
  _useClone = true;
@@ -249,6 +255,10 @@ var CacheableMemory = class {
249
255
  // Turned off by default
250
256
  _interval = 0;
251
257
  // Turned off by default
258
+ /**
259
+ * @constructor
260
+ * @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
261
+ */
252
262
  constructor(options) {
253
263
  if (options?.ttl) {
254
264
  this.setTtl(options.ttl);
@@ -264,40 +274,89 @@ var CacheableMemory = class {
264
274
  }
265
275
  this.startIntervalCheck();
266
276
  }
277
+ /**
278
+ * Gets the time-to-live
279
+ * @returns {number|string|undefined} - The time-to-live in miliseconds or a human-readable format. If undefined, it will not have a time-to-live.
280
+ */
267
281
  get ttl() {
268
282
  return this._ttl;
269
283
  }
284
+ /**
285
+ * Sets the time-to-live
286
+ * @param {number|string|undefined} value - The time-to-live in miliseconds or a human-readable format (example '1s' = 1 second, '1h' = 1 hour). If undefined, it will not have a time-to-live.
287
+ */
270
288
  set ttl(value) {
271
289
  this.setTtl(value);
272
290
  }
291
+ /**
292
+ * Gets whether to use clone
293
+ * @returns {boolean} - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
294
+ */
273
295
  get useClone() {
274
296
  return this._useClone;
275
297
  }
298
+ /**
299
+ * Sets whether to use clone
300
+ * @param {boolean} value - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
301
+ */
276
302
  set useClone(value) {
277
303
  this._useClone = value;
278
304
  }
305
+ /**
306
+ * Gets the size of the LRU cache
307
+ * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
308
+ */
279
309
  get lruSize() {
280
310
  return this._lruSize;
281
311
  }
312
+ /**
313
+ * Sets the size of the LRU cache
314
+ * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
315
+ */
282
316
  set lruSize(value) {
283
317
  this._lruSize = value;
284
318
  this.lruResize();
285
319
  }
320
+ /**
321
+ * Gets the check interval
322
+ * @returns {number} - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
323
+ */
286
324
  get checkInterval() {
287
325
  return this._checkInterval;
288
326
  }
327
+ /**
328
+ * Sets the check interval
329
+ * @param {number} value - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
330
+ */
289
331
  set checkInterval(value) {
290
332
  this._checkInterval = value;
291
333
  }
334
+ /**
335
+ * Gets the size of the cache
336
+ * @returns {number} - The size of the cache
337
+ */
292
338
  get size() {
293
339
  return this._hash0.size + this._hash1.size + this._hash2.size + this._hash3.size + this._hash4.size + this._hash5.size + this._hash6.size + this._hash7.size + this._hash8.size + this._hash9.size;
294
340
  }
341
+ /**
342
+ * Gets the keys
343
+ * @returns {IterableIterator<string>} - The keys
344
+ */
295
345
  get keys() {
296
346
  return this.concatStores().keys();
297
347
  }
348
+ /**
349
+ * Gets the items
350
+ * @returns {IterableIterator<CacheableStoreItem>} - The items
351
+ */
298
352
  get items() {
299
353
  return this.concatStores().values();
300
354
  }
355
+ /**
356
+ * Gets the value of the key
357
+ * @param {string} key - The key to get the value
358
+ * @returns {T | undefined} - The value of the key
359
+ */
301
360
  get(key) {
302
361
  const store = this.getStore(key);
303
362
  const item = store.get(key);
@@ -314,6 +373,11 @@ var CacheableMemory = class {
314
373
  }
315
374
  return this.clone(item.value);
316
375
  }
376
+ /**
377
+ * Gets the values of the keys
378
+ * @param {string[]} keys - The keys to get the values
379
+ * @returns {T[]} - The values of the keys
380
+ */
317
381
  getMany(keys) {
318
382
  const result = new Array();
319
383
  for (const key of keys) {
@@ -321,6 +385,11 @@ var CacheableMemory = class {
321
385
  }
322
386
  return result;
323
387
  }
388
+ /**
389
+ * Gets the raw value of the key
390
+ * @param {string} key - The key to get the value
391
+ * @returns {CacheableStoreItem | undefined} - The raw value of the key
392
+ */
324
393
  getRaw(key) {
325
394
  const store = this.getStore(key);
326
395
  const item = store.get(key);
@@ -334,6 +403,11 @@ var CacheableMemory = class {
334
403
  this.lruMoveToFront(key);
335
404
  return item;
336
405
  }
406
+ /**
407
+ * Gets the raw values of the keys
408
+ * @param {string[]} keys - The keys to get the values
409
+ * @returns {CacheableStoreItem[]} - The raw values of the keys
410
+ */
337
411
  getManyRaw(keys) {
338
412
  const result = new Array();
339
413
  for (const key of keys) {
@@ -341,6 +415,14 @@ var CacheableMemory = class {
341
415
  }
342
416
  return result;
343
417
  }
418
+ /**
419
+ * Sets the value of the key
420
+ * @param {string} key - The key to set the value
421
+ * @param {any} value - The value to set
422
+ * @param {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable.
423
+ * If you set undefined, it will use the default time-to-live. If both are undefined then it will not have a time-to-live.
424
+ * @returns {void}
425
+ */
344
426
  set(key, value, ttl) {
345
427
  const store = this.getStore(key);
346
428
  let expires;
@@ -364,22 +446,49 @@ var CacheableMemory = class {
364
446
  }
365
447
  }
366
448
  }
367
- store.set(key, {
449
+ const item = { key, value, expires };
450
+ store.set(
368
451
  key,
369
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
370
- value,
371
- expires
372
- });
373
- }
452
+ item
453
+ );
454
+ }
455
+ /**
456
+ * Sets the values of the keys
457
+ * @param {CacheableItem[]} items - The items to set
458
+ * @returns {void}
459
+ */
374
460
  setMany(items) {
375
461
  for (const item of items) {
376
462
  this.set(item.key, item.value, item.ttl);
377
463
  }
378
464
  }
465
+ /**
466
+ * Checks if the key exists
467
+ * @param {string} key - The key to check
468
+ * @returns {boolean} - If true, the key exists. If false, the key does not exist.
469
+ */
379
470
  has(key) {
380
471
  const item = this.get(key);
381
472
  return Boolean(item);
382
473
  }
474
+ /**
475
+ * @function hasMany
476
+ * @param {string[]} keys - The keys to check
477
+ * @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
478
+ */
479
+ hasMany(keys) {
480
+ const result = new Array();
481
+ for (const key of keys) {
482
+ const item = this.get(key);
483
+ result.push(Boolean(item));
484
+ }
485
+ return result;
486
+ }
487
+ /**
488
+ * Take will get the key and delete the entry from cache
489
+ * @param {string} key - The key to take
490
+ * @returns {T | undefined} - The value of the key
491
+ */
383
492
  take(key) {
384
493
  const item = this.get(key);
385
494
  if (!item) {
@@ -388,6 +497,11 @@ var CacheableMemory = class {
388
497
  this.delete(key);
389
498
  return item;
390
499
  }
500
+ /**
501
+ * TakeMany will get the keys and delete the entries from cache
502
+ * @param {string[]} keys - The keys to take
503
+ * @returns {T[]} - The values of the keys
504
+ */
391
505
  takeMany(keys) {
392
506
  const result = new Array();
393
507
  for (const key of keys) {
@@ -395,15 +509,29 @@ var CacheableMemory = class {
395
509
  }
396
510
  return result;
397
511
  }
512
+ /**
513
+ * Delete the key
514
+ * @param {string} key - The key to delete
515
+ * @returns {void}
516
+ */
398
517
  delete(key) {
399
518
  const store = this.getStore(key);
400
519
  store.delete(key);
401
520
  }
521
+ /**
522
+ * Delete the keys
523
+ * @param {string[]} keys - The keys to delete
524
+ * @returns {void}
525
+ */
402
526
  deleteMany(keys) {
403
527
  for (const key of keys) {
404
528
  this.delete(key);
405
529
  }
406
530
  }
531
+ /**
532
+ * Clear the cache
533
+ * @returns {void}
534
+ */
407
535
  clear() {
408
536
  this._hash0.clear();
409
537
  this._hash1.clear();
@@ -416,24 +544,24 @@ var CacheableMemory = class {
416
544
  this._hash8.clear();
417
545
  this._hash9.clear();
418
546
  this._hashCache.clear();
547
+ this._lru = new DoublyLinkedList();
419
548
  }
420
- hashKey(key) {
421
- const cacheHashNumber = this._hashCache.get(key);
422
- if (cacheHashNumber) {
423
- return cacheHashNumber;
424
- }
425
- let hash2 = 0;
426
- const primeMultiplier = 31;
427
- for (let i = 0; i < key.length; i++) {
428
- hash2 = hash2 * primeMultiplier + key.charCodeAt(i);
429
- }
430
- const result = Math.abs(hash2) % 10;
431
- this._hashCache.set(key, result);
432
- return result;
433
- }
549
+ /**
550
+ * Get the store based on the key (internal use)
551
+ * @param {string} key - The key to get the store
552
+ * @returns {CacheableHashStore} - The store
553
+ */
434
554
  getStore(key) {
435
- const hashKey = this.hashKey(key);
436
- switch (hashKey) {
555
+ const hash2 = this.hashKey(key);
556
+ return this.getStoreFromHash(hash2);
557
+ }
558
+ /**
559
+ * Get the store based on the hash (internal use)
560
+ * @param {number} hash
561
+ * @returns {Map<string, CacheableStoreItem>}
562
+ */
563
+ getStoreFromHash(hash2) {
564
+ switch (hash2) {
437
565
  case 1: {
438
566
  return this._hash1;
439
567
  }
@@ -466,24 +594,62 @@ var CacheableMemory = class {
466
594
  }
467
595
  }
468
596
  }
597
+ /**
598
+ * Hash the key (internal use)
599
+ * @param key
600
+ * @returns {number} from 0 to 9
601
+ */
602
+ hashKey(key) {
603
+ const cacheHashNumber = this._hashCache.get(key);
604
+ if (cacheHashNumber) {
605
+ return cacheHashNumber;
606
+ }
607
+ let hash2 = 0;
608
+ const primeMultiplier = 31;
609
+ for (let i = 0; i < key.length; i++) {
610
+ hash2 = hash2 * primeMultiplier + key.charCodeAt(i);
611
+ }
612
+ const result = Math.abs(hash2) % 10;
613
+ this._hashCache.set(key, result);
614
+ return result;
615
+ }
616
+ /**
617
+ * Clone the value. This is for internal use
618
+ * @param {any} value - The value to clone
619
+ * @returns {any} - The cloned value
620
+ */
469
621
  clone(value) {
470
622
  if (this.isPrimitive(value)) {
471
623
  return value;
472
624
  }
473
625
  return structuredClone(value);
474
626
  }
627
+ /**
628
+ * Add to the front of the LRU cache. This is for internal use
629
+ * @param {string} key - The key to add to the front
630
+ * @returns {void}
631
+ */
475
632
  lruAddToFront(key) {
476
633
  if (this._lruSize === 0) {
477
634
  return;
478
635
  }
479
636
  this._lru.addToFront(key);
480
637
  }
638
+ /**
639
+ * Move to the front of the LRU cache. This is for internal use
640
+ * @param {string} key - The key to move to the front
641
+ * @returns {void}
642
+ */
481
643
  lruMoveToFront(key) {
482
644
  if (this._lruSize === 0) {
483
645
  return;
484
646
  }
485
647
  this._lru.moveToFront(key);
486
648
  }
649
+ /**
650
+ * Resize the LRU cache. This is for internal use
651
+ * @returns {void}
652
+ */
487
653
  lruResize() {
488
654
  if (this._lruSize === 0) {
489
655
  return;
@@ -496,6 +662,10 @@ var CacheableMemory = class {
496
662
  }
497
663
  }
498
664
  }
665
+ /**
666
+ * Check for expiration. This is for internal use
667
+ * @returns {void}
668
+ */
499
669
  checkExpiration() {
500
670
  const stores = this.concatStores();
501
671
  for (const item of stores.values()) {
@@ -504,6 +674,10 @@ var CacheableMemory = class {
504
674
  }
505
675
  }
506
676
  }
677
+ /**
678
+ * Start the interval check. This is for internal use
679
+ * @returns {void}
680
+ */
507
681
  startIntervalCheck() {
508
682
  if (this._checkInterval > 0) {
509
683
  this._interval = setInterval(() => {
@@ -511,6 +685,10 @@ var CacheableMemory = class {
511
685
  }, this._checkInterval);
512
686
  }
513
687
  }
688
+ /**
689
+ * Stop the interval check. This is for internal use
690
+ * @returns {void}
691
+ */
514
692
  stopIntervalCheck() {
515
693
  if (this._interval) {
516
694
  clearInterval(this._interval);
@@ -518,13 +696,25 @@ var CacheableMemory = class {
518
696
  this._interval = 0;
519
697
  this._checkInterval = 0;
520
698
  }
699
+ /**
700
+ * Hash the object. This is for internal use
701
+ * @param {any} object - The object to hash
702
+ * @param {string} [algorithm='sha256'] - The algorithm to hash
703
+ * @returns {string} - The hashed string
704
+ */
521
705
  hash(object, algorithm = "sha256") {
522
706
  return hash(object, algorithm);
523
707
  }
524
- wrap(function_, options = {}) {
708
+ /**
709
+ * Wrap the function for caching
710
+ * @param {Function} function_ - The function to wrap
711
+ * @param {Object} [options] - The options to wrap
712
+ * @returns {Function} - The wrapped function
713
+ */
714
+ wrap(function_, options) {
525
715
  const wrapOptions = {
526
716
  ttl: options.ttl,
527
- key: options.key,
717
+ keyPrefix: options.keyPrefix,
528
718
  cache: this
529
719
  };
530
720
  return wrapSync(function_, wrapOptions);
@@ -540,8 +730,7 @@ var CacheableMemory = class {
540
730
  return result;
541
731
  }
542
732
  concatStores() {
543
- const result = new Map([...this._hash0, ...this._hash1, ...this._hash2, ...this._hash3, ...this._hash4, ...this._hash5, ...this._hash6, ...this._hash7, ...this._hash8, ...this._hash9]);
544
- return result;
733
+ return new Map([...this._hash0, ...this._hash1, ...this._hash2, ...this._hash3, ...this._hash4, ...this._hash5, ...this._hash6, ...this._hash7, ...this._hash8, ...this._hash9]);
545
734
  }
546
735
  setTtl(ttl) {
547
736
  if (typeof ttl === "string" || ttl === void 0) {
@@ -562,48 +751,71 @@ var KeyvCacheableMemory = class {
562
751
  lruSize: 0,
563
752
  checkInterval: 0
564
753
  };
565
- namespace;
566
- _cache = new CacheableMemory();
754
+ _defaultCache = new CacheableMemory();
755
+ _nCache = /* @__PURE__ */ new Map();
756
+ _namespace;
567
757
  constructor(options) {
568
758
  if (options) {
569
759
  this.opts = options;
570
- this._cache = new CacheableMemory(options);
760
+ this._defaultCache = new CacheableMemory(options);
761
+ if (options.namespace) {
762
+ this._namespace = options.namespace;
763
+ this._nCache.set(this._namespace, new CacheableMemory(options));
764
+ }
571
765
  }
572
766
  }
767
+ get namespace() {
768
+ return this._namespace;
769
+ }
770
+ set namespace(value) {
771
+ this._namespace = value;
772
+ }
773
+ get store() {
774
+ return this.getStore(this._namespace);
775
+ }
573
776
  async get(key) {
574
- const result = this._cache.get(key);
777
+ const result = this.getStore(this._namespace).get(key);
575
778
  if (result) {
576
779
  return result;
577
780
  }
578
781
  return void 0;
579
782
  }
580
783
  async getMany(keys) {
581
- const result = this._cache.getMany(keys);
784
+ const result = this.getStore(this._namespace).getMany(keys);
582
785
  return result;
583
786
  }
584
787
  async set(key, value, ttl) {
585
- this._cache.set(key, value, ttl);
788
+ this.getStore(this._namespace).set(key, value, ttl);
586
789
  }
587
790
  async setMany(values) {
588
- this._cache.setMany(values);
791
+ this.getStore(this._namespace).setMany(values);
589
792
  }
590
793
  async delete(key) {
591
- this._cache.delete(key);
794
+ this.getStore(this._namespace).delete(key);
592
795
  return true;
593
796
  }
594
797
  async deleteMany(key) {
595
- this._cache.deleteMany(key);
798
+ this.getStore(this._namespace).deleteMany(key);
596
799
  return true;
597
800
  }
598
801
  async clear() {
599
- this._cache.clear();
802
+ this.getStore(this._namespace).clear();
600
803
  }
601
804
  async has(key) {
602
- return this._cache.has(key);
805
+ return this.getStore(this._namespace).has(key);
603
806
  }
604
807
  on(event, listener) {
605
808
  return this;
606
809
  }
810
+ getStore(namespace) {
811
+ if (!namespace) {
812
+ return this._defaultCache;
813
+ }
814
+ if (!this._nCache.has(namespace)) {
815
+ this._nCache.set(namespace, new CacheableMemory(this.opts));
816
+ }
817
+ return this._nCache.get(namespace);
818
+ }
607
819
  };
608
820
 
609
821
  // src/stats.ts
@@ -623,36 +835,78 @@ var CacheableStats = class {
623
835
  this._enabled = options.enabled;
624
836
  }
625
837
  }
838
+ /**
839
+ * @returns {boolean} - Whether the stats are enabled
840
+ */
626
841
  get enabled() {
627
842
  return this._enabled;
628
843
  }
844
+ /**
845
+ * @param {boolean} enabled - Whether to enable the stats
846
+ */
629
847
  set enabled(enabled) {
630
848
  this._enabled = enabled;
631
849
  }
850
+ /**
851
+ * @returns {number} - The number of hits
852
+ * @readonly
853
+ */
632
854
  get hits() {
633
855
  return this._hits;
634
856
  }
857
+ /**
858
+ * @returns {number} - The number of misses
859
+ * @readonly
860
+ */
635
861
  get misses() {
636
862
  return this._misses;
637
863
  }
864
+ /**
865
+ * @returns {number} - The number of gets
866
+ * @readonly
867
+ */
638
868
  get gets() {
639
869
  return this._gets;
640
870
  }
871
+ /**
872
+ * @returns {number} - The number of sets
873
+ * @readonly
874
+ */
641
875
  get sets() {
642
876
  return this._sets;
643
877
  }
878
+ /**
879
+ * @returns {number} - The number of deletes
880
+ * @readonly
881
+ */
644
882
  get deletes() {
645
883
  return this._deletes;
646
884
  }
885
+ /**
886
+ * @returns {number} - The number of clears
887
+ * @readonly
888
+ */
647
889
  get clears() {
648
890
  return this._clears;
649
891
  }
892
+ /**
893
+ * @returns {number} - The vsize (value size) of the cache instance
894
+ * @readonly
895
+ */
650
896
  get vsize() {
651
897
  return this._vsize;
652
898
  }
899
+ /**
900
+ * @returns {number} - The ksize (key size) of the cache instance
901
+ * @readonly
902
+ */
653
903
  get ksize() {
654
904
  return this._ksize;
655
905
  }
906
+ /**
907
+ * @returns {number} - The count of the cache instance
908
+ * @readonly
909
+ */
656
910
  get count() {
657
911
  return this._count;
658
912
  }
@@ -804,6 +1058,11 @@ var Cacheable = class extends import_hookified.Hookified {
804
1058
  _nonBlocking = false;
805
1059
  _ttl;
806
1060
  _stats = new CacheableStats({ enabled: false });
1061
+ _namespace;
1062
+ /**
1063
+ * Creates a new cacheable instance
1064
+ * @param {CacheableOptions} [options] The options for the cacheable instance
1065
+ */
807
1066
  constructor(options) {
808
1067
  super();
809
1068
  if (options?.primary) {
@@ -821,40 +1080,159 @@ var Cacheable = class extends import_hookified.Hookified {
821
1080
  if (options?.ttl) {
822
1081
  this.setTtl(options.ttl);
823
1082
  }
1083
+ if (options?.namespace) {
1084
+ this._namespace = options.namespace;
1085
+ this._primary.namespace = this.getNameSpace();
1086
+ if (this._secondary) {
1087
+ this._secondary.namespace = this.getNameSpace();
1088
+ }
1089
+ }
824
1090
  }
1091
+ /**
1092
+ * The namespace for the cacheable instance
1093
+ * @returns {string | (() => string) | undefined} The namespace for the cacheable instance
1094
+ */
1095
+ get namespace() {
1096
+ return this._namespace;
1097
+ }
1098
+ /**
1099
+ * Sets the namespace for the cacheable instance
1100
+ * @param {string | (() => string) | undefined} namespace The namespace for the cacheable instance
1101
+ * @returns {void}
1102
+ */
1103
+ set namespace(namespace) {
1104
+ this._namespace = namespace;
1105
+ this._primary.namespace = this.getNameSpace();
1106
+ if (this._secondary) {
1107
+ this._secondary.namespace = this.getNameSpace();
1108
+ }
1109
+ }
1110
+ /**
1111
+ * The statistics for the cacheable instance
1112
+ * @returns {CacheableStats} The statistics for the cacheable instance
1113
+ */
825
1114
  get stats() {
826
1115
  return this._stats;
827
1116
  }
1117
+ /**
1118
+ * The primary store for the cacheable instance
1119
+ * @returns {Keyv} The primary store for the cacheable instance
1120
+ */
828
1121
  get primary() {
829
1122
  return this._primary;
830
1123
  }
1124
+ /**
1125
+ * Sets the primary store for the cacheable instance
1126
+ * @param {Keyv} primary The primary store for the cacheable instance
1127
+ */
831
1128
  set primary(primary) {
832
1129
  this._primary = primary;
833
1130
  }
1131
+ /**
1132
+ * The secondary store for the cacheable instance
1133
+ * @returns {Keyv | undefined} The secondary store for the cacheable instance
1134
+ */
834
1135
  get secondary() {
835
1136
  return this._secondary;
836
1137
  }
1138
+ /**
1139
+ * Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
1140
+ * @param {Keyv | undefined} secondary The secondary store for the cacheable instance
1141
+ * @returns {void}
1142
+ */
837
1143
  set secondary(secondary) {
838
1144
  this._secondary = secondary;
839
1145
  }
1146
+ /**
1147
+ * Gets whether the secondary store is non-blocking mode. It is set to false by default.
1148
+ * If it is set to true then the secondary store will not block the primary store.
1149
+ *
1150
+ * [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
1151
+ *
1152
+ * @returns {boolean} Whether the cacheable instance is non-blocking
1153
+ */
840
1154
  get nonBlocking() {
841
1155
  return this._nonBlocking;
842
1156
  }
1157
+ /**
1158
+ * Sets whether the secondary store is non-blocking mode. It is set to false by default.
1159
+ * If it is set to true then the secondary store will not block the primary store.
1160
+ *
1161
+ * [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
1162
+ *
1163
+ * @param {boolean} nonBlocking Whether the cacheable instance is non-blocking
1164
+ * @returns {void}
1165
+ */
843
1166
  set nonBlocking(nonBlocking) {
844
1167
  this._nonBlocking = nonBlocking;
845
1168
  }
1169
+ /**
1170
+ * The time-to-live for the cacheable instance and will be used as the default value.
1171
+ * can be a number in milliseconds or a human-readable format such as `1s` for 1 second or `1h` for 1 hour
1172
+ * or undefined if there is no time-to-live.
1173
+ *
1174
+ * [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
1175
+ *
1176
+ * @returns {number | string | undefined} The time-to-live for the cacheable instance in milliseconds, human-readable format or undefined
1177
+ * @example
1178
+ * ```typescript
1179
+ * const cacheable = new Cacheable({ ttl: '1h' });
1180
+ * console.log(cacheable.ttl); // 1h
1181
+ * ```
1182
+ */
846
1183
  get ttl() {
847
1184
  return this._ttl;
848
1185
  }
1186
+ /**
1187
+ * Sets the time-to-live for the cacheable instance and will be used as the default value.
1188
+ * If you set a number it is miliseconds, if you set a string it is a human-readable
1189
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that
1190
+ * there is no time-to-live.
1191
+ *
1192
+ * [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
1193
+ *
1194
+ * @param {number | string | undefined} ttl The time-to-live for the cacheable instance
1195
+ * @example
1196
+ * ```typescript
1197
+ * const cacheable = new Cacheable();
1198
+ * cacheable.ttl = '1h'; // Set the time-to-live to 1 hour
1199
+ * ```
1200
+ * or setting the time-to-live in milliseconds
1201
+ * ```typescript
1202
+ * const cacheable = new Cacheable();
1203
+ * cacheable.ttl = 3600000; // Set the time-to-live to 1 hour
1204
+ * ```
1205
+ */
849
1206
  set ttl(ttl) {
850
1207
  this.setTtl(ttl);
851
1208
  }
1209
+ /**
1210
+ * Sets the primary store for the cacheable instance
1211
+ * @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
1212
+ * @returns {void}
1213
+ */
852
1214
  setPrimary(primary) {
853
1215
  this._primary = primary instanceof import_keyv.Keyv ? primary : new import_keyv.Keyv(primary);
854
1216
  }
1217
+ /**
1218
+ * Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
1219
+ * @param {Keyv | KeyvStoreAdapter} secondary The secondary store for the cacheable instance
1220
+ * @returns {void}
1221
+ */
855
1222
  setSecondary(secondary) {
856
1223
  this._secondary = secondary instanceof import_keyv.Keyv ? secondary : new import_keyv.Keyv(secondary);
857
1224
  }
1225
+ getNameSpace() {
1226
+ if (typeof this._namespace === "function") {
1227
+ return this._namespace();
1228
+ }
1229
+ return this._namespace;
1230
+ }
1231
+ /**
1232
+ * Gets the value of the key. If the key does not exist in the primary store then it will check the secondary store.
1233
+ * @param {string} key The key to get the value of
1234
+ * @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
1235
+ */
858
1236
  async get(key) {
859
1237
  let result;
860
1238
  try {
@@ -881,6 +1259,11 @@ var Cacheable = class extends import_hookified.Hookified {
881
1259
  }
882
1260
  return result;
883
1261
  }
1262
+ /**
1263
+ * Gets the values of the keys. If the key does not exist in the primary store then it will check the secondary store.
1264
+ * @param {string[]} keys The keys to get the values of
1265
+ * @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
1266
+ */
884
1267
  async getMany(keys) {
885
1268
  let result = [];
886
1269
  try {
@@ -918,6 +1301,14 @@ var Cacheable = class extends import_hookified.Hookified {
918
1301
  }
919
1302
  return result;
920
1303
  }
1304
+ /**
1305
+ * Sets the value of the key. If the secondary store is set then it will also set the value in the secondary store.
1306
+ * @param {string} key the key to set the value of
1307
+ * @param {T} value The value to set
1308
+ * @param {number | string} [ttl] set a number it is miliseconds, set a string it is a human-readable
1309
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live.
1310
+ * @returns {boolean} Whether the value was set
1311
+ */
921
1312
  async set(key, value, ttl) {
922
1313
  let result = false;
923
1314
  const finalTtl = shorthandToMilliseconds(ttl ?? this._ttl);
@@ -947,6 +1338,11 @@ var Cacheable = class extends import_hookified.Hookified {
947
1338
  }
948
1339
  return result;
949
1340
  }
1341
+ /**
1342
+ * Sets the values of the keys. If the secondary store is set then it will also set the values in the secondary store.
1343
+ * @param {CacheableItem[]} items The items to set
1344
+ * @returns {boolean} Whether the values were set
1345
+ */
950
1346
  async setMany(items) {
951
1347
  let result = false;
952
1348
  try {
@@ -972,16 +1368,31 @@ var Cacheable = class extends import_hookified.Hookified {
972
1368
  }
973
1369
  return result;
974
1370
  }
1371
+ /**
1372
+ * Takes the value of the key and deletes the key. If the key does not exist then it will return undefined.
1373
+ * @param {string} key The key to take the value of
1374
+ * @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
1375
+ */
975
1376
  async take(key) {
976
1377
  const result = await this.get(key);
977
1378
  await this.delete(key);
978
1379
  return result;
979
1380
  }
1381
+ /**
1382
+ * Takes the values of the keys and deletes the keys. If the key does not exist then it will return undefined.
1383
+ * @param {string[]} keys The keys to take the values of
1384
+ * @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
1385
+ */
980
1386
  async takeMany(keys) {
981
1387
  const result = await this.getMany(keys);
982
1388
  await this.deleteMany(keys);
983
1389
  return result;
984
1390
  }
1391
+ /**
1392
+ * Checks if the key exists in the primary store. If it does not exist then it will check the secondary store.
1393
+ * @param {string} key The key to check
1394
+ * @returns {Promise<boolean>} Whether the key exists
1395
+ */
985
1396
  async has(key) {
986
1397
  const promises = [];
987
1398
  promises.push(this._primary.has(key));
@@ -996,6 +1407,11 @@ var Cacheable = class extends import_hookified.Hookified {
996
1407
  }
997
1408
  return false;
998
1409
  }
1410
+ /**
1411
+ * Checks if the keys exist in the primary store. If it does not exist then it will check the secondary store.
1412
+ * @param {string[]} keys The keys to check
1413
+ * @returns {Promise<boolean[]>} Whether the keys exist
1414
+ */
999
1415
  async hasMany(keys) {
1000
1416
  const result = await this.hasManyKeyv(this._primary, keys);
1001
1417
  const missingKeys = [];
@@ -1014,6 +1430,11 @@ var Cacheable = class extends import_hookified.Hookified {
1014
1430
  }
1015
1431
  return result;
1016
1432
  }
1433
+ /**
1434
+ * Deletes the key from the primary store. If the secondary store is set then it will also delete the key from the secondary store.
1435
+ * @param {string} key The key to delete
1436
+ * @returns {Promise<boolean>} Whether the key was deleted
1437
+ */
1017
1438
  async delete(key) {
1018
1439
  let result = false;
1019
1440
  const promises = [];
@@ -1038,6 +1459,11 @@ var Cacheable = class extends import_hookified.Hookified {
1038
1459
  }
1039
1460
  return result;
1040
1461
  }
1462
+ /**
1463
+ * Deletes the keys from the primary store. If the secondary store is set then it will also delete the keys from the secondary store.
1464
+ * @param {string[]} keys The keys to delete
1465
+ * @returns {Promise<boolean>} Whether the keys were deleted
1466
+ */
1041
1467
  async deleteMany(keys) {
1042
1468
  if (this.stats.enabled) {
1043
1469
  const statResult = await this._primary.get(keys);
@@ -1058,6 +1484,10 @@ var Cacheable = class extends import_hookified.Hookified {
1058
1484
  }
1059
1485
  return result;
1060
1486
  }
1487
+ /**
1488
+ * Clears the primary store. If the secondary store is set then it will also clear the secondary store.
1489
+ * @returns {Promise<void>}
1490
+ */
1061
1491
  async clear() {
1062
1492
  const promises = [];
1063
1493
  promises.push(this._primary.clear());
@@ -1070,6 +1500,10 @@ var Cacheable = class extends import_hookified.Hookified {
1070
1500
  this._stats.incrementClears();
1071
1501
  }
1072
1502
  }
1503
+ /**
1504
+ * Disconnects the primary store. If the secondary store is set then it will also disconnect the secondary store.
1505
+ * @returns {Promise<void>}
1506
+ */
1073
1507
  async disconnect() {
1074
1508
  const promises = [];
1075
1509
  promises.push(this._primary.disconnect());
@@ -1078,14 +1512,28 @@ var Cacheable = class extends import_hookified.Hookified {
1078
1512
  }
1079
1513
  await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
1080
1514
  }
1081
- wrap(function_, options = {}) {
1515
+ /**
1516
+ * Wraps a function with caching
1517
+ *
1518
+ * [Learn more about wrapping functions](https://cacheable.org/docs/cacheable/#wrap--memoization-for-sync-and-async-functions).
1519
+ * @param {Function} function_ The function to wrap
1520
+ * @param {WrapOptions} [options] The options for the wrap function
1521
+ * @returns {Function} The wrapped function
1522
+ */
1523
+ wrap(function_, options) {
1082
1524
  const wrapOptions = {
1083
1525
  ttl: options.ttl,
1084
- key: options.key,
1526
+ keyPrefix: options.keyPrefix,
1085
1527
  cache: this
1086
1528
  };
1087
1529
  return wrap(function_, wrapOptions);
1088
1530
  }
1531
+ /**
1532
+ * Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
1533
+ * @param {any} object the object to hash
1534
+ * @param {string} algorithm the hash algorithm to use. The default is 'sha256'
1535
+ * @returns {string} the hash of the object
1536
+ */
1089
1537
  hash(object, algorithm = "sha256") {
1090
1538
  return hash(object, algorithm);
1091
1539
  }