@keq-request/cache 5.0.0-alpha.11 → 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/CHANGELOG.md +8 -0
- package/dist/index.js +140 -107
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +142 -96
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
9
|
var __export = (target, all) => {
|
|
9
10
|
for (var name in all)
|
|
10
11
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -26,6 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
27
|
mod
|
|
27
28
|
));
|
|
28
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
29
31
|
|
|
30
32
|
// src/index.ts
|
|
31
33
|
var index_exports = {};
|
|
@@ -46,7 +48,7 @@ var R = __toESM(require("ramda"));
|
|
|
46
48
|
var import_keq = require("keq");
|
|
47
49
|
function cache(opts) {
|
|
48
50
|
const storage = opts.storage;
|
|
49
|
-
const rules = opts
|
|
51
|
+
const rules = (opts == null ? void 0 : opts.rules) || [];
|
|
50
52
|
return async function cache2(ctx, next) {
|
|
51
53
|
if (ctx.options.cache === false) {
|
|
52
54
|
await next();
|
|
@@ -67,7 +69,7 @@ function cache(opts) {
|
|
|
67
69
|
if (cOpt.key) {
|
|
68
70
|
if (typeof cOpt.key === "function") key = cOpt.key(ctx);
|
|
69
71
|
else key = cOpt.key;
|
|
70
|
-
} else if (opts
|
|
72
|
+
} else if (opts == null ? void 0 : opts.keyFactory) {
|
|
71
73
|
key = opts.keyFactory(ctx);
|
|
72
74
|
}
|
|
73
75
|
if (!key) throw new import_keq.Exception("Cache key is required");
|
|
@@ -94,27 +96,29 @@ async function getResponseBytes(response) {
|
|
|
94
96
|
|
|
95
97
|
// src/cache-entry/cache-entry.ts
|
|
96
98
|
var CacheEntry = class _CacheEntry {
|
|
97
|
-
key;
|
|
98
|
-
response;
|
|
99
|
-
/**
|
|
100
|
-
* @en bytes
|
|
101
|
-
* @zh 字节数
|
|
102
|
-
*/
|
|
103
|
-
size;
|
|
104
|
-
expiredAt;
|
|
105
99
|
constructor(options) {
|
|
100
|
+
__publicField(this, "key");
|
|
101
|
+
__publicField(this, "response");
|
|
102
|
+
/**
|
|
103
|
+
* @en bytes
|
|
104
|
+
* @zh 字节数
|
|
105
|
+
*/
|
|
106
|
+
__publicField(this, "size");
|
|
107
|
+
__publicField(this, "expiredAt");
|
|
108
|
+
var _a;
|
|
106
109
|
this.key = options.key;
|
|
107
110
|
this.response = options.response;
|
|
108
111
|
this.size = options.size;
|
|
109
|
-
this.expiredAt = options.expiredAt
|
|
112
|
+
this.expiredAt = (_a = options.expiredAt) != null ? _a : /* @__PURE__ */ new Date(864e13);
|
|
110
113
|
}
|
|
111
114
|
static async build(options) {
|
|
115
|
+
var _a;
|
|
112
116
|
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);
|
|
113
117
|
const response = options.response.clone();
|
|
114
118
|
return new _CacheEntry({
|
|
115
119
|
key: options.key,
|
|
116
120
|
response,
|
|
117
|
-
size: options.size
|
|
121
|
+
size: (_a = options.size) != null ? _a : await getResponseBytes(response),
|
|
118
122
|
expiredAt
|
|
119
123
|
});
|
|
120
124
|
}
|
|
@@ -185,7 +189,7 @@ var networkFirst = function(opts) {
|
|
|
185
189
|
if (entry) {
|
|
186
190
|
context.emitter.emit("cache:update", {
|
|
187
191
|
key,
|
|
188
|
-
oldResponse: cache2
|
|
192
|
+
oldResponse: cache2 == null ? void 0 : cache2.response,
|
|
189
193
|
newResponse: entry.response,
|
|
190
194
|
context
|
|
191
195
|
});
|
|
@@ -295,31 +299,32 @@ var KeqCacheStorage = class {
|
|
|
295
299
|
|
|
296
300
|
// src/storage/internal-storage/internal-storage.ts
|
|
297
301
|
var InternalStorage = class extends KeqCacheStorage {
|
|
298
|
-
__id__ = Math.random().toString(36).slice(2);
|
|
299
|
-
__size__;
|
|
300
|
-
__debug__;
|
|
301
|
-
__onCacheGet__;
|
|
302
|
-
__onCacheSet__;
|
|
303
|
-
__onCacheRemove__;
|
|
304
|
-
__onCacheEvict__;
|
|
305
|
-
__onCacheExpired__;
|
|
306
302
|
constructor(options) {
|
|
303
|
+
var _a;
|
|
307
304
|
super();
|
|
308
|
-
|
|
309
|
-
|
|
305
|
+
__publicField(this, "__id__", Math.random().toString(36).slice(2));
|
|
306
|
+
__publicField(this, "__size__");
|
|
307
|
+
__publicField(this, "__debug__");
|
|
308
|
+
__publicField(this, "__onCacheGet__");
|
|
309
|
+
__publicField(this, "__onCacheSet__");
|
|
310
|
+
__publicField(this, "__onCacheRemove__");
|
|
311
|
+
__publicField(this, "__onCacheEvict__");
|
|
312
|
+
__publicField(this, "__onCacheExpired__");
|
|
313
|
+
if ((options == null ? void 0 : options.size) && (typeof (options == null ? void 0 : options.size) !== "number" || options.size <= 0)) {
|
|
314
|
+
throw TypeError("Invalid size: ".concat(String(options == null ? void 0 : options.size)));
|
|
310
315
|
}
|
|
311
|
-
this.__size__ = options
|
|
312
|
-
this.__debug__ = !!options
|
|
313
|
-
this.__onCacheGet__ = options
|
|
314
|
-
this.__onCacheSet__ = options
|
|
315
|
-
this.__onCacheRemove__ = options
|
|
316
|
-
this.__onCacheEvict__ = options
|
|
316
|
+
this.__size__ = (_a = options == null ? void 0 : options.size) != null ? _a : Infinity;
|
|
317
|
+
this.__debug__ = !!(options == null ? void 0 : options.debug);
|
|
318
|
+
this.__onCacheGet__ = options == null ? void 0 : options.onCacheGet;
|
|
319
|
+
this.__onCacheSet__ = options == null ? void 0 : options.onCacheSet;
|
|
320
|
+
this.__onCacheRemove__ = options == null ? void 0 : options.onCacheRemove;
|
|
321
|
+
this.__onCacheEvict__ = options == null ? void 0 : options.onCacheEvict;
|
|
317
322
|
this.debug((log) => log("Storage Created: ", this));
|
|
318
323
|
}
|
|
319
324
|
debug(fn) {
|
|
320
325
|
if (this.__debug__) {
|
|
321
326
|
fn((...args) => {
|
|
322
|
-
debug(
|
|
327
|
+
debug("[Storage(".concat(this.__id__, ")]"), ...args);
|
|
323
328
|
});
|
|
324
329
|
}
|
|
325
330
|
}
|
|
@@ -327,9 +332,13 @@ var InternalStorage = class extends KeqCacheStorage {
|
|
|
327
332
|
|
|
328
333
|
// src/storage/memory-storage/base-memory-storage.ts
|
|
329
334
|
var BaseMemoryStorage = class extends InternalStorage {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
335
|
+
constructor() {
|
|
336
|
+
super(...arguments);
|
|
337
|
+
__publicField(this, "storage", /* @__PURE__ */ new Map());
|
|
338
|
+
__publicField(this, "visitTimeRecords", /* @__PURE__ */ new Map());
|
|
339
|
+
__publicField(this, "visitCountRecords", /* @__PURE__ */ new Map());
|
|
340
|
+
__publicField(this, "lastEvictExpiredTime", (0, import_dayjs.default)());
|
|
341
|
+
}
|
|
333
342
|
get size() {
|
|
334
343
|
const used = R2.sum(R2.pluck("size", [...this.storage.values()]));
|
|
335
344
|
const free = this.__size__ > used ? this.__size__ - used : 0;
|
|
@@ -339,22 +348,24 @@ var BaseMemoryStorage = class extends InternalStorage {
|
|
|
339
348
|
};
|
|
340
349
|
}
|
|
341
350
|
get(key) {
|
|
351
|
+
var _a;
|
|
342
352
|
this.evictExpired();
|
|
343
353
|
const entry = this.storage.get(key);
|
|
344
|
-
this.visitCountRecords.set(key, (this.visitCountRecords.get(key)
|
|
354
|
+
this.visitCountRecords.set(key, ((_a = this.visitCountRecords.get(key)) != null ? _a : 0) + 1);
|
|
345
355
|
this.visitTimeRecords.set(key, /* @__PURE__ */ new Date());
|
|
346
|
-
if (!entry) this.debug((log) => log(
|
|
347
|
-
else this.debug((log) => log(
|
|
348
|
-
return entry
|
|
356
|
+
if (!entry) this.debug((log) => log("Entry(".concat(key, ") Not Found")));
|
|
357
|
+
else this.debug((log) => log("Entry(".concat(key, ") Found: "), entry));
|
|
358
|
+
return entry == null ? void 0 : entry.clone();
|
|
349
359
|
}
|
|
350
360
|
set(value) {
|
|
361
|
+
var _a;
|
|
351
362
|
if (!this.evict(value.size)) {
|
|
352
363
|
this.debug((log) => log("Storage Size Not Enough: ", this.size.free, " < ", value.size));
|
|
353
364
|
return;
|
|
354
365
|
}
|
|
355
366
|
this.storage.set(value.key, value);
|
|
356
367
|
this.visitTimeRecords.set(value.key, /* @__PURE__ */ new Date());
|
|
357
|
-
this.visitCountRecords.set(value.key, this.visitCountRecords.get(value.key)
|
|
368
|
+
this.visitCountRecords.set(value.key, (_a = this.visitCountRecords.get(value.key)) != null ? _a : 0);
|
|
358
369
|
this.debug((log) => log("Entry Added: ", value));
|
|
359
370
|
this.debug((log) => log("Storage Size: ", this.size));
|
|
360
371
|
}
|
|
@@ -372,11 +383,11 @@ var BaseMemoryStorage = class extends InternalStorage {
|
|
|
372
383
|
remove(key) {
|
|
373
384
|
this.__remove__([key]);
|
|
374
385
|
}
|
|
375
|
-
lastEvictExpiredTime = (0, import_dayjs.default)();
|
|
376
386
|
/**
|
|
377
387
|
* @zh 清除过期的缓存
|
|
378
388
|
*/
|
|
379
389
|
evictExpired() {
|
|
390
|
+
var _a;
|
|
380
391
|
const now = (0, import_dayjs.default)();
|
|
381
392
|
if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
|
|
382
393
|
const keys = [];
|
|
@@ -386,7 +397,7 @@ var BaseMemoryStorage = class extends InternalStorage {
|
|
|
386
397
|
}
|
|
387
398
|
}
|
|
388
399
|
this.__remove__(keys);
|
|
389
|
-
this.__onCacheExpired__
|
|
400
|
+
(_a = this.__onCacheExpired__) == null ? void 0 : _a.call(this, { keys });
|
|
390
401
|
}
|
|
391
402
|
/**
|
|
392
403
|
* @en Evict the storage to make sure the size is enough
|
|
@@ -407,19 +418,23 @@ var TTLMemoryStorage = class extends BaseMemoryStorage {
|
|
|
407
418
|
super(options);
|
|
408
419
|
}
|
|
409
420
|
get(key) {
|
|
421
|
+
var _a;
|
|
410
422
|
const entry = super.get(key);
|
|
411
|
-
this.__onCacheGet__
|
|
423
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
412
424
|
return entry;
|
|
413
425
|
}
|
|
414
426
|
set(value) {
|
|
427
|
+
var _a;
|
|
415
428
|
super.set(value);
|
|
416
|
-
this.__onCacheSet__
|
|
429
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
417
430
|
}
|
|
418
431
|
remove(key) {
|
|
432
|
+
var _a;
|
|
419
433
|
super.remove(key);
|
|
420
|
-
this.__onCacheRemove__
|
|
434
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
421
435
|
}
|
|
422
436
|
evict(expectSize) {
|
|
437
|
+
var _a;
|
|
423
438
|
if (expectSize > this.__size__) {
|
|
424
439
|
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
425
440
|
return false;
|
|
@@ -443,7 +458,7 @@ var TTLMemoryStorage = class extends BaseMemoryStorage {
|
|
|
443
458
|
keys.push(entry.key);
|
|
444
459
|
}
|
|
445
460
|
this.__remove__(keys);
|
|
446
|
-
this.__onCacheEvict__
|
|
461
|
+
(_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
|
|
447
462
|
return true;
|
|
448
463
|
}
|
|
449
464
|
};
|
|
@@ -459,19 +474,23 @@ var RandomMemoryStorage = class extends BaseMemoryStorage {
|
|
|
459
474
|
super(options);
|
|
460
475
|
}
|
|
461
476
|
get(key) {
|
|
477
|
+
var _a;
|
|
462
478
|
const entry = super.get(key);
|
|
463
|
-
this.__onCacheGet__
|
|
479
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
464
480
|
return entry;
|
|
465
481
|
}
|
|
466
482
|
set(value) {
|
|
483
|
+
var _a;
|
|
467
484
|
super.set(value);
|
|
468
|
-
this.__onCacheSet__
|
|
485
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
469
486
|
}
|
|
470
487
|
remove(key) {
|
|
488
|
+
var _a;
|
|
471
489
|
super.remove(key);
|
|
472
|
-
this.__onCacheRemove__
|
|
490
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
473
491
|
}
|
|
474
492
|
evict(expectSize) {
|
|
493
|
+
var _a;
|
|
475
494
|
if (expectSize > this.__size__) {
|
|
476
495
|
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
477
496
|
return false;
|
|
@@ -489,7 +508,7 @@ var RandomMemoryStorage = class extends BaseMemoryStorage {
|
|
|
489
508
|
keys.push(entry.key);
|
|
490
509
|
}
|
|
491
510
|
this.__remove__(keys);
|
|
492
|
-
this.__onCacheEvict__
|
|
511
|
+
(_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
|
|
493
512
|
return true;
|
|
494
513
|
}
|
|
495
514
|
};
|
|
@@ -501,19 +520,23 @@ var LRUMemoryStorage = class extends BaseMemoryStorage {
|
|
|
501
520
|
super(options);
|
|
502
521
|
}
|
|
503
522
|
get(key) {
|
|
523
|
+
var _a;
|
|
504
524
|
const entry = super.get(key);
|
|
505
|
-
this.__onCacheGet__
|
|
525
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
506
526
|
return entry;
|
|
507
527
|
}
|
|
508
528
|
set(value) {
|
|
529
|
+
var _a;
|
|
509
530
|
super.set(value);
|
|
510
|
-
this.__onCacheSet__
|
|
531
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
511
532
|
}
|
|
512
533
|
remove(key) {
|
|
534
|
+
var _a;
|
|
513
535
|
super.remove(key);
|
|
514
|
-
this.__onCacheRemove__
|
|
536
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
515
537
|
}
|
|
516
538
|
evict(expectSize) {
|
|
539
|
+
var _a;
|
|
517
540
|
if (expectSize > this.__size__) {
|
|
518
541
|
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
519
542
|
return false;
|
|
@@ -536,7 +559,7 @@ var LRUMemoryStorage = class extends BaseMemoryStorage {
|
|
|
536
559
|
keys.push(entry.key);
|
|
537
560
|
}
|
|
538
561
|
this.__remove__(keys);
|
|
539
|
-
this.__onCacheEvict__
|
|
562
|
+
(_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
|
|
540
563
|
return true;
|
|
541
564
|
}
|
|
542
565
|
};
|
|
@@ -547,19 +570,23 @@ var LFUMemoryStorage = class extends BaseMemoryStorage {
|
|
|
547
570
|
super(options);
|
|
548
571
|
}
|
|
549
572
|
get(key) {
|
|
573
|
+
var _a;
|
|
550
574
|
const entry = super.get(key);
|
|
551
|
-
this.__onCacheGet__
|
|
575
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
552
576
|
return entry;
|
|
553
577
|
}
|
|
554
578
|
set(value) {
|
|
579
|
+
var _a;
|
|
555
580
|
super.set(value);
|
|
556
|
-
this.__onCacheSet__
|
|
581
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
557
582
|
}
|
|
558
583
|
remove(key) {
|
|
584
|
+
var _a;
|
|
559
585
|
super.remove(key);
|
|
560
|
-
this.__onCacheRemove__
|
|
586
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
561
587
|
}
|
|
562
588
|
evict(expectSize) {
|
|
589
|
+
var _a;
|
|
563
590
|
if (expectSize > this.__size__) {
|
|
564
591
|
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
565
592
|
return false;
|
|
@@ -579,17 +606,17 @@ var LFUMemoryStorage = class extends BaseMemoryStorage {
|
|
|
579
606
|
keys.push(entry.key);
|
|
580
607
|
}
|
|
581
608
|
this.__remove__(keys);
|
|
582
|
-
this.__onCacheEvict__
|
|
609
|
+
(_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
|
|
583
610
|
return true;
|
|
584
611
|
}
|
|
585
612
|
};
|
|
586
613
|
|
|
587
614
|
// src/storage/memory-storage/memory-storage.ts
|
|
588
615
|
var MemoryStorage = class extends KeqCacheStorage {
|
|
589
|
-
storage;
|
|
590
616
|
constructor(options) {
|
|
591
617
|
super();
|
|
592
|
-
|
|
618
|
+
__publicField(this, "storage");
|
|
619
|
+
const eviction = (options == null ? void 0 : options.eviction) || "lru" /* LRU */;
|
|
593
620
|
if (eviction === "ttl" /* TTL */) {
|
|
594
621
|
this.storage = new TTLMemoryStorage(options);
|
|
595
622
|
} else if (eviction === "random" /* RANDOM */) {
|
|
@@ -599,7 +626,7 @@ var MemoryStorage = class extends KeqCacheStorage {
|
|
|
599
626
|
} else if (eviction === "lfu" /* LFU */) {
|
|
600
627
|
this.storage = new LFUMemoryStorage(options);
|
|
601
628
|
} else {
|
|
602
|
-
throw new TypeError(
|
|
629
|
+
throw new TypeError("Invalid eviction: ".concat(String(eviction)));
|
|
603
630
|
}
|
|
604
631
|
}
|
|
605
632
|
set(entry) {
|
|
@@ -626,14 +653,15 @@ var DEFAULT_TABLE_NAME = "keq_cache_indexed_db_storage";
|
|
|
626
653
|
|
|
627
654
|
// src/storage/indexed-db-storage/base-indexed-db-storage.ts
|
|
628
655
|
var BaseIndexedDBStorage = class extends InternalStorage {
|
|
629
|
-
tableName = DEFAULT_TABLE_NAME;
|
|
630
|
-
db;
|
|
631
656
|
constructor(options) {
|
|
632
657
|
super(options);
|
|
633
|
-
|
|
634
|
-
|
|
658
|
+
__publicField(this, "tableName", DEFAULT_TABLE_NAME);
|
|
659
|
+
__publicField(this, "db");
|
|
660
|
+
__publicField(this, "lastEvictExpiredTime", (0, import_dayjs4.default)());
|
|
661
|
+
if ((options == null ? void 0 : options.tableName) === DEFAULT_TABLE_NAME) {
|
|
662
|
+
throw new TypeError('[keq-cache] IndexedDBStorage name cannot be "'.concat(DEFAULT_TABLE_NAME, '"'));
|
|
635
663
|
}
|
|
636
|
-
this.tableName = options
|
|
664
|
+
this.tableName = (options == null ? void 0 : options.tableName) || DEFAULT_TABLE_NAME;
|
|
637
665
|
}
|
|
638
666
|
async openDB() {
|
|
639
667
|
if (this.db) return this.db;
|
|
@@ -655,13 +683,13 @@ var BaseIndexedDBStorage = class extends InternalStorage {
|
|
|
655
683
|
}
|
|
656
684
|
},
|
|
657
685
|
blocked() {
|
|
658
|
-
console.error(
|
|
686
|
+
console.error("IndexedDB Table ".concat(tableName, " is blocked"));
|
|
659
687
|
},
|
|
660
688
|
blocking() {
|
|
661
|
-
console.error(
|
|
689
|
+
console.error("IndexedDB Table ".concat(tableName, " is blocking"));
|
|
662
690
|
},
|
|
663
691
|
terminated() {
|
|
664
|
-
console.error(
|
|
692
|
+
console.error("IndexedDB Table ".concat(tableName, " is terminated"));
|
|
665
693
|
}
|
|
666
694
|
});
|
|
667
695
|
this.db = db;
|
|
@@ -706,7 +734,7 @@ var BaseIndexedDBStorage = class extends InternalStorage {
|
|
|
706
734
|
try {
|
|
707
735
|
if (!await this.evict(entry.size)) {
|
|
708
736
|
const size = await this.getSize();
|
|
709
|
-
this.debug((log) => log(
|
|
737
|
+
this.debug((log) => log("Storage Size Not Enough: ".concat(size.free, " < ").concat(entry.size)));
|
|
710
738
|
return;
|
|
711
739
|
}
|
|
712
740
|
const dbMetadata = {
|
|
@@ -765,11 +793,11 @@ var BaseIndexedDBStorage = class extends InternalStorage {
|
|
|
765
793
|
return;
|
|
766
794
|
}
|
|
767
795
|
}
|
|
768
|
-
lastEvictExpiredTime = (0, import_dayjs4.default)();
|
|
769
796
|
/**
|
|
770
797
|
* @zh 清除过期的缓存
|
|
771
798
|
*/
|
|
772
799
|
async evictExpired() {
|
|
800
|
+
var _a;
|
|
773
801
|
const now = (0, import_dayjs4.default)();
|
|
774
802
|
if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
|
|
775
803
|
try {
|
|
@@ -789,7 +817,7 @@ var BaseIndexedDBStorage = class extends InternalStorage {
|
|
|
789
817
|
}
|
|
790
818
|
await this.__remove__(tx, expiredKeys);
|
|
791
819
|
await tx.done;
|
|
792
|
-
this.__onCacheExpired__
|
|
820
|
+
(_a = this.__onCacheExpired__) == null ? void 0 : _a.call(this, { keys: expiredKeys });
|
|
793
821
|
} catch (error) {
|
|
794
822
|
return;
|
|
795
823
|
}
|
|
@@ -802,19 +830,23 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
802
830
|
super(options);
|
|
803
831
|
}
|
|
804
832
|
async get(key) {
|
|
833
|
+
var _a;
|
|
805
834
|
const entry = await super.get(key);
|
|
806
|
-
this.__onCacheGet__
|
|
835
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
807
836
|
return entry;
|
|
808
837
|
}
|
|
809
838
|
async set(value) {
|
|
839
|
+
var _a;
|
|
810
840
|
await super.set(value);
|
|
811
|
-
this.__onCacheSet__
|
|
841
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
812
842
|
}
|
|
813
843
|
async remove(key) {
|
|
844
|
+
var _a;
|
|
814
845
|
await super.remove(key);
|
|
815
|
-
this.__onCacheRemove__
|
|
846
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
816
847
|
}
|
|
817
848
|
async evict(expectSize) {
|
|
849
|
+
var _a;
|
|
818
850
|
await this.evictExpired();
|
|
819
851
|
const size = await this.getSize();
|
|
820
852
|
let deficitSize = expectSize - size.free;
|
|
@@ -825,7 +857,7 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
825
857
|
const metadatas = await metadataStore.getAll();
|
|
826
858
|
const totalSize = R5.sum(metadatas.map((m) => m.size));
|
|
827
859
|
if (totalSize < deficitSize) {
|
|
828
|
-
this.debug((log) => log(
|
|
860
|
+
this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize - totalSize)));
|
|
829
861
|
await tx.abort();
|
|
830
862
|
return false;
|
|
831
863
|
}
|
|
@@ -839,7 +871,7 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
839
871
|
}
|
|
840
872
|
await this.__remove__(tx, keys);
|
|
841
873
|
await tx.done;
|
|
842
|
-
await this.__onCacheEvict__
|
|
874
|
+
await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
|
|
843
875
|
return true;
|
|
844
876
|
}
|
|
845
877
|
};
|
|
@@ -850,19 +882,23 @@ var LFUIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
850
882
|
super(options);
|
|
851
883
|
}
|
|
852
884
|
async get(key) {
|
|
885
|
+
var _a;
|
|
853
886
|
const entry = await super.get(key);
|
|
854
|
-
this.__onCacheGet__
|
|
887
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
855
888
|
return entry;
|
|
856
889
|
}
|
|
857
890
|
async set(value) {
|
|
891
|
+
var _a;
|
|
858
892
|
await super.set(value);
|
|
859
|
-
this.__onCacheSet__
|
|
893
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
860
894
|
}
|
|
861
895
|
async remove(key) {
|
|
896
|
+
var _a;
|
|
862
897
|
await super.remove(key);
|
|
863
|
-
this.__onCacheRemove__
|
|
898
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
864
899
|
}
|
|
865
900
|
async evict(expectSize) {
|
|
901
|
+
var _a;
|
|
866
902
|
await this.evictExpired();
|
|
867
903
|
const size = await this.getSize();
|
|
868
904
|
let deficitSize = expectSize - size.free;
|
|
@@ -885,13 +921,13 @@ var LFUIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
885
921
|
cursor = await cursor.continue();
|
|
886
922
|
}
|
|
887
923
|
if (deficitSize > 0) {
|
|
888
|
-
this.debug((log) => log(
|
|
924
|
+
this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
|
|
889
925
|
await tx.abort;
|
|
890
926
|
return false;
|
|
891
927
|
}
|
|
892
928
|
await this.__remove__(tx, keys);
|
|
893
929
|
await tx.done;
|
|
894
|
-
this.__onCacheEvict__
|
|
930
|
+
(_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
|
|
895
931
|
return true;
|
|
896
932
|
}
|
|
897
933
|
};
|
|
@@ -902,19 +938,23 @@ var LRUIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
902
938
|
super(options);
|
|
903
939
|
}
|
|
904
940
|
async get(key) {
|
|
941
|
+
var _a;
|
|
905
942
|
const entry = await super.get(key);
|
|
906
|
-
this.__onCacheGet__
|
|
943
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
907
944
|
return entry;
|
|
908
945
|
}
|
|
909
946
|
async set(value) {
|
|
947
|
+
var _a;
|
|
910
948
|
await super.set(value);
|
|
911
|
-
this.__onCacheSet__
|
|
949
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
912
950
|
}
|
|
913
951
|
async remove(key) {
|
|
952
|
+
var _a;
|
|
914
953
|
await super.remove(key);
|
|
915
|
-
this.__onCacheRemove__
|
|
954
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
916
955
|
}
|
|
917
956
|
async evict(expectSize) {
|
|
957
|
+
var _a;
|
|
918
958
|
await this.evictExpired();
|
|
919
959
|
const size = await this.getSize();
|
|
920
960
|
let deficitSize = expectSize - size.free;
|
|
@@ -937,13 +977,13 @@ var LRUIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
937
977
|
cursor = await cursor.continue();
|
|
938
978
|
}
|
|
939
979
|
if (deficitSize > 0) {
|
|
940
|
-
this.debug((log) => log(
|
|
980
|
+
this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
|
|
941
981
|
await tx.abort();
|
|
942
982
|
return false;
|
|
943
983
|
}
|
|
944
984
|
await this.__remove__(tx, keys);
|
|
945
985
|
await tx.done;
|
|
946
|
-
await this.__onCacheEvict__
|
|
986
|
+
await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
|
|
947
987
|
return true;
|
|
948
988
|
}
|
|
949
989
|
};
|
|
@@ -954,19 +994,23 @@ var TTLIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
954
994
|
super(options);
|
|
955
995
|
}
|
|
956
996
|
async get(key) {
|
|
997
|
+
var _a;
|
|
957
998
|
const entry = await super.get(key);
|
|
958
|
-
this.__onCacheGet__
|
|
999
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
959
1000
|
return entry;
|
|
960
1001
|
}
|
|
961
1002
|
async set(value) {
|
|
1003
|
+
var _a;
|
|
962
1004
|
await super.set(value);
|
|
963
|
-
this.__onCacheSet__
|
|
1005
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
964
1006
|
}
|
|
965
1007
|
async remove(key) {
|
|
1008
|
+
var _a;
|
|
966
1009
|
await super.remove(key);
|
|
967
|
-
this.__onCacheRemove__
|
|
1010
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
968
1011
|
}
|
|
969
1012
|
async evict(expectSize) {
|
|
1013
|
+
var _a;
|
|
970
1014
|
await this.evictExpired();
|
|
971
1015
|
const size = await this.getSize();
|
|
972
1016
|
let deficitSize = expectSize - size.free;
|
|
@@ -988,23 +1032,23 @@ var TTLIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
988
1032
|
cursor = await cursor.continue();
|
|
989
1033
|
}
|
|
990
1034
|
if (deficitSize > 0) {
|
|
991
|
-
this.debug((log) => log(
|
|
1035
|
+
this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
|
|
992
1036
|
await tx.abort();
|
|
993
1037
|
return false;
|
|
994
1038
|
}
|
|
995
1039
|
await this.__remove__(tx, keys);
|
|
996
1040
|
await tx.done;
|
|
997
|
-
await this.__onCacheEvict__
|
|
1041
|
+
await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
|
|
998
1042
|
return true;
|
|
999
1043
|
}
|
|
1000
1044
|
};
|
|
1001
1045
|
|
|
1002
1046
|
// src/storage/indexed-db-storage/indexed-db-storage.ts
|
|
1003
1047
|
var IndexedDBStorage = class extends KeqCacheStorage {
|
|
1004
|
-
storage;
|
|
1005
1048
|
constructor(options) {
|
|
1006
1049
|
super();
|
|
1007
|
-
|
|
1050
|
+
__publicField(this, "storage");
|
|
1051
|
+
const eviction = (options == null ? void 0 : options.eviction) || "lru" /* LRU */;
|
|
1008
1052
|
if (eviction === "random" /* RANDOM */) {
|
|
1009
1053
|
this.storage = new RandomIndexedDBStorage(options);
|
|
1010
1054
|
} else if (eviction === "lfu" /* LFU */) {
|
|
@@ -1014,7 +1058,7 @@ var IndexedDBStorage = class extends KeqCacheStorage {
|
|
|
1014
1058
|
} else if (eviction === "ttl" /* TTL */) {
|
|
1015
1059
|
this.storage = new TTLIndexedDBStorage(options);
|
|
1016
1060
|
} else {
|
|
1017
|
-
throw TypeError(
|
|
1061
|
+
throw TypeError("Not Supported Eviction: ".concat(String(options == null ? void 0 : options.eviction)));
|
|
1018
1062
|
}
|
|
1019
1063
|
}
|
|
1020
1064
|
set(entry) {
|
|
@@ -1030,13 +1074,13 @@ var IndexedDBStorage = class extends KeqCacheStorage {
|
|
|
1030
1074
|
|
|
1031
1075
|
// src/storage/multi-tier-storage/multi-tier-storage.ts
|
|
1032
1076
|
var MultiTierStorage = class extends KeqCacheStorage {
|
|
1033
|
-
storages;
|
|
1034
1077
|
/**
|
|
1035
1078
|
* @param storages Array of storage instances ordered by performance (fastest first)
|
|
1036
1079
|
* @zh 按性价比排序的存储实例数组,排在前面的成本低,排在后面的成本高
|
|
1037
1080
|
*/
|
|
1038
1081
|
constructor(options) {
|
|
1039
1082
|
super();
|
|
1083
|
+
__publicField(this, "storages");
|
|
1040
1084
|
if (!options.tiers || options.tiers.length === 0) {
|
|
1041
1085
|
throw new Error("At least one storage instance is required");
|
|
1042
1086
|
}
|
|
@@ -1096,22 +1140,11 @@ var MultiTierStorage = class extends KeqCacheStorage {
|
|
|
1096
1140
|
// src/storage/tier-storage/tier-storage.ts
|
|
1097
1141
|
var TierStorage = class extends MultiTierStorage {
|
|
1098
1142
|
constructor(options) {
|
|
1099
|
-
const memoryStorage = options
|
|
1100
|
-
const indexedDBStorage = options
|
|
1143
|
+
const memoryStorage = (options == null ? void 0 : options.memory) instanceof MemoryStorage ? options.memory : new MemoryStorage(options == null ? void 0 : options.memory);
|
|
1144
|
+
const indexedDBStorage = (options == null ? void 0 : options.indexedDB) instanceof IndexedDBStorage ? options.indexedDB : new IndexedDBStorage(options == null ? void 0 : options.indexedDB);
|
|
1101
1145
|
super({
|
|
1102
1146
|
tiers: [memoryStorage, indexedDBStorage]
|
|
1103
1147
|
});
|
|
1104
1148
|
}
|
|
1105
1149
|
};
|
|
1106
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
1107
|
-
0 && (module.exports = {
|
|
1108
|
-
CacheStorage,
|
|
1109
|
-
Eviction,
|
|
1110
|
-
IndexedDBStorage,
|
|
1111
|
-
MemoryStorage,
|
|
1112
|
-
Size,
|
|
1113
|
-
Strategy,
|
|
1114
|
-
TierStorage,
|
|
1115
|
-
cache
|
|
1116
|
-
});
|
|
1117
1150
|
//# sourceMappingURL=index.js.map
|