cacheable 1.7.1 → 1.8.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
@@ -69,6 +69,32 @@ var shorthandToTime = (shorthand, fromDate) => {
69
69
  return fromDate.getTime() + milliseconds;
70
70
  };
71
71
 
72
+ // src/wrap.ts
73
+ function wrapSync(function_, options) {
74
+ const { ttl, key, cache } = options;
75
+ return function(...arguments_) {
76
+ const cacheKey = key ?? cache.hash(arguments_);
77
+ let value = cache.get(cacheKey);
78
+ if (value === void 0) {
79
+ value = function_(...arguments_);
80
+ cache.set(cacheKey, value, ttl);
81
+ }
82
+ return value;
83
+ };
84
+ }
85
+ function wrap(function_, options) {
86
+ const { ttl, key, cache } = options;
87
+ return async function(...arguments_) {
88
+ const cacheKey = key ?? cache.hash(arguments_);
89
+ let value = await cache.get(cacheKey);
90
+ if (value === void 0) {
91
+ value = await function_(...arguments_);
92
+ await cache.set(cacheKey, value, ttl);
93
+ }
94
+ return value;
95
+ };
96
+ }
97
+
72
98
  // src/memory-lru.ts
73
99
  var ListNode = class {
74
100
  // eslint-disable-next-line @typescript-eslint/parameter-properties
@@ -142,6 +168,18 @@ var DoublyLinkedList = class {
142
168
  }
143
169
  };
144
170
 
171
+ // src/hash.ts
172
+ import * as crypto from "node:crypto";
173
+ function hash(object, algorithm = "sha256") {
174
+ const objectString = JSON.stringify(object);
175
+ if (!crypto.getHashes().includes(algorithm)) {
176
+ throw new Error(`Unsupported hash algorithm: '${algorithm}'`);
177
+ }
178
+ const hasher = crypto.createHash(algorithm);
179
+ hasher.update(objectString);
180
+ return hasher.digest("hex");
181
+ }
182
+
145
183
  // src/memory.ts
146
184
  var CacheableMemory = class {
147
185
  _hashCache = /* @__PURE__ */ new Map();
@@ -166,6 +204,10 @@ var CacheableMemory = class {
166
204
  // Turned off by default
167
205
  _interval = 0;
168
206
  // Turned off by default
207
+ /**
208
+ * @constructor
209
+ * @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
210
+ */
169
211
  constructor(options) {
170
212
  if (options?.ttl) {
171
213
  this.setTtl(options.ttl);
@@ -181,40 +223,89 @@ var CacheableMemory = class {
181
223
  }
182
224
  this.startIntervalCheck();
183
225
  }
226
+ /**
227
+ * Gets the time-to-live
228
+ * @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.
229
+ */
184
230
  get ttl() {
185
231
  return this._ttl;
186
232
  }
233
+ /**
234
+ * Sets the time-to-live
235
+ * @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.
236
+ */
187
237
  set ttl(value) {
188
238
  this.setTtl(value);
189
239
  }
240
+ /**
241
+ * Gets whether to use clone
242
+ * @returns {boolean} - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
243
+ */
190
244
  get useClone() {
191
245
  return this._useClone;
192
246
  }
247
+ /**
248
+ * Sets whether to use clone
249
+ * @param {boolean} value - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
250
+ */
193
251
  set useClone(value) {
194
252
  this._useClone = value;
195
253
  }
254
+ /**
255
+ * Gets the size of the LRU cache
256
+ * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
257
+ */
196
258
  get lruSize() {
197
259
  return this._lruSize;
198
260
  }
261
+ /**
262
+ * Sets the size of the LRU cache
263
+ * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
264
+ */
199
265
  set lruSize(value) {
200
266
  this._lruSize = value;
201
267
  this.lruResize();
202
268
  }
269
+ /**
270
+ * Gets the check interval
271
+ * @returns {number} - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
272
+ */
203
273
  get checkInterval() {
204
274
  return this._checkInterval;
205
275
  }
276
+ /**
277
+ * Sets the check interval
278
+ * @param {number} value - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
279
+ */
206
280
  set checkInterval(value) {
207
281
  this._checkInterval = value;
208
282
  }
283
+ /**
284
+ * Gets the size of the cache
285
+ * @returns {number} - The size of the cache
286
+ */
209
287
  get size() {
210
288
  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;
211
289
  }
290
+ /**
291
+ * Gets the keys
292
+ * @returns {IterableIterator<string>} - The keys
293
+ */
212
294
  get keys() {
213
295
  return this.concatStores().keys();
214
296
  }
297
+ /**
298
+ * Gets the items
299
+ * @returns {IterableIterator<CacheableStoreItem>} - The items
300
+ */
215
301
  get items() {
216
302
  return this.concatStores().values();
217
303
  }
304
+ /**
305
+ * Gets the value of the key
306
+ * @param {string} key - The key to get the value
307
+ * @returns {T | undefined} - The value of the key
308
+ */
218
309
  get(key) {
219
310
  const store = this.getStore(key);
220
311
  const item = store.get(key);
@@ -231,6 +322,11 @@ var CacheableMemory = class {
231
322
  }
232
323
  return this.clone(item.value);
233
324
  }
325
+ /**
326
+ * Gets the values of the keys
327
+ * @param {string[]} keys - The keys to get the values
328
+ * @returns {T[]} - The values of the keys
329
+ */
234
330
  getMany(keys) {
235
331
  const result = new Array();
236
332
  for (const key of keys) {
@@ -238,6 +334,11 @@ var CacheableMemory = class {
238
334
  }
239
335
  return result;
240
336
  }
337
+ /**
338
+ * Gets the raw value of the key
339
+ * @param {string} key - The key to get the value
340
+ * @returns {CacheableStoreItem | undefined} - The raw value of the key
341
+ */
241
342
  getRaw(key) {
242
343
  const store = this.getStore(key);
243
344
  const item = store.get(key);
@@ -251,6 +352,11 @@ var CacheableMemory = class {
251
352
  this.lruMoveToFront(key);
252
353
  return item;
253
354
  }
355
+ /**
356
+ * Gets the raw values of the keys
357
+ * @param {string[]} keys - The keys to get the values
358
+ * @returns {CacheableStoreItem[]} - The raw values of the keys
359
+ */
254
360
  getManyRaw(keys) {
255
361
  const result = new Array();
256
362
  for (const key of keys) {
@@ -258,6 +364,14 @@ var CacheableMemory = class {
258
364
  }
259
365
  return result;
260
366
  }
367
+ /**
368
+ * Sets the value of the key
369
+ * @param {string} key - The key to set the value
370
+ * @param {any} value - The value to set
371
+ * @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.
372
+ * 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.
373
+ * @returns {void}
374
+ */
261
375
  set(key, value, ttl) {
262
376
  const store = this.getStore(key);
263
377
  let expires;
@@ -288,15 +402,43 @@ var CacheableMemory = class {
288
402
  expires
289
403
  });
290
404
  }
405
+ /**
406
+ * Sets the values of the keys
407
+ * @param {CacheableItem[]} items - The items to set
408
+ * @returns {void}
409
+ */
291
410
  setMany(items) {
292
411
  for (const item of items) {
293
412
  this.set(item.key, item.value, item.ttl);
294
413
  }
295
414
  }
415
+ /**
416
+ * Checks if the key exists
417
+ * @param {string} key - The key to check
418
+ * @returns {boolean} - If true, the key exists. If false, the key does not exist.
419
+ */
296
420
  has(key) {
297
421
  const item = this.get(key);
298
422
  return Boolean(item);
299
423
  }
424
+ /**
425
+ * @function hasMany
426
+ * @param {string[]} keys - The keys to check
427
+ * @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
428
+ */
429
+ hasMany(keys) {
430
+ const result = new Array();
431
+ for (const key of keys) {
432
+ const item = this.get(key);
433
+ result.push(Boolean(item));
434
+ }
435
+ return result;
436
+ }
437
+ /**
438
+ * Take will get the key and delete the entry from cache
439
+ * @param {string} key - The key to take
440
+ * @returns {T | undefined} - The value of the key
441
+ */
300
442
  take(key) {
301
443
  const item = this.get(key);
302
444
  if (!item) {
@@ -305,6 +447,11 @@ var CacheableMemory = class {
305
447
  this.delete(key);
306
448
  return item;
307
449
  }
450
+ /**
451
+ * TakeMany will get the keys and delete the entries from cache
452
+ * @param {string[]} keys - The keys to take
453
+ * @returns {T[]} - The values of the keys
454
+ */
308
455
  takeMany(keys) {
309
456
  const result = new Array();
310
457
  for (const key of keys) {
@@ -312,15 +459,29 @@ var CacheableMemory = class {
312
459
  }
313
460
  return result;
314
461
  }
462
+ /**
463
+ * Delete the key
464
+ * @param {string} key - The key to delete
465
+ * @returns {void}
466
+ */
315
467
  delete(key) {
316
468
  const store = this.getStore(key);
317
469
  store.delete(key);
318
470
  }
471
+ /**
472
+ * Delete the keys
473
+ * @param {string[]} keys - The keys to delete
474
+ * @returns {void}
475
+ */
319
476
  deleteMany(keys) {
320
477
  for (const key of keys) {
321
478
  this.delete(key);
322
479
  }
323
480
  }
481
+ /**
482
+ * Clear the cache
483
+ * @returns {void}
484
+ */
324
485
  clear() {
325
486
  this._hash0.clear();
326
487
  this._hash1.clear();
@@ -334,20 +495,30 @@ var CacheableMemory = class {
334
495
  this._hash9.clear();
335
496
  this._hashCache.clear();
336
497
  }
498
+ /**
499
+ * Hash the key. this is used to determine which store to use (internal use)
500
+ * @param {string} key - The key to hash
501
+ * @returns {number} - The hash number
502
+ */
337
503
  hashKey(key) {
338
504
  const cacheHashNumber = this._hashCache.get(key);
339
505
  if (cacheHashNumber) {
340
506
  return cacheHashNumber;
341
507
  }
342
- let hash = 0;
508
+ let hash2 = 0;
343
509
  const primeMultiplier = 31;
344
510
  for (let i = 0; i < key.length; i++) {
345
- hash = hash * primeMultiplier + key.charCodeAt(i);
511
+ hash2 = hash2 * primeMultiplier + key.charCodeAt(i);
346
512
  }
347
- const result = Math.abs(hash) % 10;
513
+ const result = Math.abs(hash2) % 10;
348
514
  this._hashCache.set(key, result);
349
515
  return result;
350
516
  }
517
+ /**
518
+ * Get the store based on the key (internal use)
519
+ * @param {string} key - The key to get the store
520
+ * @returns {Map<string, any>} - The store
521
+ */
351
522
  getStore(key) {
352
523
  const hashKey = this.hashKey(key);
353
524
  switch (hashKey) {
@@ -383,24 +554,43 @@ var CacheableMemory = class {
383
554
  }
384
555
  }
385
556
  }
557
+ /**
558
+ * Clone the value. This is for internal use
559
+ * @param {any} value - The value to clone
560
+ * @returns {any} - The cloned value
561
+ */
386
562
  clone(value) {
387
563
  if (this.isPrimitive(value)) {
388
564
  return value;
389
565
  }
390
566
  return structuredClone(value);
391
567
  }
568
+ /**
569
+ * Add to the front of the LRU cache. This is for internal use
570
+ * @param {string} key - The key to add to the front
571
+ * @returns {void}
572
+ */
392
573
  lruAddToFront(key) {
393
574
  if (this._lruSize === 0) {
394
575
  return;
395
576
  }
396
577
  this._lru.addToFront(key);
397
578
  }
579
+ /**
580
+ * Move to the front of the LRU cache. This is for internal use
581
+ * @param {string} key - The key to move to the front
582
+ * @returns {void}
583
+ */
398
584
  lruMoveToFront(key) {
399
585
  if (this._lruSize === 0) {
400
586
  return;
401
587
  }
402
588
  this._lru.moveToFront(key);
403
589
  }
590
+ /**
591
+ * Resize the LRU cache. This is for internal use
592
+ * @returns {void}
593
+ */
404
594
  lruResize() {
405
595
  if (this._lruSize === 0) {
406
596
  return;
@@ -413,6 +603,10 @@ var CacheableMemory = class {
413
603
  }
414
604
  }
415
605
  }
606
+ /**
607
+ * Check for expiration. This is for internal use
608
+ * @returns {void}
609
+ */
416
610
  checkExpiration() {
417
611
  const stores = this.concatStores();
418
612
  for (const item of stores.values()) {
@@ -421,6 +615,10 @@ var CacheableMemory = class {
421
615
  }
422
616
  }
423
617
  }
618
+ /**
619
+ * Start the interval check. This is for internal use
620
+ * @returns {void}
621
+ */
424
622
  startIntervalCheck() {
425
623
  if (this._checkInterval > 0) {
426
624
  this._interval = setInterval(() => {
@@ -428,6 +626,10 @@ var CacheableMemory = class {
428
626
  }, this._checkInterval);
429
627
  }
430
628
  }
629
+ /**
630
+ * Stop the interval check. This is for internal use
631
+ * @returns {void}
632
+ */
431
633
  stopIntervalCheck() {
432
634
  if (this._interval) {
433
635
  clearInterval(this._interval);
@@ -435,6 +637,29 @@ var CacheableMemory = class {
435
637
  this._interval = 0;
436
638
  this._checkInterval = 0;
437
639
  }
640
+ /**
641
+ * Hash the object. This is for internal use
642
+ * @param {any} object - The object to hash
643
+ * @param {string} [algorithm='sha256'] - The algorithm to hash
644
+ * @returns {string} - The hashed string
645
+ */
646
+ hash(object, algorithm = "sha256") {
647
+ return hash(object, algorithm);
648
+ }
649
+ /**
650
+ * Wrap the function for caching
651
+ * @param {Function} function_ - The function to wrap
652
+ * @param {Object} [options] - The options to wrap
653
+ * @returns {Function} - The wrapped function
654
+ */
655
+ wrap(function_, options = {}) {
656
+ const wrapOptions = {
657
+ ttl: options.ttl,
658
+ key: options.key,
659
+ cache: this
660
+ };
661
+ return wrapSync(function_, wrapOptions);
662
+ }
438
663
  isPrimitive(value) {
439
664
  const result = false;
440
665
  if (value === null || value === void 0) {
@@ -529,36 +754,78 @@ var CacheableStats = class {
529
754
  this._enabled = options.enabled;
530
755
  }
531
756
  }
757
+ /**
758
+ * @returns {boolean} - Whether the stats are enabled
759
+ */
532
760
  get enabled() {
533
761
  return this._enabled;
534
762
  }
763
+ /**
764
+ * @param {boolean} enabled - Whether to enable the stats
765
+ */
535
766
  set enabled(enabled) {
536
767
  this._enabled = enabled;
537
768
  }
769
+ /**
770
+ * @returns {number} - The number of hits
771
+ * @readonly
772
+ */
538
773
  get hits() {
539
774
  return this._hits;
540
775
  }
776
+ /**
777
+ * @returns {number} - The number of misses
778
+ * @readonly
779
+ */
541
780
  get misses() {
542
781
  return this._misses;
543
782
  }
783
+ /**
784
+ * @returns {number} - The number of gets
785
+ * @readonly
786
+ */
544
787
  get gets() {
545
788
  return this._gets;
546
789
  }
790
+ /**
791
+ * @returns {number} - The number of sets
792
+ * @readonly
793
+ */
547
794
  get sets() {
548
795
  return this._sets;
549
796
  }
797
+ /**
798
+ * @returns {number} - The number of deletes
799
+ * @readonly
800
+ */
550
801
  get deletes() {
551
802
  return this._deletes;
552
803
  }
804
+ /**
805
+ * @returns {number} - The number of clears
806
+ * @readonly
807
+ */
553
808
  get clears() {
554
809
  return this._clears;
555
810
  }
811
+ /**
812
+ * @returns {number} - The vsize (value size) of the cache instance
813
+ * @readonly
814
+ */
556
815
  get vsize() {
557
816
  return this._vsize;
558
817
  }
818
+ /**
819
+ * @returns {number} - The ksize (key size) of the cache instance
820
+ * @readonly
821
+ */
559
822
  get ksize() {
560
823
  return this._ksize;
561
824
  }
825
+ /**
826
+ * @returns {number} - The count of the cache instance
827
+ * @readonly
828
+ */
562
829
  get count() {
563
830
  return this._count;
564
831
  }
@@ -688,6 +955,10 @@ var CacheableStats = class {
688
955
  };
689
956
 
690
957
  // src/index.ts
958
+ import {
959
+ KeyvHooks,
960
+ Keyv as Keyv2
961
+ } from "keyv";
691
962
  var CacheableHooks = /* @__PURE__ */ ((CacheableHooks2) => {
692
963
  CacheableHooks2["BEFORE_SET"] = "BEFORE_SET";
693
964
  CacheableHooks2["AFTER_SET"] = "AFTER_SET";
@@ -709,6 +980,10 @@ var Cacheable = class extends Hookified {
709
980
  _nonBlocking = false;
710
981
  _ttl;
711
982
  _stats = new CacheableStats({ enabled: false });
983
+ /**
984
+ * Creates a new cacheable instance
985
+ * @param {CacheableOptions} [options] The options for the cacheable instance
986
+ */
712
987
  constructor(options) {
713
988
  super();
714
989
  if (options?.primary) {
@@ -727,39 +1002,126 @@ var Cacheable = class extends Hookified {
727
1002
  this.setTtl(options.ttl);
728
1003
  }
729
1004
  }
1005
+ /**
1006
+ * The statistics for the cacheable instance
1007
+ * @returns {CacheableStats} The statistics for the cacheable instance
1008
+ */
730
1009
  get stats() {
731
1010
  return this._stats;
732
1011
  }
1012
+ /**
1013
+ * The primary store for the cacheable instance
1014
+ * @returns {Keyv} The primary store for the cacheable instance
1015
+ */
733
1016
  get primary() {
734
1017
  return this._primary;
735
1018
  }
1019
+ /**
1020
+ * Sets the primary store for the cacheable instance
1021
+ * @param {Keyv} primary The primary store for the cacheable instance
1022
+ */
736
1023
  set primary(primary) {
737
1024
  this._primary = primary;
738
1025
  }
1026
+ /**
1027
+ * The secondary store for the cacheable instance
1028
+ * @returns {Keyv | undefined} The secondary store for the cacheable instance
1029
+ */
739
1030
  get secondary() {
740
1031
  return this._secondary;
741
1032
  }
1033
+ /**
1034
+ * Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
1035
+ * @param {Keyv | undefined} secondary The secondary store for the cacheable instance
1036
+ * @returns {void}
1037
+ */
742
1038
  set secondary(secondary) {
743
1039
  this._secondary = secondary;
744
1040
  }
1041
+ /**
1042
+ * Gets whether the secondary store is non-blocking mode. It is set to false by default.
1043
+ * If it is set to true then the secondary store will not block the primary store.
1044
+ *
1045
+ * [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
1046
+ *
1047
+ * @returns {boolean} Whether the cacheable instance is non-blocking
1048
+ */
745
1049
  get nonBlocking() {
746
1050
  return this._nonBlocking;
747
1051
  }
1052
+ /**
1053
+ * Sets whether the secondary store is non-blocking mode. It is set to false by default.
1054
+ * If it is set to true then the secondary store will not block the primary store.
1055
+ *
1056
+ * [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
1057
+ *
1058
+ * @param {boolean} nonBlocking Whether the cacheable instance is non-blocking
1059
+ * @returns {void}
1060
+ */
748
1061
  set nonBlocking(nonBlocking) {
749
1062
  this._nonBlocking = nonBlocking;
750
1063
  }
1064
+ /**
1065
+ * The time-to-live for the cacheable instance and will be used as the default value.
1066
+ * can be a number in milliseconds or a human-readable format such as `1s` for 1 second or `1h` for 1 hour
1067
+ * or undefined if there is no time-to-live.
1068
+ *
1069
+ * [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
1070
+ *
1071
+ * @returns {number | string | undefined} The time-to-live for the cacheable instance in milliseconds, human-readable format or undefined
1072
+ * @example
1073
+ * ```typescript
1074
+ * const cacheable = new Cacheable({ ttl: '1h' });
1075
+ * console.log(cacheable.ttl); // 1h
1076
+ * ```
1077
+ */
751
1078
  get ttl() {
752
1079
  return this._ttl;
753
1080
  }
1081
+ /**
1082
+ * Sets the time-to-live for the cacheable instance and will be used as the default value.
1083
+ * If you set a number it is miliseconds, if you set a string it is a human-readable
1084
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that
1085
+ * there is no time-to-live.
1086
+ *
1087
+ * [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
1088
+ *
1089
+ * @param {number | string | undefined} ttl The time-to-live for the cacheable instance
1090
+ * @example
1091
+ * ```typescript
1092
+ * const cacheable = new Cacheable();
1093
+ * cacheable.ttl = '1h'; // Set the time-to-live to 1 hour
1094
+ * ```
1095
+ * or setting the time-to-live in milliseconds
1096
+ * ```typescript
1097
+ * const cacheable = new Cacheable();
1098
+ * cacheable.ttl = 3600000; // Set the time-to-live to 1 hour
1099
+ * ```
1100
+ */
754
1101
  set ttl(ttl) {
755
1102
  this.setTtl(ttl);
756
1103
  }
1104
+ /**
1105
+ * Sets the primary store for the cacheable instance
1106
+ * @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
1107
+ * @returns {void}
1108
+ */
757
1109
  setPrimary(primary) {
758
1110
  this._primary = primary instanceof Keyv ? primary : new Keyv(primary);
759
1111
  }
1112
+ /**
1113
+ * Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
1114
+ * @param {Keyv | KeyvStoreAdapter} secondary The secondary store for the cacheable instance
1115
+ * @returns {void}
1116
+ */
760
1117
  setSecondary(secondary) {
761
1118
  this._secondary = secondary instanceof Keyv ? secondary : new Keyv(secondary);
762
1119
  }
1120
+ /**
1121
+ * Gets the value of the key. If the key does not exist in the primary store then it will check the secondary store.
1122
+ * @param {string} key The key to get the value of
1123
+ * @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
1124
+ */
763
1125
  async get(key) {
764
1126
  let result;
765
1127
  try {
@@ -786,6 +1148,11 @@ var Cacheable = class extends Hookified {
786
1148
  }
787
1149
  return result;
788
1150
  }
1151
+ /**
1152
+ * Gets the values of the keys. If the key does not exist in the primary store then it will check the secondary store.
1153
+ * @param {string[]} keys The keys to get the values of
1154
+ * @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
1155
+ */
789
1156
  async getMany(keys) {
790
1157
  let result = [];
791
1158
  try {
@@ -823,6 +1190,15 @@ var Cacheable = class extends Hookified {
823
1190
  }
824
1191
  return result;
825
1192
  }
1193
+ /**
1194
+ * Sets the value of the key. If the secondary store is set then it will also set the value in the secondary store.
1195
+ * @param {string} key the key to set the value of
1196
+ * @param {T} value The value to set
1197
+ * @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
1198
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
1199
+ * undefined then it will not have a time-to-live.
1200
+ * @returns {boolean} Whether the value was set
1201
+ */
826
1202
  async set(key, value, ttl) {
827
1203
  let result = false;
828
1204
  const finalTtl = shorthandToMilliseconds(ttl ?? this._ttl);
@@ -852,6 +1228,11 @@ var Cacheable = class extends Hookified {
852
1228
  }
853
1229
  return result;
854
1230
  }
1231
+ /**
1232
+ * Sets the values of the keys. If the secondary store is set then it will also set the values in the secondary store.
1233
+ * @param {CacheableItem[]} items The items to set
1234
+ * @returns {boolean} Whether the values were set
1235
+ */
855
1236
  async setMany(items) {
856
1237
  let result = false;
857
1238
  try {
@@ -877,16 +1258,31 @@ var Cacheable = class extends Hookified {
877
1258
  }
878
1259
  return result;
879
1260
  }
1261
+ /**
1262
+ * Takes the value of the key and deletes the key. If the key does not exist then it will return undefined.
1263
+ * @param {string} key The key to take the value of
1264
+ * @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
1265
+ */
880
1266
  async take(key) {
881
1267
  const result = await this.get(key);
882
1268
  await this.delete(key);
883
1269
  return result;
884
1270
  }
1271
+ /**
1272
+ * Takes the values of the keys and deletes the keys. If the key does not exist then it will return undefined.
1273
+ * @param {string[]} keys The keys to take the values of
1274
+ * @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
1275
+ */
885
1276
  async takeMany(keys) {
886
1277
  const result = await this.getMany(keys);
887
1278
  await this.deleteMany(keys);
888
1279
  return result;
889
1280
  }
1281
+ /**
1282
+ * Checks if the key exists in the primary store. If it does not exist then it will check the secondary store.
1283
+ * @param {string} key The key to check
1284
+ * @returns {Promise<boolean>} Whether the key exists
1285
+ */
890
1286
  async has(key) {
891
1287
  const promises = [];
892
1288
  promises.push(this._primary.has(key));
@@ -901,6 +1297,11 @@ var Cacheable = class extends Hookified {
901
1297
  }
902
1298
  return false;
903
1299
  }
1300
+ /**
1301
+ * Checks if the keys exist in the primary store. If it does not exist then it will check the secondary store.
1302
+ * @param {string[]} keys The keys to check
1303
+ * @returns {Promise<boolean[]>} Whether the keys exist
1304
+ */
904
1305
  async hasMany(keys) {
905
1306
  const result = await this.hasManyKeyv(this._primary, keys);
906
1307
  const missingKeys = [];
@@ -919,6 +1320,11 @@ var Cacheable = class extends Hookified {
919
1320
  }
920
1321
  return result;
921
1322
  }
1323
+ /**
1324
+ * Deletes the key from the primary store. If the secondary store is set then it will also delete the key from the secondary store.
1325
+ * @param {string} key The key to delete
1326
+ * @returns {Promise<boolean>} Whether the key was deleted
1327
+ */
922
1328
  async delete(key) {
923
1329
  let result = false;
924
1330
  const promises = [];
@@ -943,6 +1349,11 @@ var Cacheable = class extends Hookified {
943
1349
  }
944
1350
  return result;
945
1351
  }
1352
+ /**
1353
+ * Deletes the keys from the primary store. If the secondary store is set then it will also delete the keys from the secondary store.
1354
+ * @param {string[]} keys The keys to delete
1355
+ * @returns {Promise<boolean>} Whether the keys were deleted
1356
+ */
946
1357
  async deleteMany(keys) {
947
1358
  if (this.stats.enabled) {
948
1359
  const statResult = await this._primary.get(keys);
@@ -963,6 +1374,10 @@ var Cacheable = class extends Hookified {
963
1374
  }
964
1375
  return result;
965
1376
  }
1377
+ /**
1378
+ * Clears the primary store. If the secondary store is set then it will also clear the secondary store.
1379
+ * @returns {Promise<void>}
1380
+ */
966
1381
  async clear() {
967
1382
  const promises = [];
968
1383
  promises.push(this._primary.clear());
@@ -975,6 +1390,10 @@ var Cacheable = class extends Hookified {
975
1390
  this._stats.incrementClears();
976
1391
  }
977
1392
  }
1393
+ /**
1394
+ * Disconnects the primary store. If the secondary store is set then it will also disconnect the secondary store.
1395
+ * @returns {Promise<void>}
1396
+ */
978
1397
  async disconnect() {
979
1398
  const promises = [];
980
1399
  promises.push(this._primary.disconnect());
@@ -983,6 +1402,31 @@ var Cacheable = class extends Hookified {
983
1402
  }
984
1403
  await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
985
1404
  }
1405
+ /**
1406
+ * Wraps a function with caching
1407
+ *
1408
+ * [Learn more about wrapping functions](https://cacheable.org/docs/cacheable/#wrap--memoization-for-sync-and-async-functions).
1409
+ * @param {Function} function_ The function to wrap
1410
+ * @param {WrapOptions} [options] The options for the wrap function
1411
+ * @returns {Function} The wrapped function
1412
+ */
1413
+ wrap(function_, options = {}) {
1414
+ const wrapOptions = {
1415
+ ttl: options.ttl,
1416
+ key: options.key,
1417
+ cache: this
1418
+ };
1419
+ return wrap(function_, wrapOptions);
1420
+ }
1421
+ /**
1422
+ * Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
1423
+ * @param {any} object the object to hash
1424
+ * @param {string} algorithm the hash algorithm to use. The default is 'sha256'
1425
+ * @returns {string} the hash of the object
1426
+ */
1427
+ hash(object, algorithm = "sha256") {
1428
+ return hash(object, algorithm);
1429
+ }
986
1430
  async deleteManyKeyv(keyv, keys) {
987
1431
  const promises = [];
988
1432
  for (const key of keys) {
@@ -1023,7 +1467,11 @@ export {
1023
1467
  CacheableHooks,
1024
1468
  CacheableMemory,
1025
1469
  CacheableStats,
1470
+ Keyv2 as Keyv,
1026
1471
  KeyvCacheableMemory,
1472
+ KeyvHooks,
1027
1473
  shorthandToMilliseconds,
1028
- shorthandToTime
1474
+ shorthandToTime,
1475
+ wrap,
1476
+ wrapSync
1029
1477
  };