cacheable 0.3.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,814 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Cacheable: () => Cacheable,
24
+ CacheableEvents: () => CacheableEvents,
25
+ CacheableHooks: () => CacheableHooks,
26
+ CacheableMemory: () => CacheableMemory,
27
+ CacheableStats: () => CacheableStats
28
+ });
29
+ module.exports = __toCommonJS(src_exports);
30
+ var import_keyv = require("keyv");
31
+ var import_hookified = require("hookified");
32
+
33
+ // src/stats.ts
34
+ var CacheableStats = class {
35
+ _hits = 0;
36
+ _misses = 0;
37
+ _gets = 0;
38
+ _sets = 0;
39
+ _deletes = 0;
40
+ _clears = 0;
41
+ _vsize = 0;
42
+ _ksize = 0;
43
+ _count = 0;
44
+ _enabled = false;
45
+ constructor(options) {
46
+ if (options?.enabled) {
47
+ this._enabled = options.enabled;
48
+ }
49
+ }
50
+ get enabled() {
51
+ return this._enabled;
52
+ }
53
+ set enabled(enabled) {
54
+ this._enabled = enabled;
55
+ }
56
+ get hits() {
57
+ return this._hits;
58
+ }
59
+ get misses() {
60
+ return this._misses;
61
+ }
62
+ get gets() {
63
+ return this._gets;
64
+ }
65
+ get sets() {
66
+ return this._sets;
67
+ }
68
+ get deletes() {
69
+ return this._deletes;
70
+ }
71
+ get clears() {
72
+ return this._clears;
73
+ }
74
+ get vsize() {
75
+ return this._vsize;
76
+ }
77
+ get ksize() {
78
+ return this._ksize;
79
+ }
80
+ get count() {
81
+ return this._count;
82
+ }
83
+ incrementHits() {
84
+ if (!this._enabled) {
85
+ return;
86
+ }
87
+ this._hits++;
88
+ }
89
+ incrementMisses() {
90
+ if (!this._enabled) {
91
+ return;
92
+ }
93
+ this._misses++;
94
+ }
95
+ incrementGets() {
96
+ if (!this._enabled) {
97
+ return;
98
+ }
99
+ this._gets++;
100
+ }
101
+ incrementSets() {
102
+ if (!this._enabled) {
103
+ return;
104
+ }
105
+ this._sets++;
106
+ }
107
+ incrementDeletes() {
108
+ if (!this._enabled) {
109
+ return;
110
+ }
111
+ this._deletes++;
112
+ }
113
+ incrementClears() {
114
+ if (!this._enabled) {
115
+ return;
116
+ }
117
+ this._clears++;
118
+ }
119
+ // eslint-disable-next-line @typescript-eslint/naming-convention
120
+ incrementVSize(value) {
121
+ if (!this._enabled) {
122
+ return;
123
+ }
124
+ this._vsize += this.roughSizeOfObject(value);
125
+ }
126
+ // eslint-disable-next-line @typescript-eslint/naming-convention
127
+ decreaseVSize(value) {
128
+ if (!this._enabled) {
129
+ return;
130
+ }
131
+ this._vsize -= this.roughSizeOfObject(value);
132
+ }
133
+ // eslint-disable-next-line @typescript-eslint/naming-convention
134
+ incrementKSize(key) {
135
+ if (!this._enabled) {
136
+ return;
137
+ }
138
+ this._ksize += this.roughSizeOfString(key);
139
+ }
140
+ // eslint-disable-next-line @typescript-eslint/naming-convention
141
+ decreaseKSize(key) {
142
+ if (!this._enabled) {
143
+ return;
144
+ }
145
+ this._ksize -= this.roughSizeOfString(key);
146
+ }
147
+ incrementCount() {
148
+ if (!this._enabled) {
149
+ return;
150
+ }
151
+ this._count++;
152
+ }
153
+ decreaseCount() {
154
+ if (!this._enabled) {
155
+ return;
156
+ }
157
+ this._count--;
158
+ }
159
+ setCount(count) {
160
+ if (!this._enabled) {
161
+ return;
162
+ }
163
+ this._count = count;
164
+ }
165
+ roughSizeOfString(value) {
166
+ return value.length * 2;
167
+ }
168
+ roughSizeOfObject(object) {
169
+ const objectList = [];
170
+ const stack = [object];
171
+ let bytes = 0;
172
+ while (stack.length > 0) {
173
+ const value = stack.pop();
174
+ if (typeof value === "boolean") {
175
+ bytes += 4;
176
+ } else if (typeof value === "string") {
177
+ bytes += value.length * 2;
178
+ } else if (typeof value === "number") {
179
+ bytes += 8;
180
+ } else if (typeof value === "object" && value !== null && !objectList.includes(value)) {
181
+ objectList.push(value);
182
+ for (const key in value) {
183
+ bytes += key.length * 2;
184
+ stack.push(value[key]);
185
+ }
186
+ }
187
+ }
188
+ return bytes;
189
+ }
190
+ reset() {
191
+ this._hits = 0;
192
+ this._misses = 0;
193
+ this._gets = 0;
194
+ this._sets = 0;
195
+ this._deletes = 0;
196
+ this._clears = 0;
197
+ this._vsize = 0;
198
+ this._ksize = 0;
199
+ this._count = 0;
200
+ }
201
+ resetStoreValues() {
202
+ this._vsize = 0;
203
+ this._ksize = 0;
204
+ this._count = 0;
205
+ }
206
+ };
207
+
208
+ // src/memory-lru.ts
209
+ var ListNode = class {
210
+ // eslint-disable-next-line @typescript-eslint/parameter-properties
211
+ value;
212
+ prev = void 0;
213
+ next = void 0;
214
+ constructor(value) {
215
+ this.value = value;
216
+ }
217
+ };
218
+ var DoublyLinkedList = class {
219
+ head = void 0;
220
+ tail = void 0;
221
+ nodesMap = /* @__PURE__ */ new Map();
222
+ // Add a new node to the front (most recently used)
223
+ addToFront(value) {
224
+ const newNode = new ListNode(value);
225
+ if (this.head) {
226
+ newNode.next = this.head;
227
+ this.head.prev = newNode;
228
+ this.head = newNode;
229
+ } else {
230
+ this.head = this.tail = newNode;
231
+ }
232
+ this.nodesMap.set(value, newNode);
233
+ }
234
+ // Move an existing node to the front (most recently used)
235
+ moveToFront(value) {
236
+ const node = this.nodesMap.get(value);
237
+ if (!node || this.head === node) {
238
+ return;
239
+ }
240
+ if (node.prev) {
241
+ node.prev.next = node.next;
242
+ }
243
+ if (node.next) {
244
+ node.next.prev = node.prev;
245
+ }
246
+ if (node === this.tail) {
247
+ this.tail = node.prev;
248
+ }
249
+ node.prev = void 0;
250
+ node.next = this.head;
251
+ if (this.head) {
252
+ this.head.prev = node;
253
+ }
254
+ this.head = node;
255
+ this.tail ||= node;
256
+ }
257
+ // Get the oldest node (tail)
258
+ getOldest() {
259
+ return this.tail ? this.tail.value : void 0;
260
+ }
261
+ // Remove the oldest node (tail)
262
+ removeOldest() {
263
+ if (!this.tail) {
264
+ return void 0;
265
+ }
266
+ const oldValue = this.tail.value;
267
+ if (this.tail.prev) {
268
+ this.tail = this.tail.prev;
269
+ this.tail.next = void 0;
270
+ } else {
271
+ this.head = this.tail = void 0;
272
+ }
273
+ this.nodesMap.delete(oldValue);
274
+ return oldValue;
275
+ }
276
+ get size() {
277
+ return this.nodesMap.size;
278
+ }
279
+ };
280
+
281
+ // src/memory.ts
282
+ var CacheableMemory = class {
283
+ _hashCache = /* @__PURE__ */ new Map();
284
+ _hash0 = /* @__PURE__ */ new Map();
285
+ _hash1 = /* @__PURE__ */ new Map();
286
+ _hash2 = /* @__PURE__ */ new Map();
287
+ _hash3 = /* @__PURE__ */ new Map();
288
+ _hash4 = /* @__PURE__ */ new Map();
289
+ _hash5 = /* @__PURE__ */ new Map();
290
+ _hash6 = /* @__PURE__ */ new Map();
291
+ _hash7 = /* @__PURE__ */ new Map();
292
+ _hash8 = /* @__PURE__ */ new Map();
293
+ _hash9 = /* @__PURE__ */ new Map();
294
+ _lru = new DoublyLinkedList();
295
+ _ttl = 0;
296
+ _useClone = true;
297
+ _lruSize = 0;
298
+ constructor(options) {
299
+ if (options?.ttl) {
300
+ this._ttl = options.ttl;
301
+ }
302
+ if (options?.useClone !== void 0) {
303
+ this._useClone = options.useClone;
304
+ }
305
+ if (options?.lruSize) {
306
+ this._lruSize = options.lruSize;
307
+ }
308
+ }
309
+ get ttl() {
310
+ return this._ttl;
311
+ }
312
+ set ttl(value) {
313
+ this._ttl = value;
314
+ }
315
+ get useClone() {
316
+ return this._useClone;
317
+ }
318
+ set useClone(value) {
319
+ this._useClone = value;
320
+ }
321
+ get lruSize() {
322
+ return this._lruSize;
323
+ }
324
+ set lruSize(value) {
325
+ this._lruSize = value;
326
+ this.lruResize();
327
+ }
328
+ get size() {
329
+ 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;
330
+ }
331
+ get keys() {
332
+ return this.concatStores().keys();
333
+ }
334
+ get(key) {
335
+ const store = this.getStore(key);
336
+ const item = store.get(key);
337
+ if (!item) {
338
+ return void 0;
339
+ }
340
+ if (item.expires && item.expires && Date.now() > item.expires) {
341
+ store.delete(key);
342
+ return void 0;
343
+ }
344
+ this.lruMoveToFront(key);
345
+ if (!this._useClone) {
346
+ return item.value;
347
+ }
348
+ return this.clone(item.value);
349
+ }
350
+ set(key, value, ttl) {
351
+ const store = this.getStore(key);
352
+ let expires;
353
+ if (ttl !== void 0 || this._ttl !== 0) {
354
+ expires = Date.now() + (ttl ?? this._ttl);
355
+ }
356
+ if (this._lruSize > 0) {
357
+ if (store.has(key)) {
358
+ this.lruMoveToFront(key);
359
+ } else {
360
+ this.lruAddToFront(key);
361
+ if (this._lru.size > this._lruSize) {
362
+ const oldestKey = this._lru.getOldest();
363
+ if (oldestKey) {
364
+ this._lru.removeOldest();
365
+ this.delete(oldestKey);
366
+ }
367
+ }
368
+ }
369
+ }
370
+ store.set(key, {
371
+ key,
372
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
373
+ value,
374
+ expires
375
+ });
376
+ }
377
+ has(key) {
378
+ const item = this.get(key);
379
+ return Boolean(item);
380
+ }
381
+ take(key) {
382
+ const item = this.get(key);
383
+ if (!item) {
384
+ return void 0;
385
+ }
386
+ this.delete(key);
387
+ return item;
388
+ }
389
+ delete(key) {
390
+ const store = this.getStore(key);
391
+ store.delete(key);
392
+ }
393
+ clear() {
394
+ this._hash0.clear();
395
+ this._hash1.clear();
396
+ this._hash2.clear();
397
+ this._hash3.clear();
398
+ this._hash4.clear();
399
+ this._hash5.clear();
400
+ this._hash6.clear();
401
+ this._hash7.clear();
402
+ this._hash8.clear();
403
+ this._hash9.clear();
404
+ this._hashCache.clear();
405
+ }
406
+ hashKey(key) {
407
+ const cacheHashNumber = this._hashCache.get(key);
408
+ if (cacheHashNumber) {
409
+ return cacheHashNumber;
410
+ }
411
+ let hash = 0;
412
+ const primeMultiplier = 31;
413
+ for (let i = 0; i < key.length; i++) {
414
+ hash = hash * primeMultiplier + key.charCodeAt(i);
415
+ }
416
+ const result = Math.abs(hash) % 10;
417
+ this._hashCache.set(key, result);
418
+ return result;
419
+ }
420
+ getStore(key) {
421
+ const hashKey = this.hashKey(key);
422
+ switch (hashKey) {
423
+ case 1: {
424
+ return this._hash1;
425
+ }
426
+ case 2: {
427
+ return this._hash2;
428
+ }
429
+ case 3: {
430
+ return this._hash3;
431
+ }
432
+ case 4: {
433
+ return this._hash4;
434
+ }
435
+ case 5: {
436
+ return this._hash5;
437
+ }
438
+ case 6: {
439
+ return this._hash6;
440
+ }
441
+ case 7: {
442
+ return this._hash7;
443
+ }
444
+ case 8: {
445
+ return this._hash8;
446
+ }
447
+ case 9: {
448
+ return this._hash9;
449
+ }
450
+ default: {
451
+ return this._hash0;
452
+ }
453
+ }
454
+ }
455
+ clone(value) {
456
+ if (this.isPrimitive(value)) {
457
+ return value;
458
+ }
459
+ return structuredClone(value);
460
+ }
461
+ lruAddToFront(key) {
462
+ if (this._lruSize === 0) {
463
+ return;
464
+ }
465
+ this._lru.addToFront(key);
466
+ }
467
+ lruMoveToFront(key) {
468
+ if (this._lruSize === 0) {
469
+ return;
470
+ }
471
+ this._lru.moveToFront(key);
472
+ }
473
+ lruResize() {
474
+ if (this._lruSize === 0) {
475
+ return;
476
+ }
477
+ while (this._lru.size > this._lruSize) {
478
+ const oldestKey = this._lru.getOldest();
479
+ if (oldestKey) {
480
+ this._lru.removeOldest();
481
+ this.delete(oldestKey);
482
+ }
483
+ }
484
+ }
485
+ isPrimitive(value) {
486
+ const result = false;
487
+ if (value === null || value === void 0) {
488
+ return true;
489
+ }
490
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
491
+ return true;
492
+ }
493
+ return result;
494
+ }
495
+ concatStores() {
496
+ const result = new Map([...this._hash0, ...this._hash1, ...this._hash2, ...this._hash3, ...this._hash4, ...this._hash5, ...this._hash6, ...this._hash7, ...this._hash8, ...this._hash9]);
497
+ return result;
498
+ }
499
+ };
500
+
501
+ // src/index.ts
502
+ var CacheableHooks = /* @__PURE__ */ ((CacheableHooks2) => {
503
+ CacheableHooks2["BEFORE_SET"] = "BEFORE_SET";
504
+ CacheableHooks2["AFTER_SET"] = "AFTER_SET";
505
+ CacheableHooks2["BEFORE_SET_MANY"] = "BEFORE_SET_MANY";
506
+ CacheableHooks2["AFTER_SET_MANY"] = "AFTER_SET_MANY";
507
+ CacheableHooks2["BEFORE_GET"] = "BEFORE_GET";
508
+ CacheableHooks2["AFTER_GET"] = "AFTER_GET";
509
+ CacheableHooks2["BEFORE_GET_MANY"] = "BEFORE_GET_MANY";
510
+ CacheableHooks2["AFTER_GET_MANY"] = "AFTER_GET_MANY";
511
+ return CacheableHooks2;
512
+ })(CacheableHooks || {});
513
+ var CacheableEvents = /* @__PURE__ */ ((CacheableEvents2) => {
514
+ CacheableEvents2["ERROR"] = "error";
515
+ return CacheableEvents2;
516
+ })(CacheableEvents || {});
517
+ var Cacheable = class extends import_hookified.Hookified {
518
+ _primary = new import_keyv.Keyv({ store: new CacheableMemory() });
519
+ _secondary;
520
+ _nonBlocking = false;
521
+ _stats = new CacheableStats({ enabled: false });
522
+ constructor(options) {
523
+ super();
524
+ if (options?.primary) {
525
+ this.setPrimary(options.primary);
526
+ }
527
+ if (options?.secondary) {
528
+ this.setSecondary(options.secondary);
529
+ }
530
+ if (options?.nonBlocking) {
531
+ this._nonBlocking = options.nonBlocking;
532
+ }
533
+ if (options?.stats) {
534
+ this._stats.enabled = options.stats;
535
+ }
536
+ }
537
+ get stats() {
538
+ return this._stats;
539
+ }
540
+ get primary() {
541
+ return this._primary;
542
+ }
543
+ set primary(primary) {
544
+ this._primary = primary;
545
+ }
546
+ get secondary() {
547
+ return this._secondary;
548
+ }
549
+ set secondary(secondary) {
550
+ this._secondary = secondary;
551
+ }
552
+ get nonBlocking() {
553
+ return this._nonBlocking;
554
+ }
555
+ set nonBlocking(nonBlocking) {
556
+ this._nonBlocking = nonBlocking;
557
+ }
558
+ setPrimary(primary) {
559
+ this._primary = primary instanceof import_keyv.Keyv ? primary : new import_keyv.Keyv(primary);
560
+ }
561
+ setSecondary(secondary) {
562
+ this._secondary = secondary instanceof import_keyv.Keyv ? secondary : new import_keyv.Keyv(secondary);
563
+ }
564
+ async get(key) {
565
+ let result;
566
+ try {
567
+ await this.hook("BEFORE_GET" /* BEFORE_GET */, key);
568
+ result = await this._primary.get(key);
569
+ if (!result && this._secondary) {
570
+ result = await this._secondary.get(key);
571
+ if (result) {
572
+ await this._primary.set(key, result);
573
+ }
574
+ }
575
+ await this.hook("AFTER_GET" /* AFTER_GET */, { key, result });
576
+ } catch (error) {
577
+ await this.emit("error" /* ERROR */, error);
578
+ }
579
+ if (this.stats.enabled) {
580
+ if (result) {
581
+ this._stats.incrementHits();
582
+ } else {
583
+ this._stats.incrementMisses();
584
+ }
585
+ this.stats.incrementGets();
586
+ }
587
+ return result;
588
+ }
589
+ async getMany(keys) {
590
+ let result = [];
591
+ try {
592
+ await this.hook("BEFORE_GET_MANY" /* BEFORE_GET_MANY */, keys);
593
+ result = await this._primary.get(keys);
594
+ if (this._secondary) {
595
+ const missingKeys = [];
596
+ for (const [i, key] of keys.entries()) {
597
+ if (!result[i]) {
598
+ missingKeys.push(key);
599
+ }
600
+ }
601
+ const secondaryResult = await this._secondary.get(missingKeys);
602
+ for (const [i, key] of keys.entries()) {
603
+ if (!result[i] && secondaryResult[i]) {
604
+ result[i] = secondaryResult[i];
605
+ await this._primary.set(key, secondaryResult[i]);
606
+ }
607
+ }
608
+ }
609
+ await this.hook("AFTER_GET_MANY" /* AFTER_GET_MANY */, { keys, result });
610
+ } catch (error) {
611
+ await this.emit("error" /* ERROR */, error);
612
+ }
613
+ if (this.stats.enabled) {
614
+ for (const item of result) {
615
+ if (item) {
616
+ this._stats.incrementHits();
617
+ } else {
618
+ this._stats.incrementMisses();
619
+ }
620
+ }
621
+ this.stats.incrementGets();
622
+ }
623
+ return result;
624
+ }
625
+ async set(key, value, ttl) {
626
+ let result = false;
627
+ try {
628
+ await this.hook("BEFORE_SET" /* BEFORE_SET */, { key, value, ttl });
629
+ const promises = [];
630
+ promises.push(this._primary.set(key, value, ttl));
631
+ if (this._secondary) {
632
+ promises.push(this._secondary.set(key, value, ttl));
633
+ }
634
+ if (this._nonBlocking) {
635
+ result = await Promise.race(promises);
636
+ } else {
637
+ const results = await Promise.all(promises);
638
+ result = results[0];
639
+ }
640
+ await this.hook("AFTER_SET" /* AFTER_SET */, { key, value, ttl });
641
+ } catch (error) {
642
+ await this.emit("error" /* ERROR */, error);
643
+ }
644
+ if (this.stats.enabled) {
645
+ this.stats.incrementKSize(key);
646
+ this.stats.incrementCount();
647
+ this.stats.incrementVSize(value);
648
+ this.stats.incrementSets();
649
+ }
650
+ return result;
651
+ }
652
+ async setMany(items) {
653
+ let result = false;
654
+ try {
655
+ await this.hook("BEFORE_SET_MANY" /* BEFORE_SET_MANY */, items);
656
+ result = await this.setManyKeyv(this._primary, items);
657
+ if (this._secondary) {
658
+ if (this._nonBlocking) {
659
+ this.setManyKeyv(this._secondary, items);
660
+ } else {
661
+ await this.setManyKeyv(this._secondary, items);
662
+ }
663
+ }
664
+ await this.hook("AFTER_SET_MANY" /* AFTER_SET_MANY */, items);
665
+ } catch (error) {
666
+ await this.emit("error" /* ERROR */, error);
667
+ }
668
+ if (this.stats.enabled) {
669
+ for (const item of items) {
670
+ this.stats.incrementKSize(item.key);
671
+ this.stats.incrementCount();
672
+ this.stats.incrementVSize(item.value);
673
+ }
674
+ }
675
+ return result;
676
+ }
677
+ async take(key) {
678
+ const result = await this.get(key);
679
+ await this.delete(key);
680
+ return result;
681
+ }
682
+ async takeMany(keys) {
683
+ const result = await this.getMany(keys);
684
+ await this.deleteMany(keys);
685
+ return result;
686
+ }
687
+ async has(key) {
688
+ const promises = [];
689
+ promises.push(this._primary.has(key));
690
+ if (this._secondary) {
691
+ promises.push(this._secondary.has(key));
692
+ }
693
+ const resultAll = await Promise.all(promises);
694
+ for (const result of resultAll) {
695
+ if (result) {
696
+ return true;
697
+ }
698
+ }
699
+ return false;
700
+ }
701
+ async hasMany(keys) {
702
+ const result = await this.hasManyKeyv(this._primary, keys);
703
+ const missingKeys = [];
704
+ for (const [i, key] of keys.entries()) {
705
+ if (!result[i] && this._secondary) {
706
+ missingKeys.push(key);
707
+ }
708
+ }
709
+ if (missingKeys.length > 0 && this._secondary) {
710
+ const secondary = await this.hasManyKeyv(this._secondary, keys);
711
+ for (const [i, key] of keys.entries()) {
712
+ if (!result[i] && secondary[i]) {
713
+ result[i] = secondary[i];
714
+ }
715
+ }
716
+ }
717
+ return result;
718
+ }
719
+ async delete(key) {
720
+ let result = false;
721
+ const promises = [];
722
+ if (this.stats.enabled) {
723
+ const statResult = await this._primary.get(key);
724
+ if (statResult) {
725
+ this.stats.decreaseKSize(key);
726
+ this.stats.decreaseVSize(statResult);
727
+ this.stats.decreaseCount();
728
+ this.stats.incrementDeletes();
729
+ }
730
+ }
731
+ promises.push(this._primary.delete(key));
732
+ if (this._secondary) {
733
+ promises.push(this._secondary.delete(key));
734
+ }
735
+ if (this.nonBlocking) {
736
+ result = await Promise.race(promises);
737
+ } else {
738
+ const resultAll = await Promise.all(promises);
739
+ result = resultAll[0];
740
+ }
741
+ return result;
742
+ }
743
+ async deleteMany(keys) {
744
+ if (this.stats.enabled) {
745
+ const statResult = await this._primary.get(keys);
746
+ for (const key of keys) {
747
+ this.stats.decreaseKSize(key);
748
+ this.stats.decreaseVSize(statResult);
749
+ this.stats.decreaseCount();
750
+ this.stats.incrementDeletes();
751
+ }
752
+ }
753
+ const result = await this.deleteManyKeyv(this._primary, keys);
754
+ if (this._secondary) {
755
+ if (this._nonBlocking) {
756
+ this.deleteManyKeyv(this._secondary, keys);
757
+ } else {
758
+ await this.deleteManyKeyv(this._secondary, keys);
759
+ }
760
+ }
761
+ return result;
762
+ }
763
+ async clear() {
764
+ const promises = [];
765
+ promises.push(this._primary.clear());
766
+ if (this._secondary) {
767
+ promises.push(this._secondary.clear());
768
+ }
769
+ await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
770
+ if (this.stats.enabled) {
771
+ this._stats.resetStoreValues();
772
+ this._stats.incrementClears();
773
+ }
774
+ }
775
+ async disconnect() {
776
+ const promises = [];
777
+ promises.push(this._primary.disconnect());
778
+ if (this._secondary) {
779
+ promises.push(this._secondary.disconnect());
780
+ }
781
+ await (this._nonBlocking ? Promise.race(promises) : Promise.all(promises));
782
+ }
783
+ async deleteManyKeyv(keyv, keys) {
784
+ const promises = [];
785
+ for (const key of keys) {
786
+ promises.push(keyv.delete(key));
787
+ }
788
+ await Promise.all(promises);
789
+ return true;
790
+ }
791
+ async setManyKeyv(keyv, items) {
792
+ const promises = [];
793
+ for (const item of items) {
794
+ promises.push(keyv.set(item.key, item.value, item.ttl));
795
+ }
796
+ await Promise.all(promises);
797
+ return true;
798
+ }
799
+ async hasManyKeyv(keyv, keys) {
800
+ const promises = [];
801
+ for (const key of keys) {
802
+ promises.push(keyv.has(key));
803
+ }
804
+ return Promise.all(promises);
805
+ }
806
+ };
807
+ // Annotate the CommonJS export names for ESM import in node:
808
+ 0 && (module.exports = {
809
+ Cacheable,
810
+ CacheableEvents,
811
+ CacheableHooks,
812
+ CacheableMemory,
813
+ CacheableStats
814
+ });