@keq-request/cache 5.0.0-alpha.10 → 5.0.0-alpha.12

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.mjs CHANGED
@@ -1,10 +1,18 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+
1
5
  // src/cache.ts
2
6
  import * as R from "ramda";
3
7
  import { Exception } from "keq";
4
8
  function cache(opts) {
5
9
  const storage = opts.storage;
6
- const rules = opts?.rules || [];
10
+ const rules = (opts == null ? void 0 : opts.rules) || [];
7
11
  return async function cache2(ctx, next) {
12
+ if (ctx.options.cache === false) {
13
+ await next();
14
+ return;
15
+ }
8
16
  let cOpt = ctx.options.cache;
9
17
  const rule = rules.find((rule2) => {
10
18
  if (rule2.pattern === void 0 || rule2.pattern === true) return true;
@@ -20,7 +28,7 @@ function cache(opts) {
20
28
  if (cOpt.key) {
21
29
  if (typeof cOpt.key === "function") key = cOpt.key(ctx);
22
30
  else key = cOpt.key;
23
- } else if (opts?.keyFactory) {
31
+ } else if (opts == null ? void 0 : opts.keyFactory) {
24
32
  key = opts.keyFactory(ctx);
25
33
  }
26
34
  if (!key) throw new Exception("Cache key is required");
@@ -47,27 +55,29 @@ async function getResponseBytes(response) {
47
55
 
48
56
  // src/cache-entry/cache-entry.ts
49
57
  var CacheEntry = class _CacheEntry {
50
- key;
51
- response;
52
- /**
53
- * @en bytes
54
- * @zh 字节数
55
- */
56
- size;
57
- expiredAt;
58
58
  constructor(options) {
59
+ __publicField(this, "key");
60
+ __publicField(this, "response");
61
+ /**
62
+ * @en bytes
63
+ * @zh 字节数
64
+ */
65
+ __publicField(this, "size");
66
+ __publicField(this, "expiredAt");
67
+ var _a;
59
68
  this.key = options.key;
60
69
  this.response = options.response;
61
70
  this.size = options.size;
62
- this.expiredAt = options.expiredAt ?? /* @__PURE__ */ new Date(864e13);
71
+ this.expiredAt = (_a = options.expiredAt) != null ? _a : /* @__PURE__ */ new Date(864e13);
63
72
  }
64
73
  static async build(options) {
74
+ var _a;
65
75
  const expiredAt = "expiredAt" in options ? options.expiredAt : "ttl" in options && typeof options.ttl === "number" && options.ttl > 0 ? new Date(Date.now() + options.ttl * 1e3) : /* @__PURE__ */ new Date(864e13);
66
76
  const response = options.response.clone();
67
77
  return new _CacheEntry({
68
78
  key: options.key,
69
79
  response,
70
- size: options.size ?? await getResponseBytes(response),
80
+ size: (_a = options.size) != null ? _a : await getResponseBytes(response),
71
81
  expiredAt
72
82
  });
73
83
  }
@@ -138,7 +148,7 @@ var networkFirst = function(opts) {
138
148
  if (entry) {
139
149
  context.emitter.emit("cache:update", {
140
150
  key,
141
- oldResponse: cache2?.response,
151
+ oldResponse: cache2 == null ? void 0 : cache2.response,
142
152
  newResponse: entry.response,
143
153
  context
144
154
  });
@@ -220,6 +230,15 @@ var Eviction = /* @__PURE__ */ ((Eviction2) => {
220
230
  return Eviction2;
221
231
  })(Eviction || {});
222
232
 
233
+ // src/constants/size.enum.ts
234
+ var Size = /* @__PURE__ */ ((Size2) => {
235
+ Size2[Size2["B"] = 1] = "B";
236
+ Size2[Size2["KB"] = 1024] = "KB";
237
+ Size2[Size2["MB"] = 1048576] = "MB";
238
+ Size2[Size2["GB"] = 1073741824] = "GB";
239
+ return Size2;
240
+ })(Size || {});
241
+
223
242
  // src/storage/memory-storage/ttl-memory-storage.ts
224
243
  import dayjs2 from "dayjs";
225
244
  import * as R3 from "ramda";
@@ -239,31 +258,32 @@ var KeqCacheStorage = class {
239
258
 
240
259
  // src/storage/internal-storage/internal-storage.ts
241
260
  var InternalStorage = class extends KeqCacheStorage {
242
- __id__ = Math.random().toString(36).slice(2);
243
- __size__;
244
- __debug__;
245
- __onCacheGet__;
246
- __onCacheSet__;
247
- __onCacheRemove__;
248
- __onCacheEvict__;
249
- __onCacheExpired__;
250
261
  constructor(options) {
262
+ var _a;
251
263
  super();
252
- if (options?.size && (typeof options?.size !== "number" || options.size <= 0)) {
253
- throw TypeError(`Invalid size: ${String(options?.size)}`);
264
+ __publicField(this, "__id__", Math.random().toString(36).slice(2));
265
+ __publicField(this, "__size__");
266
+ __publicField(this, "__debug__");
267
+ __publicField(this, "__onCacheGet__");
268
+ __publicField(this, "__onCacheSet__");
269
+ __publicField(this, "__onCacheRemove__");
270
+ __publicField(this, "__onCacheEvict__");
271
+ __publicField(this, "__onCacheExpired__");
272
+ if ((options == null ? void 0 : options.size) && (typeof (options == null ? void 0 : options.size) !== "number" || options.size <= 0)) {
273
+ throw TypeError("Invalid size: ".concat(String(options == null ? void 0 : options.size)));
254
274
  }
255
- this.__size__ = options?.size ?? Infinity;
256
- this.__debug__ = !!options?.debug;
257
- this.__onCacheGet__ = options?.onCacheGet;
258
- this.__onCacheSet__ = options?.onCacheSet;
259
- this.__onCacheRemove__ = options?.onCacheRemove;
260
- this.__onCacheEvict__ = options?.onCacheEvict;
275
+ this.__size__ = (_a = options == null ? void 0 : options.size) != null ? _a : Infinity;
276
+ this.__debug__ = !!(options == null ? void 0 : options.debug);
277
+ this.__onCacheGet__ = options == null ? void 0 : options.onCacheGet;
278
+ this.__onCacheSet__ = options == null ? void 0 : options.onCacheSet;
279
+ this.__onCacheRemove__ = options == null ? void 0 : options.onCacheRemove;
280
+ this.__onCacheEvict__ = options == null ? void 0 : options.onCacheEvict;
261
281
  this.debug((log) => log("Storage Created: ", this));
262
282
  }
263
283
  debug(fn) {
264
284
  if (this.__debug__) {
265
285
  fn((...args) => {
266
- debug(`[Storage(${this.__id__})]`, ...args);
286
+ debug("[Storage(".concat(this.__id__, ")]"), ...args);
267
287
  });
268
288
  }
269
289
  }
@@ -271,9 +291,13 @@ var InternalStorage = class extends KeqCacheStorage {
271
291
 
272
292
  // src/storage/memory-storage/base-memory-storage.ts
273
293
  var BaseMemoryStorage = class extends InternalStorage {
274
- storage = /* @__PURE__ */ new Map();
275
- visitTimeRecords = /* @__PURE__ */ new Map();
276
- visitCountRecords = /* @__PURE__ */ new Map();
294
+ constructor() {
295
+ super(...arguments);
296
+ __publicField(this, "storage", /* @__PURE__ */ new Map());
297
+ __publicField(this, "visitTimeRecords", /* @__PURE__ */ new Map());
298
+ __publicField(this, "visitCountRecords", /* @__PURE__ */ new Map());
299
+ __publicField(this, "lastEvictExpiredTime", dayjs());
300
+ }
277
301
  get size() {
278
302
  const used = R2.sum(R2.pluck("size", [...this.storage.values()]));
279
303
  const free = this.__size__ > used ? this.__size__ - used : 0;
@@ -283,22 +307,24 @@ var BaseMemoryStorage = class extends InternalStorage {
283
307
  };
284
308
  }
285
309
  get(key) {
310
+ var _a;
286
311
  this.evictExpired();
287
312
  const entry = this.storage.get(key);
288
- this.visitCountRecords.set(key, (this.visitCountRecords.get(key) ?? 0) + 1);
313
+ this.visitCountRecords.set(key, ((_a = this.visitCountRecords.get(key)) != null ? _a : 0) + 1);
289
314
  this.visitTimeRecords.set(key, /* @__PURE__ */ new Date());
290
- if (!entry) this.debug((log) => log(`Entry(${key}) Not Found`));
291
- else this.debug((log) => log(`Entry(${key}) Found: `, entry));
292
- return entry?.clone();
315
+ if (!entry) this.debug((log) => log("Entry(".concat(key, ") Not Found")));
316
+ else this.debug((log) => log("Entry(".concat(key, ") Found: "), entry));
317
+ return entry == null ? void 0 : entry.clone();
293
318
  }
294
319
  set(value) {
320
+ var _a;
295
321
  if (!this.evict(value.size)) {
296
322
  this.debug((log) => log("Storage Size Not Enough: ", this.size.free, " < ", value.size));
297
323
  return;
298
324
  }
299
325
  this.storage.set(value.key, value);
300
326
  this.visitTimeRecords.set(value.key, /* @__PURE__ */ new Date());
301
- this.visitCountRecords.set(value.key, this.visitCountRecords.get(value.key) ?? 0);
327
+ this.visitCountRecords.set(value.key, (_a = this.visitCountRecords.get(value.key)) != null ? _a : 0);
302
328
  this.debug((log) => log("Entry Added: ", value));
303
329
  this.debug((log) => log("Storage Size: ", this.size));
304
330
  }
@@ -316,11 +342,11 @@ var BaseMemoryStorage = class extends InternalStorage {
316
342
  remove(key) {
317
343
  this.__remove__([key]);
318
344
  }
319
- lastEvictExpiredTime = dayjs();
320
345
  /**
321
346
  * @zh 清除过期的缓存
322
347
  */
323
348
  evictExpired() {
349
+ var _a;
324
350
  const now = dayjs();
325
351
  if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
326
352
  const keys = [];
@@ -330,7 +356,7 @@ var BaseMemoryStorage = class extends InternalStorage {
330
356
  }
331
357
  }
332
358
  this.__remove__(keys);
333
- this.__onCacheExpired__?.({ keys });
359
+ (_a = this.__onCacheExpired__) == null ? void 0 : _a.call(this, { keys });
334
360
  }
335
361
  /**
336
362
  * @en Evict the storage to make sure the size is enough
@@ -351,19 +377,23 @@ var TTLMemoryStorage = class extends BaseMemoryStorage {
351
377
  super(options);
352
378
  }
353
379
  get(key) {
380
+ var _a;
354
381
  const entry = super.get(key);
355
- this.__onCacheGet__?.({ key });
382
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
356
383
  return entry;
357
384
  }
358
385
  set(value) {
386
+ var _a;
359
387
  super.set(value);
360
- this.__onCacheSet__?.({ key: value.key });
388
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
361
389
  }
362
390
  remove(key) {
391
+ var _a;
363
392
  super.remove(key);
364
- this.__onCacheRemove__?.({ key });
393
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
365
394
  }
366
395
  evict(expectSize) {
396
+ var _a;
367
397
  if (expectSize > this.__size__) {
368
398
  this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
369
399
  return false;
@@ -387,7 +417,7 @@ var TTLMemoryStorage = class extends BaseMemoryStorage {
387
417
  keys.push(entry.key);
388
418
  }
389
419
  this.__remove__(keys);
390
- this.__onCacheEvict__?.({ keys });
420
+ (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
391
421
  return true;
392
422
  }
393
423
  };
@@ -403,19 +433,23 @@ var RandomMemoryStorage = class extends BaseMemoryStorage {
403
433
  super(options);
404
434
  }
405
435
  get(key) {
436
+ var _a;
406
437
  const entry = super.get(key);
407
- this.__onCacheGet__?.({ key });
438
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
408
439
  return entry;
409
440
  }
410
441
  set(value) {
442
+ var _a;
411
443
  super.set(value);
412
- this.__onCacheSet__?.({ key: value.key });
444
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
413
445
  }
414
446
  remove(key) {
447
+ var _a;
415
448
  super.remove(key);
416
- this.__onCacheRemove__?.({ key });
449
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
417
450
  }
418
451
  evict(expectSize) {
452
+ var _a;
419
453
  if (expectSize > this.__size__) {
420
454
  this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
421
455
  return false;
@@ -433,7 +467,7 @@ var RandomMemoryStorage = class extends BaseMemoryStorage {
433
467
  keys.push(entry.key);
434
468
  }
435
469
  this.__remove__(keys);
436
- this.__onCacheEvict__?.({ keys });
470
+ (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
437
471
  return true;
438
472
  }
439
473
  };
@@ -445,19 +479,23 @@ var LRUMemoryStorage = class extends BaseMemoryStorage {
445
479
  super(options);
446
480
  }
447
481
  get(key) {
482
+ var _a;
448
483
  const entry = super.get(key);
449
- this.__onCacheGet__?.({ key });
484
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
450
485
  return entry;
451
486
  }
452
487
  set(value) {
488
+ var _a;
453
489
  super.set(value);
454
- this.__onCacheSet__?.({ key: value.key });
490
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
455
491
  }
456
492
  remove(key) {
493
+ var _a;
457
494
  super.remove(key);
458
- this.__onCacheRemove__?.({ key });
495
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
459
496
  }
460
497
  evict(expectSize) {
498
+ var _a;
461
499
  if (expectSize > this.__size__) {
462
500
  this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
463
501
  return false;
@@ -480,7 +518,7 @@ var LRUMemoryStorage = class extends BaseMemoryStorage {
480
518
  keys.push(entry.key);
481
519
  }
482
520
  this.__remove__(keys);
483
- this.__onCacheEvict__?.({ keys });
521
+ (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
484
522
  return true;
485
523
  }
486
524
  };
@@ -491,19 +529,23 @@ var LFUMemoryStorage = class extends BaseMemoryStorage {
491
529
  super(options);
492
530
  }
493
531
  get(key) {
532
+ var _a;
494
533
  const entry = super.get(key);
495
- this.__onCacheGet__?.({ key });
534
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
496
535
  return entry;
497
536
  }
498
537
  set(value) {
538
+ var _a;
499
539
  super.set(value);
500
- this.__onCacheSet__?.({ key: value.key });
540
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
501
541
  }
502
542
  remove(key) {
543
+ var _a;
503
544
  super.remove(key);
504
- this.__onCacheRemove__?.({ key });
545
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
505
546
  }
506
547
  evict(expectSize) {
548
+ var _a;
507
549
  if (expectSize > this.__size__) {
508
550
  this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
509
551
  return false;
@@ -523,17 +565,17 @@ var LFUMemoryStorage = class extends BaseMemoryStorage {
523
565
  keys.push(entry.key);
524
566
  }
525
567
  this.__remove__(keys);
526
- this.__onCacheEvict__?.({ keys });
568
+ (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
527
569
  return true;
528
570
  }
529
571
  };
530
572
 
531
573
  // src/storage/memory-storage/memory-storage.ts
532
574
  var MemoryStorage = class extends KeqCacheStorage {
533
- storage;
534
575
  constructor(options) {
535
576
  super();
536
- const eviction = options?.eviction || "lru" /* LRU */;
577
+ __publicField(this, "storage");
578
+ const eviction = (options == null ? void 0 : options.eviction) || "lru" /* LRU */;
537
579
  if (eviction === "ttl" /* TTL */) {
538
580
  this.storage = new TTLMemoryStorage(options);
539
581
  } else if (eviction === "random" /* RANDOM */) {
@@ -543,7 +585,7 @@ var MemoryStorage = class extends KeqCacheStorage {
543
585
  } else if (eviction === "lfu" /* LFU */) {
544
586
  this.storage = new LFUMemoryStorage(options);
545
587
  } else {
546
- throw new TypeError(`Invalid eviction: ${String(eviction)}`);
588
+ throw new TypeError("Invalid eviction: ".concat(String(eviction)));
547
589
  }
548
590
  }
549
591
  set(entry) {
@@ -570,14 +612,15 @@ var DEFAULT_TABLE_NAME = "keq_cache_indexed_db_storage";
570
612
 
571
613
  // src/storage/indexed-db-storage/base-indexed-db-storage.ts
572
614
  var BaseIndexedDBStorage = class extends InternalStorage {
573
- tableName = DEFAULT_TABLE_NAME;
574
- db;
575
615
  constructor(options) {
576
616
  super(options);
577
- if (options?.tableName === DEFAULT_TABLE_NAME) {
578
- throw new TypeError(`[keq-cache] IndexedDBStorage name cannot be "${DEFAULT_TABLE_NAME}"`);
617
+ __publicField(this, "tableName", DEFAULT_TABLE_NAME);
618
+ __publicField(this, "db");
619
+ __publicField(this, "lastEvictExpiredTime", dayjs4());
620
+ if ((options == null ? void 0 : options.tableName) === DEFAULT_TABLE_NAME) {
621
+ throw new TypeError('[keq-cache] IndexedDBStorage name cannot be "'.concat(DEFAULT_TABLE_NAME, '"'));
579
622
  }
580
- this.tableName = options?.tableName || DEFAULT_TABLE_NAME;
623
+ this.tableName = (options == null ? void 0 : options.tableName) || DEFAULT_TABLE_NAME;
581
624
  }
582
625
  async openDB() {
583
626
  if (this.db) return this.db;
@@ -599,13 +642,13 @@ var BaseIndexedDBStorage = class extends InternalStorage {
599
642
  }
600
643
  },
601
644
  blocked() {
602
- console.error(`IndexedDB Table ${tableName} is blocked`);
645
+ console.error("IndexedDB Table ".concat(tableName, " is blocked"));
603
646
  },
604
647
  blocking() {
605
- console.error(`IndexedDB Table ${tableName} is blocking`);
648
+ console.error("IndexedDB Table ".concat(tableName, " is blocking"));
606
649
  },
607
650
  terminated() {
608
- console.error(`IndexedDB Table ${tableName} is terminated`);
651
+ console.error("IndexedDB Table ".concat(tableName, " is terminated"));
609
652
  }
610
653
  });
611
654
  this.db = db;
@@ -650,7 +693,7 @@ var BaseIndexedDBStorage = class extends InternalStorage {
650
693
  try {
651
694
  if (!await this.evict(entry.size)) {
652
695
  const size = await this.getSize();
653
- this.debug((log) => log(`Storage Size Not Enough: ${size.free} < ${entry.size}`));
696
+ this.debug((log) => log("Storage Size Not Enough: ".concat(size.free, " < ").concat(entry.size)));
654
697
  return;
655
698
  }
656
699
  const dbMetadata = {
@@ -709,11 +752,11 @@ var BaseIndexedDBStorage = class extends InternalStorage {
709
752
  return;
710
753
  }
711
754
  }
712
- lastEvictExpiredTime = dayjs4();
713
755
  /**
714
756
  * @zh 清除过期的缓存
715
757
  */
716
758
  async evictExpired() {
759
+ var _a;
717
760
  const now = dayjs4();
718
761
  if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
719
762
  try {
@@ -733,7 +776,7 @@ var BaseIndexedDBStorage = class extends InternalStorage {
733
776
  }
734
777
  await this.__remove__(tx, expiredKeys);
735
778
  await tx.done;
736
- this.__onCacheExpired__?.({ keys: expiredKeys });
779
+ (_a = this.__onCacheExpired__) == null ? void 0 : _a.call(this, { keys: expiredKeys });
737
780
  } catch (error) {
738
781
  return;
739
782
  }
@@ -746,19 +789,23 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
746
789
  super(options);
747
790
  }
748
791
  async get(key) {
792
+ var _a;
749
793
  const entry = await super.get(key);
750
- this.__onCacheGet__?.({ key });
794
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
751
795
  return entry;
752
796
  }
753
797
  async set(value) {
798
+ var _a;
754
799
  await super.set(value);
755
- this.__onCacheSet__?.({ key: value.key });
800
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
756
801
  }
757
802
  async remove(key) {
803
+ var _a;
758
804
  await super.remove(key);
759
- this.__onCacheRemove__?.({ key });
805
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
760
806
  }
761
807
  async evict(expectSize) {
808
+ var _a;
762
809
  await this.evictExpired();
763
810
  const size = await this.getSize();
764
811
  let deficitSize = expectSize - size.free;
@@ -769,7 +816,7 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
769
816
  const metadatas = await metadataStore.getAll();
770
817
  const totalSize = R5.sum(metadatas.map((m) => m.size));
771
818
  if (totalSize < deficitSize) {
772
- this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize - totalSize}`));
819
+ this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize - totalSize)));
773
820
  await tx.abort();
774
821
  return false;
775
822
  }
@@ -783,7 +830,7 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
783
830
  }
784
831
  await this.__remove__(tx, keys);
785
832
  await tx.done;
786
- await this.__onCacheEvict__?.({ keys });
833
+ await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
787
834
  return true;
788
835
  }
789
836
  };
@@ -794,19 +841,23 @@ var LFUIndexedDBStorage = class extends BaseIndexedDBStorage {
794
841
  super(options);
795
842
  }
796
843
  async get(key) {
844
+ var _a;
797
845
  const entry = await super.get(key);
798
- this.__onCacheGet__?.({ key });
846
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
799
847
  return entry;
800
848
  }
801
849
  async set(value) {
850
+ var _a;
802
851
  await super.set(value);
803
- this.__onCacheSet__?.({ key: value.key });
852
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
804
853
  }
805
854
  async remove(key) {
855
+ var _a;
806
856
  await super.remove(key);
807
- this.__onCacheRemove__?.({ key });
857
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
808
858
  }
809
859
  async evict(expectSize) {
860
+ var _a;
810
861
  await this.evictExpired();
811
862
  const size = await this.getSize();
812
863
  let deficitSize = expectSize - size.free;
@@ -829,13 +880,13 @@ var LFUIndexedDBStorage = class extends BaseIndexedDBStorage {
829
880
  cursor = await cursor.continue();
830
881
  }
831
882
  if (deficitSize > 0) {
832
- this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
883
+ this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
833
884
  await tx.abort;
834
885
  return false;
835
886
  }
836
887
  await this.__remove__(tx, keys);
837
888
  await tx.done;
838
- this.__onCacheEvict__?.({ keys });
889
+ (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
839
890
  return true;
840
891
  }
841
892
  };
@@ -846,19 +897,23 @@ var LRUIndexedDBStorage = class extends BaseIndexedDBStorage {
846
897
  super(options);
847
898
  }
848
899
  async get(key) {
900
+ var _a;
849
901
  const entry = await super.get(key);
850
- this.__onCacheGet__?.({ key });
902
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
851
903
  return entry;
852
904
  }
853
905
  async set(value) {
906
+ var _a;
854
907
  await super.set(value);
855
- this.__onCacheSet__?.({ key: value.key });
908
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
856
909
  }
857
910
  async remove(key) {
911
+ var _a;
858
912
  await super.remove(key);
859
- this.__onCacheRemove__?.({ key });
913
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
860
914
  }
861
915
  async evict(expectSize) {
916
+ var _a;
862
917
  await this.evictExpired();
863
918
  const size = await this.getSize();
864
919
  let deficitSize = expectSize - size.free;
@@ -881,13 +936,13 @@ var LRUIndexedDBStorage = class extends BaseIndexedDBStorage {
881
936
  cursor = await cursor.continue();
882
937
  }
883
938
  if (deficitSize > 0) {
884
- this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
939
+ this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
885
940
  await tx.abort();
886
941
  return false;
887
942
  }
888
943
  await this.__remove__(tx, keys);
889
944
  await tx.done;
890
- await this.__onCacheEvict__?.({ keys });
945
+ await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
891
946
  return true;
892
947
  }
893
948
  };
@@ -898,19 +953,23 @@ var TTLIndexedDBStorage = class extends BaseIndexedDBStorage {
898
953
  super(options);
899
954
  }
900
955
  async get(key) {
956
+ var _a;
901
957
  const entry = await super.get(key);
902
- this.__onCacheGet__?.({ key });
958
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
903
959
  return entry;
904
960
  }
905
961
  async set(value) {
962
+ var _a;
906
963
  await super.set(value);
907
- this.__onCacheSet__?.({ key: value.key });
964
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
908
965
  }
909
966
  async remove(key) {
967
+ var _a;
910
968
  await super.remove(key);
911
- this.__onCacheRemove__?.({ key });
969
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
912
970
  }
913
971
  async evict(expectSize) {
972
+ var _a;
914
973
  await this.evictExpired();
915
974
  const size = await this.getSize();
916
975
  let deficitSize = expectSize - size.free;
@@ -932,23 +991,23 @@ var TTLIndexedDBStorage = class extends BaseIndexedDBStorage {
932
991
  cursor = await cursor.continue();
933
992
  }
934
993
  if (deficitSize > 0) {
935
- this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
994
+ this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
936
995
  await tx.abort();
937
996
  return false;
938
997
  }
939
998
  await this.__remove__(tx, keys);
940
999
  await tx.done;
941
- await this.__onCacheEvict__?.({ keys });
1000
+ await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
942
1001
  return true;
943
1002
  }
944
1003
  };
945
1004
 
946
1005
  // src/storage/indexed-db-storage/indexed-db-storage.ts
947
1006
  var IndexedDBStorage = class extends KeqCacheStorage {
948
- storage;
949
1007
  constructor(options) {
950
1008
  super();
951
- const eviction = options?.eviction || "lru" /* LRU */;
1009
+ __publicField(this, "storage");
1010
+ const eviction = (options == null ? void 0 : options.eviction) || "lru" /* LRU */;
952
1011
  if (eviction === "random" /* RANDOM */) {
953
1012
  this.storage = new RandomIndexedDBStorage(options);
954
1013
  } else if (eviction === "lfu" /* LFU */) {
@@ -958,7 +1017,7 @@ var IndexedDBStorage = class extends KeqCacheStorage {
958
1017
  } else if (eviction === "ttl" /* TTL */) {
959
1018
  this.storage = new TTLIndexedDBStorage(options);
960
1019
  } else {
961
- throw TypeError(`Not Supported Eviction: ${String(options?.eviction)}`);
1020
+ throw TypeError("Not Supported Eviction: ".concat(String(options == null ? void 0 : options.eviction)));
962
1021
  }
963
1022
  }
964
1023
  set(entry) {
@@ -974,13 +1033,13 @@ var IndexedDBStorage = class extends KeqCacheStorage {
974
1033
 
975
1034
  // src/storage/multi-tier-storage/multi-tier-storage.ts
976
1035
  var MultiTierStorage = class extends KeqCacheStorage {
977
- storages;
978
1036
  /**
979
1037
  * @param storages Array of storage instances ordered by performance (fastest first)
980
1038
  * @zh 按性价比排序的存储实例数组,排在前面的成本低,排在后面的成本高
981
1039
  */
982
1040
  constructor(options) {
983
1041
  super();
1042
+ __publicField(this, "storages");
984
1043
  if (!options.tiers || options.tiers.length === 0) {
985
1044
  throw new Error("At least one storage instance is required");
986
1045
  }
@@ -1040,8 +1099,8 @@ var MultiTierStorage = class extends KeqCacheStorage {
1040
1099
  // src/storage/tier-storage/tier-storage.ts
1041
1100
  var TierStorage = class extends MultiTierStorage {
1042
1101
  constructor(options) {
1043
- const memoryStorage = options?.memory instanceof MemoryStorage ? options.memory : new MemoryStorage(options?.memory);
1044
- const indexedDBStorage = options?.indexedDB instanceof IndexedDBStorage ? options.indexedDB : new IndexedDBStorage(options?.indexedDB);
1102
+ const memoryStorage = (options == null ? void 0 : options.memory) instanceof MemoryStorage ? options.memory : new MemoryStorage(options == null ? void 0 : options.memory);
1103
+ const indexedDBStorage = (options == null ? void 0 : options.indexedDB) instanceof IndexedDBStorage ? options.indexedDB : new IndexedDBStorage(options == null ? void 0 : options.indexedDB);
1045
1104
  super({
1046
1105
  tiers: [memoryStorage, indexedDBStorage]
1047
1106
  });
@@ -1052,6 +1111,7 @@ export {
1052
1111
  Eviction,
1053
1112
  IndexedDBStorage,
1054
1113
  MemoryStorage,
1114
+ Size,
1055
1115
  Strategy,
1056
1116
  TierStorage,
1057
1117
  cache