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
package/dist/index.cjs
CHANGED
|
@@ -4006,6 +4006,26 @@ async function findActiveSockets() {
|
|
|
4006
4006
|
active.push(currentPipe);
|
|
4007
4007
|
} catch {
|
|
4008
4008
|
}
|
|
4009
|
+
try {
|
|
4010
|
+
const { execFileSync } = await import("child_process");
|
|
4011
|
+
const stdout = execFileSync(
|
|
4012
|
+
"powershell.exe",
|
|
4013
|
+
["-NoProfile", "-NonInteractive", "-Command", '[System.IO.Directory]::GetFiles("\\\\.\\pipe\\") | Where-Object { $_ -like "*tricache-*" }'],
|
|
4014
|
+
{ timeout: 1500, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }
|
|
4015
|
+
);
|
|
4016
|
+
const candidates = stdout.split(/\r?\n/).map((name) => name.trim()).filter((name) => name.length > 0 && !active.includes(name));
|
|
4017
|
+
await Promise.all(
|
|
4018
|
+
candidates.map(async (pipePath) => {
|
|
4019
|
+
const testClient = new IpcTelemetryClient(pipePath);
|
|
4020
|
+
try {
|
|
4021
|
+
await testClient.ping(100);
|
|
4022
|
+
active.push(pipePath);
|
|
4023
|
+
} catch {
|
|
4024
|
+
}
|
|
4025
|
+
})
|
|
4026
|
+
);
|
|
4027
|
+
} catch {
|
|
4028
|
+
}
|
|
4009
4029
|
return active;
|
|
4010
4030
|
}
|
|
4011
4031
|
const dir = process.env.TMPDIR || "/tmp";
|
|
@@ -4435,6 +4455,52 @@ var CacheService = class _CacheService {
|
|
|
4435
4455
|
streamClient = null;
|
|
4436
4456
|
_lastStreamId = "$";
|
|
4437
4457
|
_destroyed = false;
|
|
4458
|
+
_pendingDiskDeletes = /* @__PURE__ */ new Set();
|
|
4459
|
+
_globalEpoch = 0;
|
|
4460
|
+
_keyMutationEpochs = /* @__PURE__ */ new Map();
|
|
4461
|
+
/**
|
|
4462
|
+
* Pattern tombstones for disk tier.
|
|
4463
|
+
* Files in the disk tier are keyed by SHA-256 of namespacedKey.
|
|
4464
|
+
* Wildcard pattern deletes cannot scan the filesystem synchronously, so we maintain
|
|
4465
|
+
* pattern tombstones in memory to intercept subsequent disk reads and evict matching files.
|
|
4466
|
+
*/
|
|
4467
|
+
_patternTombstones = [];
|
|
4468
|
+
_addPatternTombstone(prefix) {
|
|
4469
|
+
const now = Date.now();
|
|
4470
|
+
while (this._patternTombstones.length > 0 && now - this._patternTombstones[0].createdAt > 36e5) {
|
|
4471
|
+
this._patternTombstones.shift();
|
|
4472
|
+
}
|
|
4473
|
+
if (this._patternTombstones.length >= 2e3) {
|
|
4474
|
+
this._patternTombstones.shift();
|
|
4475
|
+
}
|
|
4476
|
+
this._patternTombstones.push({ prefix, createdAt: now });
|
|
4477
|
+
}
|
|
4478
|
+
_isTombstonedByPattern(namespacedKey) {
|
|
4479
|
+
if (this._patternTombstones.length === 0) return false;
|
|
4480
|
+
for (let i = this._patternTombstones.length - 1; i >= 0; i--) {
|
|
4481
|
+
if (namespacedKey.startsWith(this._patternTombstones[i].prefix)) {
|
|
4482
|
+
return true;
|
|
4483
|
+
}
|
|
4484
|
+
}
|
|
4485
|
+
return false;
|
|
4486
|
+
}
|
|
4487
|
+
_assertActive(operation) {
|
|
4488
|
+
if (this._destroyed) {
|
|
4489
|
+
throw new Error(`TriCacheError: Cannot perform '${operation}' on a destroyed CacheService instance (namespace: '${this._namespace || "(default)"}').`);
|
|
4490
|
+
}
|
|
4491
|
+
}
|
|
4492
|
+
_bumpKeyMutation(namespacedKey) {
|
|
4493
|
+
const next = (this._keyMutationEpochs.get(namespacedKey) ?? 0) + 1;
|
|
4494
|
+
this._keyMutationEpochs.set(namespacedKey, next);
|
|
4495
|
+
if (this._keyMutationEpochs.size > 5e4) {
|
|
4496
|
+
const oldest = this._keyMutationEpochs.keys().next().value;
|
|
4497
|
+
if (oldest !== void 0) this._keyMutationEpochs.delete(oldest);
|
|
4498
|
+
}
|
|
4499
|
+
return next;
|
|
4500
|
+
}
|
|
4501
|
+
_getKeyMutationEpoch(namespacedKey) {
|
|
4502
|
+
return this._globalEpoch + (this._keyMutationEpochs.get(namespacedKey) ?? 0);
|
|
4503
|
+
}
|
|
4438
4504
|
_ipcServer;
|
|
4439
4505
|
/** Timestamp (Date.now()) when the backplane subscriber last lost its connection. */
|
|
4440
4506
|
_subDisconnectedAt = null;
|
|
@@ -4523,6 +4589,8 @@ var CacheService = class _CacheService {
|
|
|
4523
4589
|
),
|
|
4524
4590
|
snapshotMaxAgeMs: options.snapshotMaxAgeMs ?? DEFAULT_SNAPSHOT_MAX_AGE,
|
|
4525
4591
|
invalidationBackplane: options.invalidationBackplane ?? true,
|
|
4592
|
+
awaitInvalidationBackplane: options.awaitInvalidationBackplane ?? false,
|
|
4593
|
+
lockFailClosed: options.lockFailClosed ?? false,
|
|
4526
4594
|
oomProtection: options.oomProtection ?? true,
|
|
4527
4595
|
oomHeapThreshold: options.oomHeapThreshold ?? 0.85,
|
|
4528
4596
|
oomCheckIntervalMs: options.oomCheckIntervalMs ?? 1e4,
|
|
@@ -4888,7 +4956,9 @@ var CacheService = class _CacheService {
|
|
|
4888
4956
|
"adaptiveTtl",
|
|
4889
4957
|
"l2WriteMode",
|
|
4890
4958
|
"instanceName",
|
|
4891
|
-
"invalidationBackplane"
|
|
4959
|
+
"invalidationBackplane",
|
|
4960
|
+
"awaitInvalidationBackplane",
|
|
4961
|
+
"lockFailClosed"
|
|
4892
4962
|
];
|
|
4893
4963
|
/** Returns the names of options that differ between `a` and the live `b`. */
|
|
4894
4964
|
static optionsDiff(a, b) {
|
|
@@ -4929,6 +4999,8 @@ var CacheService = class _CacheService {
|
|
|
4929
4999
|
frozen: o.frozen,
|
|
4930
5000
|
adaptiveTtl: o.adaptiveTtl,
|
|
4931
5001
|
invalidationBackplane: o.invalidationBackplane,
|
|
5002
|
+
awaitInvalidationBackplane: o.awaitInvalidationBackplane,
|
|
5003
|
+
lockFailClosed: o.lockFailClosed,
|
|
4932
5004
|
strictSingleton: o.strictSingleton,
|
|
4933
5005
|
remoteSnapshot: o.remoteSnapshot,
|
|
4934
5006
|
crossRegion: o.crossRegion
|
|
@@ -5214,16 +5286,36 @@ var CacheService = class _CacheService {
|
|
|
5214
5286
|
_applyInvalidationEvent(op, key, tagVersion) {
|
|
5215
5287
|
if (op === "del") {
|
|
5216
5288
|
this.l1.delete(key);
|
|
5289
|
+
this._bumpKeyMutation(key);
|
|
5290
|
+
this.inflight.delete(key);
|
|
5291
|
+
this._pendingDiskDeletes.add(key);
|
|
5217
5292
|
setImmediate(() => {
|
|
5218
|
-
|
|
5293
|
+
try {
|
|
5294
|
+
if (!this._diskDisabled) this.disk.delete(key);
|
|
5295
|
+
} finally {
|
|
5296
|
+
this._pendingDiskDeletes.delete(key);
|
|
5297
|
+
}
|
|
5219
5298
|
});
|
|
5220
5299
|
this._cascadeDependencies(key);
|
|
5221
5300
|
} else if (op === "del-glob") {
|
|
5222
5301
|
this.l1.deletePattern(key);
|
|
5302
|
+
const pfx = key.endsWith("*") ? key.slice(0, -1) : key;
|
|
5303
|
+
for (const liveKey of this.l1.liveKeys()) {
|
|
5304
|
+
if (liveKey.startsWith(pfx)) {
|
|
5305
|
+
this._bumpKeyMutation(liveKey);
|
|
5306
|
+
}
|
|
5307
|
+
}
|
|
5308
|
+
for (const inflightKey of Array.from(this.inflight.keys())) {
|
|
5309
|
+
if (inflightKey.startsWith(pfx)) {
|
|
5310
|
+
this._bumpKeyMutation(inflightKey);
|
|
5311
|
+
this.inflight.delete(inflightKey);
|
|
5312
|
+
}
|
|
5313
|
+
}
|
|
5314
|
+
this._addPatternTombstone(pfx);
|
|
5223
5315
|
} else if (op === "tag_incr") {
|
|
5224
5316
|
const tag = key;
|
|
5225
5317
|
const ver = typeof tagVersion === "number" ? tagVersion : (this.tagVersions.get(tag)?.version ?? 0) + 1;
|
|
5226
|
-
this._setLocalTagVersion(tag, ver,
|
|
5318
|
+
this._setLocalTagVersion(tag, ver, performance.now());
|
|
5227
5319
|
}
|
|
5228
5320
|
this.logger.debug("Backplane: invalidation applied", { op, key: key.slice(0, 60) });
|
|
5229
5321
|
}
|
|
@@ -5251,7 +5343,11 @@ var CacheService = class _CacheService {
|
|
|
5251
5343
|
if (!isCrossRegionRelay && this.opts.crossRegion) {
|
|
5252
5344
|
const shouldBroadcast = isExplicitInvalidation || Boolean(this.opts.crossRegion.broadcastOnSet);
|
|
5253
5345
|
if (shouldBroadcast) {
|
|
5254
|
-
|
|
5346
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
5347
|
+
await this._broadcastCrossRegion(op, key, tagVersion);
|
|
5348
|
+
} else {
|
|
5349
|
+
void this._broadcastCrossRegion(op, key, tagVersion);
|
|
5350
|
+
}
|
|
5255
5351
|
}
|
|
5256
5352
|
}
|
|
5257
5353
|
if (!this.opts.invalidationBackplane || this._redisDisabled) return;
|
|
@@ -5292,7 +5388,11 @@ var CacheService = class _CacheService {
|
|
|
5292
5388
|
);
|
|
5293
5389
|
}
|
|
5294
5390
|
this.counters.invSent++;
|
|
5295
|
-
} catch {
|
|
5391
|
+
} catch (err) {
|
|
5392
|
+
this.logger.warn("Backplane invalidation publish failed", { op, key, error: err.message });
|
|
5393
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
5394
|
+
throw err;
|
|
5395
|
+
}
|
|
5296
5396
|
}
|
|
5297
5397
|
}
|
|
5298
5398
|
_markCrossRegionEventSeen(id) {
|
|
@@ -5496,6 +5596,7 @@ var CacheService = class _CacheService {
|
|
|
5496
5596
|
}
|
|
5497
5597
|
}
|
|
5498
5598
|
async getRedis() {
|
|
5599
|
+
this._assertActive("getRedis");
|
|
5499
5600
|
if (!this.cb.isAllowed()) throw new Error("tricache: L2 circuit breaker is open");
|
|
5500
5601
|
if (this.redis) return this.redis;
|
|
5501
5602
|
if (this.redisConnecting) return this.redisConnecting;
|
|
@@ -5824,6 +5925,7 @@ var CacheService = class _CacheService {
|
|
|
5824
5925
|
* Caches negative lookups to avoid repeated DB hits for missing keys.
|
|
5825
5926
|
*/
|
|
5826
5927
|
async get(cacheKey, fetchFn, ttlSeconds = 300, opts = {}) {
|
|
5928
|
+
this._assertActive("get");
|
|
5827
5929
|
const span = this._startSpan("tricache.get");
|
|
5828
5930
|
if (this.opts.tracer) span.setAttribute("cache.key_prefix", cacheKey.split(":")[0]);
|
|
5829
5931
|
const k = this.nk(cacheKey);
|
|
@@ -5855,7 +5957,14 @@ var CacheService = class _CacheService {
|
|
|
5855
5957
|
if (swrGraceMs2 > 0 && !this.revalidating.has(k)) {
|
|
5856
5958
|
const priority2 = optPriority ?? inferPriority(cacheKey);
|
|
5857
5959
|
this.revalidating.add(k);
|
|
5858
|
-
void this._revalidate(
|
|
5960
|
+
void this._revalidate(
|
|
5961
|
+
k,
|
|
5962
|
+
fetchFn,
|
|
5963
|
+
ttlSeconds * 1e3,
|
|
5964
|
+
swrGraceMs2,
|
|
5965
|
+
priority2,
|
|
5966
|
+
l1Hit.tagVersions ?? (optTags ? Object.fromEntries(optTags.map((t) => [t, 0])) : void 0)
|
|
5967
|
+
);
|
|
5859
5968
|
this.counters.swrRevalidations++;
|
|
5860
5969
|
this.logger.debug("SWR: serving stale, revalidating", { cacheKey });
|
|
5861
5970
|
} else {
|
|
@@ -5874,7 +5983,14 @@ var CacheService = class _CacheService {
|
|
|
5874
5983
|
if ((shouldRefreshAhead || shouldXFetch) && !this.revalidating.has(k)) {
|
|
5875
5984
|
const priority2 = optPriority ?? inferPriority(cacheKey);
|
|
5876
5985
|
this.revalidating.add(k);
|
|
5877
|
-
void this._revalidate(
|
|
5986
|
+
void this._revalidate(
|
|
5987
|
+
k,
|
|
5988
|
+
fetchFn,
|
|
5989
|
+
entryTtl,
|
|
5990
|
+
optSwr * 1e3,
|
|
5991
|
+
priority2,
|
|
5992
|
+
l1Hit.tagVersions ?? (optTags ? Object.fromEntries(optTags.map((t) => [t, 0])) : void 0)
|
|
5993
|
+
);
|
|
5878
5994
|
this.counters.swrRevalidations++;
|
|
5879
5995
|
this.logger.debug(
|
|
5880
5996
|
shouldXFetch ? "XFetch: proactive background recompute" : "Refresh-ahead: proactive background recompute",
|
|
@@ -5900,44 +6016,52 @@ var CacheService = class _CacheService {
|
|
|
5900
6016
|
span.setAttribute("cache.hit", true).setAttribute("cache.stampede_coalesced", true).end();
|
|
5901
6017
|
return existing;
|
|
5902
6018
|
}
|
|
6019
|
+
const mutationEpochAtStart = this._getKeyMutationEpoch(k);
|
|
5903
6020
|
const executionPromise = (async () => {
|
|
5904
6021
|
try {
|
|
5905
|
-
if (!this._diskDisabled) {
|
|
5906
|
-
|
|
5907
|
-
|
|
5908
|
-
|
|
5909
|
-
|
|
5910
|
-
|
|
5911
|
-
|
|
5912
|
-
this.watchdog.
|
|
5913
|
-
if (
|
|
5914
|
-
|
|
5915
|
-
|
|
5916
|
-
|
|
5917
|
-
|
|
5918
|
-
|
|
5919
|
-
|
|
5920
|
-
|
|
5921
|
-
|
|
6022
|
+
if (!this._diskDisabled && !this._pendingDiskDeletes.has(k)) {
|
|
6023
|
+
if (this._isTombstonedByPattern(k)) {
|
|
6024
|
+
try {
|
|
6025
|
+
this.disk.delete(k);
|
|
6026
|
+
} catch {
|
|
6027
|
+
}
|
|
6028
|
+
} else {
|
|
6029
|
+
const isDiskAllowed = this.watchdog.isDiskAllowed();
|
|
6030
|
+
if (isDiskAllowed) {
|
|
6031
|
+
const diskStart = performance.now();
|
|
6032
|
+
const diskHit = this.disk.load(k);
|
|
6033
|
+
const diskElapsed = performance.now() - diskStart;
|
|
6034
|
+
this.watchdog.recordDisk(diskElapsed);
|
|
6035
|
+
this.watchdog.onCanaryResult(diskHit !== null, diskElapsed);
|
|
6036
|
+
if (diskHit !== null) {
|
|
6037
|
+
let isDiskStale = false;
|
|
6038
|
+
if (this.opts.tagStrategy === "generational" && diskHit.tagVersions) {
|
|
6039
|
+
const tags = Object.keys(diskHit.tagVersions);
|
|
6040
|
+
const currentVers = await Promise.all(tags.map((tag) => this._getTagVersion(tag)));
|
|
6041
|
+
for (let i = 0; i < tags.length; i++) {
|
|
6042
|
+
if (currentVers[i] > diskHit.tagVersions[tags[i]]) {
|
|
6043
|
+
isDiskStale = true;
|
|
6044
|
+
break;
|
|
6045
|
+
}
|
|
5922
6046
|
}
|
|
5923
6047
|
}
|
|
5924
|
-
|
|
5925
|
-
|
|
5926
|
-
|
|
5927
|
-
|
|
5928
|
-
|
|
5929
|
-
|
|
5930
|
-
|
|
5931
|
-
|
|
5932
|
-
|
|
5933
|
-
|
|
5934
|
-
|
|
5935
|
-
|
|
5936
|
-
|
|
5937
|
-
|
|
5938
|
-
|
|
5939
|
-
|
|
5940
|
-
|
|
6048
|
+
if (isDiskStale) {
|
|
6049
|
+
this.disk.delete(k);
|
|
6050
|
+
} else {
|
|
6051
|
+
const promoted = this.l1.importEntries(
|
|
6052
|
+
[{ key: k, entry: diskHit }],
|
|
6053
|
+
this.opts.forbiddenSnapshotPrefixes
|
|
6054
|
+
);
|
|
6055
|
+
if (promoted > 0) {
|
|
6056
|
+
const l1Check = this.l1.get(k);
|
|
6057
|
+
if (l1Check !== null) {
|
|
6058
|
+
this.counters.diskHits++;
|
|
6059
|
+
this.opts.onHit?.(cacheKey, "disk");
|
|
6060
|
+
this.logger.debug("L1.5 hit (disk \u2192 L1)", { cacheKey });
|
|
6061
|
+
span.setAttribute("cache.hit", true).setAttribute("cache.item.tier", "disk").setAttribute("cache.hit_tier", "disk");
|
|
6062
|
+
if (this.opts.frozen) deepFreeze(l1Check.value);
|
|
6063
|
+
return this.opts.cloneStrategy === "structuredClone" && l1Check.value != null && typeof l1Check.value === "object" ? structuredClone(l1Check.value) : l1Check.value;
|
|
6064
|
+
}
|
|
5941
6065
|
}
|
|
5942
6066
|
}
|
|
5943
6067
|
}
|
|
@@ -6048,6 +6172,18 @@ var CacheService = class _CacheService {
|
|
|
6048
6172
|
await this._registerTags(k, optTags, Math.ceil(effectiveTtl / 1e3));
|
|
6049
6173
|
}
|
|
6050
6174
|
}
|
|
6175
|
+
if (this._getKeyMutationEpoch(k) !== mutationEpochAtStart) {
|
|
6176
|
+
this.logger.debug("Aborting in-flight cache commit \u2014 key was mutated or deleted during fetch", { cacheKey });
|
|
6177
|
+
span.setAttribute("cache.aborted_mutation_race", true);
|
|
6178
|
+
const current = this.l1.get(k);
|
|
6179
|
+
if (current && !current.isStale && current.value !== void 0) {
|
|
6180
|
+
const freshVal = current.value;
|
|
6181
|
+
if (this.opts.frozen) deepFreeze(freshVal);
|
|
6182
|
+
return this.opts.cloneStrategy === "structuredClone" && freshVal != null && typeof freshVal === "object" ? structuredClone(freshVal) : freshVal;
|
|
6183
|
+
}
|
|
6184
|
+
if (this.opts.frozen) deepFreeze(data);
|
|
6185
|
+
return this.opts.cloneStrategy === "structuredClone" && data != null && typeof data === "object" ? structuredClone(data) : data;
|
|
6186
|
+
}
|
|
6051
6187
|
this.l1.set(k, data, storeTtl, priority, staleAt, delta, activeTagVersions);
|
|
6052
6188
|
if (!this._redisDisabled) {
|
|
6053
6189
|
try {
|
|
@@ -6164,22 +6300,40 @@ var CacheService = class _CacheService {
|
|
|
6164
6300
|
const ttl = options?.ttl ?? 300;
|
|
6165
6301
|
return this.get(cacheKey, fetchFn, ttl, options);
|
|
6166
6302
|
}
|
|
6167
|
-
async _revalidate(cacheKey, fetchFn, ttlMs, swrGraceMs, priority) {
|
|
6303
|
+
async _revalidate(cacheKey, fetchFn, ttlMs, swrGraceMs, priority, knownTagVersions) {
|
|
6304
|
+
const startEpoch = this._getKeyMutationEpoch(cacheKey);
|
|
6168
6305
|
try {
|
|
6169
6306
|
const fetchStart = Date.now();
|
|
6170
6307
|
const data = await fetchFn();
|
|
6171
6308
|
const delta = Date.now() - fetchStart;
|
|
6172
6309
|
const staleAt = Date.now() + ttlMs;
|
|
6310
|
+
if (this._getKeyMutationEpoch(cacheKey) !== startEpoch) {
|
|
6311
|
+
this.logger.debug("SWR: aborting revalidation commit \u2014 key was mutated or deleted during fetch", { cacheKey });
|
|
6312
|
+
return;
|
|
6313
|
+
}
|
|
6173
6314
|
if (this.latencyTracker && data != null) this.latencyTracker.record(cacheKey, delta);
|
|
6174
6315
|
let activeTagVersions;
|
|
6175
6316
|
const l1Existing = this.l1.get(cacheKey);
|
|
6176
|
-
|
|
6177
|
-
|
|
6317
|
+
const tagVersionsSource = l1Existing?.tagVersions ?? knownTagVersions;
|
|
6318
|
+
if (this.opts.tagStrategy === "generational" && tagVersionsSource) {
|
|
6319
|
+
const tags = Object.keys(tagVersionsSource);
|
|
6178
6320
|
const vers = await Promise.all(tags.map((tag) => this._getTagVersion(tag)));
|
|
6179
6321
|
activeTagVersions = {};
|
|
6180
6322
|
for (let i = 0; i < tags.length; i++) {
|
|
6181
6323
|
activeTagVersions[tags[i]] = vers[i];
|
|
6182
6324
|
}
|
|
6325
|
+
if (knownTagVersions) {
|
|
6326
|
+
for (const t of tags) {
|
|
6327
|
+
if (knownTagVersions[t] !== void 0 && activeTagVersions[t] !== knownTagVersions[t]) {
|
|
6328
|
+
this.logger.debug("SWR: aborting revalidation commit \u2014 tag version bumped during revalidation", { cacheKey, tag: t });
|
|
6329
|
+
return;
|
|
6330
|
+
}
|
|
6331
|
+
}
|
|
6332
|
+
}
|
|
6333
|
+
}
|
|
6334
|
+
if (this._getKeyMutationEpoch(cacheKey) !== startEpoch) {
|
|
6335
|
+
this.logger.debug("SWR: aborting revalidation commit \u2014 key was mutated or deleted during tag resolution", { cacheKey });
|
|
6336
|
+
return;
|
|
6183
6337
|
}
|
|
6184
6338
|
this.l1.set(cacheKey, data, ttlMs + swrGraceMs, priority, staleAt, delta, activeTagVersions);
|
|
6185
6339
|
if (!this._redisDisabled && this.opts.l2WriteMode === "read-write") {
|
|
@@ -6223,6 +6377,7 @@ var CacheService = class _CacheService {
|
|
|
6223
6377
|
// ── Explicit set / delete ─────────────────────────────────────────────────
|
|
6224
6378
|
/** Explicitly write a value into L1 (+ L2 in production). */
|
|
6225
6379
|
async set(cacheKey, data, ttlSeconds = 300, priority, opts) {
|
|
6380
|
+
this._assertActive("set");
|
|
6226
6381
|
const span = this._startSpan("tricache.set");
|
|
6227
6382
|
if (this.opts.tracer) {
|
|
6228
6383
|
span.setAttribute("cache.key_prefix", cacheKey.split(":")[0]);
|
|
@@ -6233,6 +6388,9 @@ var CacheService = class _CacheService {
|
|
|
6233
6388
|
const ttlMs = this._jitterTtl(effectiveTtlSeconds * 1e3);
|
|
6234
6389
|
const p = priority ?? inferPriority(cacheKey);
|
|
6235
6390
|
const k = this.nk(cacheKey);
|
|
6391
|
+
this._bumpKeyMutation(k);
|
|
6392
|
+
this.inflight.delete(k);
|
|
6393
|
+
this._pendingDiskDeletes.delete(k);
|
|
6236
6394
|
this.counters.sets++;
|
|
6237
6395
|
let activeTagVersions;
|
|
6238
6396
|
if (opts?.tags?.length) {
|
|
@@ -6287,7 +6445,11 @@ var CacheService = class _CacheService {
|
|
|
6287
6445
|
this.logger.debug("set: Redis unavailable", { cacheKey, error: err.message });
|
|
6288
6446
|
}
|
|
6289
6447
|
}
|
|
6290
|
-
|
|
6448
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6449
|
+
await this.publishInvalidation("del", k);
|
|
6450
|
+
} else {
|
|
6451
|
+
void this.publishInvalidation("del", k);
|
|
6452
|
+
}
|
|
6291
6453
|
} catch (err) {
|
|
6292
6454
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
6293
6455
|
span.recordException?.(err);
|
|
@@ -6334,6 +6496,7 @@ var CacheService = class _CacheService {
|
|
|
6334
6496
|
* await cache.delete('user:abc:*'); // all keys for user abc
|
|
6335
6497
|
*/
|
|
6336
6498
|
async delete(cacheKey) {
|
|
6499
|
+
this._assertActive("delete");
|
|
6337
6500
|
const span = this._startSpan("tricache.delete");
|
|
6338
6501
|
if (this.opts.tracer) span.setAttribute("cache.key_prefix", cacheKey.split(":")[0]);
|
|
6339
6502
|
try {
|
|
@@ -6341,11 +6504,31 @@ var CacheService = class _CacheService {
|
|
|
6341
6504
|
const k = this.nk(cacheKey);
|
|
6342
6505
|
this.counters.deletes++;
|
|
6343
6506
|
if (isPattern) {
|
|
6507
|
+
const pfx = k.endsWith("*") ? k.slice(0, -1) : k;
|
|
6508
|
+
for (const liveKey of this.l1.liveKeys()) {
|
|
6509
|
+
if (liveKey.startsWith(pfx)) {
|
|
6510
|
+
this._bumpKeyMutation(liveKey);
|
|
6511
|
+
}
|
|
6512
|
+
}
|
|
6513
|
+
for (const inflightKey of Array.from(this.inflight.keys())) {
|
|
6514
|
+
if (inflightKey.startsWith(pfx)) {
|
|
6515
|
+
this._bumpKeyMutation(inflightKey);
|
|
6516
|
+
this.inflight.delete(inflightKey);
|
|
6517
|
+
}
|
|
6518
|
+
}
|
|
6519
|
+
this._addPatternTombstone(pfx);
|
|
6344
6520
|
this.l1.deletePattern(k);
|
|
6345
6521
|
} else {
|
|
6522
|
+
this._bumpKeyMutation(k);
|
|
6523
|
+
this.inflight.delete(k);
|
|
6346
6524
|
this.l1.delete(k);
|
|
6525
|
+
this._pendingDiskDeletes.add(k);
|
|
6347
6526
|
setImmediate(() => {
|
|
6348
|
-
|
|
6527
|
+
try {
|
|
6528
|
+
if (!this._diskDisabled) this.disk.delete(k);
|
|
6529
|
+
} finally {
|
|
6530
|
+
this._pendingDiskDeletes.delete(k);
|
|
6531
|
+
}
|
|
6349
6532
|
});
|
|
6350
6533
|
this._cascadeDependencies(k);
|
|
6351
6534
|
for (const [, dependents] of this.dependencyIndex) dependents.delete(k);
|
|
@@ -6369,7 +6552,11 @@ var CacheService = class _CacheService {
|
|
|
6369
6552
|
this.logger.debug("delete: Redis unavailable", { cacheKey, error: err.message });
|
|
6370
6553
|
}
|
|
6371
6554
|
}
|
|
6372
|
-
|
|
6555
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6556
|
+
await this.publishInvalidation(isPattern ? "del-glob" : "del", k, void 0, false, true);
|
|
6557
|
+
} else {
|
|
6558
|
+
void this.publishInvalidation(isPattern ? "del-glob" : "del", k, void 0, false, true);
|
|
6559
|
+
}
|
|
6373
6560
|
} catch (err) {
|
|
6374
6561
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
6375
6562
|
span.recordException?.(err);
|
|
@@ -6396,6 +6583,7 @@ var CacheService = class _CacheService {
|
|
|
6396
6583
|
* fleet-wide.
|
|
6397
6584
|
*/
|
|
6398
6585
|
async increment(cacheKey, ttlSeconds) {
|
|
6586
|
+
this._assertActive("increment");
|
|
6399
6587
|
const k = this.nk(cacheKey);
|
|
6400
6588
|
if (this._redisDisabled) {
|
|
6401
6589
|
if (!this._incrementFallbackWarned) {
|
|
@@ -6437,19 +6625,38 @@ var CacheService = class _CacheService {
|
|
|
6437
6625
|
* await cache.clear('user:abc'); // flush all keys for one user
|
|
6438
6626
|
*/
|
|
6439
6627
|
async clear(prefix) {
|
|
6628
|
+
this._assertActive("clear");
|
|
6440
6629
|
const span = this._startSpan("tricache.clear");
|
|
6441
6630
|
if (this.opts.tracer) span.setAttribute("cache.prefix", prefix || "*");
|
|
6442
6631
|
try {
|
|
6443
6632
|
this.counters.deletes++;
|
|
6444
6633
|
const k = prefix ? this.nk(prefix.includes("*") ? prefix : `${prefix}*`) : void 0;
|
|
6445
6634
|
if (k) {
|
|
6635
|
+
const pfx = k.endsWith("*") ? k.slice(0, -1) : k;
|
|
6636
|
+
for (const liveKey of this.l1.liveKeys()) {
|
|
6637
|
+
if (liveKey.startsWith(pfx)) {
|
|
6638
|
+
this._bumpKeyMutation(liveKey);
|
|
6639
|
+
}
|
|
6640
|
+
}
|
|
6641
|
+
for (const inflightKey of Array.from(this.inflight.keys())) {
|
|
6642
|
+
if (inflightKey.startsWith(pfx)) {
|
|
6643
|
+
this._bumpKeyMutation(inflightKey);
|
|
6644
|
+
this.inflight.delete(inflightKey);
|
|
6645
|
+
}
|
|
6646
|
+
}
|
|
6647
|
+
this._addPatternTombstone(pfx);
|
|
6446
6648
|
this.l1.deletePattern(k);
|
|
6447
6649
|
} else {
|
|
6650
|
+
this._globalEpoch++;
|
|
6651
|
+
this.inflight.clear();
|
|
6652
|
+
this._patternTombstones.length = 0;
|
|
6448
6653
|
this.l1.clear();
|
|
6449
6654
|
if (!this._diskDisabled) this.disk.clear();
|
|
6450
6655
|
this._l1Counters.clear();
|
|
6451
6656
|
this.tagIndex.clear();
|
|
6452
6657
|
this.tagVersions.clear();
|
|
6658
|
+
this._pendingDiskDeletes.clear();
|
|
6659
|
+
this._keyMutationEpochs.clear();
|
|
6453
6660
|
}
|
|
6454
6661
|
if (!this._redisDisabled && this.opts.l2WriteMode === "read-write") {
|
|
6455
6662
|
try {
|
|
@@ -6461,13 +6668,23 @@ var CacheService = class _CacheService {
|
|
|
6461
6668
|
this.logger.debug("clear: Redis unavailable", { error: err.message });
|
|
6462
6669
|
}
|
|
6463
6670
|
}
|
|
6464
|
-
|
|
6465
|
-
|
|
6466
|
-
|
|
6467
|
-
|
|
6468
|
-
|
|
6469
|
-
|
|
6470
|
-
|
|
6671
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6672
|
+
await this.publishInvalidation(
|
|
6673
|
+
"del-glob",
|
|
6674
|
+
k ?? (this._namespace ? `${this._namespace}:*` : "*"),
|
|
6675
|
+
void 0,
|
|
6676
|
+
false,
|
|
6677
|
+
true
|
|
6678
|
+
);
|
|
6679
|
+
} else {
|
|
6680
|
+
void this.publishInvalidation(
|
|
6681
|
+
"del-glob",
|
|
6682
|
+
k ?? (this._namespace ? `${this._namespace}:*` : "*"),
|
|
6683
|
+
void 0,
|
|
6684
|
+
false,
|
|
6685
|
+
true
|
|
6686
|
+
);
|
|
6687
|
+
}
|
|
6471
6688
|
} catch (err) {
|
|
6472
6689
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
6473
6690
|
span.recordException?.(err);
|
|
@@ -6482,6 +6699,7 @@ var CacheService = class _CacheService {
|
|
|
6482
6699
|
* Returns the number of entries evicted.
|
|
6483
6700
|
*/
|
|
6484
6701
|
rebalance() {
|
|
6702
|
+
this._assertActive("rebalance");
|
|
6485
6703
|
return this.l1.rebalance();
|
|
6486
6704
|
}
|
|
6487
6705
|
/**
|
|
@@ -6490,6 +6708,7 @@ var CacheService = class _CacheService {
|
|
|
6490
6708
|
* Only reflects L1 state — does not query Redis or disk.
|
|
6491
6709
|
*/
|
|
6492
6710
|
ttl(cacheKey) {
|
|
6711
|
+
this._assertActive("ttl");
|
|
6493
6712
|
return this.l1.ttl(this.nk(cacheKey));
|
|
6494
6713
|
}
|
|
6495
6714
|
/**
|
|
@@ -6497,6 +6716,7 @@ var CacheService = class _CacheService {
|
|
|
6497
6716
|
* Bloom-filter fast path — no fetch, no disk or Redis round-trip.
|
|
6498
6717
|
*/
|
|
6499
6718
|
has(cacheKey) {
|
|
6719
|
+
this._assertActive("has");
|
|
6500
6720
|
return this.l1.has(this.nk(cacheKey));
|
|
6501
6721
|
}
|
|
6502
6722
|
// ── Iteration ─────────────────────────────────────────────────────────────
|
|
@@ -6507,6 +6727,7 @@ var CacheService = class _CacheService {
|
|
|
6507
6727
|
* for (const key of cache.keys()) console.log(key);
|
|
6508
6728
|
*/
|
|
6509
6729
|
*keys() {
|
|
6730
|
+
this._assertActive("keys");
|
|
6510
6731
|
const prefix = this._namespace ? this._namespace + ":" : "";
|
|
6511
6732
|
for (const key of this.l1.liveKeys()) {
|
|
6512
6733
|
if (prefix && !key.startsWith(prefix)) continue;
|
|
@@ -6521,6 +6742,7 @@ var CacheService = class _CacheService {
|
|
|
6521
6742
|
* for (const val of cache.values<User>()) console.log(val.id);
|
|
6522
6743
|
*/
|
|
6523
6744
|
*values() {
|
|
6745
|
+
this._assertActive("values");
|
|
6524
6746
|
const prefix = this._namespace ? this._namespace + ":" : "";
|
|
6525
6747
|
if (!prefix) {
|
|
6526
6748
|
yield* this.l1.liveValues();
|
|
@@ -6539,6 +6761,7 @@ var CacheService = class _CacheService {
|
|
|
6539
6761
|
* for (const [key, val] of cache.entries<User>()) console.log(key, val.id);
|
|
6540
6762
|
*/
|
|
6541
6763
|
*entries() {
|
|
6764
|
+
this._assertActive("entries");
|
|
6542
6765
|
const prefix = this._namespace ? this._namespace + ":" : "";
|
|
6543
6766
|
for (const [key, entry] of this.l1.liveEntries()) {
|
|
6544
6767
|
if (prefix && !key.startsWith(prefix)) continue;
|
|
@@ -6566,6 +6789,7 @@ var CacheService = class _CacheService {
|
|
|
6566
6789
|
* `offset` — `rawKey.slice(offset)` gives the bare key without namespace.
|
|
6567
6790
|
*/
|
|
6568
6791
|
scan(fn) {
|
|
6792
|
+
this._assertActive("scan");
|
|
6569
6793
|
const prefixLen = this._namespace ? this._namespace.length + 1 : 0;
|
|
6570
6794
|
this.l1.scan((key, entry, pfx) => {
|
|
6571
6795
|
const value = entry.value !== void 0 ? entry.value : entry.data;
|
|
@@ -6579,6 +6803,7 @@ var CacheService = class _CacheService {
|
|
|
6579
6803
|
* @param newTtlSeconds - The new TTL from now, in seconds.
|
|
6580
6804
|
*/
|
|
6581
6805
|
async touch(cacheKey, newTtlSeconds) {
|
|
6806
|
+
this._assertActive("touch");
|
|
6582
6807
|
const k = this.nk(cacheKey);
|
|
6583
6808
|
const hit = this.l1.touch(k, newTtlSeconds * 1e3);
|
|
6584
6809
|
if (hit && !this._redisDisabled) {
|
|
@@ -6599,6 +6824,7 @@ var CacheService = class _CacheService {
|
|
|
6599
6824
|
* if (fresh !== null) return fresh; // serve from L1, no network hop
|
|
6600
6825
|
*/
|
|
6601
6826
|
getIfFresh(cacheKey) {
|
|
6827
|
+
this._assertActive("getIfFresh");
|
|
6602
6828
|
const k = this.nk(cacheKey);
|
|
6603
6829
|
const entry = this.l1.getEntry(k);
|
|
6604
6830
|
if (!entry) return null;
|
|
@@ -6616,6 +6842,7 @@ var CacheService = class _CacheService {
|
|
|
6616
6842
|
* const val = await cache.peek<User>('user:123');
|
|
6617
6843
|
*/
|
|
6618
6844
|
async peek(cacheKey) {
|
|
6845
|
+
this._assertActive("peek");
|
|
6619
6846
|
const k = this.nk(cacheKey);
|
|
6620
6847
|
const l1Hit = this.l1.get(k);
|
|
6621
6848
|
if (l1Hit !== null) {
|
|
@@ -6625,25 +6852,32 @@ var CacheService = class _CacheService {
|
|
|
6625
6852
|
if (this.opts.frozen) deepFreeze(val);
|
|
6626
6853
|
return this.opts.cloneStrategy === "structuredClone" && val != null && typeof val === "object" ? structuredClone(val) : val;
|
|
6627
6854
|
}
|
|
6628
|
-
if (!this._diskDisabled && this.
|
|
6629
|
-
|
|
6630
|
-
|
|
6631
|
-
|
|
6632
|
-
|
|
6633
|
-
|
|
6634
|
-
if (
|
|
6635
|
-
const
|
|
6636
|
-
|
|
6637
|
-
|
|
6638
|
-
);
|
|
6639
|
-
|
|
6640
|
-
|
|
6641
|
-
|
|
6642
|
-
|
|
6643
|
-
this.
|
|
6644
|
-
|
|
6645
|
-
|
|
6646
|
-
|
|
6855
|
+
if (!this._diskDisabled && !this._pendingDiskDeletes.has(k)) {
|
|
6856
|
+
if (this._isTombstonedByPattern(k)) {
|
|
6857
|
+
try {
|
|
6858
|
+
this.disk.delete(k);
|
|
6859
|
+
} catch {
|
|
6860
|
+
}
|
|
6861
|
+
} else if (this.watchdog.isDiskAllowed()) {
|
|
6862
|
+
const diskStart = performance.now();
|
|
6863
|
+
const diskHit = this.disk.load(k);
|
|
6864
|
+
const diskElapsed = performance.now() - diskStart;
|
|
6865
|
+
this.watchdog.recordDisk(diskElapsed);
|
|
6866
|
+
this.watchdog.onCanaryResult(diskHit !== null, diskElapsed);
|
|
6867
|
+
if (diskHit !== null) {
|
|
6868
|
+
const promoted = this.l1.importEntries(
|
|
6869
|
+
[{ key: k, entry: diskHit }],
|
|
6870
|
+
this.opts.forbiddenSnapshotPrefixes
|
|
6871
|
+
);
|
|
6872
|
+
if (promoted > 0) {
|
|
6873
|
+
const l1Check = this.l1.get(k);
|
|
6874
|
+
if (l1Check !== null) {
|
|
6875
|
+
this.counters.gets++;
|
|
6876
|
+
this.counters.diskHits++;
|
|
6877
|
+
const val = l1Check.value;
|
|
6878
|
+
if (this.opts.frozen) deepFreeze(val);
|
|
6879
|
+
return this.opts.cloneStrategy === "structuredClone" && val != null && typeof val === "object" ? structuredClone(val) : val;
|
|
6880
|
+
}
|
|
6647
6881
|
}
|
|
6648
6882
|
}
|
|
6649
6883
|
}
|
|
@@ -6681,6 +6915,7 @@ var CacheService = class _CacheService {
|
|
|
6681
6915
|
* `(key) => number` so heterogeneous TTLs can be batched in one call.
|
|
6682
6916
|
*/
|
|
6683
6917
|
async mget(keys, fetchFn, ttl = 300, priority) {
|
|
6918
|
+
this._assertActive("mget");
|
|
6684
6919
|
const span = this._startSpan("tricache.mget");
|
|
6685
6920
|
if (this.opts.tracer) {
|
|
6686
6921
|
span.setAttribute("cache.batch.size", keys.length);
|
|
@@ -6749,8 +6984,16 @@ var CacheService = class _CacheService {
|
|
|
6749
6984
|
}
|
|
6750
6985
|
if (!this._diskDisabled && missKeys.length > 0) {
|
|
6751
6986
|
for (let j = missKeys.length - 1; j >= 0; j--) {
|
|
6752
|
-
if (!this.watchdog.isDiskAllowed()) continue;
|
|
6753
6987
|
const k = this.nk(missKeys[j]);
|
|
6988
|
+
if (this._pendingDiskDeletes.has(k)) continue;
|
|
6989
|
+
if (this._isTombstonedByPattern(k)) {
|
|
6990
|
+
try {
|
|
6991
|
+
this.disk.delete(k);
|
|
6992
|
+
} catch {
|
|
6993
|
+
}
|
|
6994
|
+
continue;
|
|
6995
|
+
}
|
|
6996
|
+
if (!this.watchdog.isDiskAllowed()) continue;
|
|
6754
6997
|
const diskStart = performance.now();
|
|
6755
6998
|
const diskHit = this.disk.load(k);
|
|
6756
6999
|
const diskElapsed = performance.now() - diskStart;
|
|
@@ -6830,6 +7073,7 @@ var CacheService = class _CacheService {
|
|
|
6830
7073
|
* });
|
|
6831
7074
|
*/
|
|
6832
7075
|
async mset(entries) {
|
|
7076
|
+
this._assertActive("mset");
|
|
6833
7077
|
const span = this._startSpan("tricache.mset");
|
|
6834
7078
|
const keys = Object.keys(entries);
|
|
6835
7079
|
if (this.opts.tracer) {
|
|
@@ -6857,6 +7101,7 @@ var CacheService = class _CacheService {
|
|
|
6857
7101
|
* await cache.mdel(['user:1', 'user:2', 'user:3']);
|
|
6858
7102
|
*/
|
|
6859
7103
|
async mdel(keys) {
|
|
7104
|
+
this._assertActive("mdel");
|
|
6860
7105
|
const span = this._startSpan("tricache.mdel");
|
|
6861
7106
|
if (this.opts.tracer) {
|
|
6862
7107
|
span.setAttribute("cache.batch.size", keys.length);
|
|
@@ -6884,6 +7129,7 @@ var CacheService = class _CacheService {
|
|
|
6884
7129
|
* console.log(`Warmed ${loaded} keys from Redis`);
|
|
6885
7130
|
*/
|
|
6886
7131
|
async warmFromL2(pattern, opts) {
|
|
7132
|
+
this._assertActive("warmFromL2");
|
|
6887
7133
|
if (this._redisDisabled) return 0;
|
|
6888
7134
|
try {
|
|
6889
7135
|
const client = await this.getRedis();
|
|
@@ -6930,6 +7176,7 @@ var CacheService = class _CacheService {
|
|
|
6930
7176
|
* await cache.invalidateTag('catalog'); // clears product:1 and any other tagged entries
|
|
6931
7177
|
*/
|
|
6932
7178
|
async invalidateTag(tag) {
|
|
7179
|
+
this._assertActive("invalidateTag");
|
|
6933
7180
|
const span = this._startSpan("tricache.invalidate_tag");
|
|
6934
7181
|
if (this.opts.tracer) {
|
|
6935
7182
|
span.setAttribute("cache.tag", tag);
|
|
@@ -6951,15 +7198,27 @@ var CacheService = class _CacheService {
|
|
|
6951
7198
|
const current = this.tagVersions.get(tag)?.version ?? 0;
|
|
6952
7199
|
newVer = current + 1;
|
|
6953
7200
|
}
|
|
6954
|
-
this._setLocalTagVersion(tag, newVer,
|
|
6955
|
-
|
|
7201
|
+
this._setLocalTagVersion(tag, newVer, performance.now());
|
|
7202
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
7203
|
+
await this.publishInvalidation("tag_incr", tag, newVer, false, true);
|
|
7204
|
+
} else {
|
|
7205
|
+
void this.publishInvalidation("tag_incr", tag, newVer, false, true);
|
|
7206
|
+
}
|
|
6956
7207
|
return;
|
|
6957
7208
|
}
|
|
6958
7209
|
const tagKey = this.nk(`_tag_:${tag}`);
|
|
6959
7210
|
const members = this.tagIndex.get(tagKey) ?? /* @__PURE__ */ new Set();
|
|
6960
7211
|
for (const k of members) {
|
|
6961
7212
|
this.l1.delete(k);
|
|
6962
|
-
|
|
7213
|
+
this._bumpKeyMutation(k);
|
|
7214
|
+
this._pendingDiskDeletes.add(k);
|
|
7215
|
+
setImmediate(() => {
|
|
7216
|
+
try {
|
|
7217
|
+
if (!this._diskDisabled) this.disk.delete(k);
|
|
7218
|
+
} finally {
|
|
7219
|
+
this._pendingDiskDeletes.delete(k);
|
|
7220
|
+
}
|
|
7221
|
+
});
|
|
6963
7222
|
}
|
|
6964
7223
|
this.tagIndex.delete(tagKey);
|
|
6965
7224
|
if (!this._redisDisabled) {
|
|
@@ -6997,13 +7256,14 @@ var CacheService = class _CacheService {
|
|
|
6997
7256
|
* await cache.invalidateTags(['case:acme', 'org:acme', 'ai-chat:acme']);
|
|
6998
7257
|
*/
|
|
6999
7258
|
async invalidateTags(tags) {
|
|
7259
|
+
this._assertActive("invalidateTags");
|
|
7000
7260
|
if (tags.length === 0) return;
|
|
7001
7261
|
if (tags.length === 1) {
|
|
7002
7262
|
await this.invalidateTag(tags[0]);
|
|
7003
7263
|
return;
|
|
7004
7264
|
}
|
|
7005
7265
|
if (this.opts.tagStrategy === "generational") {
|
|
7006
|
-
const now =
|
|
7266
|
+
const now = performance.now();
|
|
7007
7267
|
if (!this._redisDisabled) {
|
|
7008
7268
|
try {
|
|
7009
7269
|
const client = await this.getRedis();
|
|
@@ -7014,7 +7274,11 @@ var CacheService = class _CacheService {
|
|
|
7014
7274
|
const [err, newVer] = results[i] ?? [null, null];
|
|
7015
7275
|
const ver = !err && typeof newVer === "number" ? newVer : (this.tagVersions.get(tags[i])?.version ?? 0) + 1;
|
|
7016
7276
|
this._setLocalTagVersion(tags[i], ver, now);
|
|
7017
|
-
|
|
7277
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
7278
|
+
await this.publishInvalidation("tag_incr", tags[i], ver, false, true);
|
|
7279
|
+
} else {
|
|
7280
|
+
void this.publishInvalidation("tag_incr", tags[i], ver, false, true);
|
|
7281
|
+
}
|
|
7018
7282
|
}
|
|
7019
7283
|
return;
|
|
7020
7284
|
} catch (err) {
|
|
@@ -7024,7 +7288,11 @@ var CacheService = class _CacheService {
|
|
|
7024
7288
|
for (const tag of tags) {
|
|
7025
7289
|
const ver = (this.tagVersions.get(tag)?.version ?? 0) + 1;
|
|
7026
7290
|
this._setLocalTagVersion(tag, ver, now);
|
|
7027
|
-
|
|
7291
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
7292
|
+
await this.publishInvalidation("tag_incr", tag, ver, false, true);
|
|
7293
|
+
} else {
|
|
7294
|
+
void this.publishInvalidation("tag_incr", tag, ver, false, true);
|
|
7295
|
+
}
|
|
7028
7296
|
}
|
|
7029
7297
|
return;
|
|
7030
7298
|
}
|
|
@@ -7039,7 +7307,15 @@ var CacheService = class _CacheService {
|
|
|
7039
7307
|
}
|
|
7040
7308
|
for (const k of allMembers) {
|
|
7041
7309
|
this.l1.delete(k);
|
|
7042
|
-
this.
|
|
7310
|
+
this._bumpKeyMutation(k);
|
|
7311
|
+
this._pendingDiskDeletes.add(k);
|
|
7312
|
+
setImmediate(() => {
|
|
7313
|
+
try {
|
|
7314
|
+
if (!this._diskDisabled) this.disk.delete(k);
|
|
7315
|
+
} finally {
|
|
7316
|
+
this._pendingDiskDeletes.delete(k);
|
|
7317
|
+
}
|
|
7318
|
+
});
|
|
7043
7319
|
}
|
|
7044
7320
|
if (!this._redisDisabled) {
|
|
7045
7321
|
try {
|
|
@@ -7076,6 +7352,7 @@ var CacheService = class _CacheService {
|
|
|
7076
7352
|
* `l2` is `null` when Redis is disabled.
|
|
7077
7353
|
*/
|
|
7078
7354
|
async ping() {
|
|
7355
|
+
this._assertActive("ping");
|
|
7079
7356
|
const t0 = Date.now();
|
|
7080
7357
|
this.l1.has("__ping__");
|
|
7081
7358
|
const l1 = Date.now() - t0;
|
|
@@ -7155,6 +7432,7 @@ var CacheService = class _CacheService {
|
|
|
7155
7432
|
* Returns the number of keys written.
|
|
7156
7433
|
*/
|
|
7157
7434
|
async drainToL2() {
|
|
7435
|
+
this._assertActive("drainToL2");
|
|
7158
7436
|
if (this._redisDisabled) return 0;
|
|
7159
7437
|
try {
|
|
7160
7438
|
const client = await this.getRedis();
|
|
@@ -7198,6 +7476,7 @@ var CacheService = class _CacheService {
|
|
|
7198
7476
|
* if (!claimed) return res.status(409).json({ error: 'duplicate request' });
|
|
7199
7477
|
*/
|
|
7200
7478
|
async setIfAbsent(cacheKey, value, ttlSeconds = 300, priority) {
|
|
7479
|
+
this._assertActive("setIfAbsent");
|
|
7201
7480
|
const k = this.nk(cacheKey);
|
|
7202
7481
|
if (this.l1.has(k)) return false;
|
|
7203
7482
|
if (!this._redisDisabled) {
|
|
@@ -7216,6 +7495,8 @@ var CacheService = class _CacheService {
|
|
|
7216
7495
|
}
|
|
7217
7496
|
const ttlMs = this._jitterTtl(ttlSeconds * 1e3);
|
|
7218
7497
|
const p = priority ?? inferPriority(cacheKey);
|
|
7498
|
+
this._bumpKeyMutation(k);
|
|
7499
|
+
this._pendingDiskDeletes.delete(k);
|
|
7219
7500
|
this.l1.set(k, value, ttlMs, p);
|
|
7220
7501
|
this.counters.sets++;
|
|
7221
7502
|
return true;
|
|
@@ -7234,6 +7515,7 @@ var CacheService = class _CacheService {
|
|
|
7234
7515
|
* @param options TTL, acquire timeout, and retry polling intervals.
|
|
7235
7516
|
*/
|
|
7236
7517
|
async lock(resourceKey, fn, options) {
|
|
7518
|
+
this._assertActive("lock");
|
|
7237
7519
|
const ttlSeconds = Math.max(1, options?.ttl ?? 30);
|
|
7238
7520
|
const acquireTimeout = Math.max(0, options?.acquireTimeout ?? 5e3);
|
|
7239
7521
|
const retryInterval = Math.max(10, options?.retryInterval ?? 100);
|
|
@@ -7260,6 +7542,10 @@ var CacheService = class _CacheService {
|
|
|
7260
7542
|
}
|
|
7261
7543
|
} catch (err) {
|
|
7262
7544
|
if (err.message.startsWith("Failed to acquire lock")) throw err;
|
|
7545
|
+
const failClosed = options?.failClosedOnRedisError ?? this.opts.lockFailClosed;
|
|
7546
|
+
if (failClosed) {
|
|
7547
|
+
throw new Error(`Distributed lock acquisition failed for resource "${resourceKey}" due to Redis error: ${err.message}`);
|
|
7548
|
+
}
|
|
7263
7549
|
this.logger.debug("Distributed lock: Redis error, falling back to in-process mutex", {
|
|
7264
7550
|
resourceKey,
|
|
7265
7551
|
error: err.message
|
|
@@ -7605,6 +7891,7 @@ var CacheService = class _CacheService {
|
|
|
7605
7891
|
* the previous key for seamless fallback decryption of existing cache entries.
|
|
7606
7892
|
*/
|
|
7607
7893
|
async rotateEncryptionKey(newKeyBase64, newMode) {
|
|
7894
|
+
this._assertActive("rotateEncryptionKey");
|
|
7608
7895
|
this.enc.rotateKey(newKeyBase64, newMode);
|
|
7609
7896
|
if (this._workerPool) {
|
|
7610
7897
|
await this._workerPool.drainAndReinit(this.enc.toWorkerInit());
|
|
@@ -7614,6 +7901,8 @@ var CacheService = class _CacheService {
|
|
|
7614
7901
|
/** Close Redis connections and stop all background timers. */
|
|
7615
7902
|
async destroy() {
|
|
7616
7903
|
this._destroyed = true;
|
|
7904
|
+
this._pendingDiskDeletes.clear();
|
|
7905
|
+
this._keyMutationEpochs.clear();
|
|
7617
7906
|
const g = globalThis;
|
|
7618
7907
|
const key = _CacheService.globalKey(this.opts);
|
|
7619
7908
|
if (g[key] === this) {
|