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.js
CHANGED
|
@@ -204,6 +204,10 @@ var CacheableMemory = class {
|
|
|
204
204
|
// Turned off by default
|
|
205
205
|
_interval = 0;
|
|
206
206
|
// Turned off by default
|
|
207
|
+
/**
|
|
208
|
+
* @constructor
|
|
209
|
+
* @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
|
|
210
|
+
*/
|
|
207
211
|
constructor(options) {
|
|
208
212
|
if (options?.ttl) {
|
|
209
213
|
this.setTtl(options.ttl);
|
|
@@ -219,40 +223,89 @@ var CacheableMemory = class {
|
|
|
219
223
|
}
|
|
220
224
|
this.startIntervalCheck();
|
|
221
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
|
+
*/
|
|
222
230
|
get ttl() {
|
|
223
231
|
return this._ttl;
|
|
224
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
|
+
*/
|
|
225
237
|
set ttl(value) {
|
|
226
238
|
this.setTtl(value);
|
|
227
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
|
+
*/
|
|
228
244
|
get useClone() {
|
|
229
245
|
return this._useClone;
|
|
230
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
|
+
*/
|
|
231
251
|
set useClone(value) {
|
|
232
252
|
this._useClone = value;
|
|
233
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
|
+
*/
|
|
234
258
|
get lruSize() {
|
|
235
259
|
return this._lruSize;
|
|
236
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
|
+
*/
|
|
237
265
|
set lruSize(value) {
|
|
238
266
|
this._lruSize = value;
|
|
239
267
|
this.lruResize();
|
|
240
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
|
+
*/
|
|
241
273
|
get checkInterval() {
|
|
242
274
|
return this._checkInterval;
|
|
243
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
|
+
*/
|
|
244
280
|
set checkInterval(value) {
|
|
245
281
|
this._checkInterval = value;
|
|
246
282
|
}
|
|
283
|
+
/**
|
|
284
|
+
* Gets the size of the cache
|
|
285
|
+
* @returns {number} - The size of the cache
|
|
286
|
+
*/
|
|
247
287
|
get size() {
|
|
248
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;
|
|
249
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* Gets the keys
|
|
292
|
+
* @returns {IterableIterator<string>} - The keys
|
|
293
|
+
*/
|
|
250
294
|
get keys() {
|
|
251
295
|
return this.concatStores().keys();
|
|
252
296
|
}
|
|
297
|
+
/**
|
|
298
|
+
* Gets the items
|
|
299
|
+
* @returns {IterableIterator<CacheableStoreItem>} - The items
|
|
300
|
+
*/
|
|
253
301
|
get items() {
|
|
254
302
|
return this.concatStores().values();
|
|
255
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
|
+
*/
|
|
256
309
|
get(key) {
|
|
257
310
|
const store = this.getStore(key);
|
|
258
311
|
const item = store.get(key);
|
|
@@ -269,6 +322,11 @@ var CacheableMemory = class {
|
|
|
269
322
|
}
|
|
270
323
|
return this.clone(item.value);
|
|
271
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
|
+
*/
|
|
272
330
|
getMany(keys) {
|
|
273
331
|
const result = new Array();
|
|
274
332
|
for (const key of keys) {
|
|
@@ -276,6 +334,11 @@ var CacheableMemory = class {
|
|
|
276
334
|
}
|
|
277
335
|
return result;
|
|
278
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
|
+
*/
|
|
279
342
|
getRaw(key) {
|
|
280
343
|
const store = this.getStore(key);
|
|
281
344
|
const item = store.get(key);
|
|
@@ -289,6 +352,11 @@ var CacheableMemory = class {
|
|
|
289
352
|
this.lruMoveToFront(key);
|
|
290
353
|
return item;
|
|
291
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
|
+
*/
|
|
292
360
|
getManyRaw(keys) {
|
|
293
361
|
const result = new Array();
|
|
294
362
|
for (const key of keys) {
|
|
@@ -296,6 +364,14 @@ var CacheableMemory = class {
|
|
|
296
364
|
}
|
|
297
365
|
return result;
|
|
298
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
|
+
*/
|
|
299
375
|
set(key, value, ttl) {
|
|
300
376
|
const store = this.getStore(key);
|
|
301
377
|
let expires;
|
|
@@ -326,15 +402,43 @@ var CacheableMemory = class {
|
|
|
326
402
|
expires
|
|
327
403
|
});
|
|
328
404
|
}
|
|
405
|
+
/**
|
|
406
|
+
* Sets the values of the keys
|
|
407
|
+
* @param {CacheableItem[]} items - The items to set
|
|
408
|
+
* @returns {void}
|
|
409
|
+
*/
|
|
329
410
|
setMany(items) {
|
|
330
411
|
for (const item of items) {
|
|
331
412
|
this.set(item.key, item.value, item.ttl);
|
|
332
413
|
}
|
|
333
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
|
+
*/
|
|
334
420
|
has(key) {
|
|
335
421
|
const item = this.get(key);
|
|
336
422
|
return Boolean(item);
|
|
337
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
|
+
*/
|
|
338
442
|
take(key) {
|
|
339
443
|
const item = this.get(key);
|
|
340
444
|
if (!item) {
|
|
@@ -343,6 +447,11 @@ var CacheableMemory = class {
|
|
|
343
447
|
this.delete(key);
|
|
344
448
|
return item;
|
|
345
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
|
+
*/
|
|
346
455
|
takeMany(keys) {
|
|
347
456
|
const result = new Array();
|
|
348
457
|
for (const key of keys) {
|
|
@@ -350,15 +459,29 @@ var CacheableMemory = class {
|
|
|
350
459
|
}
|
|
351
460
|
return result;
|
|
352
461
|
}
|
|
462
|
+
/**
|
|
463
|
+
* Delete the key
|
|
464
|
+
* @param {string} key - The key to delete
|
|
465
|
+
* @returns {void}
|
|
466
|
+
*/
|
|
353
467
|
delete(key) {
|
|
354
468
|
const store = this.getStore(key);
|
|
355
469
|
store.delete(key);
|
|
356
470
|
}
|
|
471
|
+
/**
|
|
472
|
+
* Delete the keys
|
|
473
|
+
* @param {string[]} keys - The keys to delete
|
|
474
|
+
* @returns {void}
|
|
475
|
+
*/
|
|
357
476
|
deleteMany(keys) {
|
|
358
477
|
for (const key of keys) {
|
|
359
478
|
this.delete(key);
|
|
360
479
|
}
|
|
361
480
|
}
|
|
481
|
+
/**
|
|
482
|
+
* Clear the cache
|
|
483
|
+
* @returns {void}
|
|
484
|
+
*/
|
|
362
485
|
clear() {
|
|
363
486
|
this._hash0.clear();
|
|
364
487
|
this._hash1.clear();
|
|
@@ -372,6 +495,11 @@ var CacheableMemory = class {
|
|
|
372
495
|
this._hash9.clear();
|
|
373
496
|
this._hashCache.clear();
|
|
374
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
|
+
*/
|
|
375
503
|
hashKey(key) {
|
|
376
504
|
const cacheHashNumber = this._hashCache.get(key);
|
|
377
505
|
if (cacheHashNumber) {
|
|
@@ -386,6 +514,11 @@ var CacheableMemory = class {
|
|
|
386
514
|
this._hashCache.set(key, result);
|
|
387
515
|
return result;
|
|
388
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
|
+
*/
|
|
389
522
|
getStore(key) {
|
|
390
523
|
const hashKey = this.hashKey(key);
|
|
391
524
|
switch (hashKey) {
|
|
@@ -421,24 +554,43 @@ var CacheableMemory = class {
|
|
|
421
554
|
}
|
|
422
555
|
}
|
|
423
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
|
+
*/
|
|
424
562
|
clone(value) {
|
|
425
563
|
if (this.isPrimitive(value)) {
|
|
426
564
|
return value;
|
|
427
565
|
}
|
|
428
566
|
return structuredClone(value);
|
|
429
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
|
+
*/
|
|
430
573
|
lruAddToFront(key) {
|
|
431
574
|
if (this._lruSize === 0) {
|
|
432
575
|
return;
|
|
433
576
|
}
|
|
434
577
|
this._lru.addToFront(key);
|
|
435
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
|
+
*/
|
|
436
584
|
lruMoveToFront(key) {
|
|
437
585
|
if (this._lruSize === 0) {
|
|
438
586
|
return;
|
|
439
587
|
}
|
|
440
588
|
this._lru.moveToFront(key);
|
|
441
589
|
}
|
|
590
|
+
/**
|
|
591
|
+
* Resize the LRU cache. This is for internal use
|
|
592
|
+
* @returns {void}
|
|
593
|
+
*/
|
|
442
594
|
lruResize() {
|
|
443
595
|
if (this._lruSize === 0) {
|
|
444
596
|
return;
|
|
@@ -451,6 +603,10 @@ var CacheableMemory = class {
|
|
|
451
603
|
}
|
|
452
604
|
}
|
|
453
605
|
}
|
|
606
|
+
/**
|
|
607
|
+
* Check for expiration. This is for internal use
|
|
608
|
+
* @returns {void}
|
|
609
|
+
*/
|
|
454
610
|
checkExpiration() {
|
|
455
611
|
const stores = this.concatStores();
|
|
456
612
|
for (const item of stores.values()) {
|
|
@@ -459,6 +615,10 @@ var CacheableMemory = class {
|
|
|
459
615
|
}
|
|
460
616
|
}
|
|
461
617
|
}
|
|
618
|
+
/**
|
|
619
|
+
* Start the interval check. This is for internal use
|
|
620
|
+
* @returns {void}
|
|
621
|
+
*/
|
|
462
622
|
startIntervalCheck() {
|
|
463
623
|
if (this._checkInterval > 0) {
|
|
464
624
|
this._interval = setInterval(() => {
|
|
@@ -466,6 +626,10 @@ var CacheableMemory = class {
|
|
|
466
626
|
}, this._checkInterval);
|
|
467
627
|
}
|
|
468
628
|
}
|
|
629
|
+
/**
|
|
630
|
+
* Stop the interval check. This is for internal use
|
|
631
|
+
* @returns {void}
|
|
632
|
+
*/
|
|
469
633
|
stopIntervalCheck() {
|
|
470
634
|
if (this._interval) {
|
|
471
635
|
clearInterval(this._interval);
|
|
@@ -473,9 +637,21 @@ var CacheableMemory = class {
|
|
|
473
637
|
this._interval = 0;
|
|
474
638
|
this._checkInterval = 0;
|
|
475
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
|
+
*/
|
|
476
646
|
hash(object, algorithm = "sha256") {
|
|
477
647
|
return hash(object, algorithm);
|
|
478
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
|
+
*/
|
|
479
655
|
wrap(function_, options = {}) {
|
|
480
656
|
const wrapOptions = {
|
|
481
657
|
ttl: options.ttl,
|
|
@@ -578,36 +754,78 @@ var CacheableStats = class {
|
|
|
578
754
|
this._enabled = options.enabled;
|
|
579
755
|
}
|
|
580
756
|
}
|
|
757
|
+
/**
|
|
758
|
+
* @returns {boolean} - Whether the stats are enabled
|
|
759
|
+
*/
|
|
581
760
|
get enabled() {
|
|
582
761
|
return this._enabled;
|
|
583
762
|
}
|
|
763
|
+
/**
|
|
764
|
+
* @param {boolean} enabled - Whether to enable the stats
|
|
765
|
+
*/
|
|
584
766
|
set enabled(enabled) {
|
|
585
767
|
this._enabled = enabled;
|
|
586
768
|
}
|
|
769
|
+
/**
|
|
770
|
+
* @returns {number} - The number of hits
|
|
771
|
+
* @readonly
|
|
772
|
+
*/
|
|
587
773
|
get hits() {
|
|
588
774
|
return this._hits;
|
|
589
775
|
}
|
|
776
|
+
/**
|
|
777
|
+
* @returns {number} - The number of misses
|
|
778
|
+
* @readonly
|
|
779
|
+
*/
|
|
590
780
|
get misses() {
|
|
591
781
|
return this._misses;
|
|
592
782
|
}
|
|
783
|
+
/**
|
|
784
|
+
* @returns {number} - The number of gets
|
|
785
|
+
* @readonly
|
|
786
|
+
*/
|
|
593
787
|
get gets() {
|
|
594
788
|
return this._gets;
|
|
595
789
|
}
|
|
790
|
+
/**
|
|
791
|
+
* @returns {number} - The number of sets
|
|
792
|
+
* @readonly
|
|
793
|
+
*/
|
|
596
794
|
get sets() {
|
|
597
795
|
return this._sets;
|
|
598
796
|
}
|
|
797
|
+
/**
|
|
798
|
+
* @returns {number} - The number of deletes
|
|
799
|
+
* @readonly
|
|
800
|
+
*/
|
|
599
801
|
get deletes() {
|
|
600
802
|
return this._deletes;
|
|
601
803
|
}
|
|
804
|
+
/**
|
|
805
|
+
* @returns {number} - The number of clears
|
|
806
|
+
* @readonly
|
|
807
|
+
*/
|
|
602
808
|
get clears() {
|
|
603
809
|
return this._clears;
|
|
604
810
|
}
|
|
811
|
+
/**
|
|
812
|
+
* @returns {number} - The vsize (value size) of the cache instance
|
|
813
|
+
* @readonly
|
|
814
|
+
*/
|
|
605
815
|
get vsize() {
|
|
606
816
|
return this._vsize;
|
|
607
817
|
}
|
|
818
|
+
/**
|
|
819
|
+
* @returns {number} - The ksize (key size) of the cache instance
|
|
820
|
+
* @readonly
|
|
821
|
+
*/
|
|
608
822
|
get ksize() {
|
|
609
823
|
return this._ksize;
|
|
610
824
|
}
|
|
825
|
+
/**
|
|
826
|
+
* @returns {number} - The count of the cache instance
|
|
827
|
+
* @readonly
|
|
828
|
+
*/
|
|
611
829
|
get count() {
|
|
612
830
|
return this._count;
|
|
613
831
|
}
|
|
@@ -762,6 +980,10 @@ var Cacheable = class extends Hookified {
|
|
|
762
980
|
_nonBlocking = false;
|
|
763
981
|
_ttl;
|
|
764
982
|
_stats = new CacheableStats({ enabled: false });
|
|
983
|
+
/**
|
|
984
|
+
* Creates a new cacheable instance
|
|
985
|
+
* @param {CacheableOptions} [options] The options for the cacheable instance
|
|
986
|
+
*/
|
|
765
987
|
constructor(options) {
|
|
766
988
|
super();
|
|
767
989
|
if (options?.primary) {
|
|
@@ -780,39 +1002,126 @@ var Cacheable = class extends Hookified {
|
|
|
780
1002
|
this.setTtl(options.ttl);
|
|
781
1003
|
}
|
|
782
1004
|
}
|
|
1005
|
+
/**
|
|
1006
|
+
* The statistics for the cacheable instance
|
|
1007
|
+
* @returns {CacheableStats} The statistics for the cacheable instance
|
|
1008
|
+
*/
|
|
783
1009
|
get stats() {
|
|
784
1010
|
return this._stats;
|
|
785
1011
|
}
|
|
1012
|
+
/**
|
|
1013
|
+
* The primary store for the cacheable instance
|
|
1014
|
+
* @returns {Keyv} The primary store for the cacheable instance
|
|
1015
|
+
*/
|
|
786
1016
|
get primary() {
|
|
787
1017
|
return this._primary;
|
|
788
1018
|
}
|
|
1019
|
+
/**
|
|
1020
|
+
* Sets the primary store for the cacheable instance
|
|
1021
|
+
* @param {Keyv} primary The primary store for the cacheable instance
|
|
1022
|
+
*/
|
|
789
1023
|
set primary(primary) {
|
|
790
1024
|
this._primary = primary;
|
|
791
1025
|
}
|
|
1026
|
+
/**
|
|
1027
|
+
* The secondary store for the cacheable instance
|
|
1028
|
+
* @returns {Keyv | undefined} The secondary store for the cacheable instance
|
|
1029
|
+
*/
|
|
792
1030
|
get secondary() {
|
|
793
1031
|
return this._secondary;
|
|
794
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
|
+
*/
|
|
795
1038
|
set secondary(secondary) {
|
|
796
1039
|
this._secondary = secondary;
|
|
797
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
|
+
*/
|
|
798
1049
|
get nonBlocking() {
|
|
799
1050
|
return this._nonBlocking;
|
|
800
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
|
+
*/
|
|
801
1061
|
set nonBlocking(nonBlocking) {
|
|
802
1062
|
this._nonBlocking = nonBlocking;
|
|
803
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
|
+
*/
|
|
804
1078
|
get ttl() {
|
|
805
1079
|
return this._ttl;
|
|
806
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
|
+
*/
|
|
807
1101
|
set ttl(ttl) {
|
|
808
1102
|
this.setTtl(ttl);
|
|
809
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
|
+
*/
|
|
810
1109
|
setPrimary(primary) {
|
|
811
1110
|
this._primary = primary instanceof Keyv ? primary : new Keyv(primary);
|
|
812
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
|
+
*/
|
|
813
1117
|
setSecondary(secondary) {
|
|
814
1118
|
this._secondary = secondary instanceof Keyv ? secondary : new Keyv(secondary);
|
|
815
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
|
+
*/
|
|
816
1125
|
async get(key) {
|
|
817
1126
|
let result;
|
|
818
1127
|
try {
|
|
@@ -839,6 +1148,11 @@ var Cacheable = class extends Hookified {
|
|
|
839
1148
|
}
|
|
840
1149
|
return result;
|
|
841
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
|
+
*/
|
|
842
1156
|
async getMany(keys) {
|
|
843
1157
|
let result = [];
|
|
844
1158
|
try {
|
|
@@ -876,6 +1190,15 @@ var Cacheable = class extends Hookified {
|
|
|
876
1190
|
}
|
|
877
1191
|
return result;
|
|
878
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
|
+
*/
|
|
879
1202
|
async set(key, value, ttl) {
|
|
880
1203
|
let result = false;
|
|
881
1204
|
const finalTtl = shorthandToMilliseconds(ttl ?? this._ttl);
|
|
@@ -905,6 +1228,11 @@ var Cacheable = class extends Hookified {
|
|
|
905
1228
|
}
|
|
906
1229
|
return result;
|
|
907
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
|
+
*/
|
|
908
1236
|
async setMany(items) {
|
|
909
1237
|
let result = false;
|
|
910
1238
|
try {
|
|
@@ -930,16 +1258,31 @@ var Cacheable = class extends Hookified {
|
|
|
930
1258
|
}
|
|
931
1259
|
return result;
|
|
932
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
|
+
*/
|
|
933
1266
|
async take(key) {
|
|
934
1267
|
const result = await this.get(key);
|
|
935
1268
|
await this.delete(key);
|
|
936
1269
|
return result;
|
|
937
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
|
+
*/
|
|
938
1276
|
async takeMany(keys) {
|
|
939
1277
|
const result = await this.getMany(keys);
|
|
940
1278
|
await this.deleteMany(keys);
|
|
941
1279
|
return result;
|
|
942
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
|
+
*/
|
|
943
1286
|
async has(key) {
|
|
944
1287
|
const promises = [];
|
|
945
1288
|
promises.push(this._primary.has(key));
|
|
@@ -954,6 +1297,11 @@ var Cacheable = class extends Hookified {
|
|
|
954
1297
|
}
|
|
955
1298
|
return false;
|
|
956
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
|
+
*/
|
|
957
1305
|
async hasMany(keys) {
|
|
958
1306
|
const result = await this.hasManyKeyv(this._primary, keys);
|
|
959
1307
|
const missingKeys = [];
|
|
@@ -972,6 +1320,11 @@ var Cacheable = class extends Hookified {
|
|
|
972
1320
|
}
|
|
973
1321
|
return result;
|
|
974
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
|
+
*/
|
|
975
1328
|
async delete(key) {
|
|
976
1329
|
let result = false;
|
|
977
1330
|
const promises = [];
|
|
@@ -996,6 +1349,11 @@ var Cacheable = class extends Hookified {
|
|
|
996
1349
|
}
|
|
997
1350
|
return result;
|
|
998
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
|
+
*/
|
|
999
1357
|
async deleteMany(keys) {
|
|
1000
1358
|
if (this.stats.enabled) {
|
|
1001
1359
|
const statResult = await this._primary.get(keys);
|
|
@@ -1016,6 +1374,10 @@ var Cacheable = class extends Hookified {
|
|
|
1016
1374
|
}
|
|
1017
1375
|
return result;
|
|
1018
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
|
+
*/
|
|
1019
1381
|
async clear() {
|
|
1020
1382
|
const promises = [];
|
|
1021
1383
|
promises.push(this._primary.clear());
|
|
@@ -1028,6 +1390,10 @@ var Cacheable = class extends Hookified {
|
|
|
1028
1390
|
this._stats.incrementClears();
|
|
1029
1391
|
}
|
|
1030
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
|
+
*/
|
|
1031
1397
|
async disconnect() {
|
|
1032
1398
|
const promises = [];
|
|
1033
1399
|
promises.push(this._primary.disconnect());
|
|
@@ -1036,6 +1402,14 @@ var Cacheable = class extends Hookified {
|
|
|
1036
1402
|
}
|
|
1037
1403
|
await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
|
|
1038
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
|
+
*/
|
|
1039
1413
|
wrap(function_, options = {}) {
|
|
1040
1414
|
const wrapOptions = {
|
|
1041
1415
|
ttl: options.ttl,
|
|
@@ -1044,6 +1418,12 @@ var Cacheable = class extends Hookified {
|
|
|
1044
1418
|
};
|
|
1045
1419
|
return wrap(function_, wrapOptions);
|
|
1046
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
|
+
*/
|
|
1047
1427
|
hash(object, algorithm = "sha256") {
|
|
1048
1428
|
return hash(object, algorithm);
|
|
1049
1429
|
}
|