@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/dist/index.mjs
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
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
|
|
10
|
+
const rules = (opts == null ? void 0 : opts.rules) || [];
|
|
7
11
|
return async function cache2(ctx, next) {
|
|
8
12
|
if (ctx.options.cache === false) {
|
|
9
13
|
await next();
|
|
@@ -24,7 +28,7 @@ function cache(opts) {
|
|
|
24
28
|
if (cOpt.key) {
|
|
25
29
|
if (typeof cOpt.key === "function") key = cOpt.key(ctx);
|
|
26
30
|
else key = cOpt.key;
|
|
27
|
-
} else if (opts
|
|
31
|
+
} else if (opts == null ? void 0 : opts.keyFactory) {
|
|
28
32
|
key = opts.keyFactory(ctx);
|
|
29
33
|
}
|
|
30
34
|
if (!key) throw new Exception("Cache key is required");
|
|
@@ -51,27 +55,29 @@ async function getResponseBytes(response) {
|
|
|
51
55
|
|
|
52
56
|
// src/cache-entry/cache-entry.ts
|
|
53
57
|
var CacheEntry = class _CacheEntry {
|
|
54
|
-
key;
|
|
55
|
-
response;
|
|
56
|
-
/**
|
|
57
|
-
* @en bytes
|
|
58
|
-
* @zh 字节数
|
|
59
|
-
*/
|
|
60
|
-
size;
|
|
61
|
-
expiredAt;
|
|
62
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;
|
|
63
68
|
this.key = options.key;
|
|
64
69
|
this.response = options.response;
|
|
65
70
|
this.size = options.size;
|
|
66
|
-
this.expiredAt = options.expiredAt
|
|
71
|
+
this.expiredAt = (_a = options.expiredAt) != null ? _a : /* @__PURE__ */ new Date(864e13);
|
|
67
72
|
}
|
|
68
73
|
static async build(options) {
|
|
74
|
+
var _a;
|
|
69
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);
|
|
70
76
|
const response = options.response.clone();
|
|
71
77
|
return new _CacheEntry({
|
|
72
78
|
key: options.key,
|
|
73
79
|
response,
|
|
74
|
-
size: options.size
|
|
80
|
+
size: (_a = options.size) != null ? _a : await getResponseBytes(response),
|
|
75
81
|
expiredAt
|
|
76
82
|
});
|
|
77
83
|
}
|
|
@@ -142,7 +148,7 @@ var networkFirst = function(opts) {
|
|
|
142
148
|
if (entry) {
|
|
143
149
|
context.emitter.emit("cache:update", {
|
|
144
150
|
key,
|
|
145
|
-
oldResponse: cache2
|
|
151
|
+
oldResponse: cache2 == null ? void 0 : cache2.response,
|
|
146
152
|
newResponse: entry.response,
|
|
147
153
|
context
|
|
148
154
|
});
|
|
@@ -252,31 +258,32 @@ var KeqCacheStorage = class {
|
|
|
252
258
|
|
|
253
259
|
// src/storage/internal-storage/internal-storage.ts
|
|
254
260
|
var InternalStorage = class extends KeqCacheStorage {
|
|
255
|
-
__id__ = Math.random().toString(36).slice(2);
|
|
256
|
-
__size__;
|
|
257
|
-
__debug__;
|
|
258
|
-
__onCacheGet__;
|
|
259
|
-
__onCacheSet__;
|
|
260
|
-
__onCacheRemove__;
|
|
261
|
-
__onCacheEvict__;
|
|
262
|
-
__onCacheExpired__;
|
|
263
261
|
constructor(options) {
|
|
262
|
+
var _a;
|
|
264
263
|
super();
|
|
265
|
-
|
|
266
|
-
|
|
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)));
|
|
267
274
|
}
|
|
268
|
-
this.__size__ = options
|
|
269
|
-
this.__debug__ = !!options
|
|
270
|
-
this.__onCacheGet__ = options
|
|
271
|
-
this.__onCacheSet__ = options
|
|
272
|
-
this.__onCacheRemove__ = options
|
|
273
|
-
this.__onCacheEvict__ = options
|
|
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;
|
|
274
281
|
this.debug((log) => log("Storage Created: ", this));
|
|
275
282
|
}
|
|
276
283
|
debug(fn) {
|
|
277
284
|
if (this.__debug__) {
|
|
278
285
|
fn((...args) => {
|
|
279
|
-
debug(
|
|
286
|
+
debug("[Storage(".concat(this.__id__, ")]"), ...args);
|
|
280
287
|
});
|
|
281
288
|
}
|
|
282
289
|
}
|
|
@@ -284,9 +291,13 @@ var InternalStorage = class extends KeqCacheStorage {
|
|
|
284
291
|
|
|
285
292
|
// src/storage/memory-storage/base-memory-storage.ts
|
|
286
293
|
var BaseMemoryStorage = class extends InternalStorage {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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
|
+
}
|
|
290
301
|
get size() {
|
|
291
302
|
const used = R2.sum(R2.pluck("size", [...this.storage.values()]));
|
|
292
303
|
const free = this.__size__ > used ? this.__size__ - used : 0;
|
|
@@ -296,22 +307,24 @@ var BaseMemoryStorage = class extends InternalStorage {
|
|
|
296
307
|
};
|
|
297
308
|
}
|
|
298
309
|
get(key) {
|
|
310
|
+
var _a;
|
|
299
311
|
this.evictExpired();
|
|
300
312
|
const entry = this.storage.get(key);
|
|
301
|
-
this.visitCountRecords.set(key, (this.visitCountRecords.get(key)
|
|
313
|
+
this.visitCountRecords.set(key, ((_a = this.visitCountRecords.get(key)) != null ? _a : 0) + 1);
|
|
302
314
|
this.visitTimeRecords.set(key, /* @__PURE__ */ new Date());
|
|
303
|
-
if (!entry) this.debug((log) => log(
|
|
304
|
-
else this.debug((log) => log(
|
|
305
|
-
return entry
|
|
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();
|
|
306
318
|
}
|
|
307
319
|
set(value) {
|
|
320
|
+
var _a;
|
|
308
321
|
if (!this.evict(value.size)) {
|
|
309
322
|
this.debug((log) => log("Storage Size Not Enough: ", this.size.free, " < ", value.size));
|
|
310
323
|
return;
|
|
311
324
|
}
|
|
312
325
|
this.storage.set(value.key, value);
|
|
313
326
|
this.visitTimeRecords.set(value.key, /* @__PURE__ */ new Date());
|
|
314
|
-
this.visitCountRecords.set(value.key, this.visitCountRecords.get(value.key)
|
|
327
|
+
this.visitCountRecords.set(value.key, (_a = this.visitCountRecords.get(value.key)) != null ? _a : 0);
|
|
315
328
|
this.debug((log) => log("Entry Added: ", value));
|
|
316
329
|
this.debug((log) => log("Storage Size: ", this.size));
|
|
317
330
|
}
|
|
@@ -329,11 +342,11 @@ var BaseMemoryStorage = class extends InternalStorage {
|
|
|
329
342
|
remove(key) {
|
|
330
343
|
this.__remove__([key]);
|
|
331
344
|
}
|
|
332
|
-
lastEvictExpiredTime = dayjs();
|
|
333
345
|
/**
|
|
334
346
|
* @zh 清除过期的缓存
|
|
335
347
|
*/
|
|
336
348
|
evictExpired() {
|
|
349
|
+
var _a;
|
|
337
350
|
const now = dayjs();
|
|
338
351
|
if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
|
|
339
352
|
const keys = [];
|
|
@@ -343,7 +356,7 @@ var BaseMemoryStorage = class extends InternalStorage {
|
|
|
343
356
|
}
|
|
344
357
|
}
|
|
345
358
|
this.__remove__(keys);
|
|
346
|
-
this.__onCacheExpired__
|
|
359
|
+
(_a = this.__onCacheExpired__) == null ? void 0 : _a.call(this, { keys });
|
|
347
360
|
}
|
|
348
361
|
/**
|
|
349
362
|
* @en Evict the storage to make sure the size is enough
|
|
@@ -364,19 +377,23 @@ var TTLMemoryStorage = class extends BaseMemoryStorage {
|
|
|
364
377
|
super(options);
|
|
365
378
|
}
|
|
366
379
|
get(key) {
|
|
380
|
+
var _a;
|
|
367
381
|
const entry = super.get(key);
|
|
368
|
-
this.__onCacheGet__
|
|
382
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
369
383
|
return entry;
|
|
370
384
|
}
|
|
371
385
|
set(value) {
|
|
386
|
+
var _a;
|
|
372
387
|
super.set(value);
|
|
373
|
-
this.__onCacheSet__
|
|
388
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
374
389
|
}
|
|
375
390
|
remove(key) {
|
|
391
|
+
var _a;
|
|
376
392
|
super.remove(key);
|
|
377
|
-
this.__onCacheRemove__
|
|
393
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
378
394
|
}
|
|
379
395
|
evict(expectSize) {
|
|
396
|
+
var _a;
|
|
380
397
|
if (expectSize > this.__size__) {
|
|
381
398
|
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
382
399
|
return false;
|
|
@@ -400,7 +417,7 @@ var TTLMemoryStorage = class extends BaseMemoryStorage {
|
|
|
400
417
|
keys.push(entry.key);
|
|
401
418
|
}
|
|
402
419
|
this.__remove__(keys);
|
|
403
|
-
this.__onCacheEvict__
|
|
420
|
+
(_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
|
|
404
421
|
return true;
|
|
405
422
|
}
|
|
406
423
|
};
|
|
@@ -416,19 +433,23 @@ var RandomMemoryStorage = class extends BaseMemoryStorage {
|
|
|
416
433
|
super(options);
|
|
417
434
|
}
|
|
418
435
|
get(key) {
|
|
436
|
+
var _a;
|
|
419
437
|
const entry = super.get(key);
|
|
420
|
-
this.__onCacheGet__
|
|
438
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
421
439
|
return entry;
|
|
422
440
|
}
|
|
423
441
|
set(value) {
|
|
442
|
+
var _a;
|
|
424
443
|
super.set(value);
|
|
425
|
-
this.__onCacheSet__
|
|
444
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
426
445
|
}
|
|
427
446
|
remove(key) {
|
|
447
|
+
var _a;
|
|
428
448
|
super.remove(key);
|
|
429
|
-
this.__onCacheRemove__
|
|
449
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
430
450
|
}
|
|
431
451
|
evict(expectSize) {
|
|
452
|
+
var _a;
|
|
432
453
|
if (expectSize > this.__size__) {
|
|
433
454
|
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
434
455
|
return false;
|
|
@@ -446,7 +467,7 @@ var RandomMemoryStorage = class extends BaseMemoryStorage {
|
|
|
446
467
|
keys.push(entry.key);
|
|
447
468
|
}
|
|
448
469
|
this.__remove__(keys);
|
|
449
|
-
this.__onCacheEvict__
|
|
470
|
+
(_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
|
|
450
471
|
return true;
|
|
451
472
|
}
|
|
452
473
|
};
|
|
@@ -458,19 +479,23 @@ var LRUMemoryStorage = class extends BaseMemoryStorage {
|
|
|
458
479
|
super(options);
|
|
459
480
|
}
|
|
460
481
|
get(key) {
|
|
482
|
+
var _a;
|
|
461
483
|
const entry = super.get(key);
|
|
462
|
-
this.__onCacheGet__
|
|
484
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
463
485
|
return entry;
|
|
464
486
|
}
|
|
465
487
|
set(value) {
|
|
488
|
+
var _a;
|
|
466
489
|
super.set(value);
|
|
467
|
-
this.__onCacheSet__
|
|
490
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
468
491
|
}
|
|
469
492
|
remove(key) {
|
|
493
|
+
var _a;
|
|
470
494
|
super.remove(key);
|
|
471
|
-
this.__onCacheRemove__
|
|
495
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
472
496
|
}
|
|
473
497
|
evict(expectSize) {
|
|
498
|
+
var _a;
|
|
474
499
|
if (expectSize > this.__size__) {
|
|
475
500
|
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
476
501
|
return false;
|
|
@@ -493,7 +518,7 @@ var LRUMemoryStorage = class extends BaseMemoryStorage {
|
|
|
493
518
|
keys.push(entry.key);
|
|
494
519
|
}
|
|
495
520
|
this.__remove__(keys);
|
|
496
|
-
this.__onCacheEvict__
|
|
521
|
+
(_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
|
|
497
522
|
return true;
|
|
498
523
|
}
|
|
499
524
|
};
|
|
@@ -504,19 +529,23 @@ var LFUMemoryStorage = class extends BaseMemoryStorage {
|
|
|
504
529
|
super(options);
|
|
505
530
|
}
|
|
506
531
|
get(key) {
|
|
532
|
+
var _a;
|
|
507
533
|
const entry = super.get(key);
|
|
508
|
-
this.__onCacheGet__
|
|
534
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
509
535
|
return entry;
|
|
510
536
|
}
|
|
511
537
|
set(value) {
|
|
538
|
+
var _a;
|
|
512
539
|
super.set(value);
|
|
513
|
-
this.__onCacheSet__
|
|
540
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
514
541
|
}
|
|
515
542
|
remove(key) {
|
|
543
|
+
var _a;
|
|
516
544
|
super.remove(key);
|
|
517
|
-
this.__onCacheRemove__
|
|
545
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
518
546
|
}
|
|
519
547
|
evict(expectSize) {
|
|
548
|
+
var _a;
|
|
520
549
|
if (expectSize > this.__size__) {
|
|
521
550
|
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
522
551
|
return false;
|
|
@@ -536,17 +565,17 @@ var LFUMemoryStorage = class extends BaseMemoryStorage {
|
|
|
536
565
|
keys.push(entry.key);
|
|
537
566
|
}
|
|
538
567
|
this.__remove__(keys);
|
|
539
|
-
this.__onCacheEvict__
|
|
568
|
+
(_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
|
|
540
569
|
return true;
|
|
541
570
|
}
|
|
542
571
|
};
|
|
543
572
|
|
|
544
573
|
// src/storage/memory-storage/memory-storage.ts
|
|
545
574
|
var MemoryStorage = class extends KeqCacheStorage {
|
|
546
|
-
storage;
|
|
547
575
|
constructor(options) {
|
|
548
576
|
super();
|
|
549
|
-
|
|
577
|
+
__publicField(this, "storage");
|
|
578
|
+
const eviction = (options == null ? void 0 : options.eviction) || "lru" /* LRU */;
|
|
550
579
|
if (eviction === "ttl" /* TTL */) {
|
|
551
580
|
this.storage = new TTLMemoryStorage(options);
|
|
552
581
|
} else if (eviction === "random" /* RANDOM */) {
|
|
@@ -556,7 +585,7 @@ var MemoryStorage = class extends KeqCacheStorage {
|
|
|
556
585
|
} else if (eviction === "lfu" /* LFU */) {
|
|
557
586
|
this.storage = new LFUMemoryStorage(options);
|
|
558
587
|
} else {
|
|
559
|
-
throw new TypeError(
|
|
588
|
+
throw new TypeError("Invalid eviction: ".concat(String(eviction)));
|
|
560
589
|
}
|
|
561
590
|
}
|
|
562
591
|
set(entry) {
|
|
@@ -583,14 +612,15 @@ var DEFAULT_TABLE_NAME = "keq_cache_indexed_db_storage";
|
|
|
583
612
|
|
|
584
613
|
// src/storage/indexed-db-storage/base-indexed-db-storage.ts
|
|
585
614
|
var BaseIndexedDBStorage = class extends InternalStorage {
|
|
586
|
-
tableName = DEFAULT_TABLE_NAME;
|
|
587
|
-
db;
|
|
588
615
|
constructor(options) {
|
|
589
616
|
super(options);
|
|
590
|
-
|
|
591
|
-
|
|
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, '"'));
|
|
592
622
|
}
|
|
593
|
-
this.tableName = options
|
|
623
|
+
this.tableName = (options == null ? void 0 : options.tableName) || DEFAULT_TABLE_NAME;
|
|
594
624
|
}
|
|
595
625
|
async openDB() {
|
|
596
626
|
if (this.db) return this.db;
|
|
@@ -612,13 +642,13 @@ var BaseIndexedDBStorage = class extends InternalStorage {
|
|
|
612
642
|
}
|
|
613
643
|
},
|
|
614
644
|
blocked() {
|
|
615
|
-
console.error(
|
|
645
|
+
console.error("IndexedDB Table ".concat(tableName, " is blocked"));
|
|
616
646
|
},
|
|
617
647
|
blocking() {
|
|
618
|
-
console.error(
|
|
648
|
+
console.error("IndexedDB Table ".concat(tableName, " is blocking"));
|
|
619
649
|
},
|
|
620
650
|
terminated() {
|
|
621
|
-
console.error(
|
|
651
|
+
console.error("IndexedDB Table ".concat(tableName, " is terminated"));
|
|
622
652
|
}
|
|
623
653
|
});
|
|
624
654
|
this.db = db;
|
|
@@ -663,7 +693,7 @@ var BaseIndexedDBStorage = class extends InternalStorage {
|
|
|
663
693
|
try {
|
|
664
694
|
if (!await this.evict(entry.size)) {
|
|
665
695
|
const size = await this.getSize();
|
|
666
|
-
this.debug((log) => log(
|
|
696
|
+
this.debug((log) => log("Storage Size Not Enough: ".concat(size.free, " < ").concat(entry.size)));
|
|
667
697
|
return;
|
|
668
698
|
}
|
|
669
699
|
const dbMetadata = {
|
|
@@ -722,11 +752,11 @@ var BaseIndexedDBStorage = class extends InternalStorage {
|
|
|
722
752
|
return;
|
|
723
753
|
}
|
|
724
754
|
}
|
|
725
|
-
lastEvictExpiredTime = dayjs4();
|
|
726
755
|
/**
|
|
727
756
|
* @zh 清除过期的缓存
|
|
728
757
|
*/
|
|
729
758
|
async evictExpired() {
|
|
759
|
+
var _a;
|
|
730
760
|
const now = dayjs4();
|
|
731
761
|
if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
|
|
732
762
|
try {
|
|
@@ -746,7 +776,7 @@ var BaseIndexedDBStorage = class extends InternalStorage {
|
|
|
746
776
|
}
|
|
747
777
|
await this.__remove__(tx, expiredKeys);
|
|
748
778
|
await tx.done;
|
|
749
|
-
this.__onCacheExpired__
|
|
779
|
+
(_a = this.__onCacheExpired__) == null ? void 0 : _a.call(this, { keys: expiredKeys });
|
|
750
780
|
} catch (error) {
|
|
751
781
|
return;
|
|
752
782
|
}
|
|
@@ -759,19 +789,23 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
759
789
|
super(options);
|
|
760
790
|
}
|
|
761
791
|
async get(key) {
|
|
792
|
+
var _a;
|
|
762
793
|
const entry = await super.get(key);
|
|
763
|
-
this.__onCacheGet__
|
|
794
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
764
795
|
return entry;
|
|
765
796
|
}
|
|
766
797
|
async set(value) {
|
|
798
|
+
var _a;
|
|
767
799
|
await super.set(value);
|
|
768
|
-
this.__onCacheSet__
|
|
800
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
769
801
|
}
|
|
770
802
|
async remove(key) {
|
|
803
|
+
var _a;
|
|
771
804
|
await super.remove(key);
|
|
772
|
-
this.__onCacheRemove__
|
|
805
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
773
806
|
}
|
|
774
807
|
async evict(expectSize) {
|
|
808
|
+
var _a;
|
|
775
809
|
await this.evictExpired();
|
|
776
810
|
const size = await this.getSize();
|
|
777
811
|
let deficitSize = expectSize - size.free;
|
|
@@ -782,7 +816,7 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
782
816
|
const metadatas = await metadataStore.getAll();
|
|
783
817
|
const totalSize = R5.sum(metadatas.map((m) => m.size));
|
|
784
818
|
if (totalSize < deficitSize) {
|
|
785
|
-
this.debug((log) => log(
|
|
819
|
+
this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize - totalSize)));
|
|
786
820
|
await tx.abort();
|
|
787
821
|
return false;
|
|
788
822
|
}
|
|
@@ -796,7 +830,7 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
796
830
|
}
|
|
797
831
|
await this.__remove__(tx, keys);
|
|
798
832
|
await tx.done;
|
|
799
|
-
await this.__onCacheEvict__
|
|
833
|
+
await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
|
|
800
834
|
return true;
|
|
801
835
|
}
|
|
802
836
|
};
|
|
@@ -807,19 +841,23 @@ var LFUIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
807
841
|
super(options);
|
|
808
842
|
}
|
|
809
843
|
async get(key) {
|
|
844
|
+
var _a;
|
|
810
845
|
const entry = await super.get(key);
|
|
811
|
-
this.__onCacheGet__
|
|
846
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
812
847
|
return entry;
|
|
813
848
|
}
|
|
814
849
|
async set(value) {
|
|
850
|
+
var _a;
|
|
815
851
|
await super.set(value);
|
|
816
|
-
this.__onCacheSet__
|
|
852
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
817
853
|
}
|
|
818
854
|
async remove(key) {
|
|
855
|
+
var _a;
|
|
819
856
|
await super.remove(key);
|
|
820
|
-
this.__onCacheRemove__
|
|
857
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
821
858
|
}
|
|
822
859
|
async evict(expectSize) {
|
|
860
|
+
var _a;
|
|
823
861
|
await this.evictExpired();
|
|
824
862
|
const size = await this.getSize();
|
|
825
863
|
let deficitSize = expectSize - size.free;
|
|
@@ -842,13 +880,13 @@ var LFUIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
842
880
|
cursor = await cursor.continue();
|
|
843
881
|
}
|
|
844
882
|
if (deficitSize > 0) {
|
|
845
|
-
this.debug((log) => log(
|
|
883
|
+
this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
|
|
846
884
|
await tx.abort;
|
|
847
885
|
return false;
|
|
848
886
|
}
|
|
849
887
|
await this.__remove__(tx, keys);
|
|
850
888
|
await tx.done;
|
|
851
|
-
this.__onCacheEvict__
|
|
889
|
+
(_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
|
|
852
890
|
return true;
|
|
853
891
|
}
|
|
854
892
|
};
|
|
@@ -859,19 +897,23 @@ var LRUIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
859
897
|
super(options);
|
|
860
898
|
}
|
|
861
899
|
async get(key) {
|
|
900
|
+
var _a;
|
|
862
901
|
const entry = await super.get(key);
|
|
863
|
-
this.__onCacheGet__
|
|
902
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
864
903
|
return entry;
|
|
865
904
|
}
|
|
866
905
|
async set(value) {
|
|
906
|
+
var _a;
|
|
867
907
|
await super.set(value);
|
|
868
|
-
this.__onCacheSet__
|
|
908
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
869
909
|
}
|
|
870
910
|
async remove(key) {
|
|
911
|
+
var _a;
|
|
871
912
|
await super.remove(key);
|
|
872
|
-
this.__onCacheRemove__
|
|
913
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
873
914
|
}
|
|
874
915
|
async evict(expectSize) {
|
|
916
|
+
var _a;
|
|
875
917
|
await this.evictExpired();
|
|
876
918
|
const size = await this.getSize();
|
|
877
919
|
let deficitSize = expectSize - size.free;
|
|
@@ -894,13 +936,13 @@ var LRUIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
894
936
|
cursor = await cursor.continue();
|
|
895
937
|
}
|
|
896
938
|
if (deficitSize > 0) {
|
|
897
|
-
this.debug((log) => log(
|
|
939
|
+
this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
|
|
898
940
|
await tx.abort();
|
|
899
941
|
return false;
|
|
900
942
|
}
|
|
901
943
|
await this.__remove__(tx, keys);
|
|
902
944
|
await tx.done;
|
|
903
|
-
await this.__onCacheEvict__
|
|
945
|
+
await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
|
|
904
946
|
return true;
|
|
905
947
|
}
|
|
906
948
|
};
|
|
@@ -911,19 +953,23 @@ var TTLIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
911
953
|
super(options);
|
|
912
954
|
}
|
|
913
955
|
async get(key) {
|
|
956
|
+
var _a;
|
|
914
957
|
const entry = await super.get(key);
|
|
915
|
-
this.__onCacheGet__
|
|
958
|
+
(_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
|
|
916
959
|
return entry;
|
|
917
960
|
}
|
|
918
961
|
async set(value) {
|
|
962
|
+
var _a;
|
|
919
963
|
await super.set(value);
|
|
920
|
-
this.__onCacheSet__
|
|
964
|
+
(_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
|
|
921
965
|
}
|
|
922
966
|
async remove(key) {
|
|
967
|
+
var _a;
|
|
923
968
|
await super.remove(key);
|
|
924
|
-
this.__onCacheRemove__
|
|
969
|
+
(_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
|
|
925
970
|
}
|
|
926
971
|
async evict(expectSize) {
|
|
972
|
+
var _a;
|
|
927
973
|
await this.evictExpired();
|
|
928
974
|
const size = await this.getSize();
|
|
929
975
|
let deficitSize = expectSize - size.free;
|
|
@@ -945,23 +991,23 @@ var TTLIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
|
945
991
|
cursor = await cursor.continue();
|
|
946
992
|
}
|
|
947
993
|
if (deficitSize > 0) {
|
|
948
|
-
this.debug((log) => log(
|
|
994
|
+
this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
|
|
949
995
|
await tx.abort();
|
|
950
996
|
return false;
|
|
951
997
|
}
|
|
952
998
|
await this.__remove__(tx, keys);
|
|
953
999
|
await tx.done;
|
|
954
|
-
await this.__onCacheEvict__
|
|
1000
|
+
await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
|
|
955
1001
|
return true;
|
|
956
1002
|
}
|
|
957
1003
|
};
|
|
958
1004
|
|
|
959
1005
|
// src/storage/indexed-db-storage/indexed-db-storage.ts
|
|
960
1006
|
var IndexedDBStorage = class extends KeqCacheStorage {
|
|
961
|
-
storage;
|
|
962
1007
|
constructor(options) {
|
|
963
1008
|
super();
|
|
964
|
-
|
|
1009
|
+
__publicField(this, "storage");
|
|
1010
|
+
const eviction = (options == null ? void 0 : options.eviction) || "lru" /* LRU */;
|
|
965
1011
|
if (eviction === "random" /* RANDOM */) {
|
|
966
1012
|
this.storage = new RandomIndexedDBStorage(options);
|
|
967
1013
|
} else if (eviction === "lfu" /* LFU */) {
|
|
@@ -971,7 +1017,7 @@ var IndexedDBStorage = class extends KeqCacheStorage {
|
|
|
971
1017
|
} else if (eviction === "ttl" /* TTL */) {
|
|
972
1018
|
this.storage = new TTLIndexedDBStorage(options);
|
|
973
1019
|
} else {
|
|
974
|
-
throw TypeError(
|
|
1020
|
+
throw TypeError("Not Supported Eviction: ".concat(String(options == null ? void 0 : options.eviction)));
|
|
975
1021
|
}
|
|
976
1022
|
}
|
|
977
1023
|
set(entry) {
|
|
@@ -987,13 +1033,13 @@ var IndexedDBStorage = class extends KeqCacheStorage {
|
|
|
987
1033
|
|
|
988
1034
|
// src/storage/multi-tier-storage/multi-tier-storage.ts
|
|
989
1035
|
var MultiTierStorage = class extends KeqCacheStorage {
|
|
990
|
-
storages;
|
|
991
1036
|
/**
|
|
992
1037
|
* @param storages Array of storage instances ordered by performance (fastest first)
|
|
993
1038
|
* @zh 按性价比排序的存储实例数组,排在前面的成本低,排在后面的成本高
|
|
994
1039
|
*/
|
|
995
1040
|
constructor(options) {
|
|
996
1041
|
super();
|
|
1042
|
+
__publicField(this, "storages");
|
|
997
1043
|
if (!options.tiers || options.tiers.length === 0) {
|
|
998
1044
|
throw new Error("At least one storage instance is required");
|
|
999
1045
|
}
|
|
@@ -1053,8 +1099,8 @@ var MultiTierStorage = class extends KeqCacheStorage {
|
|
|
1053
1099
|
// src/storage/tier-storage/tier-storage.ts
|
|
1054
1100
|
var TierStorage = class extends MultiTierStorage {
|
|
1055
1101
|
constructor(options) {
|
|
1056
|
-
const memoryStorage = options
|
|
1057
|
-
const indexedDBStorage = options
|
|
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);
|
|
1058
1104
|
super({
|
|
1059
1105
|
tiers: [memoryStorage, indexedDBStorage]
|
|
1060
1106
|
});
|