tricache 1.0.0 → 1.0.2
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/README.md +36 -28
- package/dist/{cache-service-pJ-nFcVj.d.ts → cache-service-B108FzER.d.ts} +63 -3
- package/dist/cache-service-CWI4jkS7.d.cts +963 -0
- package/dist/{cache-service-26GCO3UU.js → cache-service-GYPZFB7O.js} +2 -2
- package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
- package/dist/{chunk-2AJKEQMZ.js → chunk-H4GFLQ26.js} +7 -4
- package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
- package/dist/chunk-OYDU3SMI.js +89 -0
- package/dist/{chunk-VXFTFZ5N.js → chunk-VNRDL5RA.js} +117 -138
- package/dist/{chunk-7SZSQS55.js → chunk-WYF7NOAT.js} +1831 -759
- package/dist/cli.cjs +1757 -777
- package/dist/cli.d.cts +12 -0
- package/dist/cli.js +27 -6
- package/dist/dashboard/index.cjs +111 -29
- package/dist/dashboard/index.d.cts +185 -0
- package/dist/dashboard/index.d.ts +5 -3
- package/dist/dashboard/index.js +1 -1
- package/dist/drizzle/index.cjs +1757 -773
- package/dist/drizzle/index.d.cts +46 -0
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +280 -153
- package/dist/edge/index.d.cts +127 -0
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +254 -147
- package/dist/hono/index.cjs +1805 -793
- package/dist/hono/index.d.cts +93 -0
- package/dist/hono/index.d.ts +7 -5
- package/dist/hono/index.js +16 -9
- package/dist/{hono-DuNvciyq.d.ts → hono-3rDMYVtY.d.ts} +23 -3
- package/dist/hono-C886ok3X.d.cts +391 -0
- package/dist/http/index.cjs +1946 -842
- package/dist/http/index.d.cts +103 -0
- package/dist/http/index.d.ts +13 -5
- package/dist/http/index.js +137 -57
- package/dist/index.cjs +2073 -937
- package/dist/index.d.cts +1006 -0
- package/dist/index.d.ts +40 -8
- package/dist/index.js +8 -8
- package/dist/murmur3-CMGw9PbV.d.cts +98 -0
- package/dist/nestjs/index.cjs +1849 -806
- package/dist/nestjs/index.d.cts +285 -0
- package/dist/nestjs/index.d.ts +103 -30
- package/dist/nestjs/index.js +115 -32
- package/dist/next/index.cjs +1913 -887
- package/dist/next/index.d.cts +232 -0
- package/dist/next/index.d.ts +33 -11
- package/dist/next/index.js +174 -107
- package/dist/prisma/index.cjs +11 -1
- package/dist/prisma/index.d.cts +62 -0
- package/dist/prisma/index.d.ts +2 -2
- package/dist/prisma/index.js +11 -1
- package/dist/serialize-worker.d.cts +2 -0
- package/dist/{types-5HXqBfAf.d.ts → types-BA4pz1A0.d.cts} +26 -9
- package/dist/types-BA4pz1A0.d.ts +1519 -0
- package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
- package/dist/utils-Bvzn-rKS.d.ts +29 -0
- package/package.json +96 -36
- package/src/adapters/auto-pipeliner.ts +107 -24
- package/src/adapters/node-redis.ts +123 -25
- package/src/cache-service.ts +1238 -659
- package/src/cli.ts +24 -6
- package/src/cross-region.ts +21 -3
- package/src/dashboard/handler.ts +26 -6
- package/src/dashboard/html.ts +35 -10
- package/src/dashboard/index.ts +35 -13
- package/src/dashboard/types.ts +3 -1
- package/src/disk-tier.ts +171 -65
- package/src/edge/cache.ts +202 -129
- package/src/edge/hono.ts +23 -6
- package/src/edge/types.ts +2 -0
- package/src/hono/index.ts +19 -9
- package/src/http/express.ts +110 -26
- package/src/http/fastify.ts +38 -28
- package/src/http/utils.ts +54 -20
- package/src/ipc-telemetry.ts +35 -9
- package/src/nestjs/decorators.ts +1 -1
- package/src/nestjs/index.ts +2 -0
- package/src/nestjs/keyv-adapter.ts +68 -0
- package/src/nestjs/tricache.module.ts +13 -11
- package/src/nestjs/tricache.store.ts +51 -20
- package/src/nestjs/types.ts +29 -15
- package/src/next/cache-handler.ts +132 -57
- package/src/next/index.ts +1 -0
- package/src/next/isr-handler.ts +1 -4
- package/src/next/types.ts +21 -0
- package/src/prisma/index.ts +12 -1
- package/src/smart-memory-cache.ts +65 -27
- package/src/types.ts +27 -2
- package/src/utils/cgroup.ts +12 -0
- package/src/utils/deadline.ts +14 -0
- package/src/utils/diagnostics.ts +27 -0
- package/src/utils/redis-slot.ts +13 -0
- package/src/wasm/bloom-filter-wasm.ts +9 -4
- package/src/worker-pool.ts +17 -8
- package/src/wtiny-lfu.ts +20 -6
- package/dist/chunk-BQRURGGD.js +0 -60
package/dist/edge/index.js
CHANGED
|
@@ -11,12 +11,12 @@ import {
|
|
|
11
11
|
uint8ArrayToBase64,
|
|
12
12
|
uint8ArrayToUtf8,
|
|
13
13
|
utf8ToUint8Array
|
|
14
|
-
} from "../chunk-
|
|
14
|
+
} from "../chunk-H4GFLQ26.js";
|
|
15
15
|
import {
|
|
16
16
|
buildEdgeDeterministicKey,
|
|
17
17
|
computeEdgeETag,
|
|
18
18
|
honoEdgeCache
|
|
19
|
-
} from "../chunk-
|
|
19
|
+
} from "../chunk-EUKB7RKP.js";
|
|
20
20
|
|
|
21
21
|
// src/edge/crypto.ts
|
|
22
22
|
var PREFIX_256 = "enc:v1:";
|
|
@@ -152,7 +152,8 @@ var WebCryptoEncryption = class {
|
|
|
152
152
|
};
|
|
153
153
|
|
|
154
154
|
// src/edge/cache.ts
|
|
155
|
-
var
|
|
155
|
+
var NAMESPACE_TAG = "_tricache_namespace_";
|
|
156
|
+
var EdgeCacheService = class _EdgeCacheService {
|
|
156
157
|
maxKeys;
|
|
157
158
|
maxBytes;
|
|
158
159
|
defaultTtl;
|
|
@@ -161,6 +162,10 @@ var EdgeCacheService = class {
|
|
|
161
162
|
enc;
|
|
162
163
|
tracer;
|
|
163
164
|
bloomFilter;
|
|
165
|
+
bloomAuthoritative;
|
|
166
|
+
globalEpoch = 0;
|
|
167
|
+
lastGlobalMutationAt = 0;
|
|
168
|
+
keyEpochs = /* @__PURE__ */ new Map();
|
|
164
169
|
// L1 Dual-bounded LRU
|
|
165
170
|
l1 = /* @__PURE__ */ new Map();
|
|
166
171
|
currentBytes = 0;
|
|
@@ -169,6 +174,7 @@ var EdgeCacheService = class {
|
|
|
169
174
|
// Tag indexing for edge
|
|
170
175
|
tagIndex = /* @__PURE__ */ new Map();
|
|
171
176
|
// Tag version in-isolate cache with 800ms micro-TTL (prevents HTTP fetch amplification on L1 hits)
|
|
177
|
+
static MAX_TAG_VERSION_CACHE = 1e4;
|
|
172
178
|
tagVersionCache = /* @__PURE__ */ new Map();
|
|
173
179
|
tagVersionTtlMs = 800;
|
|
174
180
|
constructor(options) {
|
|
@@ -179,6 +185,8 @@ var EdgeCacheService = class {
|
|
|
179
185
|
this.remoteStorage = options?.remoteStorage ?? null;
|
|
180
186
|
this.tracer = options?.tracer ?? null;
|
|
181
187
|
this.enc = new WebCryptoEncryption(options?.encryption);
|
|
188
|
+
this.bloomAuthoritative = options?.bloomFilterAuthoritative ?? false;
|
|
189
|
+
if (!Number.isInteger(this.maxKeys) || this.maxKeys < 0 || !Number.isFinite(this.maxBytes) || this.maxBytes < 0 || !Number.isFinite(this.defaultTtl) || this.defaultTtl < 0) throw new Error("Edge cache limits and TTL must be finite non-negative values");
|
|
182
190
|
if (options?.bloomFilter === true) {
|
|
183
191
|
try {
|
|
184
192
|
this.bloomFilter = new WasmBloomFilter();
|
|
@@ -203,6 +211,66 @@ var EdgeCacheService = class {
|
|
|
203
211
|
_estimateBytes(key, serialized) {
|
|
204
212
|
return (key.length + serialized.length) * 2 + 64;
|
|
205
213
|
}
|
|
214
|
+
fence(key) {
|
|
215
|
+
return `${this.globalEpoch}:${this.keyEpochs.get(key) ?? 0}`;
|
|
216
|
+
}
|
|
217
|
+
mutate(key) {
|
|
218
|
+
this.keyEpochs.set(key, (this.keyEpochs.get(key) ?? 0) + 1);
|
|
219
|
+
this.inflight.delete(key);
|
|
220
|
+
if (this.keyEpochs.size > 5e4) {
|
|
221
|
+
this.globalEpoch++;
|
|
222
|
+
this.keyEpochs.clear();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
invalidateWork() {
|
|
226
|
+
this.globalEpoch++;
|
|
227
|
+
this.lastGlobalMutationAt = Date.now();
|
|
228
|
+
this.inflight.clear();
|
|
229
|
+
}
|
|
230
|
+
storeL1(key, entry) {
|
|
231
|
+
this._deleteL1(key);
|
|
232
|
+
if (entry.bytes > this.maxBytes || this.maxKeys === 0 || entry.staleUntil <= Date.now()) return false;
|
|
233
|
+
this._evictIfNeeded(entry.bytes);
|
|
234
|
+
this.l1.set(key, entry);
|
|
235
|
+
this.currentBytes += entry.bytes;
|
|
236
|
+
for (const tag of entry.tags ?? []) {
|
|
237
|
+
const members = this.tagIndex.get(tag) ?? /* @__PURE__ */ new Set();
|
|
238
|
+
members.add(key);
|
|
239
|
+
this.tagIndex.set(tag, members);
|
|
240
|
+
}
|
|
241
|
+
this.bloomFilter?.add(key);
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
async commit(key, value, ttl, options, versions, epoch) {
|
|
245
|
+
const namespaced = this.nk(key);
|
|
246
|
+
if (this.fence(namespaced) !== epoch) return;
|
|
247
|
+
const serialized = JSON.stringify(value);
|
|
248
|
+
if (serialized === void 0) return;
|
|
249
|
+
const now = Date.now();
|
|
250
|
+
const entry = {
|
|
251
|
+
value,
|
|
252
|
+
serialized,
|
|
253
|
+
bytes: this._estimateBytes(namespaced, serialized),
|
|
254
|
+
expiresAt: now + ttl * 1e3,
|
|
255
|
+
staleUntil: now + (ttl + (options?.swr ?? 0)) * 1e3,
|
|
256
|
+
tags: options?.tags ? [...new Set(options.tags)] : void 0,
|
|
257
|
+
tagVersions: versions
|
|
258
|
+
};
|
|
259
|
+
this.storeL1(namespaced, entry);
|
|
260
|
+
if (this.remoteStorage) {
|
|
261
|
+
const envelope = JSON.stringify({
|
|
262
|
+
__t_edge: 2,
|
|
263
|
+
__t_val: value,
|
|
264
|
+
__t_tv: versions,
|
|
265
|
+
__t_exp: entry.expiresAt,
|
|
266
|
+
__t_stale: entry.staleUntil,
|
|
267
|
+
__t_tags: entry.tags
|
|
268
|
+
});
|
|
269
|
+
const payload = this.enc.isEnabled ? await this.enc.encrypt(envelope) : envelope;
|
|
270
|
+
if (this.fence(namespaced) !== epoch) return;
|
|
271
|
+
await this.remoteStorage.set(namespaced, payload, ttl + (options?.swr ?? 0));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
206
274
|
_evictIfNeeded(newBytes) {
|
|
207
275
|
while (this.currentBytes + newBytes > this.maxBytes && this.l1.size > 0) {
|
|
208
276
|
const oldestKey = this.l1.keys().next().value;
|
|
@@ -251,6 +319,22 @@ var EdgeCacheService = class {
|
|
|
251
319
|
}
|
|
252
320
|
};
|
|
253
321
|
}
|
|
322
|
+
_setTagVersionCache(tag, version, expiresAt) {
|
|
323
|
+
if (this.tagVersionCache.size >= _EdgeCacheService.MAX_TAG_VERSION_CACHE && !this.tagVersionCache.has(tag)) {
|
|
324
|
+
const now = Date.now();
|
|
325
|
+
for (const [k, v] of this.tagVersionCache) {
|
|
326
|
+
if (v.expiresAt <= now) {
|
|
327
|
+
this.tagVersionCache.delete(k);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (this.tagVersionCache.size >= _EdgeCacheService.MAX_TAG_VERSION_CACHE) {
|
|
331
|
+
const oldestKey = this.tagVersionCache.keys().next().value;
|
|
332
|
+
if (oldestKey !== void 0) this.tagVersionCache.delete(oldestKey);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
const known = this.tagVersionCache.get(tag)?.version ?? 0;
|
|
336
|
+
this.tagVersionCache.set(tag, { version: Math.max(known, version), expiresAt });
|
|
337
|
+
}
|
|
254
338
|
async _getTagVersion(tag) {
|
|
255
339
|
const cached = this.tagVersionCache.get(tag);
|
|
256
340
|
const now = Date.now();
|
|
@@ -272,12 +356,12 @@ var EdgeCacheService = class {
|
|
|
272
356
|
} else {
|
|
273
357
|
version = cached?.version ?? 1;
|
|
274
358
|
}
|
|
275
|
-
this.
|
|
276
|
-
return version;
|
|
359
|
+
this._setTagVersionCache(tag, version, now + this.tagVersionTtlMs);
|
|
360
|
+
return this.tagVersionCache.get(tag)?.version ?? version;
|
|
277
361
|
}
|
|
278
362
|
async _batchGetTagVersions(tags) {
|
|
279
363
|
const now = Date.now();
|
|
280
|
-
const result =
|
|
364
|
+
const result = /* @__PURE__ */ Object.create(null);
|
|
281
365
|
const missingTags = [];
|
|
282
366
|
for (const t of tags) {
|
|
283
367
|
const c = this.tagVersionCache.get(t);
|
|
@@ -295,8 +379,8 @@ var EdgeCacheService = class {
|
|
|
295
379
|
for (let i = 0; i < missingTags.length; i++) {
|
|
296
380
|
const t = missingTags[i];
|
|
297
381
|
const v = remoteVersions[this.nk(t)] ?? remoteVersions[t] ?? 1;
|
|
298
|
-
result[t] = v;
|
|
299
|
-
this.
|
|
382
|
+
result[t] = Math.max(this.tagVersionCache.get(t)?.version ?? 0, v);
|
|
383
|
+
this._setTagVersionCache(t, v, now + this.tagVersionTtlMs);
|
|
300
384
|
}
|
|
301
385
|
} else {
|
|
302
386
|
await Promise.all(missingTags.map(async (t) => {
|
|
@@ -323,7 +407,7 @@ var EdgeCacheService = class {
|
|
|
323
407
|
const span = this._startSpan("tricache.get");
|
|
324
408
|
span.setAttribute("cache.key", key);
|
|
325
409
|
const nsKey = this.nk(key);
|
|
326
|
-
|
|
410
|
+
let now = Date.now();
|
|
327
411
|
const resolvedTtl = ttlSeconds ?? this.defaultTtl;
|
|
328
412
|
try {
|
|
329
413
|
const l1Entry = this.l1.get(nsKey);
|
|
@@ -339,6 +423,7 @@ var EdgeCacheService = class {
|
|
|
339
423
|
}
|
|
340
424
|
}
|
|
341
425
|
}
|
|
426
|
+
now = Date.now();
|
|
342
427
|
if (isTagStale) {
|
|
343
428
|
this._deleteL1(nsKey);
|
|
344
429
|
} else {
|
|
@@ -350,85 +435,115 @@ var EdgeCacheService = class {
|
|
|
350
435
|
span.setAttribute("cache.hit_tier", "l1");
|
|
351
436
|
return l1Entry.value;
|
|
352
437
|
}
|
|
353
|
-
if (l1Entry.staleUntil > now && fetchFn) {
|
|
438
|
+
if (l1Entry.staleUntil > now && fetchFn && (options?.swr ?? 0) > 0) {
|
|
354
439
|
span.setAttribute("cache.hit", true);
|
|
355
440
|
span.setAttribute("cache.item.tier", "memory");
|
|
356
441
|
span.setAttribute("cache.hit_tier", "l1");
|
|
357
442
|
span.setAttribute("cache.stale", true);
|
|
358
|
-
const revalPromise = this._revalidate(key, fetchFn, resolvedTtl, options);
|
|
443
|
+
const revalPromise = this._revalidate(key, fetchFn, resolvedTtl, { ...options, tags: options?.tags ?? l1Entry.tags });
|
|
359
444
|
if (options?.ctx?.waitUntil) {
|
|
360
445
|
options.ctx.waitUntil(revalPromise);
|
|
361
446
|
}
|
|
362
447
|
return l1Entry.value;
|
|
363
448
|
}
|
|
364
|
-
this._deleteL1(nsKey);
|
|
449
|
+
if (l1Entry.staleUntil <= now) this._deleteL1(nsKey);
|
|
365
450
|
}
|
|
366
451
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
452
|
+
const existing = this.inflight.get(nsKey);
|
|
453
|
+
if (existing) {
|
|
454
|
+
span.setAttribute("cache.stampede_coalesced", true);
|
|
455
|
+
return await existing;
|
|
456
|
+
}
|
|
457
|
+
const epoch = this.fence(nsKey);
|
|
458
|
+
const execution = Promise.resolve().then(async () => {
|
|
459
|
+
try {
|
|
460
|
+
if (this.remoteStorage) {
|
|
461
|
+
if (this.bloomAuthoritative && this.bloomFilter && !this.bloomFilter.mightContain(nsKey)) {
|
|
462
|
+
span.setAttribute("cache.bloom.filtered", true);
|
|
463
|
+
} else {
|
|
464
|
+
try {
|
|
465
|
+
const rawRemote = await this.remoteStorage.get(nsKey);
|
|
466
|
+
if (rawRemote !== null) {
|
|
467
|
+
const plain = this.enc.isEnabled ? await this.enc.decrypt(rawRemote) : rawRemote;
|
|
468
|
+
const parsedJson = JSON.parse(plain);
|
|
469
|
+
let parsed;
|
|
470
|
+
let activeTagVersions;
|
|
471
|
+
let isStaleRemote = false;
|
|
472
|
+
if (parsedJson && typeof parsedJson === "object" && "__t_val" in parsedJson && "__t_tv" in parsedJson) {
|
|
473
|
+
parsed = parsedJson.__t_val;
|
|
474
|
+
activeTagVersions = parsedJson.__t_tv;
|
|
475
|
+
if (!activeTagVersions || typeof activeTagVersions !== "object" || Array.isArray(activeTagVersions) || Object.values(activeTagVersions).some((version) => !Number.isSafeInteger(version) || version < 0)) throw new Error("Invalid remote tag versions");
|
|
476
|
+
const tagNames = Object.keys(activeTagVersions);
|
|
477
|
+
const currentVers = await this._batchGetTagVersions(tagNames);
|
|
478
|
+
for (const tag of tagNames) {
|
|
479
|
+
if (currentVers[tag] > activeTagVersions[tag]) {
|
|
480
|
+
isStaleRemote = true;
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
} else {
|
|
485
|
+
parsed = parsedJson;
|
|
486
|
+
}
|
|
487
|
+
if (!isStaleRemote) {
|
|
488
|
+
const freshUntil = Number(parsedJson?.__t_exp);
|
|
489
|
+
const staleUntil = Number(parsedJson?.__t_stale);
|
|
490
|
+
const versioned = parsedJson?.__t_edge === 2 && Number.isFinite(freshUntil) && Number.isFinite(staleUntil) && staleUntil >= freshUntil;
|
|
491
|
+
if (versioned && (staleUntil <= Date.now() || freshUntil <= Date.now() && (!(options?.swr ?? 0) || !fetchFn))) {
|
|
492
|
+
isStaleRemote = true;
|
|
493
|
+
} else {
|
|
494
|
+
if (versioned && this.fence(nsKey) === epoch) {
|
|
495
|
+
const serialized = JSON.stringify(parsed);
|
|
496
|
+
const tags2 = Array.isArray(parsedJson.__t_tags) ? parsedJson.__t_tags : Object.keys(activeTagVersions ?? {}).filter((tag) => tag !== NAMESPACE_TAG);
|
|
497
|
+
this.storeL1(nsKey, {
|
|
498
|
+
value: parsed,
|
|
499
|
+
serialized,
|
|
500
|
+
bytes: this._estimateBytes(nsKey, serialized),
|
|
501
|
+
expiresAt: freshUntil,
|
|
502
|
+
staleUntil,
|
|
503
|
+
tags: tags2,
|
|
504
|
+
tagVersions: activeTagVersions
|
|
505
|
+
});
|
|
506
|
+
if (freshUntil <= Date.now() && fetchFn) {
|
|
507
|
+
const refresh = execution.then(() => this._revalidate(key, fetchFn, resolvedTtl, { ...options, tags: tags2 }));
|
|
508
|
+
options?.ctx?.waitUntil(refresh);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
span.setAttribute("cache.hit", true);
|
|
512
|
+
span.setAttribute("cache.item.tier", "remote");
|
|
513
|
+
span.setAttribute("cache.hit_tier", "l2");
|
|
514
|
+
return parsed;
|
|
515
|
+
}
|
|
388
516
|
}
|
|
389
517
|
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
}
|
|
393
|
-
if (!isStaleRemote) {
|
|
394
|
-
await this.set(key, parsed, resolvedTtl, options);
|
|
395
|
-
span.setAttribute("cache.hit", true);
|
|
396
|
-
span.setAttribute("cache.item.tier", "remote");
|
|
397
|
-
span.setAttribute("cache.hit_tier", "l2");
|
|
398
|
-
return parsed;
|
|
518
|
+
} catch (remoteErr) {
|
|
519
|
+
span.recordException?.(remoteErr);
|
|
399
520
|
}
|
|
400
521
|
}
|
|
401
|
-
} catch (remoteErr) {
|
|
402
|
-
span.recordException?.(remoteErr);
|
|
403
522
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
span.setAttribute("cache.hit_tier", "miss");
|
|
409
|
-
return null;
|
|
410
|
-
}
|
|
411
|
-
let inflightPromise = this.inflight.get(nsKey);
|
|
412
|
-
let coalesced = false;
|
|
413
|
-
if (inflightPromise) {
|
|
414
|
-
coalesced = true;
|
|
415
|
-
} else {
|
|
416
|
-
inflightPromise = (async () => {
|
|
417
|
-
try {
|
|
418
|
-
const fresh = await fetchFn();
|
|
419
|
-
await this.set(key, fresh, resolvedTtl, options);
|
|
420
|
-
return fresh;
|
|
421
|
-
} finally {
|
|
422
|
-
this.inflight.delete(nsKey);
|
|
523
|
+
if (!fetchFn) {
|
|
524
|
+
span.setAttribute("cache.hit", false);
|
|
525
|
+
span.setAttribute("cache.hit_tier", "miss");
|
|
526
|
+
return null;
|
|
423
527
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
528
|
+
const tags = [.../* @__PURE__ */ new Set([NAMESPACE_TAG, ...options?.tags ?? []])];
|
|
529
|
+
const versions = await this._batchGetTagVersions(tags);
|
|
530
|
+
const fresh = await fetchFn();
|
|
531
|
+
const current = await this._batchGetTagVersions(tags);
|
|
532
|
+
if (this.fence(nsKey) === epoch && tags.every((tag) => current[tag] === versions[tag])) {
|
|
533
|
+
try {
|
|
534
|
+
await this.commit(key, fresh, resolvedTtl, options, versions, epoch);
|
|
535
|
+
} catch (error) {
|
|
536
|
+
span.recordException?.(error);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
span.setAttribute("cache.hit", false).setAttribute("cache.hit_tier", "miss");
|
|
540
|
+
return fresh;
|
|
541
|
+
} finally {
|
|
542
|
+
if (this.inflight.get(nsKey) === execution) this.inflight.delete(nsKey);
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
this.inflight.set(nsKey, execution);
|
|
546
|
+
return await execution;
|
|
432
547
|
} catch (err) {
|
|
433
548
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
434
549
|
span.recordException?.(err);
|
|
@@ -440,15 +555,19 @@ var EdgeCacheService = class {
|
|
|
440
555
|
async _revalidate(key, fetchFn, ttl, options) {
|
|
441
556
|
const nsKey = this.nk(key);
|
|
442
557
|
if (this.inflight.has(nsKey)) return;
|
|
443
|
-
const
|
|
558
|
+
const epoch = this.fence(nsKey);
|
|
559
|
+
const task = Promise.resolve().then(async () => {
|
|
444
560
|
try {
|
|
561
|
+
const tags = [.../* @__PURE__ */ new Set([NAMESPACE_TAG, ...options?.tags ?? []])];
|
|
562
|
+
const versions = await this._batchGetTagVersions(tags);
|
|
445
563
|
const fresh = await fetchFn();
|
|
446
|
-
await this.
|
|
564
|
+
const current = await this._batchGetTagVersions(tags);
|
|
565
|
+
if (this.fence(nsKey) === epoch && tags.every((tag) => current[tag] === versions[tag])) await this.commit(key, fresh, ttl, options, versions, epoch);
|
|
447
566
|
} catch {
|
|
448
567
|
} finally {
|
|
449
|
-
this.inflight.delete(nsKey);
|
|
568
|
+
if (this.inflight.get(nsKey) === task) this.inflight.delete(nsKey);
|
|
450
569
|
}
|
|
451
|
-
})
|
|
570
|
+
});
|
|
452
571
|
this.inflight.set(nsKey, task);
|
|
453
572
|
await task;
|
|
454
573
|
}
|
|
@@ -459,48 +578,15 @@ var EdgeCacheService = class {
|
|
|
459
578
|
const span = this._startSpan("tricache.set");
|
|
460
579
|
span.setAttribute("cache.key", key);
|
|
461
580
|
const nsKey = this.nk(key);
|
|
581
|
+
this.mutate(nsKey);
|
|
582
|
+
const epoch = this.fence(nsKey);
|
|
462
583
|
const resolvedTtl = ttlSeconds ?? this.defaultTtl;
|
|
463
|
-
const now = Date.now();
|
|
464
584
|
const optObj = Array.isArray(options) ? { tags: options } : options;
|
|
465
585
|
const swrSeconds = optObj?.swr ?? 0;
|
|
466
586
|
try {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
}
|
|
471
|
-
const serialized = JSON.stringify(value);
|
|
472
|
-
const bytes = this._estimateBytes(nsKey, serialized);
|
|
473
|
-
this._deleteL1(nsKey);
|
|
474
|
-
this._evictIfNeeded(bytes);
|
|
475
|
-
const entry = {
|
|
476
|
-
value,
|
|
477
|
-
serialized,
|
|
478
|
-
bytes,
|
|
479
|
-
expiresAt: now + resolvedTtl * 1e3,
|
|
480
|
-
staleUntil: now + (resolvedTtl + swrSeconds) * 1e3,
|
|
481
|
-
tags: optObj?.tags,
|
|
482
|
-
tagVersions: activeTagVersions
|
|
483
|
-
};
|
|
484
|
-
this.l1.set(nsKey, entry);
|
|
485
|
-
this.currentBytes += bytes;
|
|
486
|
-
if (optObj?.tags) {
|
|
487
|
-
for (const tag of optObj.tags) {
|
|
488
|
-
let s = this.tagIndex.get(tag);
|
|
489
|
-
if (!s) {
|
|
490
|
-
s = /* @__PURE__ */ new Set();
|
|
491
|
-
this.tagIndex.set(tag, s);
|
|
492
|
-
}
|
|
493
|
-
s.add(nsKey);
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
if (this.remoteStorage) {
|
|
497
|
-
const toStoreRaw = activeTagVersions ? JSON.stringify({ __t_val: value, __t_tv: activeTagVersions }) : serialized;
|
|
498
|
-
const payloadToStore = this.enc.isEnabled ? await this.enc.encrypt(toStoreRaw) : toStoreRaw;
|
|
499
|
-
await this.remoteStorage.set(nsKey, payloadToStore, resolvedTtl + swrSeconds);
|
|
500
|
-
}
|
|
501
|
-
if (this.bloomFilter) {
|
|
502
|
-
this.bloomFilter.add(nsKey);
|
|
503
|
-
}
|
|
587
|
+
if (!Number.isFinite(resolvedTtl) || resolvedTtl < 0 || !Number.isFinite(swrSeconds) || swrSeconds < 0) throw new Error("Edge TTL and SWR must be finite non-negative values");
|
|
588
|
+
const versions = await this._batchGetTagVersions([.../* @__PURE__ */ new Set([NAMESPACE_TAG, ...optObj?.tags ?? []])]);
|
|
589
|
+
await this.commit(key, value, resolvedTtl, optObj, versions, epoch);
|
|
504
590
|
} catch (err) {
|
|
505
591
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
506
592
|
span.recordException?.(err);
|
|
@@ -517,6 +603,7 @@ var EdgeCacheService = class {
|
|
|
517
603
|
span.setAttribute("cache.key", key);
|
|
518
604
|
const nsKey = this.nk(key);
|
|
519
605
|
try {
|
|
606
|
+
this.mutate(nsKey);
|
|
520
607
|
this._deleteL1(nsKey);
|
|
521
608
|
if (this.remoteStorage) {
|
|
522
609
|
await this.remoteStorage.delete(nsKey);
|
|
@@ -529,6 +616,44 @@ var EdgeCacheService = class {
|
|
|
529
616
|
span.end();
|
|
530
617
|
}
|
|
531
618
|
}
|
|
619
|
+
/**
|
|
620
|
+
* Executes invalidation for a single already-resolved concrete tag without
|
|
621
|
+
* re-entering wildcard/cascade resolution.
|
|
622
|
+
*/
|
|
623
|
+
async _invalidateConcreteTag(tag) {
|
|
624
|
+
this.invalidateWork();
|
|
625
|
+
const previous = this.tagVersionCache.get(tag)?.version ?? 1;
|
|
626
|
+
let newVer = 1;
|
|
627
|
+
if (this.remoteStorage) {
|
|
628
|
+
try {
|
|
629
|
+
if (typeof this.remoteStorage.incrementTagVersion === "function") {
|
|
630
|
+
newVer = await this.remoteStorage.incrementTagVersion(this.nk(tag));
|
|
631
|
+
if (newVer <= previous) newVer = await this.remoteStorage.incrementTagVersion(this.nk(tag));
|
|
632
|
+
newVer = Math.max(previous + 1, newVer);
|
|
633
|
+
} else {
|
|
634
|
+
const key = this.nk(`tag_ver:${tag}`);
|
|
635
|
+
const current = await this._getTagVersion(tag);
|
|
636
|
+
newVer = Math.max(current + 1, Date.now());
|
|
637
|
+
await this.remoteStorage.set(key, String(newVer));
|
|
638
|
+
}
|
|
639
|
+
} catch {
|
|
640
|
+
const current = this.tagVersionCache.get(tag)?.version ?? 1;
|
|
641
|
+
newVer = current + 1;
|
|
642
|
+
}
|
|
643
|
+
} else {
|
|
644
|
+
const current = this.tagVersionCache.get(tag)?.version ?? 1;
|
|
645
|
+
newVer = current + 1;
|
|
646
|
+
}
|
|
647
|
+
this._setTagVersionCache(tag, newVer, Date.now() + this.tagVersionTtlMs);
|
|
648
|
+
const keys = this.tagIndex.get(tag);
|
|
649
|
+
if (keys) {
|
|
650
|
+
const keysArr = Array.from(keys);
|
|
651
|
+
for (const k of keysArr) {
|
|
652
|
+
this._deleteL1(k);
|
|
653
|
+
}
|
|
654
|
+
this.tagIndex.delete(tag);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
532
657
|
/**
|
|
533
658
|
* Invalidates all cached entries associated with a tag.
|
|
534
659
|
*/
|
|
@@ -555,38 +680,11 @@ var EdgeCacheService = class {
|
|
|
555
680
|
}
|
|
556
681
|
if (shouldCascade && !isPattern) matched.add(tag);
|
|
557
682
|
for (const t of matched) {
|
|
558
|
-
await this.
|
|
683
|
+
await this._invalidateConcreteTag(t);
|
|
559
684
|
}
|
|
560
685
|
return;
|
|
561
686
|
}
|
|
562
|
-
|
|
563
|
-
if (this.remoteStorage) {
|
|
564
|
-
try {
|
|
565
|
-
if (typeof this.remoteStorage.incrementTagVersion === "function") {
|
|
566
|
-
newVer = await this.remoteStorage.incrementTagVersion(this.nk(tag));
|
|
567
|
-
} else {
|
|
568
|
-
const key = this.nk(`tag_ver:${tag}`);
|
|
569
|
-
const current = await this._getTagVersion(tag);
|
|
570
|
-
newVer = Math.max(current + 1, Date.now());
|
|
571
|
-
await this.remoteStorage.set(key, String(newVer));
|
|
572
|
-
}
|
|
573
|
-
} catch {
|
|
574
|
-
const current = this.tagVersionCache.get(tag)?.version ?? 1;
|
|
575
|
-
newVer = current + 1;
|
|
576
|
-
}
|
|
577
|
-
} else {
|
|
578
|
-
const current = this.tagVersionCache.get(tag)?.version ?? 1;
|
|
579
|
-
newVer = current + 1;
|
|
580
|
-
}
|
|
581
|
-
this.tagVersionCache.set(tag, { version: newVer, expiresAt: Date.now() + 6e4 });
|
|
582
|
-
const keys = this.tagIndex.get(tag);
|
|
583
|
-
if (keys) {
|
|
584
|
-
const keysArr = Array.from(keys);
|
|
585
|
-
for (const k of keysArr) {
|
|
586
|
-
this._deleteL1(k);
|
|
587
|
-
}
|
|
588
|
-
this.tagIndex.delete(tag);
|
|
589
|
-
}
|
|
687
|
+
await this._invalidateConcreteTag(tag);
|
|
590
688
|
} catch (err) {
|
|
591
689
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
592
690
|
span.recordException?.(err);
|
|
@@ -601,6 +699,8 @@ var EdgeCacheService = class {
|
|
|
601
699
|
async clear() {
|
|
602
700
|
const span = this._startSpan("tricache.clear");
|
|
603
701
|
try {
|
|
702
|
+
const previousNamespace = await this._getTagVersion(NAMESPACE_TAG);
|
|
703
|
+
this.invalidateWork();
|
|
604
704
|
this.l1.clear();
|
|
605
705
|
this.currentBytes = 0;
|
|
606
706
|
this.tagIndex.clear();
|
|
@@ -609,8 +709,10 @@ var EdgeCacheService = class {
|
|
|
609
709
|
this.bloomFilter.reset();
|
|
610
710
|
}
|
|
611
711
|
if (this.remoteStorage) {
|
|
612
|
-
await this.remoteStorage.clear?.(this.namespace
|
|
712
|
+
await this.remoteStorage.clear?.(this.namespace ? this.namespace + ":" : void 0);
|
|
613
713
|
}
|
|
714
|
+
this._setTagVersionCache(NAMESPACE_TAG, previousNamespace, Date.now() + this.tagVersionTtlMs);
|
|
715
|
+
await this._invalidateConcreteTag(NAMESPACE_TAG);
|
|
614
716
|
} catch (err) {
|
|
615
717
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
616
718
|
span.recordException?.(err);
|
|
@@ -624,6 +726,7 @@ var EdgeCacheService = class {
|
|
|
624
726
|
*/
|
|
625
727
|
async mget(keys, fetchFn, ttlSeconds) {
|
|
626
728
|
const span = this._startSpan("tricache.mget");
|
|
729
|
+
const epochs = new Map(keys.map((key) => [key, this.fence(this.nk(key))]));
|
|
627
730
|
span.setAttribute("cache.batch.size", keys.length);
|
|
628
731
|
try {
|
|
629
732
|
const results = Array.from({ length: keys.length }, () => null);
|
|
@@ -645,10 +748,10 @@ var EdgeCacheService = class {
|
|
|
645
748
|
const isMap = fetchedMap instanceof Map;
|
|
646
749
|
for (let j = 0; j < missKeys.length; j++) {
|
|
647
750
|
const k = missKeys[j];
|
|
648
|
-
const fetchedVal = isMap ? fetchedMap.get(k) : fetchedMap[k];
|
|
751
|
+
const fetchedVal = isMap ? fetchedMap.get(k) : Object.hasOwn(fetchedMap, k) ? fetchedMap[k] : void 0;
|
|
649
752
|
if (fetchedVal !== void 0) {
|
|
650
753
|
results[missIndices[j]] = fetchedVal;
|
|
651
|
-
await this.set(k, fetchedVal, ttlSeconds);
|
|
754
|
+
if (this.fence(this.nk(k)) === epochs.get(k)) await this.set(k, fetchedVal, ttlSeconds);
|
|
652
755
|
}
|
|
653
756
|
}
|
|
654
757
|
}
|
|
@@ -710,6 +813,7 @@ var EdgeCacheService = class {
|
|
|
710
813
|
* }
|
|
711
814
|
*/
|
|
712
815
|
async hydrate(source, options) {
|
|
816
|
+
const epoch = this.globalEpoch;
|
|
713
817
|
let rawStr = null;
|
|
714
818
|
if (source && typeof source === "object") {
|
|
715
819
|
if ("get" in source && typeof source.get === "function") {
|
|
@@ -748,13 +852,14 @@ var EdgeCacheService = class {
|
|
|
748
852
|
} catch {
|
|
749
853
|
return 0;
|
|
750
854
|
}
|
|
751
|
-
if (!payload || !Array.isArray(payload.entries)) {
|
|
855
|
+
if (!payload || payload.version !== 1 || !Array.isArray(payload.entries) || this.globalEpoch !== epoch) {
|
|
752
856
|
return 0;
|
|
753
857
|
}
|
|
754
858
|
const now = Date.now();
|
|
755
859
|
const maxAgeMs = options?.maxAgeMs ?? 72e5;
|
|
756
860
|
const tolerance = options?.clockSkewToleranceMs ?? 250;
|
|
757
861
|
const writtenAt = payload.writtenAt ?? 0;
|
|
862
|
+
if (!Number.isFinite(writtenAt) || writtenAt < this.lastGlobalMutationAt) return 0;
|
|
758
863
|
if (writtenAt - now > tolerance) {
|
|
759
864
|
return 0;
|
|
760
865
|
}
|
|
@@ -767,8 +872,10 @@ var EdgeCacheService = class {
|
|
|
767
872
|
if (!item || typeof item.key !== "string") continue;
|
|
768
873
|
if (item.expiresAt <= now && item.staleUntil <= now) continue;
|
|
769
874
|
const nsKey = this.nk(item.key);
|
|
875
|
+
if (this.l1.has(nsKey) || this.keyEpochs.has(nsKey) || !Number.isFinite(item.expiresAt) || !Number.isFinite(item.staleUntil)) continue;
|
|
770
876
|
const serialized = JSON.stringify(item.value);
|
|
771
877
|
const bytes = this._estimateBytes(nsKey, serialized);
|
|
878
|
+
if (bytes > this.maxBytes || this.maxKeys === 0) continue;
|
|
772
879
|
this._deleteL1(nsKey);
|
|
773
880
|
this._evictIfNeeded(bytes);
|
|
774
881
|
const entry = {
|
|
@@ -793,7 +900,7 @@ var EdgeCacheService = class {
|
|
|
793
900
|
}
|
|
794
901
|
}
|
|
795
902
|
if (this.bloomFilter) {
|
|
796
|
-
this.bloomFilter.add(
|
|
903
|
+
this.bloomFilter.add(nsKey);
|
|
797
904
|
}
|
|
798
905
|
imported++;
|
|
799
906
|
}
|