tricache 0.8.0 → 0.9.0
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 +4 -2
- package/dashboards/prometheus-scrape.yml +18 -0
- package/dist/{cache-service-CyLdpo77.d.ts → cache-service-DVvroyqA.d.ts} +16 -1
- package/dist/{cache-service-5MZXFIGK.js → cache-service-KI5ACF3F.js} +1 -1
- package/dist/chunk-BQRURGGD.js +60 -0
- package/dist/chunk-IITRFWHY.js +196 -0
- package/dist/{chunk-6PQ5MOWR.js → chunk-PL5J3ZNT.js} +370 -81
- package/dist/cli.cjs +392 -84
- package/dist/cli.js +22 -4
- package/dist/dashboard/index.d.ts +2 -2
- package/dist/drizzle/index.cjs +350 -81
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +99 -38
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +5 -5
- package/dist/hono/index.cjs +7760 -0
- package/dist/hono/index.d.ts +91 -0
- package/dist/hono/index.js +185 -0
- package/dist/{hono-DQRS1Xd7.d.ts → hono-VA9A1m0I.d.ts} +3 -2
- package/dist/http/index.cjs +449 -119
- package/dist/http/index.d.ts +6 -30
- package/dist/http/index.js +9 -59
- package/dist/index.cjs +370 -81
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/nestjs/index.cjs +350 -81
- package/dist/nestjs/index.d.ts +2 -2
- package/dist/nestjs/index.js +1 -1
- package/dist/next/index.cjs +350 -81
- package/dist/next/index.d.ts +2 -2
- package/dist/next/index.js +1 -1
- package/dist/prisma/index.d.ts +2 -2
- package/dist/{types-B6krw64X.d.ts → types-B_QkVwJ7.d.ts} +23 -0
- package/dist/utils-CEsU_Z0s.d.ts +27 -0
- package/package.json +16 -6
- package/src/cache-service.ts +302 -35
- package/src/cli.ts +21 -4
- package/src/edge/hono.ts +123 -42
- package/src/hono/index.ts +316 -0
- package/src/http/index.ts +5 -0
- package/src/ipc-telemetry.ts +29 -1
- package/src/types.ts +25 -0
- package/dist/chunk-ZTSWCFYO.js +0 -135
|
@@ -3306,6 +3306,26 @@ async function findActiveSockets() {
|
|
|
3306
3306
|
active.push(currentPipe);
|
|
3307
3307
|
} catch {
|
|
3308
3308
|
}
|
|
3309
|
+
try {
|
|
3310
|
+
const { execFileSync } = await import("child_process");
|
|
3311
|
+
const stdout = execFileSync(
|
|
3312
|
+
"powershell.exe",
|
|
3313
|
+
["-NoProfile", "-NonInteractive", "-Command", '[System.IO.Directory]::GetFiles("\\\\.\\pipe\\") | Where-Object { $_ -like "*tricache-*" }'],
|
|
3314
|
+
{ timeout: 1500, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }
|
|
3315
|
+
);
|
|
3316
|
+
const candidates = stdout.split(/\r?\n/).map((name) => name.trim()).filter((name) => name.length > 0 && !active.includes(name));
|
|
3317
|
+
await Promise.all(
|
|
3318
|
+
candidates.map(async (pipePath) => {
|
|
3319
|
+
const testClient = new IpcTelemetryClient(pipePath);
|
|
3320
|
+
try {
|
|
3321
|
+
await testClient.ping(100);
|
|
3322
|
+
active.push(pipePath);
|
|
3323
|
+
} catch {
|
|
3324
|
+
}
|
|
3325
|
+
})
|
|
3326
|
+
);
|
|
3327
|
+
} catch {
|
|
3328
|
+
}
|
|
3309
3329
|
return active;
|
|
3310
3330
|
}
|
|
3311
3331
|
const dir = process.env.TMPDIR || "/tmp";
|
|
@@ -3735,6 +3755,52 @@ var CacheService = class _CacheService {
|
|
|
3735
3755
|
streamClient = null;
|
|
3736
3756
|
_lastStreamId = "$";
|
|
3737
3757
|
_destroyed = false;
|
|
3758
|
+
_pendingDiskDeletes = /* @__PURE__ */ new Set();
|
|
3759
|
+
_globalEpoch = 0;
|
|
3760
|
+
_keyMutationEpochs = /* @__PURE__ */ new Map();
|
|
3761
|
+
/**
|
|
3762
|
+
* Pattern tombstones for disk tier.
|
|
3763
|
+
* Files in the disk tier are keyed by SHA-256 of namespacedKey.
|
|
3764
|
+
* Wildcard pattern deletes cannot scan the filesystem synchronously, so we maintain
|
|
3765
|
+
* pattern tombstones in memory to intercept subsequent disk reads and evict matching files.
|
|
3766
|
+
*/
|
|
3767
|
+
_patternTombstones = [];
|
|
3768
|
+
_addPatternTombstone(prefix) {
|
|
3769
|
+
const now = Date.now();
|
|
3770
|
+
while (this._patternTombstones.length > 0 && now - this._patternTombstones[0].createdAt > 36e5) {
|
|
3771
|
+
this._patternTombstones.shift();
|
|
3772
|
+
}
|
|
3773
|
+
if (this._patternTombstones.length >= 2e3) {
|
|
3774
|
+
this._patternTombstones.shift();
|
|
3775
|
+
}
|
|
3776
|
+
this._patternTombstones.push({ prefix, createdAt: now });
|
|
3777
|
+
}
|
|
3778
|
+
_isTombstonedByPattern(namespacedKey) {
|
|
3779
|
+
if (this._patternTombstones.length === 0) return false;
|
|
3780
|
+
for (let i = this._patternTombstones.length - 1; i >= 0; i--) {
|
|
3781
|
+
if (namespacedKey.startsWith(this._patternTombstones[i].prefix)) {
|
|
3782
|
+
return true;
|
|
3783
|
+
}
|
|
3784
|
+
}
|
|
3785
|
+
return false;
|
|
3786
|
+
}
|
|
3787
|
+
_assertActive(operation) {
|
|
3788
|
+
if (this._destroyed) {
|
|
3789
|
+
throw new Error(`TriCacheError: Cannot perform '${operation}' on a destroyed CacheService instance (namespace: '${this._namespace || "(default)"}').`);
|
|
3790
|
+
}
|
|
3791
|
+
}
|
|
3792
|
+
_bumpKeyMutation(namespacedKey) {
|
|
3793
|
+
const next = (this._keyMutationEpochs.get(namespacedKey) ?? 0) + 1;
|
|
3794
|
+
this._keyMutationEpochs.set(namespacedKey, next);
|
|
3795
|
+
if (this._keyMutationEpochs.size > 5e4) {
|
|
3796
|
+
const oldest = this._keyMutationEpochs.keys().next().value;
|
|
3797
|
+
if (oldest !== void 0) this._keyMutationEpochs.delete(oldest);
|
|
3798
|
+
}
|
|
3799
|
+
return next;
|
|
3800
|
+
}
|
|
3801
|
+
_getKeyMutationEpoch(namespacedKey) {
|
|
3802
|
+
return this._globalEpoch + (this._keyMutationEpochs.get(namespacedKey) ?? 0);
|
|
3803
|
+
}
|
|
3738
3804
|
_ipcServer;
|
|
3739
3805
|
/** Timestamp (Date.now()) when the backplane subscriber last lost its connection. */
|
|
3740
3806
|
_subDisconnectedAt = null;
|
|
@@ -3823,6 +3889,8 @@ var CacheService = class _CacheService {
|
|
|
3823
3889
|
),
|
|
3824
3890
|
snapshotMaxAgeMs: options.snapshotMaxAgeMs ?? DEFAULT_SNAPSHOT_MAX_AGE,
|
|
3825
3891
|
invalidationBackplane: options.invalidationBackplane ?? true,
|
|
3892
|
+
awaitInvalidationBackplane: options.awaitInvalidationBackplane ?? false,
|
|
3893
|
+
lockFailClosed: options.lockFailClosed ?? false,
|
|
3826
3894
|
oomProtection: options.oomProtection ?? true,
|
|
3827
3895
|
oomHeapThreshold: options.oomHeapThreshold ?? 0.85,
|
|
3828
3896
|
oomCheckIntervalMs: options.oomCheckIntervalMs ?? 1e4,
|
|
@@ -4188,7 +4256,9 @@ var CacheService = class _CacheService {
|
|
|
4188
4256
|
"adaptiveTtl",
|
|
4189
4257
|
"l2WriteMode",
|
|
4190
4258
|
"instanceName",
|
|
4191
|
-
"invalidationBackplane"
|
|
4259
|
+
"invalidationBackplane",
|
|
4260
|
+
"awaitInvalidationBackplane",
|
|
4261
|
+
"lockFailClosed"
|
|
4192
4262
|
];
|
|
4193
4263
|
/** Returns the names of options that differ between `a` and the live `b`. */
|
|
4194
4264
|
static optionsDiff(a, b) {
|
|
@@ -4229,6 +4299,8 @@ var CacheService = class _CacheService {
|
|
|
4229
4299
|
frozen: o.frozen,
|
|
4230
4300
|
adaptiveTtl: o.adaptiveTtl,
|
|
4231
4301
|
invalidationBackplane: o.invalidationBackplane,
|
|
4302
|
+
awaitInvalidationBackplane: o.awaitInvalidationBackplane,
|
|
4303
|
+
lockFailClosed: o.lockFailClosed,
|
|
4232
4304
|
strictSingleton: o.strictSingleton,
|
|
4233
4305
|
remoteSnapshot: o.remoteSnapshot,
|
|
4234
4306
|
crossRegion: o.crossRegion
|
|
@@ -4514,16 +4586,36 @@ var CacheService = class _CacheService {
|
|
|
4514
4586
|
_applyInvalidationEvent(op, key, tagVersion) {
|
|
4515
4587
|
if (op === "del") {
|
|
4516
4588
|
this.l1.delete(key);
|
|
4589
|
+
this._bumpKeyMutation(key);
|
|
4590
|
+
this.inflight.delete(key);
|
|
4591
|
+
this._pendingDiskDeletes.add(key);
|
|
4517
4592
|
setImmediate(() => {
|
|
4518
|
-
|
|
4593
|
+
try {
|
|
4594
|
+
if (!this._diskDisabled) this.disk.delete(key);
|
|
4595
|
+
} finally {
|
|
4596
|
+
this._pendingDiskDeletes.delete(key);
|
|
4597
|
+
}
|
|
4519
4598
|
});
|
|
4520
4599
|
this._cascadeDependencies(key);
|
|
4521
4600
|
} else if (op === "del-glob") {
|
|
4522
4601
|
this.l1.deletePattern(key);
|
|
4602
|
+
const pfx = key.endsWith("*") ? key.slice(0, -1) : key;
|
|
4603
|
+
for (const liveKey of this.l1.liveKeys()) {
|
|
4604
|
+
if (liveKey.startsWith(pfx)) {
|
|
4605
|
+
this._bumpKeyMutation(liveKey);
|
|
4606
|
+
}
|
|
4607
|
+
}
|
|
4608
|
+
for (const inflightKey of Array.from(this.inflight.keys())) {
|
|
4609
|
+
if (inflightKey.startsWith(pfx)) {
|
|
4610
|
+
this._bumpKeyMutation(inflightKey);
|
|
4611
|
+
this.inflight.delete(inflightKey);
|
|
4612
|
+
}
|
|
4613
|
+
}
|
|
4614
|
+
this._addPatternTombstone(pfx);
|
|
4523
4615
|
} else if (op === "tag_incr") {
|
|
4524
4616
|
const tag = key;
|
|
4525
4617
|
const ver = typeof tagVersion === "number" ? tagVersion : (this.tagVersions.get(tag)?.version ?? 0) + 1;
|
|
4526
|
-
this._setLocalTagVersion(tag, ver,
|
|
4618
|
+
this._setLocalTagVersion(tag, ver, performance.now());
|
|
4527
4619
|
}
|
|
4528
4620
|
this.logger.debug("Backplane: invalidation applied", { op, key: key.slice(0, 60) });
|
|
4529
4621
|
}
|
|
@@ -4551,7 +4643,11 @@ var CacheService = class _CacheService {
|
|
|
4551
4643
|
if (!isCrossRegionRelay && this.opts.crossRegion) {
|
|
4552
4644
|
const shouldBroadcast = isExplicitInvalidation || Boolean(this.opts.crossRegion.broadcastOnSet);
|
|
4553
4645
|
if (shouldBroadcast) {
|
|
4554
|
-
|
|
4646
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
4647
|
+
await this._broadcastCrossRegion(op, key, tagVersion);
|
|
4648
|
+
} else {
|
|
4649
|
+
void this._broadcastCrossRegion(op, key, tagVersion);
|
|
4650
|
+
}
|
|
4555
4651
|
}
|
|
4556
4652
|
}
|
|
4557
4653
|
if (!this.opts.invalidationBackplane || this._redisDisabled) return;
|
|
@@ -4592,7 +4688,11 @@ var CacheService = class _CacheService {
|
|
|
4592
4688
|
);
|
|
4593
4689
|
}
|
|
4594
4690
|
this.counters.invSent++;
|
|
4595
|
-
} catch {
|
|
4691
|
+
} catch (err) {
|
|
4692
|
+
this.logger.warn("Backplane invalidation publish failed", { op, key, error: err.message });
|
|
4693
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
4694
|
+
throw err;
|
|
4695
|
+
}
|
|
4596
4696
|
}
|
|
4597
4697
|
}
|
|
4598
4698
|
_markCrossRegionEventSeen(id) {
|
|
@@ -4796,6 +4896,7 @@ var CacheService = class _CacheService {
|
|
|
4796
4896
|
}
|
|
4797
4897
|
}
|
|
4798
4898
|
async getRedis() {
|
|
4899
|
+
this._assertActive("getRedis");
|
|
4799
4900
|
if (!this.cb.isAllowed()) throw new Error("tricache: L2 circuit breaker is open");
|
|
4800
4901
|
if (this.redis) return this.redis;
|
|
4801
4902
|
if (this.redisConnecting) return this.redisConnecting;
|
|
@@ -5124,6 +5225,7 @@ var CacheService = class _CacheService {
|
|
|
5124
5225
|
* Caches negative lookups to avoid repeated DB hits for missing keys.
|
|
5125
5226
|
*/
|
|
5126
5227
|
async get(cacheKey, fetchFn, ttlSeconds = 300, opts = {}) {
|
|
5228
|
+
this._assertActive("get");
|
|
5127
5229
|
const span = this._startSpan("tricache.get");
|
|
5128
5230
|
if (this.opts.tracer) span.setAttribute("cache.key_prefix", cacheKey.split(":")[0]);
|
|
5129
5231
|
const k = this.nk(cacheKey);
|
|
@@ -5155,7 +5257,14 @@ var CacheService = class _CacheService {
|
|
|
5155
5257
|
if (swrGraceMs2 > 0 && !this.revalidating.has(k)) {
|
|
5156
5258
|
const priority2 = optPriority ?? inferPriority(cacheKey);
|
|
5157
5259
|
this.revalidating.add(k);
|
|
5158
|
-
void this._revalidate(
|
|
5260
|
+
void this._revalidate(
|
|
5261
|
+
k,
|
|
5262
|
+
fetchFn,
|
|
5263
|
+
ttlSeconds * 1e3,
|
|
5264
|
+
swrGraceMs2,
|
|
5265
|
+
priority2,
|
|
5266
|
+
l1Hit.tagVersions ?? (optTags ? Object.fromEntries(optTags.map((t) => [t, 0])) : void 0)
|
|
5267
|
+
);
|
|
5159
5268
|
this.counters.swrRevalidations++;
|
|
5160
5269
|
this.logger.debug("SWR: serving stale, revalidating", { cacheKey });
|
|
5161
5270
|
} else {
|
|
@@ -5174,7 +5283,14 @@ var CacheService = class _CacheService {
|
|
|
5174
5283
|
if ((shouldRefreshAhead || shouldXFetch) && !this.revalidating.has(k)) {
|
|
5175
5284
|
const priority2 = optPriority ?? inferPriority(cacheKey);
|
|
5176
5285
|
this.revalidating.add(k);
|
|
5177
|
-
void this._revalidate(
|
|
5286
|
+
void this._revalidate(
|
|
5287
|
+
k,
|
|
5288
|
+
fetchFn,
|
|
5289
|
+
entryTtl,
|
|
5290
|
+
optSwr * 1e3,
|
|
5291
|
+
priority2,
|
|
5292
|
+
l1Hit.tagVersions ?? (optTags ? Object.fromEntries(optTags.map((t) => [t, 0])) : void 0)
|
|
5293
|
+
);
|
|
5178
5294
|
this.counters.swrRevalidations++;
|
|
5179
5295
|
this.logger.debug(
|
|
5180
5296
|
shouldXFetch ? "XFetch: proactive background recompute" : "Refresh-ahead: proactive background recompute",
|
|
@@ -5200,44 +5316,52 @@ var CacheService = class _CacheService {
|
|
|
5200
5316
|
span.setAttribute("cache.hit", true).setAttribute("cache.stampede_coalesced", true).end();
|
|
5201
5317
|
return existing;
|
|
5202
5318
|
}
|
|
5319
|
+
const mutationEpochAtStart = this._getKeyMutationEpoch(k);
|
|
5203
5320
|
const executionPromise = (async () => {
|
|
5204
5321
|
try {
|
|
5205
|
-
if (!this._diskDisabled) {
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
|
|
5209
|
-
|
|
5210
|
-
|
|
5211
|
-
|
|
5212
|
-
this.watchdog.
|
|
5213
|
-
if (
|
|
5214
|
-
|
|
5215
|
-
|
|
5216
|
-
|
|
5217
|
-
|
|
5218
|
-
|
|
5219
|
-
|
|
5220
|
-
|
|
5221
|
-
|
|
5322
|
+
if (!this._diskDisabled && !this._pendingDiskDeletes.has(k)) {
|
|
5323
|
+
if (this._isTombstonedByPattern(k)) {
|
|
5324
|
+
try {
|
|
5325
|
+
this.disk.delete(k);
|
|
5326
|
+
} catch {
|
|
5327
|
+
}
|
|
5328
|
+
} else {
|
|
5329
|
+
const isDiskAllowed = this.watchdog.isDiskAllowed();
|
|
5330
|
+
if (isDiskAllowed) {
|
|
5331
|
+
const diskStart = performance.now();
|
|
5332
|
+
const diskHit = this.disk.load(k);
|
|
5333
|
+
const diskElapsed = performance.now() - diskStart;
|
|
5334
|
+
this.watchdog.recordDisk(diskElapsed);
|
|
5335
|
+
this.watchdog.onCanaryResult(diskHit !== null, diskElapsed);
|
|
5336
|
+
if (diskHit !== null) {
|
|
5337
|
+
let isDiskStale = false;
|
|
5338
|
+
if (this.opts.tagStrategy === "generational" && diskHit.tagVersions) {
|
|
5339
|
+
const tags = Object.keys(diskHit.tagVersions);
|
|
5340
|
+
const currentVers = await Promise.all(tags.map((tag) => this._getTagVersion(tag)));
|
|
5341
|
+
for (let i = 0; i < tags.length; i++) {
|
|
5342
|
+
if (currentVers[i] > diskHit.tagVersions[tags[i]]) {
|
|
5343
|
+
isDiskStale = true;
|
|
5344
|
+
break;
|
|
5345
|
+
}
|
|
5222
5346
|
}
|
|
5223
5347
|
}
|
|
5224
|
-
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
|
|
5228
|
-
|
|
5229
|
-
|
|
5230
|
-
|
|
5231
|
-
|
|
5232
|
-
|
|
5233
|
-
|
|
5234
|
-
|
|
5235
|
-
|
|
5236
|
-
|
|
5237
|
-
|
|
5238
|
-
|
|
5239
|
-
|
|
5240
|
-
|
|
5348
|
+
if (isDiskStale) {
|
|
5349
|
+
this.disk.delete(k);
|
|
5350
|
+
} else {
|
|
5351
|
+
const promoted = this.l1.importEntries(
|
|
5352
|
+
[{ key: k, entry: diskHit }],
|
|
5353
|
+
this.opts.forbiddenSnapshotPrefixes
|
|
5354
|
+
);
|
|
5355
|
+
if (promoted > 0) {
|
|
5356
|
+
const l1Check = this.l1.get(k);
|
|
5357
|
+
if (l1Check !== null) {
|
|
5358
|
+
this.counters.diskHits++;
|
|
5359
|
+
this.opts.onHit?.(cacheKey, "disk");
|
|
5360
|
+
this.logger.debug("L1.5 hit (disk \u2192 L1)", { cacheKey });
|
|
5361
|
+
span.setAttribute("cache.hit", true).setAttribute("cache.item.tier", "disk").setAttribute("cache.hit_tier", "disk");
|
|
5362
|
+
if (this.opts.frozen) deepFreeze(l1Check.value);
|
|
5363
|
+
return this.opts.cloneStrategy === "structuredClone" && l1Check.value != null && typeof l1Check.value === "object" ? structuredClone(l1Check.value) : l1Check.value;
|
|
5364
|
+
}
|
|
5241
5365
|
}
|
|
5242
5366
|
}
|
|
5243
5367
|
}
|
|
@@ -5348,6 +5472,18 @@ var CacheService = class _CacheService {
|
|
|
5348
5472
|
await this._registerTags(k, optTags, Math.ceil(effectiveTtl / 1e3));
|
|
5349
5473
|
}
|
|
5350
5474
|
}
|
|
5475
|
+
if (this._getKeyMutationEpoch(k) !== mutationEpochAtStart) {
|
|
5476
|
+
this.logger.debug("Aborting in-flight cache commit \u2014 key was mutated or deleted during fetch", { cacheKey });
|
|
5477
|
+
span.setAttribute("cache.aborted_mutation_race", true);
|
|
5478
|
+
const current = this.l1.get(k);
|
|
5479
|
+
if (current && !current.isStale && current.value !== void 0) {
|
|
5480
|
+
const freshVal = current.value;
|
|
5481
|
+
if (this.opts.frozen) deepFreeze(freshVal);
|
|
5482
|
+
return this.opts.cloneStrategy === "structuredClone" && freshVal != null && typeof freshVal === "object" ? structuredClone(freshVal) : freshVal;
|
|
5483
|
+
}
|
|
5484
|
+
if (this.opts.frozen) deepFreeze(data);
|
|
5485
|
+
return this.opts.cloneStrategy === "structuredClone" && data != null && typeof data === "object" ? structuredClone(data) : data;
|
|
5486
|
+
}
|
|
5351
5487
|
this.l1.set(k, data, storeTtl, priority, staleAt, delta, activeTagVersions);
|
|
5352
5488
|
if (!this._redisDisabled) {
|
|
5353
5489
|
try {
|
|
@@ -5464,22 +5600,40 @@ var CacheService = class _CacheService {
|
|
|
5464
5600
|
const ttl = options?.ttl ?? 300;
|
|
5465
5601
|
return this.get(cacheKey, fetchFn, ttl, options);
|
|
5466
5602
|
}
|
|
5467
|
-
async _revalidate(cacheKey, fetchFn, ttlMs, swrGraceMs, priority) {
|
|
5603
|
+
async _revalidate(cacheKey, fetchFn, ttlMs, swrGraceMs, priority, knownTagVersions) {
|
|
5604
|
+
const startEpoch = this._getKeyMutationEpoch(cacheKey);
|
|
5468
5605
|
try {
|
|
5469
5606
|
const fetchStart = Date.now();
|
|
5470
5607
|
const data = await fetchFn();
|
|
5471
5608
|
const delta = Date.now() - fetchStart;
|
|
5472
5609
|
const staleAt = Date.now() + ttlMs;
|
|
5610
|
+
if (this._getKeyMutationEpoch(cacheKey) !== startEpoch) {
|
|
5611
|
+
this.logger.debug("SWR: aborting revalidation commit \u2014 key was mutated or deleted during fetch", { cacheKey });
|
|
5612
|
+
return;
|
|
5613
|
+
}
|
|
5473
5614
|
if (this.latencyTracker && data != null) this.latencyTracker.record(cacheKey, delta);
|
|
5474
5615
|
let activeTagVersions;
|
|
5475
5616
|
const l1Existing = this.l1.get(cacheKey);
|
|
5476
|
-
|
|
5477
|
-
|
|
5617
|
+
const tagVersionsSource = l1Existing?.tagVersions ?? knownTagVersions;
|
|
5618
|
+
if (this.opts.tagStrategy === "generational" && tagVersionsSource) {
|
|
5619
|
+
const tags = Object.keys(tagVersionsSource);
|
|
5478
5620
|
const vers = await Promise.all(tags.map((tag) => this._getTagVersion(tag)));
|
|
5479
5621
|
activeTagVersions = {};
|
|
5480
5622
|
for (let i = 0; i < tags.length; i++) {
|
|
5481
5623
|
activeTagVersions[tags[i]] = vers[i];
|
|
5482
5624
|
}
|
|
5625
|
+
if (knownTagVersions) {
|
|
5626
|
+
for (const t of tags) {
|
|
5627
|
+
if (knownTagVersions[t] !== void 0 && activeTagVersions[t] !== knownTagVersions[t]) {
|
|
5628
|
+
this.logger.debug("SWR: aborting revalidation commit \u2014 tag version bumped during revalidation", { cacheKey, tag: t });
|
|
5629
|
+
return;
|
|
5630
|
+
}
|
|
5631
|
+
}
|
|
5632
|
+
}
|
|
5633
|
+
}
|
|
5634
|
+
if (this._getKeyMutationEpoch(cacheKey) !== startEpoch) {
|
|
5635
|
+
this.logger.debug("SWR: aborting revalidation commit \u2014 key was mutated or deleted during tag resolution", { cacheKey });
|
|
5636
|
+
return;
|
|
5483
5637
|
}
|
|
5484
5638
|
this.l1.set(cacheKey, data, ttlMs + swrGraceMs, priority, staleAt, delta, activeTagVersions);
|
|
5485
5639
|
if (!this._redisDisabled && this.opts.l2WriteMode === "read-write") {
|
|
@@ -5523,6 +5677,7 @@ var CacheService = class _CacheService {
|
|
|
5523
5677
|
// ── Explicit set / delete ─────────────────────────────────────────────────
|
|
5524
5678
|
/** Explicitly write a value into L1 (+ L2 in production). */
|
|
5525
5679
|
async set(cacheKey, data, ttlSeconds = 300, priority, opts) {
|
|
5680
|
+
this._assertActive("set");
|
|
5526
5681
|
const span = this._startSpan("tricache.set");
|
|
5527
5682
|
if (this.opts.tracer) {
|
|
5528
5683
|
span.setAttribute("cache.key_prefix", cacheKey.split(":")[0]);
|
|
@@ -5533,6 +5688,9 @@ var CacheService = class _CacheService {
|
|
|
5533
5688
|
const ttlMs = this._jitterTtl(effectiveTtlSeconds * 1e3);
|
|
5534
5689
|
const p = priority ?? inferPriority(cacheKey);
|
|
5535
5690
|
const k = this.nk(cacheKey);
|
|
5691
|
+
this._bumpKeyMutation(k);
|
|
5692
|
+
this.inflight.delete(k);
|
|
5693
|
+
this._pendingDiskDeletes.delete(k);
|
|
5536
5694
|
this.counters.sets++;
|
|
5537
5695
|
let activeTagVersions;
|
|
5538
5696
|
if (opts?.tags?.length) {
|
|
@@ -5587,7 +5745,11 @@ var CacheService = class _CacheService {
|
|
|
5587
5745
|
this.logger.debug("set: Redis unavailable", { cacheKey, error: err.message });
|
|
5588
5746
|
}
|
|
5589
5747
|
}
|
|
5590
|
-
|
|
5748
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
5749
|
+
await this.publishInvalidation("del", k);
|
|
5750
|
+
} else {
|
|
5751
|
+
void this.publishInvalidation("del", k);
|
|
5752
|
+
}
|
|
5591
5753
|
} catch (err) {
|
|
5592
5754
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
5593
5755
|
span.recordException?.(err);
|
|
@@ -5634,6 +5796,7 @@ var CacheService = class _CacheService {
|
|
|
5634
5796
|
* await cache.delete('user:abc:*'); // all keys for user abc
|
|
5635
5797
|
*/
|
|
5636
5798
|
async delete(cacheKey) {
|
|
5799
|
+
this._assertActive("delete");
|
|
5637
5800
|
const span = this._startSpan("tricache.delete");
|
|
5638
5801
|
if (this.opts.tracer) span.setAttribute("cache.key_prefix", cacheKey.split(":")[0]);
|
|
5639
5802
|
try {
|
|
@@ -5641,11 +5804,31 @@ var CacheService = class _CacheService {
|
|
|
5641
5804
|
const k = this.nk(cacheKey);
|
|
5642
5805
|
this.counters.deletes++;
|
|
5643
5806
|
if (isPattern) {
|
|
5807
|
+
const pfx = k.endsWith("*") ? k.slice(0, -1) : k;
|
|
5808
|
+
for (const liveKey of this.l1.liveKeys()) {
|
|
5809
|
+
if (liveKey.startsWith(pfx)) {
|
|
5810
|
+
this._bumpKeyMutation(liveKey);
|
|
5811
|
+
}
|
|
5812
|
+
}
|
|
5813
|
+
for (const inflightKey of Array.from(this.inflight.keys())) {
|
|
5814
|
+
if (inflightKey.startsWith(pfx)) {
|
|
5815
|
+
this._bumpKeyMutation(inflightKey);
|
|
5816
|
+
this.inflight.delete(inflightKey);
|
|
5817
|
+
}
|
|
5818
|
+
}
|
|
5819
|
+
this._addPatternTombstone(pfx);
|
|
5644
5820
|
this.l1.deletePattern(k);
|
|
5645
5821
|
} else {
|
|
5822
|
+
this._bumpKeyMutation(k);
|
|
5823
|
+
this.inflight.delete(k);
|
|
5646
5824
|
this.l1.delete(k);
|
|
5825
|
+
this._pendingDiskDeletes.add(k);
|
|
5647
5826
|
setImmediate(() => {
|
|
5648
|
-
|
|
5827
|
+
try {
|
|
5828
|
+
if (!this._diskDisabled) this.disk.delete(k);
|
|
5829
|
+
} finally {
|
|
5830
|
+
this._pendingDiskDeletes.delete(k);
|
|
5831
|
+
}
|
|
5649
5832
|
});
|
|
5650
5833
|
this._cascadeDependencies(k);
|
|
5651
5834
|
for (const [, dependents] of this.dependencyIndex) dependents.delete(k);
|
|
@@ -5669,7 +5852,11 @@ var CacheService = class _CacheService {
|
|
|
5669
5852
|
this.logger.debug("delete: Redis unavailable", { cacheKey, error: err.message });
|
|
5670
5853
|
}
|
|
5671
5854
|
}
|
|
5672
|
-
|
|
5855
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
5856
|
+
await this.publishInvalidation(isPattern ? "del-glob" : "del", k, void 0, false, true);
|
|
5857
|
+
} else {
|
|
5858
|
+
void this.publishInvalidation(isPattern ? "del-glob" : "del", k, void 0, false, true);
|
|
5859
|
+
}
|
|
5673
5860
|
} catch (err) {
|
|
5674
5861
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
5675
5862
|
span.recordException?.(err);
|
|
@@ -5696,6 +5883,7 @@ var CacheService = class _CacheService {
|
|
|
5696
5883
|
* fleet-wide.
|
|
5697
5884
|
*/
|
|
5698
5885
|
async increment(cacheKey, ttlSeconds) {
|
|
5886
|
+
this._assertActive("increment");
|
|
5699
5887
|
const k = this.nk(cacheKey);
|
|
5700
5888
|
if (this._redisDisabled) {
|
|
5701
5889
|
if (!this._incrementFallbackWarned) {
|
|
@@ -5737,19 +5925,38 @@ var CacheService = class _CacheService {
|
|
|
5737
5925
|
* await cache.clear('user:abc'); // flush all keys for one user
|
|
5738
5926
|
*/
|
|
5739
5927
|
async clear(prefix) {
|
|
5928
|
+
this._assertActive("clear");
|
|
5740
5929
|
const span = this._startSpan("tricache.clear");
|
|
5741
5930
|
if (this.opts.tracer) span.setAttribute("cache.prefix", prefix || "*");
|
|
5742
5931
|
try {
|
|
5743
5932
|
this.counters.deletes++;
|
|
5744
5933
|
const k = prefix ? this.nk(prefix.includes("*") ? prefix : `${prefix}*`) : void 0;
|
|
5745
5934
|
if (k) {
|
|
5935
|
+
const pfx = k.endsWith("*") ? k.slice(0, -1) : k;
|
|
5936
|
+
for (const liveKey of this.l1.liveKeys()) {
|
|
5937
|
+
if (liveKey.startsWith(pfx)) {
|
|
5938
|
+
this._bumpKeyMutation(liveKey);
|
|
5939
|
+
}
|
|
5940
|
+
}
|
|
5941
|
+
for (const inflightKey of Array.from(this.inflight.keys())) {
|
|
5942
|
+
if (inflightKey.startsWith(pfx)) {
|
|
5943
|
+
this._bumpKeyMutation(inflightKey);
|
|
5944
|
+
this.inflight.delete(inflightKey);
|
|
5945
|
+
}
|
|
5946
|
+
}
|
|
5947
|
+
this._addPatternTombstone(pfx);
|
|
5746
5948
|
this.l1.deletePattern(k);
|
|
5747
5949
|
} else {
|
|
5950
|
+
this._globalEpoch++;
|
|
5951
|
+
this.inflight.clear();
|
|
5952
|
+
this._patternTombstones.length = 0;
|
|
5748
5953
|
this.l1.clear();
|
|
5749
5954
|
if (!this._diskDisabled) this.disk.clear();
|
|
5750
5955
|
this._l1Counters.clear();
|
|
5751
5956
|
this.tagIndex.clear();
|
|
5752
5957
|
this.tagVersions.clear();
|
|
5958
|
+
this._pendingDiskDeletes.clear();
|
|
5959
|
+
this._keyMutationEpochs.clear();
|
|
5753
5960
|
}
|
|
5754
5961
|
if (!this._redisDisabled && this.opts.l2WriteMode === "read-write") {
|
|
5755
5962
|
try {
|
|
@@ -5761,13 +5968,23 @@ var CacheService = class _CacheService {
|
|
|
5761
5968
|
this.logger.debug("clear: Redis unavailable", { error: err.message });
|
|
5762
5969
|
}
|
|
5763
5970
|
}
|
|
5764
|
-
|
|
5765
|
-
|
|
5766
|
-
|
|
5767
|
-
|
|
5768
|
-
|
|
5769
|
-
|
|
5770
|
-
|
|
5971
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
5972
|
+
await this.publishInvalidation(
|
|
5973
|
+
"del-glob",
|
|
5974
|
+
k ?? (this._namespace ? `${this._namespace}:*` : "*"),
|
|
5975
|
+
void 0,
|
|
5976
|
+
false,
|
|
5977
|
+
true
|
|
5978
|
+
);
|
|
5979
|
+
} else {
|
|
5980
|
+
void this.publishInvalidation(
|
|
5981
|
+
"del-glob",
|
|
5982
|
+
k ?? (this._namespace ? `${this._namespace}:*` : "*"),
|
|
5983
|
+
void 0,
|
|
5984
|
+
false,
|
|
5985
|
+
true
|
|
5986
|
+
);
|
|
5987
|
+
}
|
|
5771
5988
|
} catch (err) {
|
|
5772
5989
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
5773
5990
|
span.recordException?.(err);
|
|
@@ -5782,6 +5999,7 @@ var CacheService = class _CacheService {
|
|
|
5782
5999
|
* Returns the number of entries evicted.
|
|
5783
6000
|
*/
|
|
5784
6001
|
rebalance() {
|
|
6002
|
+
this._assertActive("rebalance");
|
|
5785
6003
|
return this.l1.rebalance();
|
|
5786
6004
|
}
|
|
5787
6005
|
/**
|
|
@@ -5790,6 +6008,7 @@ var CacheService = class _CacheService {
|
|
|
5790
6008
|
* Only reflects L1 state — does not query Redis or disk.
|
|
5791
6009
|
*/
|
|
5792
6010
|
ttl(cacheKey) {
|
|
6011
|
+
this._assertActive("ttl");
|
|
5793
6012
|
return this.l1.ttl(this.nk(cacheKey));
|
|
5794
6013
|
}
|
|
5795
6014
|
/**
|
|
@@ -5797,6 +6016,7 @@ var CacheService = class _CacheService {
|
|
|
5797
6016
|
* Bloom-filter fast path — no fetch, no disk or Redis round-trip.
|
|
5798
6017
|
*/
|
|
5799
6018
|
has(cacheKey) {
|
|
6019
|
+
this._assertActive("has");
|
|
5800
6020
|
return this.l1.has(this.nk(cacheKey));
|
|
5801
6021
|
}
|
|
5802
6022
|
// ── Iteration ─────────────────────────────────────────────────────────────
|
|
@@ -5807,6 +6027,7 @@ var CacheService = class _CacheService {
|
|
|
5807
6027
|
* for (const key of cache.keys()) console.log(key);
|
|
5808
6028
|
*/
|
|
5809
6029
|
*keys() {
|
|
6030
|
+
this._assertActive("keys");
|
|
5810
6031
|
const prefix = this._namespace ? this._namespace + ":" : "";
|
|
5811
6032
|
for (const key of this.l1.liveKeys()) {
|
|
5812
6033
|
if (prefix && !key.startsWith(prefix)) continue;
|
|
@@ -5821,6 +6042,7 @@ var CacheService = class _CacheService {
|
|
|
5821
6042
|
* for (const val of cache.values<User>()) console.log(val.id);
|
|
5822
6043
|
*/
|
|
5823
6044
|
*values() {
|
|
6045
|
+
this._assertActive("values");
|
|
5824
6046
|
const prefix = this._namespace ? this._namespace + ":" : "";
|
|
5825
6047
|
if (!prefix) {
|
|
5826
6048
|
yield* this.l1.liveValues();
|
|
@@ -5839,6 +6061,7 @@ var CacheService = class _CacheService {
|
|
|
5839
6061
|
* for (const [key, val] of cache.entries<User>()) console.log(key, val.id);
|
|
5840
6062
|
*/
|
|
5841
6063
|
*entries() {
|
|
6064
|
+
this._assertActive("entries");
|
|
5842
6065
|
const prefix = this._namespace ? this._namespace + ":" : "";
|
|
5843
6066
|
for (const [key, entry] of this.l1.liveEntries()) {
|
|
5844
6067
|
if (prefix && !key.startsWith(prefix)) continue;
|
|
@@ -5866,6 +6089,7 @@ var CacheService = class _CacheService {
|
|
|
5866
6089
|
* `offset` — `rawKey.slice(offset)` gives the bare key without namespace.
|
|
5867
6090
|
*/
|
|
5868
6091
|
scan(fn) {
|
|
6092
|
+
this._assertActive("scan");
|
|
5869
6093
|
const prefixLen = this._namespace ? this._namespace.length + 1 : 0;
|
|
5870
6094
|
this.l1.scan((key, entry, pfx) => {
|
|
5871
6095
|
const value = entry.value !== void 0 ? entry.value : entry.data;
|
|
@@ -5879,6 +6103,7 @@ var CacheService = class _CacheService {
|
|
|
5879
6103
|
* @param newTtlSeconds - The new TTL from now, in seconds.
|
|
5880
6104
|
*/
|
|
5881
6105
|
async touch(cacheKey, newTtlSeconds) {
|
|
6106
|
+
this._assertActive("touch");
|
|
5882
6107
|
const k = this.nk(cacheKey);
|
|
5883
6108
|
const hit = this.l1.touch(k, newTtlSeconds * 1e3);
|
|
5884
6109
|
if (hit && !this._redisDisabled) {
|
|
@@ -5899,6 +6124,7 @@ var CacheService = class _CacheService {
|
|
|
5899
6124
|
* if (fresh !== null) return fresh; // serve from L1, no network hop
|
|
5900
6125
|
*/
|
|
5901
6126
|
getIfFresh(cacheKey) {
|
|
6127
|
+
this._assertActive("getIfFresh");
|
|
5902
6128
|
const k = this.nk(cacheKey);
|
|
5903
6129
|
const entry = this.l1.getEntry(k);
|
|
5904
6130
|
if (!entry) return null;
|
|
@@ -5916,6 +6142,7 @@ var CacheService = class _CacheService {
|
|
|
5916
6142
|
* const val = await cache.peek<User>('user:123');
|
|
5917
6143
|
*/
|
|
5918
6144
|
async peek(cacheKey) {
|
|
6145
|
+
this._assertActive("peek");
|
|
5919
6146
|
const k = this.nk(cacheKey);
|
|
5920
6147
|
const l1Hit = this.l1.get(k);
|
|
5921
6148
|
if (l1Hit !== null) {
|
|
@@ -5925,25 +6152,32 @@ var CacheService = class _CacheService {
|
|
|
5925
6152
|
if (this.opts.frozen) deepFreeze(val);
|
|
5926
6153
|
return this.opts.cloneStrategy === "structuredClone" && val != null && typeof val === "object" ? structuredClone(val) : val;
|
|
5927
6154
|
}
|
|
5928
|
-
if (!this._diskDisabled && this.
|
|
5929
|
-
|
|
5930
|
-
|
|
5931
|
-
|
|
5932
|
-
|
|
5933
|
-
|
|
5934
|
-
if (
|
|
5935
|
-
const
|
|
5936
|
-
|
|
5937
|
-
|
|
5938
|
-
);
|
|
5939
|
-
|
|
5940
|
-
|
|
5941
|
-
|
|
5942
|
-
|
|
5943
|
-
this.
|
|
5944
|
-
|
|
5945
|
-
|
|
5946
|
-
|
|
6155
|
+
if (!this._diskDisabled && !this._pendingDiskDeletes.has(k)) {
|
|
6156
|
+
if (this._isTombstonedByPattern(k)) {
|
|
6157
|
+
try {
|
|
6158
|
+
this.disk.delete(k);
|
|
6159
|
+
} catch {
|
|
6160
|
+
}
|
|
6161
|
+
} else if (this.watchdog.isDiskAllowed()) {
|
|
6162
|
+
const diskStart = performance.now();
|
|
6163
|
+
const diskHit = this.disk.load(k);
|
|
6164
|
+
const diskElapsed = performance.now() - diskStart;
|
|
6165
|
+
this.watchdog.recordDisk(diskElapsed);
|
|
6166
|
+
this.watchdog.onCanaryResult(diskHit !== null, diskElapsed);
|
|
6167
|
+
if (diskHit !== null) {
|
|
6168
|
+
const promoted = this.l1.importEntries(
|
|
6169
|
+
[{ key: k, entry: diskHit }],
|
|
6170
|
+
this.opts.forbiddenSnapshotPrefixes
|
|
6171
|
+
);
|
|
6172
|
+
if (promoted > 0) {
|
|
6173
|
+
const l1Check = this.l1.get(k);
|
|
6174
|
+
if (l1Check !== null) {
|
|
6175
|
+
this.counters.gets++;
|
|
6176
|
+
this.counters.diskHits++;
|
|
6177
|
+
const val = l1Check.value;
|
|
6178
|
+
if (this.opts.frozen) deepFreeze(val);
|
|
6179
|
+
return this.opts.cloneStrategy === "structuredClone" && val != null && typeof val === "object" ? structuredClone(val) : val;
|
|
6180
|
+
}
|
|
5947
6181
|
}
|
|
5948
6182
|
}
|
|
5949
6183
|
}
|
|
@@ -5981,6 +6215,7 @@ var CacheService = class _CacheService {
|
|
|
5981
6215
|
* `(key) => number` so heterogeneous TTLs can be batched in one call.
|
|
5982
6216
|
*/
|
|
5983
6217
|
async mget(keys, fetchFn, ttl = 300, priority) {
|
|
6218
|
+
this._assertActive("mget");
|
|
5984
6219
|
const span = this._startSpan("tricache.mget");
|
|
5985
6220
|
if (this.opts.tracer) {
|
|
5986
6221
|
span.setAttribute("cache.batch.size", keys.length);
|
|
@@ -6049,8 +6284,16 @@ var CacheService = class _CacheService {
|
|
|
6049
6284
|
}
|
|
6050
6285
|
if (!this._diskDisabled && missKeys.length > 0) {
|
|
6051
6286
|
for (let j = missKeys.length - 1; j >= 0; j--) {
|
|
6052
|
-
if (!this.watchdog.isDiskAllowed()) continue;
|
|
6053
6287
|
const k = this.nk(missKeys[j]);
|
|
6288
|
+
if (this._pendingDiskDeletes.has(k)) continue;
|
|
6289
|
+
if (this._isTombstonedByPattern(k)) {
|
|
6290
|
+
try {
|
|
6291
|
+
this.disk.delete(k);
|
|
6292
|
+
} catch {
|
|
6293
|
+
}
|
|
6294
|
+
continue;
|
|
6295
|
+
}
|
|
6296
|
+
if (!this.watchdog.isDiskAllowed()) continue;
|
|
6054
6297
|
const diskStart = performance.now();
|
|
6055
6298
|
const diskHit = this.disk.load(k);
|
|
6056
6299
|
const diskElapsed = performance.now() - diskStart;
|
|
@@ -6130,6 +6373,7 @@ var CacheService = class _CacheService {
|
|
|
6130
6373
|
* });
|
|
6131
6374
|
*/
|
|
6132
6375
|
async mset(entries) {
|
|
6376
|
+
this._assertActive("mset");
|
|
6133
6377
|
const span = this._startSpan("tricache.mset");
|
|
6134
6378
|
const keys = Object.keys(entries);
|
|
6135
6379
|
if (this.opts.tracer) {
|
|
@@ -6157,6 +6401,7 @@ var CacheService = class _CacheService {
|
|
|
6157
6401
|
* await cache.mdel(['user:1', 'user:2', 'user:3']);
|
|
6158
6402
|
*/
|
|
6159
6403
|
async mdel(keys) {
|
|
6404
|
+
this._assertActive("mdel");
|
|
6160
6405
|
const span = this._startSpan("tricache.mdel");
|
|
6161
6406
|
if (this.opts.tracer) {
|
|
6162
6407
|
span.setAttribute("cache.batch.size", keys.length);
|
|
@@ -6184,6 +6429,7 @@ var CacheService = class _CacheService {
|
|
|
6184
6429
|
* console.log(`Warmed ${loaded} keys from Redis`);
|
|
6185
6430
|
*/
|
|
6186
6431
|
async warmFromL2(pattern, opts) {
|
|
6432
|
+
this._assertActive("warmFromL2");
|
|
6187
6433
|
if (this._redisDisabled) return 0;
|
|
6188
6434
|
try {
|
|
6189
6435
|
const client = await this.getRedis();
|
|
@@ -6230,6 +6476,7 @@ var CacheService = class _CacheService {
|
|
|
6230
6476
|
* await cache.invalidateTag('catalog'); // clears product:1 and any other tagged entries
|
|
6231
6477
|
*/
|
|
6232
6478
|
async invalidateTag(tag) {
|
|
6479
|
+
this._assertActive("invalidateTag");
|
|
6233
6480
|
const span = this._startSpan("tricache.invalidate_tag");
|
|
6234
6481
|
if (this.opts.tracer) {
|
|
6235
6482
|
span.setAttribute("cache.tag", tag);
|
|
@@ -6251,15 +6498,27 @@ var CacheService = class _CacheService {
|
|
|
6251
6498
|
const current = this.tagVersions.get(tag)?.version ?? 0;
|
|
6252
6499
|
newVer = current + 1;
|
|
6253
6500
|
}
|
|
6254
|
-
this._setLocalTagVersion(tag, newVer,
|
|
6255
|
-
|
|
6501
|
+
this._setLocalTagVersion(tag, newVer, performance.now());
|
|
6502
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6503
|
+
await this.publishInvalidation("tag_incr", tag, newVer, false, true);
|
|
6504
|
+
} else {
|
|
6505
|
+
void this.publishInvalidation("tag_incr", tag, newVer, false, true);
|
|
6506
|
+
}
|
|
6256
6507
|
return;
|
|
6257
6508
|
}
|
|
6258
6509
|
const tagKey = this.nk(`_tag_:${tag}`);
|
|
6259
6510
|
const members = this.tagIndex.get(tagKey) ?? /* @__PURE__ */ new Set();
|
|
6260
6511
|
for (const k of members) {
|
|
6261
6512
|
this.l1.delete(k);
|
|
6262
|
-
|
|
6513
|
+
this._bumpKeyMutation(k);
|
|
6514
|
+
this._pendingDiskDeletes.add(k);
|
|
6515
|
+
setImmediate(() => {
|
|
6516
|
+
try {
|
|
6517
|
+
if (!this._diskDisabled) this.disk.delete(k);
|
|
6518
|
+
} finally {
|
|
6519
|
+
this._pendingDiskDeletes.delete(k);
|
|
6520
|
+
}
|
|
6521
|
+
});
|
|
6263
6522
|
}
|
|
6264
6523
|
this.tagIndex.delete(tagKey);
|
|
6265
6524
|
if (!this._redisDisabled) {
|
|
@@ -6297,13 +6556,14 @@ var CacheService = class _CacheService {
|
|
|
6297
6556
|
* await cache.invalidateTags(['case:acme', 'org:acme', 'ai-chat:acme']);
|
|
6298
6557
|
*/
|
|
6299
6558
|
async invalidateTags(tags) {
|
|
6559
|
+
this._assertActive("invalidateTags");
|
|
6300
6560
|
if (tags.length === 0) return;
|
|
6301
6561
|
if (tags.length === 1) {
|
|
6302
6562
|
await this.invalidateTag(tags[0]);
|
|
6303
6563
|
return;
|
|
6304
6564
|
}
|
|
6305
6565
|
if (this.opts.tagStrategy === "generational") {
|
|
6306
|
-
const now =
|
|
6566
|
+
const now = performance.now();
|
|
6307
6567
|
if (!this._redisDisabled) {
|
|
6308
6568
|
try {
|
|
6309
6569
|
const client = await this.getRedis();
|
|
@@ -6314,7 +6574,11 @@ var CacheService = class _CacheService {
|
|
|
6314
6574
|
const [err, newVer] = results[i] ?? [null, null];
|
|
6315
6575
|
const ver = !err && typeof newVer === "number" ? newVer : (this.tagVersions.get(tags[i])?.version ?? 0) + 1;
|
|
6316
6576
|
this._setLocalTagVersion(tags[i], ver, now);
|
|
6317
|
-
|
|
6577
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6578
|
+
await this.publishInvalidation("tag_incr", tags[i], ver, false, true);
|
|
6579
|
+
} else {
|
|
6580
|
+
void this.publishInvalidation("tag_incr", tags[i], ver, false, true);
|
|
6581
|
+
}
|
|
6318
6582
|
}
|
|
6319
6583
|
return;
|
|
6320
6584
|
} catch (err) {
|
|
@@ -6324,7 +6588,11 @@ var CacheService = class _CacheService {
|
|
|
6324
6588
|
for (const tag of tags) {
|
|
6325
6589
|
const ver = (this.tagVersions.get(tag)?.version ?? 0) + 1;
|
|
6326
6590
|
this._setLocalTagVersion(tag, ver, now);
|
|
6327
|
-
|
|
6591
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6592
|
+
await this.publishInvalidation("tag_incr", tag, ver, false, true);
|
|
6593
|
+
} else {
|
|
6594
|
+
void this.publishInvalidation("tag_incr", tag, ver, false, true);
|
|
6595
|
+
}
|
|
6328
6596
|
}
|
|
6329
6597
|
return;
|
|
6330
6598
|
}
|
|
@@ -6339,7 +6607,15 @@ var CacheService = class _CacheService {
|
|
|
6339
6607
|
}
|
|
6340
6608
|
for (const k of allMembers) {
|
|
6341
6609
|
this.l1.delete(k);
|
|
6342
|
-
this.
|
|
6610
|
+
this._bumpKeyMutation(k);
|
|
6611
|
+
this._pendingDiskDeletes.add(k);
|
|
6612
|
+
setImmediate(() => {
|
|
6613
|
+
try {
|
|
6614
|
+
if (!this._diskDisabled) this.disk.delete(k);
|
|
6615
|
+
} finally {
|
|
6616
|
+
this._pendingDiskDeletes.delete(k);
|
|
6617
|
+
}
|
|
6618
|
+
});
|
|
6343
6619
|
}
|
|
6344
6620
|
if (!this._redisDisabled) {
|
|
6345
6621
|
try {
|
|
@@ -6376,6 +6652,7 @@ var CacheService = class _CacheService {
|
|
|
6376
6652
|
* `l2` is `null` when Redis is disabled.
|
|
6377
6653
|
*/
|
|
6378
6654
|
async ping() {
|
|
6655
|
+
this._assertActive("ping");
|
|
6379
6656
|
const t0 = Date.now();
|
|
6380
6657
|
this.l1.has("__ping__");
|
|
6381
6658
|
const l1 = Date.now() - t0;
|
|
@@ -6455,6 +6732,7 @@ var CacheService = class _CacheService {
|
|
|
6455
6732
|
* Returns the number of keys written.
|
|
6456
6733
|
*/
|
|
6457
6734
|
async drainToL2() {
|
|
6735
|
+
this._assertActive("drainToL2");
|
|
6458
6736
|
if (this._redisDisabled) return 0;
|
|
6459
6737
|
try {
|
|
6460
6738
|
const client = await this.getRedis();
|
|
@@ -6498,6 +6776,7 @@ var CacheService = class _CacheService {
|
|
|
6498
6776
|
* if (!claimed) return res.status(409).json({ error: 'duplicate request' });
|
|
6499
6777
|
*/
|
|
6500
6778
|
async setIfAbsent(cacheKey, value, ttlSeconds = 300, priority) {
|
|
6779
|
+
this._assertActive("setIfAbsent");
|
|
6501
6780
|
const k = this.nk(cacheKey);
|
|
6502
6781
|
if (this.l1.has(k)) return false;
|
|
6503
6782
|
if (!this._redisDisabled) {
|
|
@@ -6516,6 +6795,8 @@ var CacheService = class _CacheService {
|
|
|
6516
6795
|
}
|
|
6517
6796
|
const ttlMs = this._jitterTtl(ttlSeconds * 1e3);
|
|
6518
6797
|
const p = priority ?? inferPriority(cacheKey);
|
|
6798
|
+
this._bumpKeyMutation(k);
|
|
6799
|
+
this._pendingDiskDeletes.delete(k);
|
|
6519
6800
|
this.l1.set(k, value, ttlMs, p);
|
|
6520
6801
|
this.counters.sets++;
|
|
6521
6802
|
return true;
|
|
@@ -6534,6 +6815,7 @@ var CacheService = class _CacheService {
|
|
|
6534
6815
|
* @param options TTL, acquire timeout, and retry polling intervals.
|
|
6535
6816
|
*/
|
|
6536
6817
|
async lock(resourceKey, fn, options) {
|
|
6818
|
+
this._assertActive("lock");
|
|
6537
6819
|
const ttlSeconds = Math.max(1, options?.ttl ?? 30);
|
|
6538
6820
|
const acquireTimeout = Math.max(0, options?.acquireTimeout ?? 5e3);
|
|
6539
6821
|
const retryInterval = Math.max(10, options?.retryInterval ?? 100);
|
|
@@ -6560,6 +6842,10 @@ var CacheService = class _CacheService {
|
|
|
6560
6842
|
}
|
|
6561
6843
|
} catch (err) {
|
|
6562
6844
|
if (err.message.startsWith("Failed to acquire lock")) throw err;
|
|
6845
|
+
const failClosed = options?.failClosedOnRedisError ?? this.opts.lockFailClosed;
|
|
6846
|
+
if (failClosed) {
|
|
6847
|
+
throw new Error(`Distributed lock acquisition failed for resource "${resourceKey}" due to Redis error: ${err.message}`);
|
|
6848
|
+
}
|
|
6563
6849
|
this.logger.debug("Distributed lock: Redis error, falling back to in-process mutex", {
|
|
6564
6850
|
resourceKey,
|
|
6565
6851
|
error: err.message
|
|
@@ -6905,6 +7191,7 @@ var CacheService = class _CacheService {
|
|
|
6905
7191
|
* the previous key for seamless fallback decryption of existing cache entries.
|
|
6906
7192
|
*/
|
|
6907
7193
|
async rotateEncryptionKey(newKeyBase64, newMode) {
|
|
7194
|
+
this._assertActive("rotateEncryptionKey");
|
|
6908
7195
|
this.enc.rotateKey(newKeyBase64, newMode);
|
|
6909
7196
|
if (this._workerPool) {
|
|
6910
7197
|
await this._workerPool.drainAndReinit(this.enc.toWorkerInit());
|
|
@@ -6914,6 +7201,8 @@ var CacheService = class _CacheService {
|
|
|
6914
7201
|
/** Close Redis connections and stop all background timers. */
|
|
6915
7202
|
async destroy() {
|
|
6916
7203
|
this._destroyed = true;
|
|
7204
|
+
this._pendingDiskDeletes.clear();
|
|
7205
|
+
this._keyMutationEpochs.clear();
|
|
6917
7206
|
const g = globalThis;
|
|
6918
7207
|
const key = _CacheService.globalKey(this.opts);
|
|
6919
7208
|
if (g[key] === this) {
|