cacheable 1.8.0 → 1.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -25
- package/dist/index.cjs +504 -56
- package/dist/index.d.cts +445 -29
- package/dist/index.d.ts +445 -29
- package/dist/index.js +504 -56
- package/package.json +13 -10
package/dist/index.js
CHANGED
|
@@ -69,11 +69,23 @@ var shorthandToTime = (shorthand, fromDate) => {
|
|
|
69
69
|
return fromDate.getTime() + milliseconds;
|
|
70
70
|
};
|
|
71
71
|
|
|
72
|
+
// src/hash.ts
|
|
73
|
+
import * as crypto from "node:crypto";
|
|
74
|
+
function hash(object, algorithm = "sha256") {
|
|
75
|
+
const objectString = JSON.stringify(object);
|
|
76
|
+
if (!crypto.getHashes().includes(algorithm)) {
|
|
77
|
+
throw new Error(`Unsupported hash algorithm: '${algorithm}'`);
|
|
78
|
+
}
|
|
79
|
+
const hasher = crypto.createHash(algorithm);
|
|
80
|
+
hasher.update(objectString);
|
|
81
|
+
return hasher.digest("hex");
|
|
82
|
+
}
|
|
83
|
+
|
|
72
84
|
// src/wrap.ts
|
|
73
85
|
function wrapSync(function_, options) {
|
|
74
|
-
const { ttl,
|
|
86
|
+
const { ttl, keyPrefix, cache } = options;
|
|
75
87
|
return function(...arguments_) {
|
|
76
|
-
const cacheKey =
|
|
88
|
+
const cacheKey = createWrapKey(function_, arguments_, keyPrefix);
|
|
77
89
|
let value = cache.get(cacheKey);
|
|
78
90
|
if (value === void 0) {
|
|
79
91
|
value = function_(...arguments_);
|
|
@@ -83,9 +95,9 @@ function wrapSync(function_, options) {
|
|
|
83
95
|
};
|
|
84
96
|
}
|
|
85
97
|
function wrap(function_, options) {
|
|
86
|
-
const { ttl,
|
|
98
|
+
const { ttl, keyPrefix, cache } = options;
|
|
87
99
|
return async function(...arguments_) {
|
|
88
|
-
const cacheKey =
|
|
100
|
+
const cacheKey = createWrapKey(function_, arguments_, keyPrefix);
|
|
89
101
|
let value = await cache.get(cacheKey);
|
|
90
102
|
if (value === void 0) {
|
|
91
103
|
value = await function_(...arguments_);
|
|
@@ -94,6 +106,12 @@ function wrap(function_, options) {
|
|
|
94
106
|
return value;
|
|
95
107
|
};
|
|
96
108
|
}
|
|
109
|
+
function createWrapKey(function_, arguments_, keyPrefix) {
|
|
110
|
+
if (!keyPrefix) {
|
|
111
|
+
return `${function_.name}::${hash(arguments_)}`;
|
|
112
|
+
}
|
|
113
|
+
return `${keyPrefix}::${function_.name}::${hash(arguments_)}`;
|
|
114
|
+
}
|
|
97
115
|
|
|
98
116
|
// src/memory-lru.ts
|
|
99
117
|
var ListNode = class {
|
|
@@ -168,20 +186,9 @@ var DoublyLinkedList = class {
|
|
|
168
186
|
}
|
|
169
187
|
};
|
|
170
188
|
|
|
171
|
-
// src/hash.ts
|
|
172
|
-
import * as crypto from "node:crypto";
|
|
173
|
-
function hash(object, algorithm = "sha256") {
|
|
174
|
-
const objectString = JSON.stringify(object);
|
|
175
|
-
if (!crypto.getHashes().includes(algorithm)) {
|
|
176
|
-
throw new Error(`Unsupported hash algorithm: '${algorithm}'`);
|
|
177
|
-
}
|
|
178
|
-
const hasher = crypto.createHash(algorithm);
|
|
179
|
-
hasher.update(objectString);
|
|
180
|
-
return hasher.digest("hex");
|
|
181
|
-
}
|
|
182
|
-
|
|
183
189
|
// src/memory.ts
|
|
184
190
|
var CacheableMemory = class {
|
|
191
|
+
_lru = new DoublyLinkedList();
|
|
185
192
|
_hashCache = /* @__PURE__ */ new Map();
|
|
186
193
|
_hash0 = /* @__PURE__ */ new Map();
|
|
187
194
|
_hash1 = /* @__PURE__ */ new Map();
|
|
@@ -193,7 +200,6 @@ var CacheableMemory = class {
|
|
|
193
200
|
_hash7 = /* @__PURE__ */ new Map();
|
|
194
201
|
_hash8 = /* @__PURE__ */ new Map();
|
|
195
202
|
_hash9 = /* @__PURE__ */ new Map();
|
|
196
|
-
_lru = new DoublyLinkedList();
|
|
197
203
|
_ttl;
|
|
198
204
|
// Turned off by default
|
|
199
205
|
_useClone = true;
|
|
@@ -204,6 +210,10 @@ var CacheableMemory = class {
|
|
|
204
210
|
// Turned off by default
|
|
205
211
|
_interval = 0;
|
|
206
212
|
// Turned off by default
|
|
213
|
+
/**
|
|
214
|
+
* @constructor
|
|
215
|
+
* @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
|
|
216
|
+
*/
|
|
207
217
|
constructor(options) {
|
|
208
218
|
if (options?.ttl) {
|
|
209
219
|
this.setTtl(options.ttl);
|
|
@@ -219,40 +229,89 @@ var CacheableMemory = class {
|
|
|
219
229
|
}
|
|
220
230
|
this.startIntervalCheck();
|
|
221
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Gets the time-to-live
|
|
234
|
+
* @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.
|
|
235
|
+
*/
|
|
222
236
|
get ttl() {
|
|
223
237
|
return this._ttl;
|
|
224
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Sets the time-to-live
|
|
241
|
+
* @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.
|
|
242
|
+
*/
|
|
225
243
|
set ttl(value) {
|
|
226
244
|
this.setTtl(value);
|
|
227
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* Gets whether to use clone
|
|
248
|
+
* @returns {boolean} - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
|
|
249
|
+
*/
|
|
228
250
|
get useClone() {
|
|
229
251
|
return this._useClone;
|
|
230
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Sets whether to use clone
|
|
255
|
+
* @param {boolean} value - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
|
|
256
|
+
*/
|
|
231
257
|
set useClone(value) {
|
|
232
258
|
this._useClone = value;
|
|
233
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* Gets the size of the LRU cache
|
|
262
|
+
* @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
|
|
263
|
+
*/
|
|
234
264
|
get lruSize() {
|
|
235
265
|
return this._lruSize;
|
|
236
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Sets the size of the LRU cache
|
|
269
|
+
* @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
|
|
270
|
+
*/
|
|
237
271
|
set lruSize(value) {
|
|
238
272
|
this._lruSize = value;
|
|
239
273
|
this.lruResize();
|
|
240
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* Gets the check interval
|
|
277
|
+
* @returns {number} - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
|
|
278
|
+
*/
|
|
241
279
|
get checkInterval() {
|
|
242
280
|
return this._checkInterval;
|
|
243
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Sets the check interval
|
|
284
|
+
* @param {number} value - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
|
|
285
|
+
*/
|
|
244
286
|
set checkInterval(value) {
|
|
245
287
|
this._checkInterval = value;
|
|
246
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* Gets the size of the cache
|
|
291
|
+
* @returns {number} - The size of the cache
|
|
292
|
+
*/
|
|
247
293
|
get size() {
|
|
248
294
|
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
295
|
}
|
|
296
|
+
/**
|
|
297
|
+
* Gets the keys
|
|
298
|
+
* @returns {IterableIterator<string>} - The keys
|
|
299
|
+
*/
|
|
250
300
|
get keys() {
|
|
251
301
|
return this.concatStores().keys();
|
|
252
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Gets the items
|
|
305
|
+
* @returns {IterableIterator<CacheableStoreItem>} - The items
|
|
306
|
+
*/
|
|
253
307
|
get items() {
|
|
254
308
|
return this.concatStores().values();
|
|
255
309
|
}
|
|
310
|
+
/**
|
|
311
|
+
* Gets the value of the key
|
|
312
|
+
* @param {string} key - The key to get the value
|
|
313
|
+
* @returns {T | undefined} - The value of the key
|
|
314
|
+
*/
|
|
256
315
|
get(key) {
|
|
257
316
|
const store = this.getStore(key);
|
|
258
317
|
const item = store.get(key);
|
|
@@ -269,6 +328,11 @@ var CacheableMemory = class {
|
|
|
269
328
|
}
|
|
270
329
|
return this.clone(item.value);
|
|
271
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Gets the values of the keys
|
|
333
|
+
* @param {string[]} keys - The keys to get the values
|
|
334
|
+
* @returns {T[]} - The values of the keys
|
|
335
|
+
*/
|
|
272
336
|
getMany(keys) {
|
|
273
337
|
const result = new Array();
|
|
274
338
|
for (const key of keys) {
|
|
@@ -276,6 +340,11 @@ var CacheableMemory = class {
|
|
|
276
340
|
}
|
|
277
341
|
return result;
|
|
278
342
|
}
|
|
343
|
+
/**
|
|
344
|
+
* Gets the raw value of the key
|
|
345
|
+
* @param {string} key - The key to get the value
|
|
346
|
+
* @returns {CacheableStoreItem | undefined} - The raw value of the key
|
|
347
|
+
*/
|
|
279
348
|
getRaw(key) {
|
|
280
349
|
const store = this.getStore(key);
|
|
281
350
|
const item = store.get(key);
|
|
@@ -289,6 +358,11 @@ var CacheableMemory = class {
|
|
|
289
358
|
this.lruMoveToFront(key);
|
|
290
359
|
return item;
|
|
291
360
|
}
|
|
361
|
+
/**
|
|
362
|
+
* Gets the raw values of the keys
|
|
363
|
+
* @param {string[]} keys - The keys to get the values
|
|
364
|
+
* @returns {CacheableStoreItem[]} - The raw values of the keys
|
|
365
|
+
*/
|
|
292
366
|
getManyRaw(keys) {
|
|
293
367
|
const result = new Array();
|
|
294
368
|
for (const key of keys) {
|
|
@@ -296,6 +370,14 @@ var CacheableMemory = class {
|
|
|
296
370
|
}
|
|
297
371
|
return result;
|
|
298
372
|
}
|
|
373
|
+
/**
|
|
374
|
+
* Sets the value of the key
|
|
375
|
+
* @param {string} key - The key to set the value
|
|
376
|
+
* @param {any} value - The value to set
|
|
377
|
+
* @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.
|
|
378
|
+
* 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.
|
|
379
|
+
* @returns {void}
|
|
380
|
+
*/
|
|
299
381
|
set(key, value, ttl) {
|
|
300
382
|
const store = this.getStore(key);
|
|
301
383
|
let expires;
|
|
@@ -319,22 +401,49 @@ var CacheableMemory = class {
|
|
|
319
401
|
}
|
|
320
402
|
}
|
|
321
403
|
}
|
|
322
|
-
|
|
404
|
+
const item = { key, value, expires };
|
|
405
|
+
store.set(
|
|
323
406
|
key,
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
407
|
+
item
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Sets the values of the keys
|
|
412
|
+
* @param {CacheableItem[]} items - The items to set
|
|
413
|
+
* @returns {void}
|
|
414
|
+
*/
|
|
329
415
|
setMany(items) {
|
|
330
416
|
for (const item of items) {
|
|
331
417
|
this.set(item.key, item.value, item.ttl);
|
|
332
418
|
}
|
|
333
419
|
}
|
|
420
|
+
/**
|
|
421
|
+
* Checks if the key exists
|
|
422
|
+
* @param {string} key - The key to check
|
|
423
|
+
* @returns {boolean} - If true, the key exists. If false, the key does not exist.
|
|
424
|
+
*/
|
|
334
425
|
has(key) {
|
|
335
426
|
const item = this.get(key);
|
|
336
427
|
return Boolean(item);
|
|
337
428
|
}
|
|
429
|
+
/**
|
|
430
|
+
* @function hasMany
|
|
431
|
+
* @param {string[]} keys - The keys to check
|
|
432
|
+
* @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
|
|
433
|
+
*/
|
|
434
|
+
hasMany(keys) {
|
|
435
|
+
const result = new Array();
|
|
436
|
+
for (const key of keys) {
|
|
437
|
+
const item = this.get(key);
|
|
438
|
+
result.push(Boolean(item));
|
|
439
|
+
}
|
|
440
|
+
return result;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Take will get the key and delete the entry from cache
|
|
444
|
+
* @param {string} key - The key to take
|
|
445
|
+
* @returns {T | undefined} - The value of the key
|
|
446
|
+
*/
|
|
338
447
|
take(key) {
|
|
339
448
|
const item = this.get(key);
|
|
340
449
|
if (!item) {
|
|
@@ -343,6 +452,11 @@ var CacheableMemory = class {
|
|
|
343
452
|
this.delete(key);
|
|
344
453
|
return item;
|
|
345
454
|
}
|
|
455
|
+
/**
|
|
456
|
+
* TakeMany will get the keys and delete the entries from cache
|
|
457
|
+
* @param {string[]} keys - The keys to take
|
|
458
|
+
* @returns {T[]} - The values of the keys
|
|
459
|
+
*/
|
|
346
460
|
takeMany(keys) {
|
|
347
461
|
const result = new Array();
|
|
348
462
|
for (const key of keys) {
|
|
@@ -350,15 +464,29 @@ var CacheableMemory = class {
|
|
|
350
464
|
}
|
|
351
465
|
return result;
|
|
352
466
|
}
|
|
467
|
+
/**
|
|
468
|
+
* Delete the key
|
|
469
|
+
* @param {string} key - The key to delete
|
|
470
|
+
* @returns {void}
|
|
471
|
+
*/
|
|
353
472
|
delete(key) {
|
|
354
473
|
const store = this.getStore(key);
|
|
355
474
|
store.delete(key);
|
|
356
475
|
}
|
|
476
|
+
/**
|
|
477
|
+
* Delete the keys
|
|
478
|
+
* @param {string[]} keys - The keys to delete
|
|
479
|
+
* @returns {void}
|
|
480
|
+
*/
|
|
357
481
|
deleteMany(keys) {
|
|
358
482
|
for (const key of keys) {
|
|
359
483
|
this.delete(key);
|
|
360
484
|
}
|
|
361
485
|
}
|
|
486
|
+
/**
|
|
487
|
+
* Clear the cache
|
|
488
|
+
* @returns {void}
|
|
489
|
+
*/
|
|
362
490
|
clear() {
|
|
363
491
|
this._hash0.clear();
|
|
364
492
|
this._hash1.clear();
|
|
@@ -371,24 +499,24 @@ var CacheableMemory = class {
|
|
|
371
499
|
this._hash8.clear();
|
|
372
500
|
this._hash9.clear();
|
|
373
501
|
this._hashCache.clear();
|
|
502
|
+
this._lru = new DoublyLinkedList();
|
|
374
503
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
let hash2 = 0;
|
|
381
|
-
const primeMultiplier = 31;
|
|
382
|
-
for (let i = 0; i < key.length; i++) {
|
|
383
|
-
hash2 = hash2 * primeMultiplier + key.charCodeAt(i);
|
|
384
|
-
}
|
|
385
|
-
const result = Math.abs(hash2) % 10;
|
|
386
|
-
this._hashCache.set(key, result);
|
|
387
|
-
return result;
|
|
388
|
-
}
|
|
504
|
+
/**
|
|
505
|
+
* Get the store based on the key (internal use)
|
|
506
|
+
* @param {string} key - The key to get the store
|
|
507
|
+
* @returns {CacheableHashStore} - The store
|
|
508
|
+
*/
|
|
389
509
|
getStore(key) {
|
|
390
|
-
const
|
|
391
|
-
|
|
510
|
+
const hash2 = this.hashKey(key);
|
|
511
|
+
return this.getStoreFromHash(hash2);
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Get the store based on the hash (internal use)
|
|
515
|
+
* @param {number} hash
|
|
516
|
+
* @returns {Map<string, CacheableStoreItem>}
|
|
517
|
+
*/
|
|
518
|
+
getStoreFromHash(hash2) {
|
|
519
|
+
switch (hash2) {
|
|
392
520
|
case 1: {
|
|
393
521
|
return this._hash1;
|
|
394
522
|
}
|
|
@@ -421,24 +549,62 @@ var CacheableMemory = class {
|
|
|
421
549
|
}
|
|
422
550
|
}
|
|
423
551
|
}
|
|
552
|
+
/**
|
|
553
|
+
* Hash the key (internal use)
|
|
554
|
+
* @param key
|
|
555
|
+
* @returns {number} from 0 to 9
|
|
556
|
+
*/
|
|
557
|
+
hashKey(key) {
|
|
558
|
+
const cacheHashNumber = this._hashCache.get(key);
|
|
559
|
+
if (cacheHashNumber) {
|
|
560
|
+
return cacheHashNumber;
|
|
561
|
+
}
|
|
562
|
+
let hash2 = 0;
|
|
563
|
+
const primeMultiplier = 31;
|
|
564
|
+
for (let i = 0; i < key.length; i++) {
|
|
565
|
+
hash2 = hash2 * primeMultiplier + key.charCodeAt(i);
|
|
566
|
+
}
|
|
567
|
+
const result = Math.abs(hash2) % 10;
|
|
568
|
+
this._hashCache.set(key, result);
|
|
569
|
+
return result;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Clone the value. This is for internal use
|
|
573
|
+
* @param {any} value - The value to clone
|
|
574
|
+
* @returns {any} - The cloned value
|
|
575
|
+
*/
|
|
424
576
|
clone(value) {
|
|
425
577
|
if (this.isPrimitive(value)) {
|
|
426
578
|
return value;
|
|
427
579
|
}
|
|
428
580
|
return structuredClone(value);
|
|
429
581
|
}
|
|
582
|
+
/**
|
|
583
|
+
* Add to the front of the LRU cache. This is for internal use
|
|
584
|
+
* @param {string} key - The key to add to the front
|
|
585
|
+
* @returns {void}
|
|
586
|
+
*/
|
|
430
587
|
lruAddToFront(key) {
|
|
431
588
|
if (this._lruSize === 0) {
|
|
432
589
|
return;
|
|
433
590
|
}
|
|
434
591
|
this._lru.addToFront(key);
|
|
435
592
|
}
|
|
593
|
+
/**
|
|
594
|
+
* Move to the front of the LRU cache. This is for internal use
|
|
595
|
+
* @param {string} key - The key to move to the front
|
|
596
|
+
* @returns {void}
|
|
597
|
+
*/
|
|
436
598
|
lruMoveToFront(key) {
|
|
437
599
|
if (this._lruSize === 0) {
|
|
438
600
|
return;
|
|
439
601
|
}
|
|
440
602
|
this._lru.moveToFront(key);
|
|
441
603
|
}
|
|
604
|
+
/**
|
|
605
|
+
* Resize the LRU cache. This is for internal use
|
|
606
|
+
* @returns {void}
|
|
607
|
+
*/
|
|
442
608
|
lruResize() {
|
|
443
609
|
if (this._lruSize === 0) {
|
|
444
610
|
return;
|
|
@@ -451,6 +617,10 @@ var CacheableMemory = class {
|
|
|
451
617
|
}
|
|
452
618
|
}
|
|
453
619
|
}
|
|
620
|
+
/**
|
|
621
|
+
* Check for expiration. This is for internal use
|
|
622
|
+
* @returns {void}
|
|
623
|
+
*/
|
|
454
624
|
checkExpiration() {
|
|
455
625
|
const stores = this.concatStores();
|
|
456
626
|
for (const item of stores.values()) {
|
|
@@ -459,6 +629,10 @@ var CacheableMemory = class {
|
|
|
459
629
|
}
|
|
460
630
|
}
|
|
461
631
|
}
|
|
632
|
+
/**
|
|
633
|
+
* Start the interval check. This is for internal use
|
|
634
|
+
* @returns {void}
|
|
635
|
+
*/
|
|
462
636
|
startIntervalCheck() {
|
|
463
637
|
if (this._checkInterval > 0) {
|
|
464
638
|
this._interval = setInterval(() => {
|
|
@@ -466,6 +640,10 @@ var CacheableMemory = class {
|
|
|
466
640
|
}, this._checkInterval);
|
|
467
641
|
}
|
|
468
642
|
}
|
|
643
|
+
/**
|
|
644
|
+
* Stop the interval check. This is for internal use
|
|
645
|
+
* @returns {void}
|
|
646
|
+
*/
|
|
469
647
|
stopIntervalCheck() {
|
|
470
648
|
if (this._interval) {
|
|
471
649
|
clearInterval(this._interval);
|
|
@@ -473,13 +651,25 @@ var CacheableMemory = class {
|
|
|
473
651
|
this._interval = 0;
|
|
474
652
|
this._checkInterval = 0;
|
|
475
653
|
}
|
|
654
|
+
/**
|
|
655
|
+
* Hash the object. This is for internal use
|
|
656
|
+
* @param {any} object - The object to hash
|
|
657
|
+
* @param {string} [algorithm='sha256'] - The algorithm to hash
|
|
658
|
+
* @returns {string} - The hashed string
|
|
659
|
+
*/
|
|
476
660
|
hash(object, algorithm = "sha256") {
|
|
477
661
|
return hash(object, algorithm);
|
|
478
662
|
}
|
|
479
|
-
|
|
663
|
+
/**
|
|
664
|
+
* Wrap the function for caching
|
|
665
|
+
* @param {Function} function_ - The function to wrap
|
|
666
|
+
* @param {Object} [options] - The options to wrap
|
|
667
|
+
* @returns {Function} - The wrapped function
|
|
668
|
+
*/
|
|
669
|
+
wrap(function_, options) {
|
|
480
670
|
const wrapOptions = {
|
|
481
671
|
ttl: options.ttl,
|
|
482
|
-
|
|
672
|
+
keyPrefix: options.keyPrefix,
|
|
483
673
|
cache: this
|
|
484
674
|
};
|
|
485
675
|
return wrapSync(function_, wrapOptions);
|
|
@@ -495,8 +685,7 @@ var CacheableMemory = class {
|
|
|
495
685
|
return result;
|
|
496
686
|
}
|
|
497
687
|
concatStores() {
|
|
498
|
-
|
|
499
|
-
return result;
|
|
688
|
+
return new Map([...this._hash0, ...this._hash1, ...this._hash2, ...this._hash3, ...this._hash4, ...this._hash5, ...this._hash6, ...this._hash7, ...this._hash8, ...this._hash9]);
|
|
500
689
|
}
|
|
501
690
|
setTtl(ttl) {
|
|
502
691
|
if (typeof ttl === "string" || ttl === void 0) {
|
|
@@ -517,48 +706,71 @@ var KeyvCacheableMemory = class {
|
|
|
517
706
|
lruSize: 0,
|
|
518
707
|
checkInterval: 0
|
|
519
708
|
};
|
|
520
|
-
|
|
521
|
-
|
|
709
|
+
_defaultCache = new CacheableMemory();
|
|
710
|
+
_nCache = /* @__PURE__ */ new Map();
|
|
711
|
+
_namespace;
|
|
522
712
|
constructor(options) {
|
|
523
713
|
if (options) {
|
|
524
714
|
this.opts = options;
|
|
525
|
-
this.
|
|
715
|
+
this._defaultCache = new CacheableMemory(options);
|
|
716
|
+
if (options.namespace) {
|
|
717
|
+
this._namespace = options.namespace;
|
|
718
|
+
this._nCache.set(this._namespace, new CacheableMemory(options));
|
|
719
|
+
}
|
|
526
720
|
}
|
|
527
721
|
}
|
|
722
|
+
get namespace() {
|
|
723
|
+
return this._namespace;
|
|
724
|
+
}
|
|
725
|
+
set namespace(value) {
|
|
726
|
+
this._namespace = value;
|
|
727
|
+
}
|
|
728
|
+
get store() {
|
|
729
|
+
return this.getStore(this._namespace);
|
|
730
|
+
}
|
|
528
731
|
async get(key) {
|
|
529
|
-
const result = this.
|
|
732
|
+
const result = this.getStore(this._namespace).get(key);
|
|
530
733
|
if (result) {
|
|
531
734
|
return result;
|
|
532
735
|
}
|
|
533
736
|
return void 0;
|
|
534
737
|
}
|
|
535
738
|
async getMany(keys) {
|
|
536
|
-
const result = this.
|
|
739
|
+
const result = this.getStore(this._namespace).getMany(keys);
|
|
537
740
|
return result;
|
|
538
741
|
}
|
|
539
742
|
async set(key, value, ttl) {
|
|
540
|
-
this.
|
|
743
|
+
this.getStore(this._namespace).set(key, value, ttl);
|
|
541
744
|
}
|
|
542
745
|
async setMany(values) {
|
|
543
|
-
this.
|
|
746
|
+
this.getStore(this._namespace).setMany(values);
|
|
544
747
|
}
|
|
545
748
|
async delete(key) {
|
|
546
|
-
this.
|
|
749
|
+
this.getStore(this._namespace).delete(key);
|
|
547
750
|
return true;
|
|
548
751
|
}
|
|
549
752
|
async deleteMany(key) {
|
|
550
|
-
this.
|
|
753
|
+
this.getStore(this._namespace).deleteMany(key);
|
|
551
754
|
return true;
|
|
552
755
|
}
|
|
553
756
|
async clear() {
|
|
554
|
-
this.
|
|
757
|
+
this.getStore(this._namespace).clear();
|
|
555
758
|
}
|
|
556
759
|
async has(key) {
|
|
557
|
-
return this.
|
|
760
|
+
return this.getStore(this._namespace).has(key);
|
|
558
761
|
}
|
|
559
762
|
on(event, listener) {
|
|
560
763
|
return this;
|
|
561
764
|
}
|
|
765
|
+
getStore(namespace) {
|
|
766
|
+
if (!namespace) {
|
|
767
|
+
return this._defaultCache;
|
|
768
|
+
}
|
|
769
|
+
if (!this._nCache.has(namespace)) {
|
|
770
|
+
this._nCache.set(namespace, new CacheableMemory(this.opts));
|
|
771
|
+
}
|
|
772
|
+
return this._nCache.get(namespace);
|
|
773
|
+
}
|
|
562
774
|
};
|
|
563
775
|
|
|
564
776
|
// src/stats.ts
|
|
@@ -578,36 +790,78 @@ var CacheableStats = class {
|
|
|
578
790
|
this._enabled = options.enabled;
|
|
579
791
|
}
|
|
580
792
|
}
|
|
793
|
+
/**
|
|
794
|
+
* @returns {boolean} - Whether the stats are enabled
|
|
795
|
+
*/
|
|
581
796
|
get enabled() {
|
|
582
797
|
return this._enabled;
|
|
583
798
|
}
|
|
799
|
+
/**
|
|
800
|
+
* @param {boolean} enabled - Whether to enable the stats
|
|
801
|
+
*/
|
|
584
802
|
set enabled(enabled) {
|
|
585
803
|
this._enabled = enabled;
|
|
586
804
|
}
|
|
805
|
+
/**
|
|
806
|
+
* @returns {number} - The number of hits
|
|
807
|
+
* @readonly
|
|
808
|
+
*/
|
|
587
809
|
get hits() {
|
|
588
810
|
return this._hits;
|
|
589
811
|
}
|
|
812
|
+
/**
|
|
813
|
+
* @returns {number} - The number of misses
|
|
814
|
+
* @readonly
|
|
815
|
+
*/
|
|
590
816
|
get misses() {
|
|
591
817
|
return this._misses;
|
|
592
818
|
}
|
|
819
|
+
/**
|
|
820
|
+
* @returns {number} - The number of gets
|
|
821
|
+
* @readonly
|
|
822
|
+
*/
|
|
593
823
|
get gets() {
|
|
594
824
|
return this._gets;
|
|
595
825
|
}
|
|
826
|
+
/**
|
|
827
|
+
* @returns {number} - The number of sets
|
|
828
|
+
* @readonly
|
|
829
|
+
*/
|
|
596
830
|
get sets() {
|
|
597
831
|
return this._sets;
|
|
598
832
|
}
|
|
833
|
+
/**
|
|
834
|
+
* @returns {number} - The number of deletes
|
|
835
|
+
* @readonly
|
|
836
|
+
*/
|
|
599
837
|
get deletes() {
|
|
600
838
|
return this._deletes;
|
|
601
839
|
}
|
|
840
|
+
/**
|
|
841
|
+
* @returns {number} - The number of clears
|
|
842
|
+
* @readonly
|
|
843
|
+
*/
|
|
602
844
|
get clears() {
|
|
603
845
|
return this._clears;
|
|
604
846
|
}
|
|
847
|
+
/**
|
|
848
|
+
* @returns {number} - The vsize (value size) of the cache instance
|
|
849
|
+
* @readonly
|
|
850
|
+
*/
|
|
605
851
|
get vsize() {
|
|
606
852
|
return this._vsize;
|
|
607
853
|
}
|
|
854
|
+
/**
|
|
855
|
+
* @returns {number} - The ksize (key size) of the cache instance
|
|
856
|
+
* @readonly
|
|
857
|
+
*/
|
|
608
858
|
get ksize() {
|
|
609
859
|
return this._ksize;
|
|
610
860
|
}
|
|
861
|
+
/**
|
|
862
|
+
* @returns {number} - The count of the cache instance
|
|
863
|
+
* @readonly
|
|
864
|
+
*/
|
|
611
865
|
get count() {
|
|
612
866
|
return this._count;
|
|
613
867
|
}
|
|
@@ -762,6 +1016,11 @@ var Cacheable = class extends Hookified {
|
|
|
762
1016
|
_nonBlocking = false;
|
|
763
1017
|
_ttl;
|
|
764
1018
|
_stats = new CacheableStats({ enabled: false });
|
|
1019
|
+
_namespace;
|
|
1020
|
+
/**
|
|
1021
|
+
* Creates a new cacheable instance
|
|
1022
|
+
* @param {CacheableOptions} [options] The options for the cacheable instance
|
|
1023
|
+
*/
|
|
765
1024
|
constructor(options) {
|
|
766
1025
|
super();
|
|
767
1026
|
if (options?.primary) {
|
|
@@ -779,40 +1038,159 @@ var Cacheable = class extends Hookified {
|
|
|
779
1038
|
if (options?.ttl) {
|
|
780
1039
|
this.setTtl(options.ttl);
|
|
781
1040
|
}
|
|
1041
|
+
if (options?.namespace) {
|
|
1042
|
+
this._namespace = options.namespace;
|
|
1043
|
+
this._primary.namespace = this.getNameSpace();
|
|
1044
|
+
if (this._secondary) {
|
|
1045
|
+
this._secondary.namespace = this.getNameSpace();
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
782
1048
|
}
|
|
1049
|
+
/**
|
|
1050
|
+
* The namespace for the cacheable instance
|
|
1051
|
+
* @returns {string | (() => string) | undefined} The namespace for the cacheable instance
|
|
1052
|
+
*/
|
|
1053
|
+
get namespace() {
|
|
1054
|
+
return this._namespace;
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* Sets the namespace for the cacheable instance
|
|
1058
|
+
* @param {string | (() => string) | undefined} namespace The namespace for the cacheable instance
|
|
1059
|
+
* @returns {void}
|
|
1060
|
+
*/
|
|
1061
|
+
set namespace(namespace) {
|
|
1062
|
+
this._namespace = namespace;
|
|
1063
|
+
this._primary.namespace = this.getNameSpace();
|
|
1064
|
+
if (this._secondary) {
|
|
1065
|
+
this._secondary.namespace = this.getNameSpace();
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* The statistics for the cacheable instance
|
|
1070
|
+
* @returns {CacheableStats} The statistics for the cacheable instance
|
|
1071
|
+
*/
|
|
783
1072
|
get stats() {
|
|
784
1073
|
return this._stats;
|
|
785
1074
|
}
|
|
1075
|
+
/**
|
|
1076
|
+
* The primary store for the cacheable instance
|
|
1077
|
+
* @returns {Keyv} The primary store for the cacheable instance
|
|
1078
|
+
*/
|
|
786
1079
|
get primary() {
|
|
787
1080
|
return this._primary;
|
|
788
1081
|
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Sets the primary store for the cacheable instance
|
|
1084
|
+
* @param {Keyv} primary The primary store for the cacheable instance
|
|
1085
|
+
*/
|
|
789
1086
|
set primary(primary) {
|
|
790
1087
|
this._primary = primary;
|
|
791
1088
|
}
|
|
1089
|
+
/**
|
|
1090
|
+
* The secondary store for the cacheable instance
|
|
1091
|
+
* @returns {Keyv | undefined} The secondary store for the cacheable instance
|
|
1092
|
+
*/
|
|
792
1093
|
get secondary() {
|
|
793
1094
|
return this._secondary;
|
|
794
1095
|
}
|
|
1096
|
+
/**
|
|
1097
|
+
* Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
|
|
1098
|
+
* @param {Keyv | undefined} secondary The secondary store for the cacheable instance
|
|
1099
|
+
* @returns {void}
|
|
1100
|
+
*/
|
|
795
1101
|
set secondary(secondary) {
|
|
796
1102
|
this._secondary = secondary;
|
|
797
1103
|
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Gets whether the secondary store is non-blocking mode. It is set to false by default.
|
|
1106
|
+
* If it is set to true then the secondary store will not block the primary store.
|
|
1107
|
+
*
|
|
1108
|
+
* [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
|
|
1109
|
+
*
|
|
1110
|
+
* @returns {boolean} Whether the cacheable instance is non-blocking
|
|
1111
|
+
*/
|
|
798
1112
|
get nonBlocking() {
|
|
799
1113
|
return this._nonBlocking;
|
|
800
1114
|
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Sets whether the secondary store is non-blocking mode. It is set to false by default.
|
|
1117
|
+
* If it is set to true then the secondary store will not block the primary store.
|
|
1118
|
+
*
|
|
1119
|
+
* [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
|
|
1120
|
+
*
|
|
1121
|
+
* @param {boolean} nonBlocking Whether the cacheable instance is non-blocking
|
|
1122
|
+
* @returns {void}
|
|
1123
|
+
*/
|
|
801
1124
|
set nonBlocking(nonBlocking) {
|
|
802
1125
|
this._nonBlocking = nonBlocking;
|
|
803
1126
|
}
|
|
1127
|
+
/**
|
|
1128
|
+
* The time-to-live for the cacheable instance and will be used as the default value.
|
|
1129
|
+
* can be a number in milliseconds or a human-readable format such as `1s` for 1 second or `1h` for 1 hour
|
|
1130
|
+
* or undefined if there is no time-to-live.
|
|
1131
|
+
*
|
|
1132
|
+
* [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
|
|
1133
|
+
*
|
|
1134
|
+
* @returns {number | string | undefined} The time-to-live for the cacheable instance in milliseconds, human-readable format or undefined
|
|
1135
|
+
* @example
|
|
1136
|
+
* ```typescript
|
|
1137
|
+
* const cacheable = new Cacheable({ ttl: '1h' });
|
|
1138
|
+
* console.log(cacheable.ttl); // 1h
|
|
1139
|
+
* ```
|
|
1140
|
+
*/
|
|
804
1141
|
get ttl() {
|
|
805
1142
|
return this._ttl;
|
|
806
1143
|
}
|
|
1144
|
+
/**
|
|
1145
|
+
* Sets the time-to-live for the cacheable instance and will be used as the default value.
|
|
1146
|
+
* If you set a number it is miliseconds, if you set a string it is a human-readable
|
|
1147
|
+
* format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that
|
|
1148
|
+
* there is no time-to-live.
|
|
1149
|
+
*
|
|
1150
|
+
* [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
|
|
1151
|
+
*
|
|
1152
|
+
* @param {number | string | undefined} ttl The time-to-live for the cacheable instance
|
|
1153
|
+
* @example
|
|
1154
|
+
* ```typescript
|
|
1155
|
+
* const cacheable = new Cacheable();
|
|
1156
|
+
* cacheable.ttl = '1h'; // Set the time-to-live to 1 hour
|
|
1157
|
+
* ```
|
|
1158
|
+
* or setting the time-to-live in milliseconds
|
|
1159
|
+
* ```typescript
|
|
1160
|
+
* const cacheable = new Cacheable();
|
|
1161
|
+
* cacheable.ttl = 3600000; // Set the time-to-live to 1 hour
|
|
1162
|
+
* ```
|
|
1163
|
+
*/
|
|
807
1164
|
set ttl(ttl) {
|
|
808
1165
|
this.setTtl(ttl);
|
|
809
1166
|
}
|
|
1167
|
+
/**
|
|
1168
|
+
* Sets the primary store for the cacheable instance
|
|
1169
|
+
* @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
|
|
1170
|
+
* @returns {void}
|
|
1171
|
+
*/
|
|
810
1172
|
setPrimary(primary) {
|
|
811
1173
|
this._primary = primary instanceof Keyv ? primary : new Keyv(primary);
|
|
812
1174
|
}
|
|
1175
|
+
/**
|
|
1176
|
+
* Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
|
|
1177
|
+
* @param {Keyv | KeyvStoreAdapter} secondary The secondary store for the cacheable instance
|
|
1178
|
+
* @returns {void}
|
|
1179
|
+
*/
|
|
813
1180
|
setSecondary(secondary) {
|
|
814
1181
|
this._secondary = secondary instanceof Keyv ? secondary : new Keyv(secondary);
|
|
815
1182
|
}
|
|
1183
|
+
getNameSpace() {
|
|
1184
|
+
if (typeof this._namespace === "function") {
|
|
1185
|
+
return this._namespace();
|
|
1186
|
+
}
|
|
1187
|
+
return this._namespace;
|
|
1188
|
+
}
|
|
1189
|
+
/**
|
|
1190
|
+
* Gets the value of the key. If the key does not exist in the primary store then it will check the secondary store.
|
|
1191
|
+
* @param {string} key The key to get the value of
|
|
1192
|
+
* @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
|
|
1193
|
+
*/
|
|
816
1194
|
async get(key) {
|
|
817
1195
|
let result;
|
|
818
1196
|
try {
|
|
@@ -839,6 +1217,11 @@ var Cacheable = class extends Hookified {
|
|
|
839
1217
|
}
|
|
840
1218
|
return result;
|
|
841
1219
|
}
|
|
1220
|
+
/**
|
|
1221
|
+
* Gets the values of the keys. If the key does not exist in the primary store then it will check the secondary store.
|
|
1222
|
+
* @param {string[]} keys The keys to get the values of
|
|
1223
|
+
* @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
|
|
1224
|
+
*/
|
|
842
1225
|
async getMany(keys) {
|
|
843
1226
|
let result = [];
|
|
844
1227
|
try {
|
|
@@ -876,6 +1259,14 @@ var Cacheable = class extends Hookified {
|
|
|
876
1259
|
}
|
|
877
1260
|
return result;
|
|
878
1261
|
}
|
|
1262
|
+
/**
|
|
1263
|
+
* Sets the value of the key. If the secondary store is set then it will also set the value in the secondary store.
|
|
1264
|
+
* @param {string} key the key to set the value of
|
|
1265
|
+
* @param {T} value The value to set
|
|
1266
|
+
* @param {number | string} [ttl] set a number it is miliseconds, set a string it is a human-readable
|
|
1267
|
+
* format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live.
|
|
1268
|
+
* @returns {boolean} Whether the value was set
|
|
1269
|
+
*/
|
|
879
1270
|
async set(key, value, ttl) {
|
|
880
1271
|
let result = false;
|
|
881
1272
|
const finalTtl = shorthandToMilliseconds(ttl ?? this._ttl);
|
|
@@ -905,6 +1296,11 @@ var Cacheable = class extends Hookified {
|
|
|
905
1296
|
}
|
|
906
1297
|
return result;
|
|
907
1298
|
}
|
|
1299
|
+
/**
|
|
1300
|
+
* Sets the values of the keys. If the secondary store is set then it will also set the values in the secondary store.
|
|
1301
|
+
* @param {CacheableItem[]} items The items to set
|
|
1302
|
+
* @returns {boolean} Whether the values were set
|
|
1303
|
+
*/
|
|
908
1304
|
async setMany(items) {
|
|
909
1305
|
let result = false;
|
|
910
1306
|
try {
|
|
@@ -930,16 +1326,31 @@ var Cacheable = class extends Hookified {
|
|
|
930
1326
|
}
|
|
931
1327
|
return result;
|
|
932
1328
|
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Takes the value of the key and deletes the key. If the key does not exist then it will return undefined.
|
|
1331
|
+
* @param {string} key The key to take the value of
|
|
1332
|
+
* @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
|
|
1333
|
+
*/
|
|
933
1334
|
async take(key) {
|
|
934
1335
|
const result = await this.get(key);
|
|
935
1336
|
await this.delete(key);
|
|
936
1337
|
return result;
|
|
937
1338
|
}
|
|
1339
|
+
/**
|
|
1340
|
+
* Takes the values of the keys and deletes the keys. If the key does not exist then it will return undefined.
|
|
1341
|
+
* @param {string[]} keys The keys to take the values of
|
|
1342
|
+
* @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
|
|
1343
|
+
*/
|
|
938
1344
|
async takeMany(keys) {
|
|
939
1345
|
const result = await this.getMany(keys);
|
|
940
1346
|
await this.deleteMany(keys);
|
|
941
1347
|
return result;
|
|
942
1348
|
}
|
|
1349
|
+
/**
|
|
1350
|
+
* Checks if the key exists in the primary store. If it does not exist then it will check the secondary store.
|
|
1351
|
+
* @param {string} key The key to check
|
|
1352
|
+
* @returns {Promise<boolean>} Whether the key exists
|
|
1353
|
+
*/
|
|
943
1354
|
async has(key) {
|
|
944
1355
|
const promises = [];
|
|
945
1356
|
promises.push(this._primary.has(key));
|
|
@@ -954,6 +1365,11 @@ var Cacheable = class extends Hookified {
|
|
|
954
1365
|
}
|
|
955
1366
|
return false;
|
|
956
1367
|
}
|
|
1368
|
+
/**
|
|
1369
|
+
* Checks if the keys exist in the primary store. If it does not exist then it will check the secondary store.
|
|
1370
|
+
* @param {string[]} keys The keys to check
|
|
1371
|
+
* @returns {Promise<boolean[]>} Whether the keys exist
|
|
1372
|
+
*/
|
|
957
1373
|
async hasMany(keys) {
|
|
958
1374
|
const result = await this.hasManyKeyv(this._primary, keys);
|
|
959
1375
|
const missingKeys = [];
|
|
@@ -972,6 +1388,11 @@ var Cacheable = class extends Hookified {
|
|
|
972
1388
|
}
|
|
973
1389
|
return result;
|
|
974
1390
|
}
|
|
1391
|
+
/**
|
|
1392
|
+
* Deletes the key from the primary store. If the secondary store is set then it will also delete the key from the secondary store.
|
|
1393
|
+
* @param {string} key The key to delete
|
|
1394
|
+
* @returns {Promise<boolean>} Whether the key was deleted
|
|
1395
|
+
*/
|
|
975
1396
|
async delete(key) {
|
|
976
1397
|
let result = false;
|
|
977
1398
|
const promises = [];
|
|
@@ -996,6 +1417,11 @@ var Cacheable = class extends Hookified {
|
|
|
996
1417
|
}
|
|
997
1418
|
return result;
|
|
998
1419
|
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Deletes the keys from the primary store. If the secondary store is set then it will also delete the keys from the secondary store.
|
|
1422
|
+
* @param {string[]} keys The keys to delete
|
|
1423
|
+
* @returns {Promise<boolean>} Whether the keys were deleted
|
|
1424
|
+
*/
|
|
999
1425
|
async deleteMany(keys) {
|
|
1000
1426
|
if (this.stats.enabled) {
|
|
1001
1427
|
const statResult = await this._primary.get(keys);
|
|
@@ -1016,6 +1442,10 @@ var Cacheable = class extends Hookified {
|
|
|
1016
1442
|
}
|
|
1017
1443
|
return result;
|
|
1018
1444
|
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Clears the primary store. If the secondary store is set then it will also clear the secondary store.
|
|
1447
|
+
* @returns {Promise<void>}
|
|
1448
|
+
*/
|
|
1019
1449
|
async clear() {
|
|
1020
1450
|
const promises = [];
|
|
1021
1451
|
promises.push(this._primary.clear());
|
|
@@ -1028,6 +1458,10 @@ var Cacheable = class extends Hookified {
|
|
|
1028
1458
|
this._stats.incrementClears();
|
|
1029
1459
|
}
|
|
1030
1460
|
}
|
|
1461
|
+
/**
|
|
1462
|
+
* Disconnects the primary store. If the secondary store is set then it will also disconnect the secondary store.
|
|
1463
|
+
* @returns {Promise<void>}
|
|
1464
|
+
*/
|
|
1031
1465
|
async disconnect() {
|
|
1032
1466
|
const promises = [];
|
|
1033
1467
|
promises.push(this._primary.disconnect());
|
|
@@ -1036,14 +1470,28 @@ var Cacheable = class extends Hookified {
|
|
|
1036
1470
|
}
|
|
1037
1471
|
await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
|
|
1038
1472
|
}
|
|
1039
|
-
|
|
1473
|
+
/**
|
|
1474
|
+
* Wraps a function with caching
|
|
1475
|
+
*
|
|
1476
|
+
* [Learn more about wrapping functions](https://cacheable.org/docs/cacheable/#wrap--memoization-for-sync-and-async-functions).
|
|
1477
|
+
* @param {Function} function_ The function to wrap
|
|
1478
|
+
* @param {WrapOptions} [options] The options for the wrap function
|
|
1479
|
+
* @returns {Function} The wrapped function
|
|
1480
|
+
*/
|
|
1481
|
+
wrap(function_, options) {
|
|
1040
1482
|
const wrapOptions = {
|
|
1041
1483
|
ttl: options.ttl,
|
|
1042
|
-
|
|
1484
|
+
keyPrefix: options.keyPrefix,
|
|
1043
1485
|
cache: this
|
|
1044
1486
|
};
|
|
1045
1487
|
return wrap(function_, wrapOptions);
|
|
1046
1488
|
}
|
|
1489
|
+
/**
|
|
1490
|
+
* Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
|
|
1491
|
+
* @param {any} object the object to hash
|
|
1492
|
+
* @param {string} algorithm the hash algorithm to use. The default is 'sha256'
|
|
1493
|
+
* @returns {string} the hash of the object
|
|
1494
|
+
*/
|
|
1047
1495
|
hash(object, algorithm = "sha256") {
|
|
1048
1496
|
return hash(object, algorithm);
|
|
1049
1497
|
}
|