cacheable 1.8.0 → 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/README.md +48 -23
- package/dist/index.cjs +380 -0
- package/dist/index.d.cts +393 -2
- package/dist/index.d.ts +393 -2
- package/dist/index.js +380 -0
- package/package.json +6 -3
package/dist/index.cjs
CHANGED
|
@@ -249,6 +249,10 @@ var CacheableMemory = class {
|
|
|
249
249
|
// Turned off by default
|
|
250
250
|
_interval = 0;
|
|
251
251
|
// Turned off by default
|
|
252
|
+
/**
|
|
253
|
+
* @constructor
|
|
254
|
+
* @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
|
|
255
|
+
*/
|
|
252
256
|
constructor(options) {
|
|
253
257
|
if (options?.ttl) {
|
|
254
258
|
this.setTtl(options.ttl);
|
|
@@ -264,40 +268,89 @@ var CacheableMemory = class {
|
|
|
264
268
|
}
|
|
265
269
|
this.startIntervalCheck();
|
|
266
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
|
+
*/
|
|
267
275
|
get ttl() {
|
|
268
276
|
return this._ttl;
|
|
269
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
|
+
*/
|
|
270
282
|
set ttl(value) {
|
|
271
283
|
this.setTtl(value);
|
|
272
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
|
+
*/
|
|
273
289
|
get useClone() {
|
|
274
290
|
return this._useClone;
|
|
275
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
|
+
*/
|
|
276
296
|
set useClone(value) {
|
|
277
297
|
this._useClone = value;
|
|
278
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
|
+
*/
|
|
279
303
|
get lruSize() {
|
|
280
304
|
return this._lruSize;
|
|
281
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
|
+
*/
|
|
282
310
|
set lruSize(value) {
|
|
283
311
|
this._lruSize = value;
|
|
284
312
|
this.lruResize();
|
|
285
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
|
+
*/
|
|
286
318
|
get checkInterval() {
|
|
287
319
|
return this._checkInterval;
|
|
288
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
|
+
*/
|
|
289
325
|
set checkInterval(value) {
|
|
290
326
|
this._checkInterval = value;
|
|
291
327
|
}
|
|
328
|
+
/**
|
|
329
|
+
* Gets the size of the cache
|
|
330
|
+
* @returns {number} - The size of the cache
|
|
331
|
+
*/
|
|
292
332
|
get size() {
|
|
293
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;
|
|
294
334
|
}
|
|
335
|
+
/**
|
|
336
|
+
* Gets the keys
|
|
337
|
+
* @returns {IterableIterator<string>} - The keys
|
|
338
|
+
*/
|
|
295
339
|
get keys() {
|
|
296
340
|
return this.concatStores().keys();
|
|
297
341
|
}
|
|
342
|
+
/**
|
|
343
|
+
* Gets the items
|
|
344
|
+
* @returns {IterableIterator<CacheableStoreItem>} - The items
|
|
345
|
+
*/
|
|
298
346
|
get items() {
|
|
299
347
|
return this.concatStores().values();
|
|
300
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
|
+
*/
|
|
301
354
|
get(key) {
|
|
302
355
|
const store = this.getStore(key);
|
|
303
356
|
const item = store.get(key);
|
|
@@ -314,6 +367,11 @@ var CacheableMemory = class {
|
|
|
314
367
|
}
|
|
315
368
|
return this.clone(item.value);
|
|
316
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
|
+
*/
|
|
317
375
|
getMany(keys) {
|
|
318
376
|
const result = new Array();
|
|
319
377
|
for (const key of keys) {
|
|
@@ -321,6 +379,11 @@ var CacheableMemory = class {
|
|
|
321
379
|
}
|
|
322
380
|
return result;
|
|
323
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
|
+
*/
|
|
324
387
|
getRaw(key) {
|
|
325
388
|
const store = this.getStore(key);
|
|
326
389
|
const item = store.get(key);
|
|
@@ -334,6 +397,11 @@ var CacheableMemory = class {
|
|
|
334
397
|
this.lruMoveToFront(key);
|
|
335
398
|
return item;
|
|
336
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
|
+
*/
|
|
337
405
|
getManyRaw(keys) {
|
|
338
406
|
const result = new Array();
|
|
339
407
|
for (const key of keys) {
|
|
@@ -341,6 +409,14 @@ var CacheableMemory = class {
|
|
|
341
409
|
}
|
|
342
410
|
return result;
|
|
343
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
|
+
*/
|
|
344
420
|
set(key, value, ttl) {
|
|
345
421
|
const store = this.getStore(key);
|
|
346
422
|
let expires;
|
|
@@ -371,15 +447,43 @@ var CacheableMemory = class {
|
|
|
371
447
|
expires
|
|
372
448
|
});
|
|
373
449
|
}
|
|
450
|
+
/**
|
|
451
|
+
* Sets the values of the keys
|
|
452
|
+
* @param {CacheableItem[]} items - The items to set
|
|
453
|
+
* @returns {void}
|
|
454
|
+
*/
|
|
374
455
|
setMany(items) {
|
|
375
456
|
for (const item of items) {
|
|
376
457
|
this.set(item.key, item.value, item.ttl);
|
|
377
458
|
}
|
|
378
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
|
+
*/
|
|
379
465
|
has(key) {
|
|
380
466
|
const item = this.get(key);
|
|
381
467
|
return Boolean(item);
|
|
382
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
|
+
*/
|
|
383
487
|
take(key) {
|
|
384
488
|
const item = this.get(key);
|
|
385
489
|
if (!item) {
|
|
@@ -388,6 +492,11 @@ var CacheableMemory = class {
|
|
|
388
492
|
this.delete(key);
|
|
389
493
|
return item;
|
|
390
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
|
+
*/
|
|
391
500
|
takeMany(keys) {
|
|
392
501
|
const result = new Array();
|
|
393
502
|
for (const key of keys) {
|
|
@@ -395,15 +504,29 @@ var CacheableMemory = class {
|
|
|
395
504
|
}
|
|
396
505
|
return result;
|
|
397
506
|
}
|
|
507
|
+
/**
|
|
508
|
+
* Delete the key
|
|
509
|
+
* @param {string} key - The key to delete
|
|
510
|
+
* @returns {void}
|
|
511
|
+
*/
|
|
398
512
|
delete(key) {
|
|
399
513
|
const store = this.getStore(key);
|
|
400
514
|
store.delete(key);
|
|
401
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* Delete the keys
|
|
518
|
+
* @param {string[]} keys - The keys to delete
|
|
519
|
+
* @returns {void}
|
|
520
|
+
*/
|
|
402
521
|
deleteMany(keys) {
|
|
403
522
|
for (const key of keys) {
|
|
404
523
|
this.delete(key);
|
|
405
524
|
}
|
|
406
525
|
}
|
|
526
|
+
/**
|
|
527
|
+
* Clear the cache
|
|
528
|
+
* @returns {void}
|
|
529
|
+
*/
|
|
407
530
|
clear() {
|
|
408
531
|
this._hash0.clear();
|
|
409
532
|
this._hash1.clear();
|
|
@@ -417,6 +540,11 @@ var CacheableMemory = class {
|
|
|
417
540
|
this._hash9.clear();
|
|
418
541
|
this._hashCache.clear();
|
|
419
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
|
+
*/
|
|
420
548
|
hashKey(key) {
|
|
421
549
|
const cacheHashNumber = this._hashCache.get(key);
|
|
422
550
|
if (cacheHashNumber) {
|
|
@@ -431,6 +559,11 @@ var CacheableMemory = class {
|
|
|
431
559
|
this._hashCache.set(key, result);
|
|
432
560
|
return result;
|
|
433
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
|
+
*/
|
|
434
567
|
getStore(key) {
|
|
435
568
|
const hashKey = this.hashKey(key);
|
|
436
569
|
switch (hashKey) {
|
|
@@ -466,24 +599,43 @@ var CacheableMemory = class {
|
|
|
466
599
|
}
|
|
467
600
|
}
|
|
468
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
|
+
*/
|
|
469
607
|
clone(value) {
|
|
470
608
|
if (this.isPrimitive(value)) {
|
|
471
609
|
return value;
|
|
472
610
|
}
|
|
473
611
|
return structuredClone(value);
|
|
474
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
|
+
*/
|
|
475
618
|
lruAddToFront(key) {
|
|
476
619
|
if (this._lruSize === 0) {
|
|
477
620
|
return;
|
|
478
621
|
}
|
|
479
622
|
this._lru.addToFront(key);
|
|
480
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
|
+
*/
|
|
481
629
|
lruMoveToFront(key) {
|
|
482
630
|
if (this._lruSize === 0) {
|
|
483
631
|
return;
|
|
484
632
|
}
|
|
485
633
|
this._lru.moveToFront(key);
|
|
486
634
|
}
|
|
635
|
+
/**
|
|
636
|
+
* Resize the LRU cache. This is for internal use
|
|
637
|
+
* @returns {void}
|
|
638
|
+
*/
|
|
487
639
|
lruResize() {
|
|
488
640
|
if (this._lruSize === 0) {
|
|
489
641
|
return;
|
|
@@ -496,6 +648,10 @@ var CacheableMemory = class {
|
|
|
496
648
|
}
|
|
497
649
|
}
|
|
498
650
|
}
|
|
651
|
+
/**
|
|
652
|
+
* Check for expiration. This is for internal use
|
|
653
|
+
* @returns {void}
|
|
654
|
+
*/
|
|
499
655
|
checkExpiration() {
|
|
500
656
|
const stores = this.concatStores();
|
|
501
657
|
for (const item of stores.values()) {
|
|
@@ -504,6 +660,10 @@ var CacheableMemory = class {
|
|
|
504
660
|
}
|
|
505
661
|
}
|
|
506
662
|
}
|
|
663
|
+
/**
|
|
664
|
+
* Start the interval check. This is for internal use
|
|
665
|
+
* @returns {void}
|
|
666
|
+
*/
|
|
507
667
|
startIntervalCheck() {
|
|
508
668
|
if (this._checkInterval > 0) {
|
|
509
669
|
this._interval = setInterval(() => {
|
|
@@ -511,6 +671,10 @@ var CacheableMemory = class {
|
|
|
511
671
|
}, this._checkInterval);
|
|
512
672
|
}
|
|
513
673
|
}
|
|
674
|
+
/**
|
|
675
|
+
* Stop the interval check. This is for internal use
|
|
676
|
+
* @returns {void}
|
|
677
|
+
*/
|
|
514
678
|
stopIntervalCheck() {
|
|
515
679
|
if (this._interval) {
|
|
516
680
|
clearInterval(this._interval);
|
|
@@ -518,9 +682,21 @@ var CacheableMemory = class {
|
|
|
518
682
|
this._interval = 0;
|
|
519
683
|
this._checkInterval = 0;
|
|
520
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
|
+
*/
|
|
521
691
|
hash(object, algorithm = "sha256") {
|
|
522
692
|
return hash(object, algorithm);
|
|
523
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
|
+
*/
|
|
524
700
|
wrap(function_, options = {}) {
|
|
525
701
|
const wrapOptions = {
|
|
526
702
|
ttl: options.ttl,
|
|
@@ -623,36 +799,78 @@ var CacheableStats = class {
|
|
|
623
799
|
this._enabled = options.enabled;
|
|
624
800
|
}
|
|
625
801
|
}
|
|
802
|
+
/**
|
|
803
|
+
* @returns {boolean} - Whether the stats are enabled
|
|
804
|
+
*/
|
|
626
805
|
get enabled() {
|
|
627
806
|
return this._enabled;
|
|
628
807
|
}
|
|
808
|
+
/**
|
|
809
|
+
* @param {boolean} enabled - Whether to enable the stats
|
|
810
|
+
*/
|
|
629
811
|
set enabled(enabled) {
|
|
630
812
|
this._enabled = enabled;
|
|
631
813
|
}
|
|
814
|
+
/**
|
|
815
|
+
* @returns {number} - The number of hits
|
|
816
|
+
* @readonly
|
|
817
|
+
*/
|
|
632
818
|
get hits() {
|
|
633
819
|
return this._hits;
|
|
634
820
|
}
|
|
821
|
+
/**
|
|
822
|
+
* @returns {number} - The number of misses
|
|
823
|
+
* @readonly
|
|
824
|
+
*/
|
|
635
825
|
get misses() {
|
|
636
826
|
return this._misses;
|
|
637
827
|
}
|
|
828
|
+
/**
|
|
829
|
+
* @returns {number} - The number of gets
|
|
830
|
+
* @readonly
|
|
831
|
+
*/
|
|
638
832
|
get gets() {
|
|
639
833
|
return this._gets;
|
|
640
834
|
}
|
|
835
|
+
/**
|
|
836
|
+
* @returns {number} - The number of sets
|
|
837
|
+
* @readonly
|
|
838
|
+
*/
|
|
641
839
|
get sets() {
|
|
642
840
|
return this._sets;
|
|
643
841
|
}
|
|
842
|
+
/**
|
|
843
|
+
* @returns {number} - The number of deletes
|
|
844
|
+
* @readonly
|
|
845
|
+
*/
|
|
644
846
|
get deletes() {
|
|
645
847
|
return this._deletes;
|
|
646
848
|
}
|
|
849
|
+
/**
|
|
850
|
+
* @returns {number} - The number of clears
|
|
851
|
+
* @readonly
|
|
852
|
+
*/
|
|
647
853
|
get clears() {
|
|
648
854
|
return this._clears;
|
|
649
855
|
}
|
|
856
|
+
/**
|
|
857
|
+
* @returns {number} - The vsize (value size) of the cache instance
|
|
858
|
+
* @readonly
|
|
859
|
+
*/
|
|
650
860
|
get vsize() {
|
|
651
861
|
return this._vsize;
|
|
652
862
|
}
|
|
863
|
+
/**
|
|
864
|
+
* @returns {number} - The ksize (key size) of the cache instance
|
|
865
|
+
* @readonly
|
|
866
|
+
*/
|
|
653
867
|
get ksize() {
|
|
654
868
|
return this._ksize;
|
|
655
869
|
}
|
|
870
|
+
/**
|
|
871
|
+
* @returns {number} - The count of the cache instance
|
|
872
|
+
* @readonly
|
|
873
|
+
*/
|
|
656
874
|
get count() {
|
|
657
875
|
return this._count;
|
|
658
876
|
}
|
|
@@ -804,6 +1022,10 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
804
1022
|
_nonBlocking = false;
|
|
805
1023
|
_ttl;
|
|
806
1024
|
_stats = new CacheableStats({ enabled: false });
|
|
1025
|
+
/**
|
|
1026
|
+
* Creates a new cacheable instance
|
|
1027
|
+
* @param {CacheableOptions} [options] The options for the cacheable instance
|
|
1028
|
+
*/
|
|
807
1029
|
constructor(options) {
|
|
808
1030
|
super();
|
|
809
1031
|
if (options?.primary) {
|
|
@@ -822,39 +1044,126 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
822
1044
|
this.setTtl(options.ttl);
|
|
823
1045
|
}
|
|
824
1046
|
}
|
|
1047
|
+
/**
|
|
1048
|
+
* The statistics for the cacheable instance
|
|
1049
|
+
* @returns {CacheableStats} The statistics for the cacheable instance
|
|
1050
|
+
*/
|
|
825
1051
|
get stats() {
|
|
826
1052
|
return this._stats;
|
|
827
1053
|
}
|
|
1054
|
+
/**
|
|
1055
|
+
* The primary store for the cacheable instance
|
|
1056
|
+
* @returns {Keyv} The primary store for the cacheable instance
|
|
1057
|
+
*/
|
|
828
1058
|
get primary() {
|
|
829
1059
|
return this._primary;
|
|
830
1060
|
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Sets the primary store for the cacheable instance
|
|
1063
|
+
* @param {Keyv} primary The primary store for the cacheable instance
|
|
1064
|
+
*/
|
|
831
1065
|
set primary(primary) {
|
|
832
1066
|
this._primary = primary;
|
|
833
1067
|
}
|
|
1068
|
+
/**
|
|
1069
|
+
* The secondary store for the cacheable instance
|
|
1070
|
+
* @returns {Keyv | undefined} The secondary store for the cacheable instance
|
|
1071
|
+
*/
|
|
834
1072
|
get secondary() {
|
|
835
1073
|
return this._secondary;
|
|
836
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
|
+
*/
|
|
837
1080
|
set secondary(secondary) {
|
|
838
1081
|
this._secondary = secondary;
|
|
839
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
|
+
*/
|
|
840
1091
|
get nonBlocking() {
|
|
841
1092
|
return this._nonBlocking;
|
|
842
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
|
+
*/
|
|
843
1103
|
set nonBlocking(nonBlocking) {
|
|
844
1104
|
this._nonBlocking = nonBlocking;
|
|
845
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
|
+
*/
|
|
846
1120
|
get ttl() {
|
|
847
1121
|
return this._ttl;
|
|
848
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
|
+
*/
|
|
849
1143
|
set ttl(ttl) {
|
|
850
1144
|
this.setTtl(ttl);
|
|
851
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
|
+
*/
|
|
852
1151
|
setPrimary(primary) {
|
|
853
1152
|
this._primary = primary instanceof import_keyv.Keyv ? primary : new import_keyv.Keyv(primary);
|
|
854
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
|
+
*/
|
|
855
1159
|
setSecondary(secondary) {
|
|
856
1160
|
this._secondary = secondary instanceof import_keyv.Keyv ? secondary : new import_keyv.Keyv(secondary);
|
|
857
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
|
+
*/
|
|
858
1167
|
async get(key) {
|
|
859
1168
|
let result;
|
|
860
1169
|
try {
|
|
@@ -881,6 +1190,11 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
881
1190
|
}
|
|
882
1191
|
return result;
|
|
883
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
|
+
*/
|
|
884
1198
|
async getMany(keys) {
|
|
885
1199
|
let result = [];
|
|
886
1200
|
try {
|
|
@@ -918,6 +1232,15 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
918
1232
|
}
|
|
919
1233
|
return result;
|
|
920
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
|
+
*/
|
|
921
1244
|
async set(key, value, ttl) {
|
|
922
1245
|
let result = false;
|
|
923
1246
|
const finalTtl = shorthandToMilliseconds(ttl ?? this._ttl);
|
|
@@ -947,6 +1270,11 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
947
1270
|
}
|
|
948
1271
|
return result;
|
|
949
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
|
+
*/
|
|
950
1278
|
async setMany(items) {
|
|
951
1279
|
let result = false;
|
|
952
1280
|
try {
|
|
@@ -972,16 +1300,31 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
972
1300
|
}
|
|
973
1301
|
return result;
|
|
974
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
|
+
*/
|
|
975
1308
|
async take(key) {
|
|
976
1309
|
const result = await this.get(key);
|
|
977
1310
|
await this.delete(key);
|
|
978
1311
|
return result;
|
|
979
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
|
+
*/
|
|
980
1318
|
async takeMany(keys) {
|
|
981
1319
|
const result = await this.getMany(keys);
|
|
982
1320
|
await this.deleteMany(keys);
|
|
983
1321
|
return result;
|
|
984
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
|
+
*/
|
|
985
1328
|
async has(key) {
|
|
986
1329
|
const promises = [];
|
|
987
1330
|
promises.push(this._primary.has(key));
|
|
@@ -996,6 +1339,11 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
996
1339
|
}
|
|
997
1340
|
return false;
|
|
998
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
|
+
*/
|
|
999
1347
|
async hasMany(keys) {
|
|
1000
1348
|
const result = await this.hasManyKeyv(this._primary, keys);
|
|
1001
1349
|
const missingKeys = [];
|
|
@@ -1014,6 +1362,11 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
1014
1362
|
}
|
|
1015
1363
|
return result;
|
|
1016
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
|
+
*/
|
|
1017
1370
|
async delete(key) {
|
|
1018
1371
|
let result = false;
|
|
1019
1372
|
const promises = [];
|
|
@@ -1038,6 +1391,11 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
1038
1391
|
}
|
|
1039
1392
|
return result;
|
|
1040
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
|
+
*/
|
|
1041
1399
|
async deleteMany(keys) {
|
|
1042
1400
|
if (this.stats.enabled) {
|
|
1043
1401
|
const statResult = await this._primary.get(keys);
|
|
@@ -1058,6 +1416,10 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
1058
1416
|
}
|
|
1059
1417
|
return result;
|
|
1060
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
|
+
*/
|
|
1061
1423
|
async clear() {
|
|
1062
1424
|
const promises = [];
|
|
1063
1425
|
promises.push(this._primary.clear());
|
|
@@ -1070,6 +1432,10 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
1070
1432
|
this._stats.incrementClears();
|
|
1071
1433
|
}
|
|
1072
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
|
+
*/
|
|
1073
1439
|
async disconnect() {
|
|
1074
1440
|
const promises = [];
|
|
1075
1441
|
promises.push(this._primary.disconnect());
|
|
@@ -1078,6 +1444,14 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
1078
1444
|
}
|
|
1079
1445
|
await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
|
|
1080
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
|
+
*/
|
|
1081
1455
|
wrap(function_, options = {}) {
|
|
1082
1456
|
const wrapOptions = {
|
|
1083
1457
|
ttl: options.ttl,
|
|
@@ -1086,6 +1460,12 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
1086
1460
|
};
|
|
1087
1461
|
return wrap(function_, wrapOptions);
|
|
1088
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
|
+
*/
|
|
1089
1469
|
hash(object, algorithm = "sha256") {
|
|
1090
1470
|
return hash(object, algorithm);
|
|
1091
1471
|
}
|