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.cjs CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -25,9 +35,13 @@ __export(src_exports, {
25
35
  CacheableHooks: () => CacheableHooks,
26
36
  CacheableMemory: () => CacheableMemory,
27
37
  CacheableStats: () => CacheableStats,
38
+ Keyv: () => import_keyv2.Keyv,
28
39
  KeyvCacheableMemory: () => KeyvCacheableMemory,
40
+ KeyvHooks: () => import_keyv2.KeyvHooks,
29
41
  shorthandToMilliseconds: () => shorthandToMilliseconds,
30
- shorthandToTime: () => shorthandToTime
42
+ shorthandToTime: () => shorthandToTime,
43
+ wrap: () => wrap,
44
+ wrapSync: () => wrapSync
31
45
  });
32
46
  module.exports = __toCommonJS(src_exports);
33
47
  var import_keyv = require("keyv");
@@ -100,6 +114,32 @@ var shorthandToTime = (shorthand, fromDate) => {
100
114
  return fromDate.getTime() + milliseconds;
101
115
  };
102
116
 
117
+ // src/wrap.ts
118
+ function wrapSync(function_, options) {
119
+ const { ttl, key, cache } = options;
120
+ return function(...arguments_) {
121
+ const cacheKey = key ?? cache.hash(arguments_);
122
+ let value = cache.get(cacheKey);
123
+ if (value === void 0) {
124
+ value = function_(...arguments_);
125
+ cache.set(cacheKey, value, ttl);
126
+ }
127
+ return value;
128
+ };
129
+ }
130
+ function wrap(function_, options) {
131
+ const { ttl, key, cache } = options;
132
+ return async function(...arguments_) {
133
+ const cacheKey = key ?? cache.hash(arguments_);
134
+ let value = await cache.get(cacheKey);
135
+ if (value === void 0) {
136
+ value = await function_(...arguments_);
137
+ await cache.set(cacheKey, value, ttl);
138
+ }
139
+ return value;
140
+ };
141
+ }
142
+
103
143
  // src/memory-lru.ts
104
144
  var ListNode = class {
105
145
  // eslint-disable-next-line @typescript-eslint/parameter-properties
@@ -173,6 +213,18 @@ var DoublyLinkedList = class {
173
213
  }
174
214
  };
175
215
 
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
+
176
228
  // src/memory.ts
177
229
  var CacheableMemory = class {
178
230
  _hashCache = /* @__PURE__ */ new Map();
@@ -197,6 +249,10 @@ var CacheableMemory = class {
197
249
  // Turned off by default
198
250
  _interval = 0;
199
251
  // Turned off by default
252
+ /**
253
+ * @constructor
254
+ * @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
255
+ */
200
256
  constructor(options) {
201
257
  if (options?.ttl) {
202
258
  this.setTtl(options.ttl);
@@ -212,40 +268,89 @@ var CacheableMemory = class {
212
268
  }
213
269
  this.startIntervalCheck();
214
270
  }
271
+ /**
272
+ * Gets the time-to-live
273
+ * @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.
274
+ */
215
275
  get ttl() {
216
276
  return this._ttl;
217
277
  }
278
+ /**
279
+ * Sets the time-to-live
280
+ * @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.
281
+ */
218
282
  set ttl(value) {
219
283
  this.setTtl(value);
220
284
  }
285
+ /**
286
+ * Gets whether to use clone
287
+ * @returns {boolean} - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
288
+ */
221
289
  get useClone() {
222
290
  return this._useClone;
223
291
  }
292
+ /**
293
+ * Sets whether to use clone
294
+ * @param {boolean} value - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
295
+ */
224
296
  set useClone(value) {
225
297
  this._useClone = value;
226
298
  }
299
+ /**
300
+ * Gets the size of the LRU cache
301
+ * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
302
+ */
227
303
  get lruSize() {
228
304
  return this._lruSize;
229
305
  }
306
+ /**
307
+ * Sets the size of the LRU cache
308
+ * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
309
+ */
230
310
  set lruSize(value) {
231
311
  this._lruSize = value;
232
312
  this.lruResize();
233
313
  }
314
+ /**
315
+ * Gets the check interval
316
+ * @returns {number} - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
317
+ */
234
318
  get checkInterval() {
235
319
  return this._checkInterval;
236
320
  }
321
+ /**
322
+ * Sets the check interval
323
+ * @param {number} value - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
324
+ */
237
325
  set checkInterval(value) {
238
326
  this._checkInterval = value;
239
327
  }
328
+ /**
329
+ * Gets the size of the cache
330
+ * @returns {number} - The size of the cache
331
+ */
240
332
  get size() {
241
333
  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;
242
334
  }
335
+ /**
336
+ * Gets the keys
337
+ * @returns {IterableIterator<string>} - The keys
338
+ */
243
339
  get keys() {
244
340
  return this.concatStores().keys();
245
341
  }
342
+ /**
343
+ * Gets the items
344
+ * @returns {IterableIterator<CacheableStoreItem>} - The items
345
+ */
246
346
  get items() {
247
347
  return this.concatStores().values();
248
348
  }
349
+ /**
350
+ * Gets the value of the key
351
+ * @param {string} key - The key to get the value
352
+ * @returns {T | undefined} - The value of the key
353
+ */
249
354
  get(key) {
250
355
  const store = this.getStore(key);
251
356
  const item = store.get(key);
@@ -262,6 +367,11 @@ var CacheableMemory = class {
262
367
  }
263
368
  return this.clone(item.value);
264
369
  }
370
+ /**
371
+ * Gets the values of the keys
372
+ * @param {string[]} keys - The keys to get the values
373
+ * @returns {T[]} - The values of the keys
374
+ */
265
375
  getMany(keys) {
266
376
  const result = new Array();
267
377
  for (const key of keys) {
@@ -269,6 +379,11 @@ var CacheableMemory = class {
269
379
  }
270
380
  return result;
271
381
  }
382
+ /**
383
+ * Gets the raw value of the key
384
+ * @param {string} key - The key to get the value
385
+ * @returns {CacheableStoreItem | undefined} - The raw value of the key
386
+ */
272
387
  getRaw(key) {
273
388
  const store = this.getStore(key);
274
389
  const item = store.get(key);
@@ -282,6 +397,11 @@ var CacheableMemory = class {
282
397
  this.lruMoveToFront(key);
283
398
  return item;
284
399
  }
400
+ /**
401
+ * Gets the raw values of the keys
402
+ * @param {string[]} keys - The keys to get the values
403
+ * @returns {CacheableStoreItem[]} - The raw values of the keys
404
+ */
285
405
  getManyRaw(keys) {
286
406
  const result = new Array();
287
407
  for (const key of keys) {
@@ -289,6 +409,14 @@ var CacheableMemory = class {
289
409
  }
290
410
  return result;
291
411
  }
412
+ /**
413
+ * Sets the value of the key
414
+ * @param {string} key - The key to set the value
415
+ * @param {any} value - The value to set
416
+ * @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.
417
+ * 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.
418
+ * @returns {void}
419
+ */
292
420
  set(key, value, ttl) {
293
421
  const store = this.getStore(key);
294
422
  let expires;
@@ -319,15 +447,43 @@ var CacheableMemory = class {
319
447
  expires
320
448
  });
321
449
  }
450
+ /**
451
+ * Sets the values of the keys
452
+ * @param {CacheableItem[]} items - The items to set
453
+ * @returns {void}
454
+ */
322
455
  setMany(items) {
323
456
  for (const item of items) {
324
457
  this.set(item.key, item.value, item.ttl);
325
458
  }
326
459
  }
460
+ /**
461
+ * Checks if the key exists
462
+ * @param {string} key - The key to check
463
+ * @returns {boolean} - If true, the key exists. If false, the key does not exist.
464
+ */
327
465
  has(key) {
328
466
  const item = this.get(key);
329
467
  return Boolean(item);
330
468
  }
469
+ /**
470
+ * @function hasMany
471
+ * @param {string[]} keys - The keys to check
472
+ * @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
473
+ */
474
+ hasMany(keys) {
475
+ const result = new Array();
476
+ for (const key of keys) {
477
+ const item = this.get(key);
478
+ result.push(Boolean(item));
479
+ }
480
+ return result;
481
+ }
482
+ /**
483
+ * Take will get the key and delete the entry from cache
484
+ * @param {string} key - The key to take
485
+ * @returns {T | undefined} - The value of the key
486
+ */
331
487
  take(key) {
332
488
  const item = this.get(key);
333
489
  if (!item) {
@@ -336,6 +492,11 @@ var CacheableMemory = class {
336
492
  this.delete(key);
337
493
  return item;
338
494
  }
495
+ /**
496
+ * TakeMany will get the keys and delete the entries from cache
497
+ * @param {string[]} keys - The keys to take
498
+ * @returns {T[]} - The values of the keys
499
+ */
339
500
  takeMany(keys) {
340
501
  const result = new Array();
341
502
  for (const key of keys) {
@@ -343,15 +504,29 @@ var CacheableMemory = class {
343
504
  }
344
505
  return result;
345
506
  }
507
+ /**
508
+ * Delete the key
509
+ * @param {string} key - The key to delete
510
+ * @returns {void}
511
+ */
346
512
  delete(key) {
347
513
  const store = this.getStore(key);
348
514
  store.delete(key);
349
515
  }
516
+ /**
517
+ * Delete the keys
518
+ * @param {string[]} keys - The keys to delete
519
+ * @returns {void}
520
+ */
350
521
  deleteMany(keys) {
351
522
  for (const key of keys) {
352
523
  this.delete(key);
353
524
  }
354
525
  }
526
+ /**
527
+ * Clear the cache
528
+ * @returns {void}
529
+ */
355
530
  clear() {
356
531
  this._hash0.clear();
357
532
  this._hash1.clear();
@@ -365,20 +540,30 @@ var CacheableMemory = class {
365
540
  this._hash9.clear();
366
541
  this._hashCache.clear();
367
542
  }
543
+ /**
544
+ * Hash the key. this is used to determine which store to use (internal use)
545
+ * @param {string} key - The key to hash
546
+ * @returns {number} - The hash number
547
+ */
368
548
  hashKey(key) {
369
549
  const cacheHashNumber = this._hashCache.get(key);
370
550
  if (cacheHashNumber) {
371
551
  return cacheHashNumber;
372
552
  }
373
- let hash = 0;
553
+ let hash2 = 0;
374
554
  const primeMultiplier = 31;
375
555
  for (let i = 0; i < key.length; i++) {
376
- hash = hash * primeMultiplier + key.charCodeAt(i);
556
+ hash2 = hash2 * primeMultiplier + key.charCodeAt(i);
377
557
  }
378
- const result = Math.abs(hash) % 10;
558
+ const result = Math.abs(hash2) % 10;
379
559
  this._hashCache.set(key, result);
380
560
  return result;
381
561
  }
562
+ /**
563
+ * Get the store based on the key (internal use)
564
+ * @param {string} key - The key to get the store
565
+ * @returns {Map<string, any>} - The store
566
+ */
382
567
  getStore(key) {
383
568
  const hashKey = this.hashKey(key);
384
569
  switch (hashKey) {
@@ -414,24 +599,43 @@ var CacheableMemory = class {
414
599
  }
415
600
  }
416
601
  }
602
+ /**
603
+ * Clone the value. This is for internal use
604
+ * @param {any} value - The value to clone
605
+ * @returns {any} - The cloned value
606
+ */
417
607
  clone(value) {
418
608
  if (this.isPrimitive(value)) {
419
609
  return value;
420
610
  }
421
611
  return structuredClone(value);
422
612
  }
613
+ /**
614
+ * Add to the front of the LRU cache. This is for internal use
615
+ * @param {string} key - The key to add to the front
616
+ * @returns {void}
617
+ */
423
618
  lruAddToFront(key) {
424
619
  if (this._lruSize === 0) {
425
620
  return;
426
621
  }
427
622
  this._lru.addToFront(key);
428
623
  }
624
+ /**
625
+ * Move to the front of the LRU cache. This is for internal use
626
+ * @param {string} key - The key to move to the front
627
+ * @returns {void}
628
+ */
429
629
  lruMoveToFront(key) {
430
630
  if (this._lruSize === 0) {
431
631
  return;
432
632
  }
433
633
  this._lru.moveToFront(key);
434
634
  }
635
+ /**
636
+ * Resize the LRU cache. This is for internal use
637
+ * @returns {void}
638
+ */
435
639
  lruResize() {
436
640
  if (this._lruSize === 0) {
437
641
  return;
@@ -444,6 +648,10 @@ var CacheableMemory = class {
444
648
  }
445
649
  }
446
650
  }
651
+ /**
652
+ * Check for expiration. This is for internal use
653
+ * @returns {void}
654
+ */
447
655
  checkExpiration() {
448
656
  const stores = this.concatStores();
449
657
  for (const item of stores.values()) {
@@ -452,6 +660,10 @@ var CacheableMemory = class {
452
660
  }
453
661
  }
454
662
  }
663
+ /**
664
+ * Start the interval check. This is for internal use
665
+ * @returns {void}
666
+ */
455
667
  startIntervalCheck() {
456
668
  if (this._checkInterval > 0) {
457
669
  this._interval = setInterval(() => {
@@ -459,6 +671,10 @@ var CacheableMemory = class {
459
671
  }, this._checkInterval);
460
672
  }
461
673
  }
674
+ /**
675
+ * Stop the interval check. This is for internal use
676
+ * @returns {void}
677
+ */
462
678
  stopIntervalCheck() {
463
679
  if (this._interval) {
464
680
  clearInterval(this._interval);
@@ -466,6 +682,29 @@ var CacheableMemory = class {
466
682
  this._interval = 0;
467
683
  this._checkInterval = 0;
468
684
  }
685
+ /**
686
+ * Hash the object. This is for internal use
687
+ * @param {any} object - The object to hash
688
+ * @param {string} [algorithm='sha256'] - The algorithm to hash
689
+ * @returns {string} - The hashed string
690
+ */
691
+ hash(object, algorithm = "sha256") {
692
+ return hash(object, algorithm);
693
+ }
694
+ /**
695
+ * Wrap the function for caching
696
+ * @param {Function} function_ - The function to wrap
697
+ * @param {Object} [options] - The options to wrap
698
+ * @returns {Function} - The wrapped function
699
+ */
700
+ wrap(function_, options = {}) {
701
+ const wrapOptions = {
702
+ ttl: options.ttl,
703
+ key: options.key,
704
+ cache: this
705
+ };
706
+ return wrapSync(function_, wrapOptions);
707
+ }
469
708
  isPrimitive(value) {
470
709
  const result = false;
471
710
  if (value === null || value === void 0) {
@@ -560,36 +799,78 @@ var CacheableStats = class {
560
799
  this._enabled = options.enabled;
561
800
  }
562
801
  }
802
+ /**
803
+ * @returns {boolean} - Whether the stats are enabled
804
+ */
563
805
  get enabled() {
564
806
  return this._enabled;
565
807
  }
808
+ /**
809
+ * @param {boolean} enabled - Whether to enable the stats
810
+ */
566
811
  set enabled(enabled) {
567
812
  this._enabled = enabled;
568
813
  }
814
+ /**
815
+ * @returns {number} - The number of hits
816
+ * @readonly
817
+ */
569
818
  get hits() {
570
819
  return this._hits;
571
820
  }
821
+ /**
822
+ * @returns {number} - The number of misses
823
+ * @readonly
824
+ */
572
825
  get misses() {
573
826
  return this._misses;
574
827
  }
828
+ /**
829
+ * @returns {number} - The number of gets
830
+ * @readonly
831
+ */
575
832
  get gets() {
576
833
  return this._gets;
577
834
  }
835
+ /**
836
+ * @returns {number} - The number of sets
837
+ * @readonly
838
+ */
578
839
  get sets() {
579
840
  return this._sets;
580
841
  }
842
+ /**
843
+ * @returns {number} - The number of deletes
844
+ * @readonly
845
+ */
581
846
  get deletes() {
582
847
  return this._deletes;
583
848
  }
849
+ /**
850
+ * @returns {number} - The number of clears
851
+ * @readonly
852
+ */
584
853
  get clears() {
585
854
  return this._clears;
586
855
  }
856
+ /**
857
+ * @returns {number} - The vsize (value size) of the cache instance
858
+ * @readonly
859
+ */
587
860
  get vsize() {
588
861
  return this._vsize;
589
862
  }
863
+ /**
864
+ * @returns {number} - The ksize (key size) of the cache instance
865
+ * @readonly
866
+ */
590
867
  get ksize() {
591
868
  return this._ksize;
592
869
  }
870
+ /**
871
+ * @returns {number} - The count of the cache instance
872
+ * @readonly
873
+ */
593
874
  get count() {
594
875
  return this._count;
595
876
  }
@@ -719,6 +1000,7 @@ var CacheableStats = class {
719
1000
  };
720
1001
 
721
1002
  // src/index.ts
1003
+ var import_keyv2 = require("keyv");
722
1004
  var CacheableHooks = /* @__PURE__ */ ((CacheableHooks2) => {
723
1005
  CacheableHooks2["BEFORE_SET"] = "BEFORE_SET";
724
1006
  CacheableHooks2["AFTER_SET"] = "AFTER_SET";
@@ -740,6 +1022,10 @@ var Cacheable = class extends import_hookified.Hookified {
740
1022
  _nonBlocking = false;
741
1023
  _ttl;
742
1024
  _stats = new CacheableStats({ enabled: false });
1025
+ /**
1026
+ * Creates a new cacheable instance
1027
+ * @param {CacheableOptions} [options] The options for the cacheable instance
1028
+ */
743
1029
  constructor(options) {
744
1030
  super();
745
1031
  if (options?.primary) {
@@ -758,39 +1044,126 @@ var Cacheable = class extends import_hookified.Hookified {
758
1044
  this.setTtl(options.ttl);
759
1045
  }
760
1046
  }
1047
+ /**
1048
+ * The statistics for the cacheable instance
1049
+ * @returns {CacheableStats} The statistics for the cacheable instance
1050
+ */
761
1051
  get stats() {
762
1052
  return this._stats;
763
1053
  }
1054
+ /**
1055
+ * The primary store for the cacheable instance
1056
+ * @returns {Keyv} The primary store for the cacheable instance
1057
+ */
764
1058
  get primary() {
765
1059
  return this._primary;
766
1060
  }
1061
+ /**
1062
+ * Sets the primary store for the cacheable instance
1063
+ * @param {Keyv} primary The primary store for the cacheable instance
1064
+ */
767
1065
  set primary(primary) {
768
1066
  this._primary = primary;
769
1067
  }
1068
+ /**
1069
+ * The secondary store for the cacheable instance
1070
+ * @returns {Keyv | undefined} The secondary store for the cacheable instance
1071
+ */
770
1072
  get secondary() {
771
1073
  return this._secondary;
772
1074
  }
1075
+ /**
1076
+ * Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
1077
+ * @param {Keyv | undefined} secondary The secondary store for the cacheable instance
1078
+ * @returns {void}
1079
+ */
773
1080
  set secondary(secondary) {
774
1081
  this._secondary = secondary;
775
1082
  }
1083
+ /**
1084
+ * Gets whether the secondary store is non-blocking mode. It is set to false by default.
1085
+ * If it is set to true then the secondary store will not block the primary store.
1086
+ *
1087
+ * [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
1088
+ *
1089
+ * @returns {boolean} Whether the cacheable instance is non-blocking
1090
+ */
776
1091
  get nonBlocking() {
777
1092
  return this._nonBlocking;
778
1093
  }
1094
+ /**
1095
+ * Sets whether the secondary store is non-blocking mode. It is set to false by default.
1096
+ * If it is set to true then the secondary store will not block the primary store.
1097
+ *
1098
+ * [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
1099
+ *
1100
+ * @param {boolean} nonBlocking Whether the cacheable instance is non-blocking
1101
+ * @returns {void}
1102
+ */
779
1103
  set nonBlocking(nonBlocking) {
780
1104
  this._nonBlocking = nonBlocking;
781
1105
  }
1106
+ /**
1107
+ * The time-to-live for the cacheable instance and will be used as the default value.
1108
+ * can be a number in milliseconds or a human-readable format such as `1s` for 1 second or `1h` for 1 hour
1109
+ * or undefined if there is no time-to-live.
1110
+ *
1111
+ * [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
1112
+ *
1113
+ * @returns {number | string | undefined} The time-to-live for the cacheable instance in milliseconds, human-readable format or undefined
1114
+ * @example
1115
+ * ```typescript
1116
+ * const cacheable = new Cacheable({ ttl: '1h' });
1117
+ * console.log(cacheable.ttl); // 1h
1118
+ * ```
1119
+ */
782
1120
  get ttl() {
783
1121
  return this._ttl;
784
1122
  }
1123
+ /**
1124
+ * Sets the time-to-live for the cacheable instance and will be used as the default value.
1125
+ * If you set a number it is miliseconds, if you set a string it is a human-readable
1126
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that
1127
+ * there is no time-to-live.
1128
+ *
1129
+ * [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
1130
+ *
1131
+ * @param {number | string | undefined} ttl The time-to-live for the cacheable instance
1132
+ * @example
1133
+ * ```typescript
1134
+ * const cacheable = new Cacheable();
1135
+ * cacheable.ttl = '1h'; // Set the time-to-live to 1 hour
1136
+ * ```
1137
+ * or setting the time-to-live in milliseconds
1138
+ * ```typescript
1139
+ * const cacheable = new Cacheable();
1140
+ * cacheable.ttl = 3600000; // Set the time-to-live to 1 hour
1141
+ * ```
1142
+ */
785
1143
  set ttl(ttl) {
786
1144
  this.setTtl(ttl);
787
1145
  }
1146
+ /**
1147
+ * Sets the primary store for the cacheable instance
1148
+ * @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
1149
+ * @returns {void}
1150
+ */
788
1151
  setPrimary(primary) {
789
1152
  this._primary = primary instanceof import_keyv.Keyv ? primary : new import_keyv.Keyv(primary);
790
1153
  }
1154
+ /**
1155
+ * Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
1156
+ * @param {Keyv | KeyvStoreAdapter} secondary The secondary store for the cacheable instance
1157
+ * @returns {void}
1158
+ */
791
1159
  setSecondary(secondary) {
792
1160
  this._secondary = secondary instanceof import_keyv.Keyv ? secondary : new import_keyv.Keyv(secondary);
793
1161
  }
1162
+ /**
1163
+ * Gets the value of the key. If the key does not exist in the primary store then it will check the secondary store.
1164
+ * @param {string} key The key to get the value of
1165
+ * @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
1166
+ */
794
1167
  async get(key) {
795
1168
  let result;
796
1169
  try {
@@ -817,6 +1190,11 @@ var Cacheable = class extends import_hookified.Hookified {
817
1190
  }
818
1191
  return result;
819
1192
  }
1193
+ /**
1194
+ * Gets the values of the keys. If the key does not exist in the primary store then it will check the secondary store.
1195
+ * @param {string[]} keys The keys to get the values of
1196
+ * @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
1197
+ */
820
1198
  async getMany(keys) {
821
1199
  let result = [];
822
1200
  try {
@@ -854,6 +1232,15 @@ var Cacheable = class extends import_hookified.Hookified {
854
1232
  }
855
1233
  return result;
856
1234
  }
1235
+ /**
1236
+ * Sets the value of the key. If the secondary store is set then it will also set the value in the secondary store.
1237
+ * @param {string} key the key to set the value of
1238
+ * @param {T} value The value to set
1239
+ * @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
1240
+ * 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
1241
+ * undefined then it will not have a time-to-live.
1242
+ * @returns {boolean} Whether the value was set
1243
+ */
857
1244
  async set(key, value, ttl) {
858
1245
  let result = false;
859
1246
  const finalTtl = shorthandToMilliseconds(ttl ?? this._ttl);
@@ -883,6 +1270,11 @@ var Cacheable = class extends import_hookified.Hookified {
883
1270
  }
884
1271
  return result;
885
1272
  }
1273
+ /**
1274
+ * Sets the values of the keys. If the secondary store is set then it will also set the values in the secondary store.
1275
+ * @param {CacheableItem[]} items The items to set
1276
+ * @returns {boolean} Whether the values were set
1277
+ */
886
1278
  async setMany(items) {
887
1279
  let result = false;
888
1280
  try {
@@ -908,16 +1300,31 @@ var Cacheable = class extends import_hookified.Hookified {
908
1300
  }
909
1301
  return result;
910
1302
  }
1303
+ /**
1304
+ * Takes the value of the key and deletes the key. If the key does not exist then it will return undefined.
1305
+ * @param {string} key The key to take the value of
1306
+ * @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
1307
+ */
911
1308
  async take(key) {
912
1309
  const result = await this.get(key);
913
1310
  await this.delete(key);
914
1311
  return result;
915
1312
  }
1313
+ /**
1314
+ * Takes the values of the keys and deletes the keys. If the key does not exist then it will return undefined.
1315
+ * @param {string[]} keys The keys to take the values of
1316
+ * @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
1317
+ */
916
1318
  async takeMany(keys) {
917
1319
  const result = await this.getMany(keys);
918
1320
  await this.deleteMany(keys);
919
1321
  return result;
920
1322
  }
1323
+ /**
1324
+ * Checks if the key exists in the primary store. If it does not exist then it will check the secondary store.
1325
+ * @param {string} key The key to check
1326
+ * @returns {Promise<boolean>} Whether the key exists
1327
+ */
921
1328
  async has(key) {
922
1329
  const promises = [];
923
1330
  promises.push(this._primary.has(key));
@@ -932,6 +1339,11 @@ var Cacheable = class extends import_hookified.Hookified {
932
1339
  }
933
1340
  return false;
934
1341
  }
1342
+ /**
1343
+ * Checks if the keys exist in the primary store. If it does not exist then it will check the secondary store.
1344
+ * @param {string[]} keys The keys to check
1345
+ * @returns {Promise<boolean[]>} Whether the keys exist
1346
+ */
935
1347
  async hasMany(keys) {
936
1348
  const result = await this.hasManyKeyv(this._primary, keys);
937
1349
  const missingKeys = [];
@@ -950,6 +1362,11 @@ var Cacheable = class extends import_hookified.Hookified {
950
1362
  }
951
1363
  return result;
952
1364
  }
1365
+ /**
1366
+ * Deletes the key from the primary store. If the secondary store is set then it will also delete the key from the secondary store.
1367
+ * @param {string} key The key to delete
1368
+ * @returns {Promise<boolean>} Whether the key was deleted
1369
+ */
953
1370
  async delete(key) {
954
1371
  let result = false;
955
1372
  const promises = [];
@@ -974,6 +1391,11 @@ var Cacheable = class extends import_hookified.Hookified {
974
1391
  }
975
1392
  return result;
976
1393
  }
1394
+ /**
1395
+ * Deletes the keys from the primary store. If the secondary store is set then it will also delete the keys from the secondary store.
1396
+ * @param {string[]} keys The keys to delete
1397
+ * @returns {Promise<boolean>} Whether the keys were deleted
1398
+ */
977
1399
  async deleteMany(keys) {
978
1400
  if (this.stats.enabled) {
979
1401
  const statResult = await this._primary.get(keys);
@@ -994,6 +1416,10 @@ var Cacheable = class extends import_hookified.Hookified {
994
1416
  }
995
1417
  return result;
996
1418
  }
1419
+ /**
1420
+ * Clears the primary store. If the secondary store is set then it will also clear the secondary store.
1421
+ * @returns {Promise<void>}
1422
+ */
997
1423
  async clear() {
998
1424
  const promises = [];
999
1425
  promises.push(this._primary.clear());
@@ -1006,6 +1432,10 @@ var Cacheable = class extends import_hookified.Hookified {
1006
1432
  this._stats.incrementClears();
1007
1433
  }
1008
1434
  }
1435
+ /**
1436
+ * Disconnects the primary store. If the secondary store is set then it will also disconnect the secondary store.
1437
+ * @returns {Promise<void>}
1438
+ */
1009
1439
  async disconnect() {
1010
1440
  const promises = [];
1011
1441
  promises.push(this._primary.disconnect());
@@ -1014,6 +1444,31 @@ var Cacheable = class extends import_hookified.Hookified {
1014
1444
  }
1015
1445
  await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
1016
1446
  }
1447
+ /**
1448
+ * Wraps a function with caching
1449
+ *
1450
+ * [Learn more about wrapping functions](https://cacheable.org/docs/cacheable/#wrap--memoization-for-sync-and-async-functions).
1451
+ * @param {Function} function_ The function to wrap
1452
+ * @param {WrapOptions} [options] The options for the wrap function
1453
+ * @returns {Function} The wrapped function
1454
+ */
1455
+ wrap(function_, options = {}) {
1456
+ const wrapOptions = {
1457
+ ttl: options.ttl,
1458
+ key: options.key,
1459
+ cache: this
1460
+ };
1461
+ return wrap(function_, wrapOptions);
1462
+ }
1463
+ /**
1464
+ * Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
1465
+ * @param {any} object the object to hash
1466
+ * @param {string} algorithm the hash algorithm to use. The default is 'sha256'
1467
+ * @returns {string} the hash of the object
1468
+ */
1469
+ hash(object, algorithm = "sha256") {
1470
+ return hash(object, algorithm);
1471
+ }
1017
1472
  async deleteManyKeyv(keyv, keys) {
1018
1473
  const promises = [];
1019
1474
  for (const key of keys) {
@@ -1055,7 +1510,11 @@ var Cacheable = class extends import_hookified.Hookified {
1055
1510
  CacheableHooks,
1056
1511
  CacheableMemory,
1057
1512
  CacheableStats,
1513
+ Keyv,
1058
1514
  KeyvCacheableMemory,
1515
+ KeyvHooks,
1059
1516
  shorthandToMilliseconds,
1060
- shorthandToTime
1517
+ shorthandToTime,
1518
+ wrap,
1519
+ wrapSync
1061
1520
  });