@keq-request/cache 5.0.0-alpha.7 → 5.0.0-beta.1
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 +322 -0
- package/dist/index.d.mts +354 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +353 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1271 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1237 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +14 -22
- package/tsdown.config.ts +12 -0
- package/jest.config.cts +0 -14
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1237 @@
|
|
|
1
|
+
import * as R from "ramda";
|
|
2
|
+
import * as fastq from "fastq";
|
|
3
|
+
import { Exception, createProxyResponse } from "keq";
|
|
4
|
+
import dayjs from "dayjs";
|
|
5
|
+
import { openDB } from "idb";
|
|
6
|
+
//#region src/exceptions/cache-exception.ts
|
|
7
|
+
var CacheException = class extends Exception {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(`[@keq-request/cache] ${message}`);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/utils/get-response-bytes.ts
|
|
14
|
+
async function getResponseBytes(response) {
|
|
15
|
+
const contentLength = response.headers.get("content-length");
|
|
16
|
+
if (contentLength) return parseInt(contentLength);
|
|
17
|
+
return (await response.clone().arrayBuffer()).byteLength;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/constants/max-expired-at.ts
|
|
21
|
+
const MAX_EXPIRED_AT = /* @__PURE__ */ new Date(864e13);
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/constants/eviction.enum.ts
|
|
24
|
+
let Eviction = /* @__PURE__ */ function(Eviction) {
|
|
25
|
+
Eviction["LRU"] = "lru";
|
|
26
|
+
Eviction["LFU"] = "lfu";
|
|
27
|
+
Eviction["RANDOM"] = "random";
|
|
28
|
+
Eviction["TTL"] = "ttl";
|
|
29
|
+
return Eviction;
|
|
30
|
+
}({});
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/constants/size.enum.ts
|
|
33
|
+
let Size = /* @__PURE__ */ function(Size) {
|
|
34
|
+
Size[Size["B"] = 1] = "B";
|
|
35
|
+
Size[Size["KB"] = 1024] = "KB";
|
|
36
|
+
Size[Size["MB"] = 1048576] = "MB";
|
|
37
|
+
Size[Size["GB"] = 1073741824] = "GB";
|
|
38
|
+
return Size;
|
|
39
|
+
}({});
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/utils/logger.ts
|
|
42
|
+
var Logger = class {
|
|
43
|
+
static debug(...args) {
|
|
44
|
+
console.debug("[@keq-request/cache] [DEBUG] ", ...args);
|
|
45
|
+
}
|
|
46
|
+
static error(...args) {
|
|
47
|
+
console.error("[@keq-request/cache] [ERROR] ", ...args);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/utils/random.ts
|
|
52
|
+
function random(min, max) {
|
|
53
|
+
return Math.floor(Math.random() * (max - min)) + min;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/strategies/cache-first.ts
|
|
57
|
+
const cacheFirst = async function cacheFirst(handler, context, next) {
|
|
58
|
+
const [cacheKey, cacheValue] = await handler.getCache();
|
|
59
|
+
if (handler.options.debug) Logger.debug([
|
|
60
|
+
"",
|
|
61
|
+
`Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
|
|
62
|
+
"Strategy: Cache First",
|
|
63
|
+
`Cache Key: ${cacheKey}`,
|
|
64
|
+
`Cache Status: ${cacheValue ? "HIT" : "MISS"}`
|
|
65
|
+
].join("\n"));
|
|
66
|
+
if (cacheValue) {
|
|
67
|
+
context.emitter.emit("cache:hit", {
|
|
68
|
+
key: cacheKey,
|
|
69
|
+
response: cacheValue.response,
|
|
70
|
+
context
|
|
71
|
+
});
|
|
72
|
+
context.res = cacheValue.response;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
context.emitter.emit("cache:miss", {
|
|
76
|
+
key: cacheKey,
|
|
77
|
+
context
|
|
78
|
+
});
|
|
79
|
+
await next();
|
|
80
|
+
const [, entry] = await handler.setCache(context);
|
|
81
|
+
if (handler.options.debug) Logger.debug([
|
|
82
|
+
"",
|
|
83
|
+
`Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
|
|
84
|
+
"Strategy: Cache First",
|
|
85
|
+
`Cache Key: ${cacheKey}`,
|
|
86
|
+
`ACTIONS: ${entry ? "UPDATED" : "EXCLUDED"}`
|
|
87
|
+
].join("\n"));
|
|
88
|
+
if (entry) context.emitter.emit("cache:update", {
|
|
89
|
+
key: entry.key,
|
|
90
|
+
oldResponse: void 0,
|
|
91
|
+
newResponse: entry.response,
|
|
92
|
+
context
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/strategies/network-first.ts
|
|
97
|
+
const networkFirst = async function networkFirst(handler, context, next) {
|
|
98
|
+
try {
|
|
99
|
+
await next();
|
|
100
|
+
const [cacheKey, cache] = await handler.getCache();
|
|
101
|
+
const [, entry] = await handler.setCache(context);
|
|
102
|
+
if (handler.options.debug) Logger.debug([
|
|
103
|
+
"",
|
|
104
|
+
`Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
|
|
105
|
+
"Strategy: Network First",
|
|
106
|
+
`Cache Key: ${cacheKey}`,
|
|
107
|
+
`ACTIONS: ${entry ? "UPDATED" : "EXCLUDED"}`
|
|
108
|
+
].join("\n"));
|
|
109
|
+
if (entry) context.emitter.emit("cache:update", {
|
|
110
|
+
key: entry.key,
|
|
111
|
+
oldResponse: cache?.response,
|
|
112
|
+
newResponse: entry.response,
|
|
113
|
+
context
|
|
114
|
+
});
|
|
115
|
+
} catch (err) {
|
|
116
|
+
const [cacheKey, cache] = await handler.getCache();
|
|
117
|
+
if (handler.options.debug) Logger.debug([
|
|
118
|
+
"",
|
|
119
|
+
`Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
|
|
120
|
+
"Strategy: Network First",
|
|
121
|
+
`Cache Key: ${cacheKey}`,
|
|
122
|
+
`Cache Status: ${cache ? "HIT" : "MISS"}`
|
|
123
|
+
].join("\n"));
|
|
124
|
+
if (!cache) {
|
|
125
|
+
context.emitter.emit("cache:miss", {
|
|
126
|
+
key: cacheKey,
|
|
127
|
+
context
|
|
128
|
+
});
|
|
129
|
+
throw err;
|
|
130
|
+
}
|
|
131
|
+
context.emitter.emit("cache:hit", {
|
|
132
|
+
key: cacheKey,
|
|
133
|
+
response: cache.response,
|
|
134
|
+
context
|
|
135
|
+
});
|
|
136
|
+
context.res = cache.response;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/strategies/network-only.ts
|
|
141
|
+
const networkOnly = async function(handler, context, next) {
|
|
142
|
+
await next();
|
|
143
|
+
};
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/strategies/stale-while-revalidate.ts
|
|
146
|
+
const staleWhileRevalidate = async function(handler, context, next) {
|
|
147
|
+
const [cacheKey, cache] = await handler.getCache();
|
|
148
|
+
if (handler.options.debug) Logger.debug([
|
|
149
|
+
"",
|
|
150
|
+
`Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
|
|
151
|
+
"Strategy: Stale While Revalidate",
|
|
152
|
+
`Cache Key: ${cacheKey}`,
|
|
153
|
+
`Cache Status: ${cache ? "HIT" : "MISS"}`
|
|
154
|
+
].join("\n"));
|
|
155
|
+
if (cache) context.emitter.emit("cache:hit", {
|
|
156
|
+
key: cacheKey,
|
|
157
|
+
response: cache.response,
|
|
158
|
+
context
|
|
159
|
+
});
|
|
160
|
+
else context.emitter.emit("cache:miss", {
|
|
161
|
+
key: cacheKey,
|
|
162
|
+
context
|
|
163
|
+
});
|
|
164
|
+
if (cache) {
|
|
165
|
+
const orchestrator = context.orchestration.fork();
|
|
166
|
+
context.res = cache.response;
|
|
167
|
+
setTimeout(async () => {
|
|
168
|
+
try {
|
|
169
|
+
await orchestrator.execute();
|
|
170
|
+
const context = orchestrator.context;
|
|
171
|
+
const [cacheKey, entry] = await handler.setCache(context);
|
|
172
|
+
if (handler.options.debug) Logger.debug([
|
|
173
|
+
"",
|
|
174
|
+
`Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
|
|
175
|
+
"Strategy: Stale While Revalidate",
|
|
176
|
+
`Cache Key: ${cacheKey}`,
|
|
177
|
+
`ACTIONS: ${entry ? "UPDATED" : "EXCLUDED"}`
|
|
178
|
+
].join("\n"));
|
|
179
|
+
if (entry) context.emitter.emit("cache:update", {
|
|
180
|
+
key: cacheKey,
|
|
181
|
+
oldResponse: cache.response,
|
|
182
|
+
newResponse: entry.response,
|
|
183
|
+
context
|
|
184
|
+
});
|
|
185
|
+
} catch (err) {}
|
|
186
|
+
}, 1);
|
|
187
|
+
} else {
|
|
188
|
+
await next();
|
|
189
|
+
const [cacheKey, entry] = await handler.setCache(context);
|
|
190
|
+
if (handler.options.debug) Logger.debug([
|
|
191
|
+
"",
|
|
192
|
+
`Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
|
|
193
|
+
"Strategy: Stale While Revalidate",
|
|
194
|
+
`Cache Key: ${cacheKey}`,
|
|
195
|
+
`ACTIONS: ${entry ? "UPDATED" : "EXCLUDED"}`
|
|
196
|
+
].join("\n"));
|
|
197
|
+
if (entry) context.emitter.emit("cache:update", {
|
|
198
|
+
key: cacheKey,
|
|
199
|
+
oldResponse: void 0,
|
|
200
|
+
newResponse: entry.response,
|
|
201
|
+
context
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/constants/strategy.enum.ts
|
|
207
|
+
const Strategy = {
|
|
208
|
+
STALE_WHILE_REVALIDATE: staleWhileRevalidate,
|
|
209
|
+
NETWORK_FIRST: networkFirst,
|
|
210
|
+
NETWORK_ONLY: networkOnly,
|
|
211
|
+
CACHE_FIRST: cacheFirst
|
|
212
|
+
};
|
|
213
|
+
//#endregion
|
|
214
|
+
//#region \0@oxc-project+runtime@0.127.0/helpers/typeof.js
|
|
215
|
+
function _typeof(o) {
|
|
216
|
+
"@babel/helpers - typeof";
|
|
217
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
218
|
+
return typeof o;
|
|
219
|
+
} : function(o) {
|
|
220
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
221
|
+
}, _typeof(o);
|
|
222
|
+
}
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region \0@oxc-project+runtime@0.127.0/helpers/toPrimitive.js
|
|
225
|
+
function toPrimitive(t, r) {
|
|
226
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
227
|
+
var e = t[Symbol.toPrimitive];
|
|
228
|
+
if (void 0 !== e) {
|
|
229
|
+
var i = e.call(t, r || "default");
|
|
230
|
+
if ("object" != _typeof(i)) return i;
|
|
231
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
232
|
+
}
|
|
233
|
+
return ("string" === r ? String : Number)(t);
|
|
234
|
+
}
|
|
235
|
+
//#endregion
|
|
236
|
+
//#region \0@oxc-project+runtime@0.127.0/helpers/toPropertyKey.js
|
|
237
|
+
function toPropertyKey(t) {
|
|
238
|
+
var i = toPrimitive(t, "string");
|
|
239
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
240
|
+
}
|
|
241
|
+
//#endregion
|
|
242
|
+
//#region \0@oxc-project+runtime@0.127.0/helpers/defineProperty.js
|
|
243
|
+
function _defineProperty(e, r, t) {
|
|
244
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
245
|
+
value: t,
|
|
246
|
+
enumerable: !0,
|
|
247
|
+
configurable: !0,
|
|
248
|
+
writable: !0
|
|
249
|
+
}) : e[r] = t, e;
|
|
250
|
+
}
|
|
251
|
+
//#endregion
|
|
252
|
+
//#region src/cache-entry/cache-entry.ts
|
|
253
|
+
var CacheEntry = class CacheEntry {
|
|
254
|
+
constructor(options) {
|
|
255
|
+
_defineProperty(this, "key", void 0);
|
|
256
|
+
_defineProperty(this, "response", void 0);
|
|
257
|
+
_defineProperty(
|
|
258
|
+
this,
|
|
259
|
+
/**
|
|
260
|
+
* @en bytes
|
|
261
|
+
* @zh 字节数
|
|
262
|
+
*/
|
|
263
|
+
"size",
|
|
264
|
+
void 0
|
|
265
|
+
);
|
|
266
|
+
_defineProperty(this, "expiredAt", void 0);
|
|
267
|
+
this.key = options.key;
|
|
268
|
+
this.response = createProxyResponse(options.response);
|
|
269
|
+
this.size = options.size;
|
|
270
|
+
this.expiredAt = options.expiredAt ?? MAX_EXPIRED_AT;
|
|
271
|
+
}
|
|
272
|
+
static async build(options) {
|
|
273
|
+
const expiredAt = "expiredAt" in options ? options.expiredAt : "ttl" in options && typeof options.ttl === "number" && options.ttl > 0 ? new Date(Date.now() + options.ttl * 1e3) : MAX_EXPIRED_AT;
|
|
274
|
+
const response = options.response.clone();
|
|
275
|
+
return new CacheEntry({
|
|
276
|
+
key: options.key,
|
|
277
|
+
response,
|
|
278
|
+
size: options.size ?? await getResponseBytes(response),
|
|
279
|
+
expiredAt
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
clone() {
|
|
283
|
+
return new CacheEntry({
|
|
284
|
+
key: this.key,
|
|
285
|
+
response: this.response.clone(),
|
|
286
|
+
size: this.size,
|
|
287
|
+
expiredAt: this.expiredAt
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
assignResponseHeaders(headers) {
|
|
291
|
+
this.response = new Response(this.response.body, {
|
|
292
|
+
status: this.response.status,
|
|
293
|
+
statusText: this.response.statusText,
|
|
294
|
+
headers
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
//#endregion
|
|
299
|
+
//#region src/request-cache-handler/request-cache-handler.ts
|
|
300
|
+
var RequestCacheHandler = class {
|
|
301
|
+
constructor(cacheKey, storage, options) {
|
|
302
|
+
this.cacheKey = cacheKey;
|
|
303
|
+
this.storage = storage;
|
|
304
|
+
this.options = options;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Resolve cache key for request context
|
|
308
|
+
*/
|
|
309
|
+
static resolveRequestCacheKey(context, options) {
|
|
310
|
+
if (typeof options.key === "string") return options.key;
|
|
311
|
+
else if (typeof options.key === "function") return options.key(context);
|
|
312
|
+
else if (R.isNil(options.key) && context.locationId) return context.locationId;
|
|
313
|
+
else throw new CacheException("Cannot resolve cache key");
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Get cache from storage
|
|
317
|
+
*/
|
|
318
|
+
async getCache() {
|
|
319
|
+
const key = this.cacheKey;
|
|
320
|
+
if (this.options.serverTiming) {
|
|
321
|
+
const startAt = /* @__PURE__ */ new Date();
|
|
322
|
+
const entry = await this.storage.get(key);
|
|
323
|
+
if (entry) {
|
|
324
|
+
const dur = (/* @__PURE__ */ new Date()).getTime() - startAt.getTime();
|
|
325
|
+
const HeadersWithServerTiming = new Headers(entry.response.headers);
|
|
326
|
+
HeadersWithServerTiming.set("Server-Timing", `keq-cache; dur=${dur}; desc="HIT"`);
|
|
327
|
+
entry.assignResponseHeaders(HeadersWithServerTiming);
|
|
328
|
+
}
|
|
329
|
+
return [key, entry];
|
|
330
|
+
}
|
|
331
|
+
return [key, await this.storage.get(key)];
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Store response that in context to storage
|
|
335
|
+
*/
|
|
336
|
+
async setCache(context) {
|
|
337
|
+
const options = this.options;
|
|
338
|
+
const key = this.cacheKey;
|
|
339
|
+
if (!context.response) return [key, void 0];
|
|
340
|
+
if (options.exclude && await options.exclude(context.response)) return [key, void 0];
|
|
341
|
+
const entry = await CacheEntry.build({
|
|
342
|
+
key,
|
|
343
|
+
response: context.response,
|
|
344
|
+
ttl: options.ttl
|
|
345
|
+
});
|
|
346
|
+
this.storage.set(entry);
|
|
347
|
+
return [key, entry];
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
//#endregion
|
|
351
|
+
//#region src/cache.ts
|
|
352
|
+
function cache(options) {
|
|
353
|
+
const storage = options.storage;
|
|
354
|
+
const rules = options?.rules || [];
|
|
355
|
+
return async function cache(ctx, next) {
|
|
356
|
+
if (ctx.options.cache === false) {
|
|
357
|
+
await next();
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
let requestCacheOptions = ctx.options.cache;
|
|
361
|
+
const rule = rules.find((rule) => {
|
|
362
|
+
if (rule.pattern === void 0 || rule.pattern === true) return true;
|
|
363
|
+
if (typeof rule.pattern === "function") return rule.pattern(ctx);
|
|
364
|
+
return rule.pattern.test(ctx.request.__url__.href);
|
|
365
|
+
});
|
|
366
|
+
if (rule) requestCacheOptions = R.mergeRight(rule, requestCacheOptions || {});
|
|
367
|
+
if (!requestCacheOptions || R.isEmpty(requestCacheOptions)) {
|
|
368
|
+
await next();
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
if (!requestCacheOptions.key) requestCacheOptions.key = options.keyFactory;
|
|
372
|
+
if (!ctx.locationId && !requestCacheOptions.key) {
|
|
373
|
+
console.warn("[@keq/cache] Warning: Cannot resolve Cache Key. Cache is skipped.");
|
|
374
|
+
await next();
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (requestCacheOptions.serverTiming === void 0 && options.serverTiming !== void 0) requestCacheOptions.serverTiming = options.serverTiming;
|
|
378
|
+
const cacheKey = RequestCacheHandler.resolveRequestCacheKey(ctx, requestCacheOptions);
|
|
379
|
+
const handler = new RequestCacheHandler(cacheKey, storage, requestCacheOptions);
|
|
380
|
+
const strategy = requestCacheOptions.strategy;
|
|
381
|
+
if (requestCacheOptions.concurrent) await strategy(handler, ctx, next);
|
|
382
|
+
else {
|
|
383
|
+
if (!ctx.global.core) ctx.global.core = {};
|
|
384
|
+
if (!ctx.global.core.cache) ctx.global.core.cache = {};
|
|
385
|
+
if (!ctx.global.core.cache[cacheKey]) ctx.global.core.cache[cacheKey] = fastq.promise(async ({ next }) => {
|
|
386
|
+
await next();
|
|
387
|
+
}, 1);
|
|
388
|
+
await ctx.global.core.cache[cacheKey].push({ next: async () => {
|
|
389
|
+
await strategy(handler, ctx, next);
|
|
390
|
+
} });
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
//#endregion
|
|
395
|
+
//#region src/storage/keq-cache-storage.ts
|
|
396
|
+
var KeqCacheStorage = class {};
|
|
397
|
+
//#endregion
|
|
398
|
+
//#region src/storage/internal-storage/internal-storage.ts
|
|
399
|
+
var InternalStorage = class extends KeqCacheStorage {
|
|
400
|
+
constructor(options) {
|
|
401
|
+
super();
|
|
402
|
+
_defineProperty(this, "__id__", Math.random().toString(36).slice(2));
|
|
403
|
+
_defineProperty(this, "__size__", void 0);
|
|
404
|
+
_defineProperty(this, "__debug__", void 0);
|
|
405
|
+
_defineProperty(this, "__onCacheGet__", void 0);
|
|
406
|
+
_defineProperty(this, "__onCacheSet__", void 0);
|
|
407
|
+
_defineProperty(this, "__onCacheRemove__", void 0);
|
|
408
|
+
_defineProperty(this, "__onCacheEvict__", void 0);
|
|
409
|
+
_defineProperty(this, "__onCacheExpired__", void 0);
|
|
410
|
+
if (options?.size && (typeof options?.size !== "number" || options.size <= 0)) throw new CacheException(`Invalid size: ${String(options?.size)}`);
|
|
411
|
+
this.__size__ = options?.size ?? Infinity;
|
|
412
|
+
this.__debug__ = !!options?.debug;
|
|
413
|
+
this.__onCacheGet__ = options?.onCacheGet;
|
|
414
|
+
this.__onCacheSet__ = options?.onCacheSet;
|
|
415
|
+
this.__onCacheRemove__ = options?.onCacheRemove;
|
|
416
|
+
this.__onCacheEvict__ = options?.onCacheEvict;
|
|
417
|
+
this.debug((log) => log("Storage Created: ", this));
|
|
418
|
+
}
|
|
419
|
+
debug(fn) {
|
|
420
|
+
if (this.__debug__) fn((...args) => {
|
|
421
|
+
Logger.debug(`[Storage(${this.__id__})]`, ...args);
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
//#endregion
|
|
426
|
+
//#region src/storage/memory-storage/utils/humanize-size.ts
|
|
427
|
+
/**
|
|
428
|
+
* @en Humanize size in bytes to KB, MB, GB
|
|
429
|
+
* @zh 将字节数转换为 KB、MB、GB 等易读格式
|
|
430
|
+
*/
|
|
431
|
+
function humanizeSize(size) {
|
|
432
|
+
if (size < 1024) return `${size} B`;
|
|
433
|
+
if (size < 1024 * 1024) return `${(size / 1024).toFixed(2)} KB`;
|
|
434
|
+
if (size < 1024 * 1024 * 1024) return `${(size / (1024 * 1024)).toFixed(2)} MB`;
|
|
435
|
+
return `${(size / (1024 * 1024 * 1024)).toFixed(2)} GB`;
|
|
436
|
+
}
|
|
437
|
+
//#endregion
|
|
438
|
+
//#region src/storage/memory-storage/utils/serialize-response-body.ts
|
|
439
|
+
/**
|
|
440
|
+
* @en Serialize the response body based on content-type
|
|
441
|
+
* @zh 根据 content-type 序列化响应体
|
|
442
|
+
*/
|
|
443
|
+
async function serializeResponseBody(response) {
|
|
444
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
445
|
+
try {
|
|
446
|
+
if (contentType.includes("application/json")) {
|
|
447
|
+
const json = await response.json();
|
|
448
|
+
return JSON.stringify(json);
|
|
449
|
+
}
|
|
450
|
+
if (contentType.includes("text/") || contentType.includes("application/xml") || contentType.includes("application/javascript")) return await response.text();
|
|
451
|
+
return "[Binary or unsupported content]";
|
|
452
|
+
} catch {
|
|
453
|
+
return "[Unable to serialize]";
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
//#endregion
|
|
457
|
+
//#region src/storage/memory-storage/base-memory-storage.ts
|
|
458
|
+
var BaseMemoryStorage = class extends InternalStorage {
|
|
459
|
+
constructor(..._args) {
|
|
460
|
+
super(..._args);
|
|
461
|
+
_defineProperty(this, "storage", /* @__PURE__ */ new Map());
|
|
462
|
+
_defineProperty(this, "visitTimeRecords", /* @__PURE__ */ new Map());
|
|
463
|
+
_defineProperty(this, "visitCountRecords", /* @__PURE__ */ new Map());
|
|
464
|
+
_defineProperty(this, "lastEvictExpiredTime", dayjs());
|
|
465
|
+
}
|
|
466
|
+
get size() {
|
|
467
|
+
const used = R.sum(R.pluck("size", [...this.storage.values()]));
|
|
468
|
+
return {
|
|
469
|
+
used,
|
|
470
|
+
free: this.__size__ > used ? this.__size__ - used : 0
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
get(key) {
|
|
474
|
+
this.evictExpired();
|
|
475
|
+
const entry = this.storage.get(key);
|
|
476
|
+
this.visitCountRecords.set(key, (this.visitCountRecords.get(key) ?? 0) + 1);
|
|
477
|
+
this.visitTimeRecords.set(key, /* @__PURE__ */ new Date());
|
|
478
|
+
if (!entry) this.debug((log) => log(`Entry(${key}) Not Found`));
|
|
479
|
+
else this.debug((log) => log(`Entry(${key}) Found: `, entry));
|
|
480
|
+
return entry?.clone();
|
|
481
|
+
}
|
|
482
|
+
set(value) {
|
|
483
|
+
if (!this.evict(value.size)) {
|
|
484
|
+
this.debug((log) => log("Storage Size Not Enough: ", this.size.free, " < ", value.size));
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
this.storage.set(value.key, value);
|
|
488
|
+
this.visitTimeRecords.set(value.key, /* @__PURE__ */ new Date());
|
|
489
|
+
this.visitCountRecords.set(value.key, this.visitCountRecords.get(value.key) ?? 0);
|
|
490
|
+
this.debug((log) => log("Entry Added: ", value));
|
|
491
|
+
this.debug((log) => log("Storage Size: ", this.size));
|
|
492
|
+
}
|
|
493
|
+
__remove__(keys) {
|
|
494
|
+
for (const key of keys) {
|
|
495
|
+
const entry = this.storage.get(key);
|
|
496
|
+
if (!entry) return;
|
|
497
|
+
this.storage.delete(key);
|
|
498
|
+
this.visitCountRecords.delete(key);
|
|
499
|
+
this.visitTimeRecords.delete(key);
|
|
500
|
+
this.debug((log) => log("Entry Removed: ", entry));
|
|
501
|
+
this.debug((log) => log("Storage Size: ", this.size));
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
remove(key) {
|
|
505
|
+
this.__remove__([key]);
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* @zh 清除过期的缓存
|
|
509
|
+
*/
|
|
510
|
+
evictExpired() {
|
|
511
|
+
const now = dayjs();
|
|
512
|
+
if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
|
|
513
|
+
const keys = [];
|
|
514
|
+
for (const [key, entry] of this.storage.entries()) if (entry.expiredAt && now.isAfter(entry.expiredAt)) keys.push(key);
|
|
515
|
+
this.__remove__(keys);
|
|
516
|
+
this.__onCacheExpired__?.({ keys });
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* @en Evict the storage to make sure the size is enough
|
|
520
|
+
* @zh 清除缓存以确保有足够的空间
|
|
521
|
+
*
|
|
522
|
+
* @return {boolean} - is evicted successfully
|
|
523
|
+
*/
|
|
524
|
+
evict(expectSize) {
|
|
525
|
+
this.evictExpired();
|
|
526
|
+
return this.size.free >= expectSize;
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* @en Print all cached data using console.table for debugging
|
|
530
|
+
* @zh 使用 console.table 打印所有缓存数据,用于调试
|
|
531
|
+
*/
|
|
532
|
+
async print() {
|
|
533
|
+
if (this.storage.size === 0) {
|
|
534
|
+
console.log("MemoryStorage is empty");
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const entries = await Promise.all([...this.storage.entries()].map(async ([key, entry]) => {
|
|
538
|
+
const body = await serializeResponseBody(entry.response.clone());
|
|
539
|
+
return {
|
|
540
|
+
key,
|
|
541
|
+
size: humanizeSize(entry.size),
|
|
542
|
+
"Expired Time": entry.expiredAt.getTime() >= MAX_EXPIRED_AT.getTime() ? "-" : entry.expiredAt.toISOString(),
|
|
543
|
+
"Visit Count": this.visitCountRecords.get(key) ?? 0,
|
|
544
|
+
"Last Visit Time": this.visitTimeRecords.get(key)?.toISOString() ?? "-",
|
|
545
|
+
"Response Status": entry.response.status,
|
|
546
|
+
"Response URL": entry.response.url,
|
|
547
|
+
"Response Body": body
|
|
548
|
+
};
|
|
549
|
+
}));
|
|
550
|
+
console.table(entries);
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
//#endregion
|
|
554
|
+
//#region src/storage/memory-storage/ttl-memory-storage.ts
|
|
555
|
+
var TTLMemoryStorage = class extends BaseMemoryStorage {
|
|
556
|
+
constructor(options) {
|
|
557
|
+
super(options);
|
|
558
|
+
}
|
|
559
|
+
get(key) {
|
|
560
|
+
const entry = super.get(key);
|
|
561
|
+
this.__onCacheGet__?.({ key });
|
|
562
|
+
return entry;
|
|
563
|
+
}
|
|
564
|
+
set(value) {
|
|
565
|
+
super.set(value);
|
|
566
|
+
this.__onCacheSet__?.({ key: value.key });
|
|
567
|
+
}
|
|
568
|
+
remove(key) {
|
|
569
|
+
super.remove(key);
|
|
570
|
+
this.__onCacheRemove__?.({ key });
|
|
571
|
+
}
|
|
572
|
+
evict(expectSize) {
|
|
573
|
+
if (expectSize > this.__size__) {
|
|
574
|
+
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
575
|
+
return false;
|
|
576
|
+
}
|
|
577
|
+
this.evictExpired();
|
|
578
|
+
let deficitSize = expectSize - this.size.free;
|
|
579
|
+
if (deficitSize <= 0) return true;
|
|
580
|
+
const entries = [...this.storage.values()].sort((a, b) => {
|
|
581
|
+
const aExpiredAt = dayjs(a.expiredAt);
|
|
582
|
+
const bExpiredAt = dayjs(b.expiredAt);
|
|
583
|
+
return aExpiredAt.isBefore(bExpiredAt) ? 1 : -1;
|
|
584
|
+
});
|
|
585
|
+
if (R.sum(R.pluck("size", entries)) < deficitSize) {
|
|
586
|
+
this.debug((log) => log("Storage Size Not Enough: ", this.size.free, " < ", deficitSize));
|
|
587
|
+
return false;
|
|
588
|
+
}
|
|
589
|
+
const keys = [];
|
|
590
|
+
while (deficitSize > 0 && entries.length) {
|
|
591
|
+
const entry = entries.pop();
|
|
592
|
+
deficitSize -= entry.size;
|
|
593
|
+
keys.push(entry.key);
|
|
594
|
+
}
|
|
595
|
+
this.__remove__(keys);
|
|
596
|
+
this.__onCacheEvict__?.({ keys });
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
//#endregion
|
|
601
|
+
//#region src/storage/memory-storage/random-memory-storage.ts
|
|
602
|
+
var RandomMemoryStorage = class extends BaseMemoryStorage {
|
|
603
|
+
constructor(options) {
|
|
604
|
+
super(options);
|
|
605
|
+
}
|
|
606
|
+
get(key) {
|
|
607
|
+
const entry = super.get(key);
|
|
608
|
+
this.__onCacheGet__?.({ key });
|
|
609
|
+
return entry;
|
|
610
|
+
}
|
|
611
|
+
set(value) {
|
|
612
|
+
super.set(value);
|
|
613
|
+
this.__onCacheSet__?.({ key: value.key });
|
|
614
|
+
}
|
|
615
|
+
remove(key) {
|
|
616
|
+
super.remove(key);
|
|
617
|
+
this.__onCacheRemove__?.({ key });
|
|
618
|
+
}
|
|
619
|
+
evict(expectSize) {
|
|
620
|
+
if (expectSize > this.__size__) {
|
|
621
|
+
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
622
|
+
return false;
|
|
623
|
+
}
|
|
624
|
+
this.evictExpired();
|
|
625
|
+
let deficitSize = expectSize - this.size.free;
|
|
626
|
+
if (deficitSize <= 0) return true;
|
|
627
|
+
const entries = [...this.storage.values()];
|
|
628
|
+
const keys = [];
|
|
629
|
+
while (deficitSize > 0 && entries.length) {
|
|
630
|
+
const index = random(0, entries.length - 1);
|
|
631
|
+
const entry = entries[index];
|
|
632
|
+
deficitSize -= entry.size;
|
|
633
|
+
entries.splice(index, 1);
|
|
634
|
+
keys.push(entry.key);
|
|
635
|
+
}
|
|
636
|
+
this.__remove__(keys);
|
|
637
|
+
this.__onCacheEvict__?.({ keys });
|
|
638
|
+
return true;
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
//#endregion
|
|
642
|
+
//#region src/storage/memory-storage/lru-memory-storage.ts
|
|
643
|
+
var LRUMemoryStorage = class extends BaseMemoryStorage {
|
|
644
|
+
constructor(options) {
|
|
645
|
+
super(options);
|
|
646
|
+
}
|
|
647
|
+
get(key) {
|
|
648
|
+
const entry = super.get(key);
|
|
649
|
+
this.__onCacheGet__?.({ key });
|
|
650
|
+
return entry;
|
|
651
|
+
}
|
|
652
|
+
set(value) {
|
|
653
|
+
super.set(value);
|
|
654
|
+
this.__onCacheSet__?.({ key: value.key });
|
|
655
|
+
}
|
|
656
|
+
remove(key) {
|
|
657
|
+
super.remove(key);
|
|
658
|
+
this.__onCacheRemove__?.({ key });
|
|
659
|
+
}
|
|
660
|
+
evict(expectSize) {
|
|
661
|
+
if (expectSize > this.__size__) {
|
|
662
|
+
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
this.evictExpired();
|
|
666
|
+
let deficitSize = expectSize - this.size.free;
|
|
667
|
+
if (deficitSize <= 0) return true;
|
|
668
|
+
const entries = [...this.storage.values()].sort((a, b) => {
|
|
669
|
+
const aVisitAt = this.visitTimeRecords.get(a.key);
|
|
670
|
+
const bVisitAt = this.visitTimeRecords.get(b.key);
|
|
671
|
+
if (aVisitAt === bVisitAt) return 0;
|
|
672
|
+
if (!aVisitAt) return 1;
|
|
673
|
+
if (!bVisitAt) return -1;
|
|
674
|
+
return dayjs(aVisitAt).isBefore(dayjs(bVisitAt)) ? 1 : -1;
|
|
675
|
+
});
|
|
676
|
+
const keys = [];
|
|
677
|
+
while (deficitSize > 0 && entries.length) {
|
|
678
|
+
const entry = entries.pop();
|
|
679
|
+
deficitSize -= entry.size;
|
|
680
|
+
keys.push(entry.key);
|
|
681
|
+
}
|
|
682
|
+
this.__remove__(keys);
|
|
683
|
+
this.__onCacheEvict__?.({ keys });
|
|
684
|
+
return true;
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
//#endregion
|
|
688
|
+
//#region src/storage/memory-storage/lfu-memory-storage.ts
|
|
689
|
+
var LFUMemoryStorage = class extends BaseMemoryStorage {
|
|
690
|
+
constructor(options) {
|
|
691
|
+
super(options);
|
|
692
|
+
}
|
|
693
|
+
get(key) {
|
|
694
|
+
const entry = super.get(key);
|
|
695
|
+
this.__onCacheGet__?.({ key });
|
|
696
|
+
return entry;
|
|
697
|
+
}
|
|
698
|
+
set(value) {
|
|
699
|
+
super.set(value);
|
|
700
|
+
this.__onCacheSet__?.({ key: value.key });
|
|
701
|
+
}
|
|
702
|
+
remove(key) {
|
|
703
|
+
super.remove(key);
|
|
704
|
+
this.__onCacheRemove__?.({ key });
|
|
705
|
+
}
|
|
706
|
+
evict(expectSize) {
|
|
707
|
+
if (expectSize > this.__size__) {
|
|
708
|
+
this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
this.evictExpired();
|
|
712
|
+
let deficitSize = expectSize - this.size.free;
|
|
713
|
+
if (deficitSize <= 0) return true;
|
|
714
|
+
const entries = [...this.storage.values()].sort((a, b) => {
|
|
715
|
+
const aVisitCount = this.visitCountRecords.get(a.key) || 0;
|
|
716
|
+
return (this.visitCountRecords.get(b.key) || 0) - aVisitCount;
|
|
717
|
+
});
|
|
718
|
+
const keys = [];
|
|
719
|
+
while (deficitSize > 0 && entries.length) {
|
|
720
|
+
const entry = entries.pop();
|
|
721
|
+
deficitSize -= entry.size;
|
|
722
|
+
keys.push(entry.key);
|
|
723
|
+
}
|
|
724
|
+
this.__remove__(keys);
|
|
725
|
+
this.__onCacheEvict__?.({ keys });
|
|
726
|
+
return true;
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
//#endregion
|
|
730
|
+
//#region src/storage/memory-storage/memory-storage.ts
|
|
731
|
+
var MemoryStorage = class extends KeqCacheStorage {
|
|
732
|
+
constructor(options) {
|
|
733
|
+
super();
|
|
734
|
+
_defineProperty(this, "storage", void 0);
|
|
735
|
+
const eviction = options?.eviction || "lru";
|
|
736
|
+
if (eviction === "ttl") this.storage = new TTLMemoryStorage(options);
|
|
737
|
+
else if (eviction === "random") this.storage = new RandomMemoryStorage(options);
|
|
738
|
+
else if (eviction === "lru") this.storage = new LRUMemoryStorage(options);
|
|
739
|
+
else if (eviction === "lfu") this.storage = new LFUMemoryStorage(options);
|
|
740
|
+
else throw new TypeError(`Invalid eviction: ${String(eviction)}`);
|
|
741
|
+
}
|
|
742
|
+
set(entry) {
|
|
743
|
+
return this.storage.set(entry);
|
|
744
|
+
}
|
|
745
|
+
get(key) {
|
|
746
|
+
return this.storage.get(key);
|
|
747
|
+
}
|
|
748
|
+
remove(key) {
|
|
749
|
+
return this.storage.remove(key);
|
|
750
|
+
}
|
|
751
|
+
async print() {
|
|
752
|
+
return this.storage.print();
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
//#endregion
|
|
756
|
+
//#region src/storage/indexed-db-storage/constants/default-table-name.ts
|
|
757
|
+
const DEFAULT_TABLE_NAME = "keq_cache_indexed_db_storage";
|
|
758
|
+
//#endregion
|
|
759
|
+
//#region src/storage/indexed-db-storage/base-indexed-db-storage.ts
|
|
760
|
+
var BaseIndexedDBStorage = class extends InternalStorage {
|
|
761
|
+
constructor(options) {
|
|
762
|
+
super(options);
|
|
763
|
+
_defineProperty(this, "tableName", DEFAULT_TABLE_NAME);
|
|
764
|
+
_defineProperty(this, "db", void 0);
|
|
765
|
+
_defineProperty(this, "lastEvictExpiredTime", dayjs(0));
|
|
766
|
+
if (options?.tableName === "keq_cache_indexed_db_storage") throw new CacheException(`IndexedDBStorage name cannot be "${DEFAULT_TABLE_NAME}"`);
|
|
767
|
+
this.tableName = options?.tableName || "keq_cache_indexed_db_storage";
|
|
768
|
+
}
|
|
769
|
+
async openDB() {
|
|
770
|
+
if (this.db) return this.db;
|
|
771
|
+
const tableName = this.tableName;
|
|
772
|
+
const db = await openDB(tableName, 2, {
|
|
773
|
+
upgrade(db) {
|
|
774
|
+
if (!db.objectStoreNames.contains("metadata")) db.createObjectStore("metadata", { keyPath: "key" }).createIndex("expiredAt", "expiredAt");
|
|
775
|
+
if (!db.objectStoreNames.contains("response")) db.createObjectStore("response", { keyPath: "key" }).createIndex("responseStatus", "responseStatus");
|
|
776
|
+
if (!db.objectStoreNames.contains("visits")) {
|
|
777
|
+
const visitsStore = db.createObjectStore("visits", { keyPath: "key" });
|
|
778
|
+
visitsStore.createIndex("visitCount", "visitCount");
|
|
779
|
+
visitsStore.createIndex("lastVisitedAt", "lastVisitedAt");
|
|
780
|
+
}
|
|
781
|
+
},
|
|
782
|
+
blocked() {
|
|
783
|
+
Logger.error(`[@keq-request/cache] IndexedDB Table ${tableName} is blocked`);
|
|
784
|
+
},
|
|
785
|
+
blocking() {
|
|
786
|
+
Logger.error(`[@keq-request/cache] IndexedDB Table ${tableName} is blocking`);
|
|
787
|
+
},
|
|
788
|
+
terminated() {
|
|
789
|
+
Logger.error(`[@keq-request/cache] IndexedDB Table ${tableName} is terminated`);
|
|
790
|
+
}
|
|
791
|
+
});
|
|
792
|
+
this.db = db;
|
|
793
|
+
return db;
|
|
794
|
+
}
|
|
795
|
+
async getSize() {
|
|
796
|
+
const items = await (await this.openDB()).getAll("metadata");
|
|
797
|
+
const used = R.sum(items.map((entry) => entry.size));
|
|
798
|
+
return {
|
|
799
|
+
used,
|
|
800
|
+
free: this.__size__ - used
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
async get(key) {
|
|
804
|
+
await this.evictExpired();
|
|
805
|
+
try {
|
|
806
|
+
const db = await this.openDB();
|
|
807
|
+
const dbMetadata = await db.get("metadata", key);
|
|
808
|
+
const dbResponse = await db.get("response", key);
|
|
809
|
+
const dbVisits = await db.get("visits", key);
|
|
810
|
+
if (!dbMetadata || !dbResponse) return;
|
|
811
|
+
await db.put("visits", {
|
|
812
|
+
key: dbMetadata.key,
|
|
813
|
+
visitCount: dbVisits ? dbVisits.visitCount + 1 : 1,
|
|
814
|
+
lastVisitedAt: /* @__PURE__ */ new Date()
|
|
815
|
+
});
|
|
816
|
+
const response = new Response(dbResponse.responseBody, {
|
|
817
|
+
status: dbResponse.responseStatus,
|
|
818
|
+
headers: new Headers(dbResponse.responseHeaders),
|
|
819
|
+
statusText: dbResponse.responseStatusText
|
|
820
|
+
});
|
|
821
|
+
return await CacheEntry.build({
|
|
822
|
+
key: dbMetadata.key,
|
|
823
|
+
expiredAt: dbMetadata.expiredAt,
|
|
824
|
+
response,
|
|
825
|
+
size: dbMetadata.size
|
|
826
|
+
});
|
|
827
|
+
} catch (error) {
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
async set(entry) {
|
|
832
|
+
try {
|
|
833
|
+
if (!await this.evict(entry.size)) {
|
|
834
|
+
const size = await this.getSize();
|
|
835
|
+
this.debug((log) => log(`Storage Size Not Enough: ${size.free} < ${entry.size}`));
|
|
836
|
+
return;
|
|
837
|
+
}
|
|
838
|
+
const dbMetadata = {
|
|
839
|
+
key: entry.key,
|
|
840
|
+
size: entry.size,
|
|
841
|
+
expiredAt: entry.expiredAt,
|
|
842
|
+
visitedAt: /* @__PURE__ */ new Date(),
|
|
843
|
+
visitCount: 0
|
|
844
|
+
};
|
|
845
|
+
const response = entry.response.clone();
|
|
846
|
+
const dbResponse = {
|
|
847
|
+
key: entry.key,
|
|
848
|
+
responseBody: await response.arrayBuffer(),
|
|
849
|
+
responseHeaders: [...response.headers.entries()],
|
|
850
|
+
responseStatus: response.status,
|
|
851
|
+
responseStatusText: response.statusText
|
|
852
|
+
};
|
|
853
|
+
const tx = (await this.openDB()).transaction([
|
|
854
|
+
"metadata",
|
|
855
|
+
"response",
|
|
856
|
+
"visits"
|
|
857
|
+
], "readwrite");
|
|
858
|
+
const metadataStore = tx.objectStore("metadata");
|
|
859
|
+
const responseStore = tx.objectStore("response");
|
|
860
|
+
const visitsStore = tx.objectStore("visits");
|
|
861
|
+
const dbVisits = await visitsStore.get(entry.key) || {
|
|
862
|
+
key: entry.key,
|
|
863
|
+
visitCount: 0,
|
|
864
|
+
lastVisitedAt: /* @__PURE__ */ new Date()
|
|
865
|
+
};
|
|
866
|
+
await Promise.all([
|
|
867
|
+
metadataStore.put(dbMetadata),
|
|
868
|
+
responseStore.put(dbResponse),
|
|
869
|
+
visitsStore.put(dbVisits)
|
|
870
|
+
]);
|
|
871
|
+
await tx.done;
|
|
872
|
+
} catch (error) {
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
async __remove__(tx, keys) {
|
|
877
|
+
await Promise.all(R.unnest(keys.map((key) => [
|
|
878
|
+
tx.objectStore("metadata").delete(key),
|
|
879
|
+
tx.objectStore("response").delete(key),
|
|
880
|
+
tx.objectStore("visits").delete(key)
|
|
881
|
+
])));
|
|
882
|
+
}
|
|
883
|
+
async remove(key) {
|
|
884
|
+
try {
|
|
885
|
+
const tx = (await this.openDB()).transaction([
|
|
886
|
+
"metadata",
|
|
887
|
+
"response",
|
|
888
|
+
"visits"
|
|
889
|
+
], "readwrite");
|
|
890
|
+
await this.__remove__(tx, [key]);
|
|
891
|
+
await tx.done;
|
|
892
|
+
} catch (error) {
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* @zh 清除过期的缓存
|
|
898
|
+
*/
|
|
899
|
+
async evictExpired() {
|
|
900
|
+
const now = dayjs();
|
|
901
|
+
if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
|
|
902
|
+
this.lastEvictExpiredTime = now;
|
|
903
|
+
try {
|
|
904
|
+
const tx = (await this.openDB()).transaction([
|
|
905
|
+
"metadata",
|
|
906
|
+
"response",
|
|
907
|
+
"visits"
|
|
908
|
+
], "readwrite");
|
|
909
|
+
let cursor = await tx.objectStore("metadata").index("expiredAt").openCursor(IDBKeyRange.upperBound(now.toDate()));
|
|
910
|
+
const expiredKeys = [];
|
|
911
|
+
while (cursor) if (dayjs(cursor.value.expiredAt).isBefore(now)) {
|
|
912
|
+
expiredKeys.push(cursor.value.key);
|
|
913
|
+
cursor = await cursor.continue();
|
|
914
|
+
} else break;
|
|
915
|
+
await this.__remove__(tx, expiredKeys);
|
|
916
|
+
await tx.done;
|
|
917
|
+
this.__onCacheExpired__?.({ keys: expiredKeys });
|
|
918
|
+
} catch (error) {
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
//#endregion
|
|
924
|
+
//#region src/storage/indexed-db-storage/random-indexed-db-storage.ts
|
|
925
|
+
var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
926
|
+
constructor(options) {
|
|
927
|
+
super(options);
|
|
928
|
+
}
|
|
929
|
+
async get(key) {
|
|
930
|
+
const entry = await super.get(key);
|
|
931
|
+
this.__onCacheGet__?.({ key });
|
|
932
|
+
return entry;
|
|
933
|
+
}
|
|
934
|
+
async set(value) {
|
|
935
|
+
await super.set(value);
|
|
936
|
+
this.__onCacheSet__?.({ key: value.key });
|
|
937
|
+
}
|
|
938
|
+
async remove(key) {
|
|
939
|
+
await super.remove(key);
|
|
940
|
+
this.__onCacheRemove__?.({ key });
|
|
941
|
+
}
|
|
942
|
+
async evict(expectSize) {
|
|
943
|
+
await this.evictExpired();
|
|
944
|
+
let deficitSize = expectSize - (await this.getSize()).free;
|
|
945
|
+
if (deficitSize <= 0) return true;
|
|
946
|
+
const tx = (await this.openDB()).transaction([
|
|
947
|
+
"metadata",
|
|
948
|
+
"response",
|
|
949
|
+
"visits"
|
|
950
|
+
], "readwrite");
|
|
951
|
+
const metadatas = await tx.objectStore("metadata").getAll();
|
|
952
|
+
const totalSize = R.sum(metadatas.map((m) => m.size));
|
|
953
|
+
if (totalSize < deficitSize) {
|
|
954
|
+
this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize - totalSize}`));
|
|
955
|
+
tx.abort();
|
|
956
|
+
return false;
|
|
957
|
+
}
|
|
958
|
+
const keys = [];
|
|
959
|
+
while (deficitSize > 0 && metadatas.length) {
|
|
960
|
+
const index = random(0, metadatas.length - 1);
|
|
961
|
+
const metadata = metadatas[index];
|
|
962
|
+
deficitSize -= metadata.size;
|
|
963
|
+
keys.push(metadata.key);
|
|
964
|
+
metadatas.splice(index, 1);
|
|
965
|
+
}
|
|
966
|
+
await this.__remove__(tx, keys);
|
|
967
|
+
await tx.done;
|
|
968
|
+
this.__onCacheEvict__?.({ keys });
|
|
969
|
+
return true;
|
|
970
|
+
}
|
|
971
|
+
};
|
|
972
|
+
//#endregion
|
|
973
|
+
//#region src/storage/indexed-db-storage/lfu-indexed-db-storage.ts
|
|
974
|
+
var LFUIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
975
|
+
constructor(options) {
|
|
976
|
+
super(options);
|
|
977
|
+
}
|
|
978
|
+
async get(key) {
|
|
979
|
+
const entry = await super.get(key);
|
|
980
|
+
this.__onCacheGet__?.({ key });
|
|
981
|
+
return entry;
|
|
982
|
+
}
|
|
983
|
+
async set(value) {
|
|
984
|
+
await super.set(value);
|
|
985
|
+
this.__onCacheSet__?.({ key: value.key });
|
|
986
|
+
}
|
|
987
|
+
async remove(key) {
|
|
988
|
+
await super.remove(key);
|
|
989
|
+
this.__onCacheRemove__?.({ key });
|
|
990
|
+
}
|
|
991
|
+
async evict(expectSize) {
|
|
992
|
+
await this.evictExpired();
|
|
993
|
+
let deficitSize = expectSize - (await this.getSize()).free;
|
|
994
|
+
if (deficitSize <= 0) return true;
|
|
995
|
+
const tx = (await this.openDB()).transaction([
|
|
996
|
+
"metadata",
|
|
997
|
+
"response",
|
|
998
|
+
"visits"
|
|
999
|
+
], "readwrite");
|
|
1000
|
+
const metadataStore = tx.objectStore("metadata");
|
|
1001
|
+
let cursor = await tx.objectStore("visits").index("visitCount").openCursor();
|
|
1002
|
+
const keys = [];
|
|
1003
|
+
while (deficitSize > 0 && cursor) {
|
|
1004
|
+
const metadata = await metadataStore.get(cursor.value.key);
|
|
1005
|
+
if (!metadata) {
|
|
1006
|
+
await cursor.delete();
|
|
1007
|
+
cursor = await cursor.continue();
|
|
1008
|
+
continue;
|
|
1009
|
+
}
|
|
1010
|
+
deficitSize -= metadata.size;
|
|
1011
|
+
keys.push(cursor.value.key);
|
|
1012
|
+
cursor = await cursor.continue();
|
|
1013
|
+
}
|
|
1014
|
+
if (deficitSize > 0) {
|
|
1015
|
+
this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
|
|
1016
|
+
tx.abort();
|
|
1017
|
+
return false;
|
|
1018
|
+
}
|
|
1019
|
+
await this.__remove__(tx, keys);
|
|
1020
|
+
await tx.done;
|
|
1021
|
+
this.__onCacheEvict__?.({ keys });
|
|
1022
|
+
return true;
|
|
1023
|
+
}
|
|
1024
|
+
};
|
|
1025
|
+
//#endregion
|
|
1026
|
+
//#region src/storage/indexed-db-storage/lru-indexed-db-storage.ts
|
|
1027
|
+
var LRUIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
1028
|
+
constructor(options) {
|
|
1029
|
+
super(options);
|
|
1030
|
+
}
|
|
1031
|
+
async get(key) {
|
|
1032
|
+
const entry = await super.get(key);
|
|
1033
|
+
this.__onCacheGet__?.({ key });
|
|
1034
|
+
return entry;
|
|
1035
|
+
}
|
|
1036
|
+
async set(value) {
|
|
1037
|
+
await super.set(value);
|
|
1038
|
+
this.__onCacheSet__?.({ key: value.key });
|
|
1039
|
+
}
|
|
1040
|
+
async remove(key) {
|
|
1041
|
+
await super.remove(key);
|
|
1042
|
+
this.__onCacheRemove__?.({ key });
|
|
1043
|
+
}
|
|
1044
|
+
async evict(expectSize) {
|
|
1045
|
+
await this.evictExpired();
|
|
1046
|
+
let deficitSize = expectSize - (await this.getSize()).free;
|
|
1047
|
+
if (deficitSize <= 0) return true;
|
|
1048
|
+
const tx = (await this.openDB()).transaction([
|
|
1049
|
+
"metadata",
|
|
1050
|
+
"response",
|
|
1051
|
+
"visits"
|
|
1052
|
+
], "readwrite");
|
|
1053
|
+
const metadataStore = tx.objectStore("metadata");
|
|
1054
|
+
const visitsStore = tx.objectStore("visits");
|
|
1055
|
+
const keys = [];
|
|
1056
|
+
let cursor = await visitsStore.index("lastVisitedAt").openCursor();
|
|
1057
|
+
while (deficitSize > 0 && cursor) {
|
|
1058
|
+
const metadata = await metadataStore.get(cursor.value.key);
|
|
1059
|
+
if (!metadata) {
|
|
1060
|
+
await cursor.delete();
|
|
1061
|
+
cursor = await cursor.continue();
|
|
1062
|
+
continue;
|
|
1063
|
+
}
|
|
1064
|
+
deficitSize -= metadata.size;
|
|
1065
|
+
keys.push(cursor.value.key);
|
|
1066
|
+
cursor = await cursor.continue();
|
|
1067
|
+
}
|
|
1068
|
+
if (deficitSize > 0) {
|
|
1069
|
+
this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
|
|
1070
|
+
tx.abort();
|
|
1071
|
+
return false;
|
|
1072
|
+
}
|
|
1073
|
+
await this.__remove__(tx, keys);
|
|
1074
|
+
await tx.done;
|
|
1075
|
+
this.__onCacheEvict__?.({ keys });
|
|
1076
|
+
return true;
|
|
1077
|
+
}
|
|
1078
|
+
};
|
|
1079
|
+
//#endregion
|
|
1080
|
+
//#region src/storage/indexed-db-storage/ttl-indexed-db-storage.ts
|
|
1081
|
+
var TTLIndexedDBStorage = class extends BaseIndexedDBStorage {
|
|
1082
|
+
constructor(options) {
|
|
1083
|
+
super(options);
|
|
1084
|
+
}
|
|
1085
|
+
async get(key) {
|
|
1086
|
+
const entry = await super.get(key);
|
|
1087
|
+
this.__onCacheGet__?.({ key });
|
|
1088
|
+
return entry;
|
|
1089
|
+
}
|
|
1090
|
+
async set(value) {
|
|
1091
|
+
await super.set(value);
|
|
1092
|
+
this.__onCacheSet__?.({ key: value.key });
|
|
1093
|
+
}
|
|
1094
|
+
async remove(key) {
|
|
1095
|
+
await super.remove(key);
|
|
1096
|
+
this.__onCacheRemove__?.({ key });
|
|
1097
|
+
}
|
|
1098
|
+
async evict(expectSize) {
|
|
1099
|
+
await this.evictExpired();
|
|
1100
|
+
let deficitSize = expectSize - (await this.getSize()).free;
|
|
1101
|
+
if (deficitSize <= 0) return true;
|
|
1102
|
+
const tx = (await this.openDB()).transaction([
|
|
1103
|
+
"metadata",
|
|
1104
|
+
"response",
|
|
1105
|
+
"visits"
|
|
1106
|
+
], "readwrite");
|
|
1107
|
+
const metadataStore = tx.objectStore("metadata");
|
|
1108
|
+
const keys = [];
|
|
1109
|
+
let cursor = await metadataStore.index("expiredAt").openCursor();
|
|
1110
|
+
while (deficitSize > 0 && cursor) {
|
|
1111
|
+
const metadata = await metadataStore.get(cursor.value.key);
|
|
1112
|
+
if (!metadata) {
|
|
1113
|
+
await cursor.delete();
|
|
1114
|
+
cursor = await cursor.continue();
|
|
1115
|
+
continue;
|
|
1116
|
+
}
|
|
1117
|
+
deficitSize -= metadata.size;
|
|
1118
|
+
keys.push(cursor.value.key);
|
|
1119
|
+
cursor = await cursor.continue();
|
|
1120
|
+
}
|
|
1121
|
+
if (deficitSize > 0) {
|
|
1122
|
+
this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
|
|
1123
|
+
tx.abort();
|
|
1124
|
+
return false;
|
|
1125
|
+
}
|
|
1126
|
+
await this.__remove__(tx, keys);
|
|
1127
|
+
await tx.done;
|
|
1128
|
+
this.__onCacheEvict__?.({ keys });
|
|
1129
|
+
return true;
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
//#endregion
|
|
1133
|
+
//#region src/storage/indexed-db-storage/indexed-db-storage.ts
|
|
1134
|
+
var IndexedDBStorage = class extends KeqCacheStorage {
|
|
1135
|
+
constructor(options) {
|
|
1136
|
+
super();
|
|
1137
|
+
_defineProperty(this, "storage", void 0);
|
|
1138
|
+
const eviction = options?.eviction || "lru";
|
|
1139
|
+
if (eviction === "random") this.storage = new RandomIndexedDBStorage(options);
|
|
1140
|
+
else if (eviction === "lfu") this.storage = new LFUIndexedDBStorage(options);
|
|
1141
|
+
else if (eviction === "lru") this.storage = new LRUIndexedDBStorage(options);
|
|
1142
|
+
else if (eviction === "ttl") this.storage = new TTLIndexedDBStorage(options);
|
|
1143
|
+
else throw new CacheException(`Not Supported Eviction: ${String(options?.eviction)}`);
|
|
1144
|
+
}
|
|
1145
|
+
set(entry) {
|
|
1146
|
+
return this.storage.set(entry);
|
|
1147
|
+
}
|
|
1148
|
+
get(key) {
|
|
1149
|
+
return this.storage.get(key);
|
|
1150
|
+
}
|
|
1151
|
+
remove(key) {
|
|
1152
|
+
return this.storage.remove(key);
|
|
1153
|
+
}
|
|
1154
|
+
};
|
|
1155
|
+
//#endregion
|
|
1156
|
+
//#region src/storage/multi-tier-storage/multi-tier-storage.ts
|
|
1157
|
+
/**
|
|
1158
|
+
* @en Multi-tier cache storage that manages multiple KeqCacheStorage instances in tiers
|
|
1159
|
+
* @zh 多层缓存存储,管理多个分层的 KeqCacheStorage 实例
|
|
1160
|
+
*/
|
|
1161
|
+
var MultiTierStorage = class extends KeqCacheStorage {
|
|
1162
|
+
/**
|
|
1163
|
+
* @param storages Array of storage instances ordered by performance (fastest first)
|
|
1164
|
+
* @zh 按性价比排序的存储实例数组,排在前面的成本低,排在后面的成本高
|
|
1165
|
+
*/
|
|
1166
|
+
constructor(options) {
|
|
1167
|
+
super();
|
|
1168
|
+
_defineProperty(this, "storages", void 0);
|
|
1169
|
+
if (!options.tiers || options.tiers.length === 0) throw new Error("At least one storage instance is required");
|
|
1170
|
+
this.storages = [...options.tiers];
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* @en Get cache entry, searching from lowest to highest tier
|
|
1174
|
+
* @zh 获取缓存条目,从最底层到高层依次搜索
|
|
1175
|
+
*/
|
|
1176
|
+
async get(key) {
|
|
1177
|
+
for (let i = 0; i < this.storages.length; i++) {
|
|
1178
|
+
const entry = await this.storages[i].get(key);
|
|
1179
|
+
if (entry) {
|
|
1180
|
+
if (i > 0) await this.syncToLowerTiers(entry, i);
|
|
1181
|
+
return entry;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* @en Set cache entry to all tiers concurrently
|
|
1187
|
+
* @zh 并发写入所有层的缓存
|
|
1188
|
+
*/
|
|
1189
|
+
async set(entry) {
|
|
1190
|
+
const promises = this.storages.map(async (storage) => storage.set(entry));
|
|
1191
|
+
await Promise.all(promises);
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* @en Remove cache entry from all tiers
|
|
1195
|
+
* @zh 从所有层删除缓存条目
|
|
1196
|
+
*/
|
|
1197
|
+
async remove(key) {
|
|
1198
|
+
const promises = this.storages.map(async (storage) => storage.remove(key));
|
|
1199
|
+
await Promise.all(promises);
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* @en Sync cache entry to all lower tiers (tiers with index < currentTierIndex)
|
|
1203
|
+
* @zh 将缓存条目同步到所有低层存储(索引小于当前层的存储)
|
|
1204
|
+
*/
|
|
1205
|
+
async syncToLowerTiers(entry, currentTierIndex) {
|
|
1206
|
+
const lowerTierStorages = this.storages.slice(0, currentTierIndex);
|
|
1207
|
+
if (lowerTierStorages.length === 0) return;
|
|
1208
|
+
const promises = lowerTierStorages.map(async (storage) => storage.set(entry.clone()));
|
|
1209
|
+
try {
|
|
1210
|
+
await Promise.all(promises);
|
|
1211
|
+
} catch (error) {
|
|
1212
|
+
console.warn("Failed to sync cache entry to lower tiers:", error);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
};
|
|
1216
|
+
//#endregion
|
|
1217
|
+
//#region src/storage/tier-storage/tier-storage.ts
|
|
1218
|
+
/**
|
|
1219
|
+
* @en Two-tier cache storage that combines MemoryStorage (L1) and IndexedDBStorage (L2)
|
|
1220
|
+
* @zh 二级缓存存储,结合了MemoryStorage(一级)和IndexedDBStorage(二级)
|
|
1221
|
+
*
|
|
1222
|
+
* This is a convenience wrapper around MultiTierStorage that provides:
|
|
1223
|
+
* - Fast in-memory cache (L1)
|
|
1224
|
+
* - Persistent IndexedDB cache (L2)
|
|
1225
|
+
* - Simplified configuration options
|
|
1226
|
+
*/
|
|
1227
|
+
var TierStorage = class extends MultiTierStorage {
|
|
1228
|
+
constructor(options) {
|
|
1229
|
+
const memoryStorage = options?.memory instanceof MemoryStorage ? options.memory : new MemoryStorage(options?.memory);
|
|
1230
|
+
const indexedDBStorage = options?.indexedDB instanceof IndexedDBStorage ? options.indexedDB : new IndexedDBStorage(options?.indexedDB);
|
|
1231
|
+
super({ tiers: [memoryStorage, indexedDBStorage] });
|
|
1232
|
+
}
|
|
1233
|
+
};
|
|
1234
|
+
//#endregion
|
|
1235
|
+
export { Eviction, IndexedDBStorage, KeqCacheStorage, MemoryStorage, MultiTierStorage, Size, Strategy, TierStorage, cache };
|
|
1236
|
+
|
|
1237
|
+
//# sourceMappingURL=index.mjs.map
|