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/next/index.cjs
CHANGED
|
@@ -3902,6 +3902,52 @@ var CacheService = class _CacheService {
|
|
|
3902
3902
|
streamClient = null;
|
|
3903
3903
|
_lastStreamId = "$";
|
|
3904
3904
|
_destroyed = false;
|
|
3905
|
+
_pendingDiskDeletes = /* @__PURE__ */ new Set();
|
|
3906
|
+
_globalEpoch = 0;
|
|
3907
|
+
_keyMutationEpochs = /* @__PURE__ */ new Map();
|
|
3908
|
+
/**
|
|
3909
|
+
* Pattern tombstones for disk tier.
|
|
3910
|
+
* Files in the disk tier are keyed by SHA-256 of namespacedKey.
|
|
3911
|
+
* Wildcard pattern deletes cannot scan the filesystem synchronously, so we maintain
|
|
3912
|
+
* pattern tombstones in memory to intercept subsequent disk reads and evict matching files.
|
|
3913
|
+
*/
|
|
3914
|
+
_patternTombstones = [];
|
|
3915
|
+
_addPatternTombstone(prefix) {
|
|
3916
|
+
const now = Date.now();
|
|
3917
|
+
while (this._patternTombstones.length > 0 && now - this._patternTombstones[0].createdAt > 36e5) {
|
|
3918
|
+
this._patternTombstones.shift();
|
|
3919
|
+
}
|
|
3920
|
+
if (this._patternTombstones.length >= 2e3) {
|
|
3921
|
+
this._patternTombstones.shift();
|
|
3922
|
+
}
|
|
3923
|
+
this._patternTombstones.push({ prefix, createdAt: now });
|
|
3924
|
+
}
|
|
3925
|
+
_isTombstonedByPattern(namespacedKey) {
|
|
3926
|
+
if (this._patternTombstones.length === 0) return false;
|
|
3927
|
+
for (let i = this._patternTombstones.length - 1; i >= 0; i--) {
|
|
3928
|
+
if (namespacedKey.startsWith(this._patternTombstones[i].prefix)) {
|
|
3929
|
+
return true;
|
|
3930
|
+
}
|
|
3931
|
+
}
|
|
3932
|
+
return false;
|
|
3933
|
+
}
|
|
3934
|
+
_assertActive(operation) {
|
|
3935
|
+
if (this._destroyed) {
|
|
3936
|
+
throw new Error(`TriCacheError: Cannot perform '${operation}' on a destroyed CacheService instance (namespace: '${this._namespace || "(default)"}').`);
|
|
3937
|
+
}
|
|
3938
|
+
}
|
|
3939
|
+
_bumpKeyMutation(namespacedKey) {
|
|
3940
|
+
const next = (this._keyMutationEpochs.get(namespacedKey) ?? 0) + 1;
|
|
3941
|
+
this._keyMutationEpochs.set(namespacedKey, next);
|
|
3942
|
+
if (this._keyMutationEpochs.size > 5e4) {
|
|
3943
|
+
const oldest = this._keyMutationEpochs.keys().next().value;
|
|
3944
|
+
if (oldest !== void 0) this._keyMutationEpochs.delete(oldest);
|
|
3945
|
+
}
|
|
3946
|
+
return next;
|
|
3947
|
+
}
|
|
3948
|
+
_getKeyMutationEpoch(namespacedKey) {
|
|
3949
|
+
return this._globalEpoch + (this._keyMutationEpochs.get(namespacedKey) ?? 0);
|
|
3950
|
+
}
|
|
3905
3951
|
_ipcServer;
|
|
3906
3952
|
/** Timestamp (Date.now()) when the backplane subscriber last lost its connection. */
|
|
3907
3953
|
_subDisconnectedAt = null;
|
|
@@ -3990,6 +4036,8 @@ var CacheService = class _CacheService {
|
|
|
3990
4036
|
),
|
|
3991
4037
|
snapshotMaxAgeMs: options.snapshotMaxAgeMs ?? DEFAULT_SNAPSHOT_MAX_AGE,
|
|
3992
4038
|
invalidationBackplane: options.invalidationBackplane ?? true,
|
|
4039
|
+
awaitInvalidationBackplane: options.awaitInvalidationBackplane ?? false,
|
|
4040
|
+
lockFailClosed: options.lockFailClosed ?? false,
|
|
3993
4041
|
oomProtection: options.oomProtection ?? true,
|
|
3994
4042
|
oomHeapThreshold: options.oomHeapThreshold ?? 0.85,
|
|
3995
4043
|
oomCheckIntervalMs: options.oomCheckIntervalMs ?? 1e4,
|
|
@@ -4355,7 +4403,9 @@ var CacheService = class _CacheService {
|
|
|
4355
4403
|
"adaptiveTtl",
|
|
4356
4404
|
"l2WriteMode",
|
|
4357
4405
|
"instanceName",
|
|
4358
|
-
"invalidationBackplane"
|
|
4406
|
+
"invalidationBackplane",
|
|
4407
|
+
"awaitInvalidationBackplane",
|
|
4408
|
+
"lockFailClosed"
|
|
4359
4409
|
];
|
|
4360
4410
|
/** Returns the names of options that differ between `a` and the live `b`. */
|
|
4361
4411
|
static optionsDiff(a, b) {
|
|
@@ -4396,6 +4446,8 @@ var CacheService = class _CacheService {
|
|
|
4396
4446
|
frozen: o.frozen,
|
|
4397
4447
|
adaptiveTtl: o.adaptiveTtl,
|
|
4398
4448
|
invalidationBackplane: o.invalidationBackplane,
|
|
4449
|
+
awaitInvalidationBackplane: o.awaitInvalidationBackplane,
|
|
4450
|
+
lockFailClosed: o.lockFailClosed,
|
|
4399
4451
|
strictSingleton: o.strictSingleton,
|
|
4400
4452
|
remoteSnapshot: o.remoteSnapshot,
|
|
4401
4453
|
crossRegion: o.crossRegion
|
|
@@ -4681,16 +4733,36 @@ var CacheService = class _CacheService {
|
|
|
4681
4733
|
_applyInvalidationEvent(op, key, tagVersion) {
|
|
4682
4734
|
if (op === "del") {
|
|
4683
4735
|
this.l1.delete(key);
|
|
4736
|
+
this._bumpKeyMutation(key);
|
|
4737
|
+
this.inflight.delete(key);
|
|
4738
|
+
this._pendingDiskDeletes.add(key);
|
|
4684
4739
|
setImmediate(() => {
|
|
4685
|
-
|
|
4740
|
+
try {
|
|
4741
|
+
if (!this._diskDisabled) this.disk.delete(key);
|
|
4742
|
+
} finally {
|
|
4743
|
+
this._pendingDiskDeletes.delete(key);
|
|
4744
|
+
}
|
|
4686
4745
|
});
|
|
4687
4746
|
this._cascadeDependencies(key);
|
|
4688
4747
|
} else if (op === "del-glob") {
|
|
4689
4748
|
this.l1.deletePattern(key);
|
|
4749
|
+
const pfx = key.endsWith("*") ? key.slice(0, -1) : key;
|
|
4750
|
+
for (const liveKey of this.l1.liveKeys()) {
|
|
4751
|
+
if (liveKey.startsWith(pfx)) {
|
|
4752
|
+
this._bumpKeyMutation(liveKey);
|
|
4753
|
+
}
|
|
4754
|
+
}
|
|
4755
|
+
for (const inflightKey of Array.from(this.inflight.keys())) {
|
|
4756
|
+
if (inflightKey.startsWith(pfx)) {
|
|
4757
|
+
this._bumpKeyMutation(inflightKey);
|
|
4758
|
+
this.inflight.delete(inflightKey);
|
|
4759
|
+
}
|
|
4760
|
+
}
|
|
4761
|
+
this._addPatternTombstone(pfx);
|
|
4690
4762
|
} else if (op === "tag_incr") {
|
|
4691
4763
|
const tag = key;
|
|
4692
4764
|
const ver = typeof tagVersion === "number" ? tagVersion : (this.tagVersions.get(tag)?.version ?? 0) + 1;
|
|
4693
|
-
this._setLocalTagVersion(tag, ver,
|
|
4765
|
+
this._setLocalTagVersion(tag, ver, performance.now());
|
|
4694
4766
|
}
|
|
4695
4767
|
this.logger.debug("Backplane: invalidation applied", { op, key: key.slice(0, 60) });
|
|
4696
4768
|
}
|
|
@@ -4718,7 +4790,11 @@ var CacheService = class _CacheService {
|
|
|
4718
4790
|
if (!isCrossRegionRelay && this.opts.crossRegion) {
|
|
4719
4791
|
const shouldBroadcast = isExplicitInvalidation || Boolean(this.opts.crossRegion.broadcastOnSet);
|
|
4720
4792
|
if (shouldBroadcast) {
|
|
4721
|
-
|
|
4793
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
4794
|
+
await this._broadcastCrossRegion(op, key, tagVersion);
|
|
4795
|
+
} else {
|
|
4796
|
+
void this._broadcastCrossRegion(op, key, tagVersion);
|
|
4797
|
+
}
|
|
4722
4798
|
}
|
|
4723
4799
|
}
|
|
4724
4800
|
if (!this.opts.invalidationBackplane || this._redisDisabled) return;
|
|
@@ -4759,7 +4835,11 @@ var CacheService = class _CacheService {
|
|
|
4759
4835
|
);
|
|
4760
4836
|
}
|
|
4761
4837
|
this.counters.invSent++;
|
|
4762
|
-
} catch {
|
|
4838
|
+
} catch (err) {
|
|
4839
|
+
this.logger.warn("Backplane invalidation publish failed", { op, key, error: err.message });
|
|
4840
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
4841
|
+
throw err;
|
|
4842
|
+
}
|
|
4763
4843
|
}
|
|
4764
4844
|
}
|
|
4765
4845
|
_markCrossRegionEventSeen(id) {
|
|
@@ -4963,6 +5043,7 @@ var CacheService = class _CacheService {
|
|
|
4963
5043
|
}
|
|
4964
5044
|
}
|
|
4965
5045
|
async getRedis() {
|
|
5046
|
+
this._assertActive("getRedis");
|
|
4966
5047
|
if (!this.cb.isAllowed()) throw new Error("tricache: L2 circuit breaker is open");
|
|
4967
5048
|
if (this.redis) return this.redis;
|
|
4968
5049
|
if (this.redisConnecting) return this.redisConnecting;
|
|
@@ -5291,6 +5372,7 @@ var CacheService = class _CacheService {
|
|
|
5291
5372
|
* Caches negative lookups to avoid repeated DB hits for missing keys.
|
|
5292
5373
|
*/
|
|
5293
5374
|
async get(cacheKey, fetchFn, ttlSeconds = 300, opts = {}) {
|
|
5375
|
+
this._assertActive("get");
|
|
5294
5376
|
const span = this._startSpan("tricache.get");
|
|
5295
5377
|
if (this.opts.tracer) span.setAttribute("cache.key_prefix", cacheKey.split(":")[0]);
|
|
5296
5378
|
const k = this.nk(cacheKey);
|
|
@@ -5322,7 +5404,14 @@ var CacheService = class _CacheService {
|
|
|
5322
5404
|
if (swrGraceMs2 > 0 && !this.revalidating.has(k)) {
|
|
5323
5405
|
const priority2 = optPriority ?? inferPriority(cacheKey);
|
|
5324
5406
|
this.revalidating.add(k);
|
|
5325
|
-
void this._revalidate(
|
|
5407
|
+
void this._revalidate(
|
|
5408
|
+
k,
|
|
5409
|
+
fetchFn,
|
|
5410
|
+
ttlSeconds * 1e3,
|
|
5411
|
+
swrGraceMs2,
|
|
5412
|
+
priority2,
|
|
5413
|
+
l1Hit.tagVersions ?? (optTags ? Object.fromEntries(optTags.map((t) => [t, 0])) : void 0)
|
|
5414
|
+
);
|
|
5326
5415
|
this.counters.swrRevalidations++;
|
|
5327
5416
|
this.logger.debug("SWR: serving stale, revalidating", { cacheKey });
|
|
5328
5417
|
} else {
|
|
@@ -5341,7 +5430,14 @@ var CacheService = class _CacheService {
|
|
|
5341
5430
|
if ((shouldRefreshAhead || shouldXFetch) && !this.revalidating.has(k)) {
|
|
5342
5431
|
const priority2 = optPriority ?? inferPriority(cacheKey);
|
|
5343
5432
|
this.revalidating.add(k);
|
|
5344
|
-
void this._revalidate(
|
|
5433
|
+
void this._revalidate(
|
|
5434
|
+
k,
|
|
5435
|
+
fetchFn,
|
|
5436
|
+
entryTtl,
|
|
5437
|
+
optSwr * 1e3,
|
|
5438
|
+
priority2,
|
|
5439
|
+
l1Hit.tagVersions ?? (optTags ? Object.fromEntries(optTags.map((t) => [t, 0])) : void 0)
|
|
5440
|
+
);
|
|
5345
5441
|
this.counters.swrRevalidations++;
|
|
5346
5442
|
this.logger.debug(
|
|
5347
5443
|
shouldXFetch ? "XFetch: proactive background recompute" : "Refresh-ahead: proactive background recompute",
|
|
@@ -5367,44 +5463,52 @@ var CacheService = class _CacheService {
|
|
|
5367
5463
|
span.setAttribute("cache.hit", true).setAttribute("cache.stampede_coalesced", true).end();
|
|
5368
5464
|
return existing;
|
|
5369
5465
|
}
|
|
5466
|
+
const mutationEpochAtStart = this._getKeyMutationEpoch(k);
|
|
5370
5467
|
const executionPromise = (async () => {
|
|
5371
5468
|
try {
|
|
5372
|
-
if (!this._diskDisabled) {
|
|
5373
|
-
|
|
5374
|
-
|
|
5375
|
-
|
|
5376
|
-
|
|
5377
|
-
|
|
5378
|
-
|
|
5379
|
-
this.watchdog.
|
|
5380
|
-
if (
|
|
5381
|
-
|
|
5382
|
-
|
|
5383
|
-
|
|
5384
|
-
|
|
5385
|
-
|
|
5386
|
-
|
|
5387
|
-
|
|
5388
|
-
|
|
5469
|
+
if (!this._diskDisabled && !this._pendingDiskDeletes.has(k)) {
|
|
5470
|
+
if (this._isTombstonedByPattern(k)) {
|
|
5471
|
+
try {
|
|
5472
|
+
this.disk.delete(k);
|
|
5473
|
+
} catch {
|
|
5474
|
+
}
|
|
5475
|
+
} else {
|
|
5476
|
+
const isDiskAllowed = this.watchdog.isDiskAllowed();
|
|
5477
|
+
if (isDiskAllowed) {
|
|
5478
|
+
const diskStart = performance.now();
|
|
5479
|
+
const diskHit = this.disk.load(k);
|
|
5480
|
+
const diskElapsed = performance.now() - diskStart;
|
|
5481
|
+
this.watchdog.recordDisk(diskElapsed);
|
|
5482
|
+
this.watchdog.onCanaryResult(diskHit !== null, diskElapsed);
|
|
5483
|
+
if (diskHit !== null) {
|
|
5484
|
+
let isDiskStale = false;
|
|
5485
|
+
if (this.opts.tagStrategy === "generational" && diskHit.tagVersions) {
|
|
5486
|
+
const tags = Object.keys(diskHit.tagVersions);
|
|
5487
|
+
const currentVers = await Promise.all(tags.map((tag) => this._getTagVersion(tag)));
|
|
5488
|
+
for (let i = 0; i < tags.length; i++) {
|
|
5489
|
+
if (currentVers[i] > diskHit.tagVersions[tags[i]]) {
|
|
5490
|
+
isDiskStale = true;
|
|
5491
|
+
break;
|
|
5492
|
+
}
|
|
5389
5493
|
}
|
|
5390
5494
|
}
|
|
5391
|
-
|
|
5392
|
-
|
|
5393
|
-
|
|
5394
|
-
|
|
5395
|
-
|
|
5396
|
-
|
|
5397
|
-
|
|
5398
|
-
|
|
5399
|
-
|
|
5400
|
-
|
|
5401
|
-
|
|
5402
|
-
|
|
5403
|
-
|
|
5404
|
-
|
|
5405
|
-
|
|
5406
|
-
|
|
5407
|
-
|
|
5495
|
+
if (isDiskStale) {
|
|
5496
|
+
this.disk.delete(k);
|
|
5497
|
+
} else {
|
|
5498
|
+
const promoted = this.l1.importEntries(
|
|
5499
|
+
[{ key: k, entry: diskHit }],
|
|
5500
|
+
this.opts.forbiddenSnapshotPrefixes
|
|
5501
|
+
);
|
|
5502
|
+
if (promoted > 0) {
|
|
5503
|
+
const l1Check = this.l1.get(k);
|
|
5504
|
+
if (l1Check !== null) {
|
|
5505
|
+
this.counters.diskHits++;
|
|
5506
|
+
this.opts.onHit?.(cacheKey, "disk");
|
|
5507
|
+
this.logger.debug("L1.5 hit (disk \u2192 L1)", { cacheKey });
|
|
5508
|
+
span.setAttribute("cache.hit", true).setAttribute("cache.item.tier", "disk").setAttribute("cache.hit_tier", "disk");
|
|
5509
|
+
if (this.opts.frozen) deepFreeze(l1Check.value);
|
|
5510
|
+
return this.opts.cloneStrategy === "structuredClone" && l1Check.value != null && typeof l1Check.value === "object" ? structuredClone(l1Check.value) : l1Check.value;
|
|
5511
|
+
}
|
|
5408
5512
|
}
|
|
5409
5513
|
}
|
|
5410
5514
|
}
|
|
@@ -5515,6 +5619,18 @@ var CacheService = class _CacheService {
|
|
|
5515
5619
|
await this._registerTags(k, optTags, Math.ceil(effectiveTtl / 1e3));
|
|
5516
5620
|
}
|
|
5517
5621
|
}
|
|
5622
|
+
if (this._getKeyMutationEpoch(k) !== mutationEpochAtStart) {
|
|
5623
|
+
this.logger.debug("Aborting in-flight cache commit \u2014 key was mutated or deleted during fetch", { cacheKey });
|
|
5624
|
+
span.setAttribute("cache.aborted_mutation_race", true);
|
|
5625
|
+
const current = this.l1.get(k);
|
|
5626
|
+
if (current && !current.isStale && current.value !== void 0) {
|
|
5627
|
+
const freshVal = current.value;
|
|
5628
|
+
if (this.opts.frozen) deepFreeze(freshVal);
|
|
5629
|
+
return this.opts.cloneStrategy === "structuredClone" && freshVal != null && typeof freshVal === "object" ? structuredClone(freshVal) : freshVal;
|
|
5630
|
+
}
|
|
5631
|
+
if (this.opts.frozen) deepFreeze(data);
|
|
5632
|
+
return this.opts.cloneStrategy === "structuredClone" && data != null && typeof data === "object" ? structuredClone(data) : data;
|
|
5633
|
+
}
|
|
5518
5634
|
this.l1.set(k, data, storeTtl, priority, staleAt, delta, activeTagVersions);
|
|
5519
5635
|
if (!this._redisDisabled) {
|
|
5520
5636
|
try {
|
|
@@ -5631,22 +5747,40 @@ var CacheService = class _CacheService {
|
|
|
5631
5747
|
const ttl = options?.ttl ?? 300;
|
|
5632
5748
|
return this.get(cacheKey, fetchFn, ttl, options);
|
|
5633
5749
|
}
|
|
5634
|
-
async _revalidate(cacheKey, fetchFn, ttlMs, swrGraceMs, priority) {
|
|
5750
|
+
async _revalidate(cacheKey, fetchFn, ttlMs, swrGraceMs, priority, knownTagVersions) {
|
|
5751
|
+
const startEpoch = this._getKeyMutationEpoch(cacheKey);
|
|
5635
5752
|
try {
|
|
5636
5753
|
const fetchStart = Date.now();
|
|
5637
5754
|
const data = await fetchFn();
|
|
5638
5755
|
const delta = Date.now() - fetchStart;
|
|
5639
5756
|
const staleAt = Date.now() + ttlMs;
|
|
5757
|
+
if (this._getKeyMutationEpoch(cacheKey) !== startEpoch) {
|
|
5758
|
+
this.logger.debug("SWR: aborting revalidation commit \u2014 key was mutated or deleted during fetch", { cacheKey });
|
|
5759
|
+
return;
|
|
5760
|
+
}
|
|
5640
5761
|
if (this.latencyTracker && data != null) this.latencyTracker.record(cacheKey, delta);
|
|
5641
5762
|
let activeTagVersions;
|
|
5642
5763
|
const l1Existing = this.l1.get(cacheKey);
|
|
5643
|
-
|
|
5644
|
-
|
|
5764
|
+
const tagVersionsSource = l1Existing?.tagVersions ?? knownTagVersions;
|
|
5765
|
+
if (this.opts.tagStrategy === "generational" && tagVersionsSource) {
|
|
5766
|
+
const tags = Object.keys(tagVersionsSource);
|
|
5645
5767
|
const vers = await Promise.all(tags.map((tag) => this._getTagVersion(tag)));
|
|
5646
5768
|
activeTagVersions = {};
|
|
5647
5769
|
for (let i = 0; i < tags.length; i++) {
|
|
5648
5770
|
activeTagVersions[tags[i]] = vers[i];
|
|
5649
5771
|
}
|
|
5772
|
+
if (knownTagVersions) {
|
|
5773
|
+
for (const t of tags) {
|
|
5774
|
+
if (knownTagVersions[t] !== void 0 && activeTagVersions[t] !== knownTagVersions[t]) {
|
|
5775
|
+
this.logger.debug("SWR: aborting revalidation commit \u2014 tag version bumped during revalidation", { cacheKey, tag: t });
|
|
5776
|
+
return;
|
|
5777
|
+
}
|
|
5778
|
+
}
|
|
5779
|
+
}
|
|
5780
|
+
}
|
|
5781
|
+
if (this._getKeyMutationEpoch(cacheKey) !== startEpoch) {
|
|
5782
|
+
this.logger.debug("SWR: aborting revalidation commit \u2014 key was mutated or deleted during tag resolution", { cacheKey });
|
|
5783
|
+
return;
|
|
5650
5784
|
}
|
|
5651
5785
|
this.l1.set(cacheKey, data, ttlMs + swrGraceMs, priority, staleAt, delta, activeTagVersions);
|
|
5652
5786
|
if (!this._redisDisabled && this.opts.l2WriteMode === "read-write") {
|
|
@@ -5690,6 +5824,7 @@ var CacheService = class _CacheService {
|
|
|
5690
5824
|
// ── Explicit set / delete ─────────────────────────────────────────────────
|
|
5691
5825
|
/** Explicitly write a value into L1 (+ L2 in production). */
|
|
5692
5826
|
async set(cacheKey, data, ttlSeconds = 300, priority, opts) {
|
|
5827
|
+
this._assertActive("set");
|
|
5693
5828
|
const span = this._startSpan("tricache.set");
|
|
5694
5829
|
if (this.opts.tracer) {
|
|
5695
5830
|
span.setAttribute("cache.key_prefix", cacheKey.split(":")[0]);
|
|
@@ -5700,6 +5835,9 @@ var CacheService = class _CacheService {
|
|
|
5700
5835
|
const ttlMs = this._jitterTtl(effectiveTtlSeconds * 1e3);
|
|
5701
5836
|
const p = priority ?? inferPriority(cacheKey);
|
|
5702
5837
|
const k = this.nk(cacheKey);
|
|
5838
|
+
this._bumpKeyMutation(k);
|
|
5839
|
+
this.inflight.delete(k);
|
|
5840
|
+
this._pendingDiskDeletes.delete(k);
|
|
5703
5841
|
this.counters.sets++;
|
|
5704
5842
|
let activeTagVersions;
|
|
5705
5843
|
if (opts?.tags?.length) {
|
|
@@ -5754,7 +5892,11 @@ var CacheService = class _CacheService {
|
|
|
5754
5892
|
this.logger.debug("set: Redis unavailable", { cacheKey, error: err.message });
|
|
5755
5893
|
}
|
|
5756
5894
|
}
|
|
5757
|
-
|
|
5895
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
5896
|
+
await this.publishInvalidation("del", k);
|
|
5897
|
+
} else {
|
|
5898
|
+
void this.publishInvalidation("del", k);
|
|
5899
|
+
}
|
|
5758
5900
|
} catch (err) {
|
|
5759
5901
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
5760
5902
|
span.recordException?.(err);
|
|
@@ -5801,6 +5943,7 @@ var CacheService = class _CacheService {
|
|
|
5801
5943
|
* await cache.delete('user:abc:*'); // all keys for user abc
|
|
5802
5944
|
*/
|
|
5803
5945
|
async delete(cacheKey) {
|
|
5946
|
+
this._assertActive("delete");
|
|
5804
5947
|
const span = this._startSpan("tricache.delete");
|
|
5805
5948
|
if (this.opts.tracer) span.setAttribute("cache.key_prefix", cacheKey.split(":")[0]);
|
|
5806
5949
|
try {
|
|
@@ -5808,11 +5951,31 @@ var CacheService = class _CacheService {
|
|
|
5808
5951
|
const k = this.nk(cacheKey);
|
|
5809
5952
|
this.counters.deletes++;
|
|
5810
5953
|
if (isPattern) {
|
|
5954
|
+
const pfx = k.endsWith("*") ? k.slice(0, -1) : k;
|
|
5955
|
+
for (const liveKey of this.l1.liveKeys()) {
|
|
5956
|
+
if (liveKey.startsWith(pfx)) {
|
|
5957
|
+
this._bumpKeyMutation(liveKey);
|
|
5958
|
+
}
|
|
5959
|
+
}
|
|
5960
|
+
for (const inflightKey of Array.from(this.inflight.keys())) {
|
|
5961
|
+
if (inflightKey.startsWith(pfx)) {
|
|
5962
|
+
this._bumpKeyMutation(inflightKey);
|
|
5963
|
+
this.inflight.delete(inflightKey);
|
|
5964
|
+
}
|
|
5965
|
+
}
|
|
5966
|
+
this._addPatternTombstone(pfx);
|
|
5811
5967
|
this.l1.deletePattern(k);
|
|
5812
5968
|
} else {
|
|
5969
|
+
this._bumpKeyMutation(k);
|
|
5970
|
+
this.inflight.delete(k);
|
|
5813
5971
|
this.l1.delete(k);
|
|
5972
|
+
this._pendingDiskDeletes.add(k);
|
|
5814
5973
|
setImmediate(() => {
|
|
5815
|
-
|
|
5974
|
+
try {
|
|
5975
|
+
if (!this._diskDisabled) this.disk.delete(k);
|
|
5976
|
+
} finally {
|
|
5977
|
+
this._pendingDiskDeletes.delete(k);
|
|
5978
|
+
}
|
|
5816
5979
|
});
|
|
5817
5980
|
this._cascadeDependencies(k);
|
|
5818
5981
|
for (const [, dependents] of this.dependencyIndex) dependents.delete(k);
|
|
@@ -5836,7 +5999,11 @@ var CacheService = class _CacheService {
|
|
|
5836
5999
|
this.logger.debug("delete: Redis unavailable", { cacheKey, error: err.message });
|
|
5837
6000
|
}
|
|
5838
6001
|
}
|
|
5839
|
-
|
|
6002
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6003
|
+
await this.publishInvalidation(isPattern ? "del-glob" : "del", k, void 0, false, true);
|
|
6004
|
+
} else {
|
|
6005
|
+
void this.publishInvalidation(isPattern ? "del-glob" : "del", k, void 0, false, true);
|
|
6006
|
+
}
|
|
5840
6007
|
} catch (err) {
|
|
5841
6008
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
5842
6009
|
span.recordException?.(err);
|
|
@@ -5863,6 +6030,7 @@ var CacheService = class _CacheService {
|
|
|
5863
6030
|
* fleet-wide.
|
|
5864
6031
|
*/
|
|
5865
6032
|
async increment(cacheKey, ttlSeconds) {
|
|
6033
|
+
this._assertActive("increment");
|
|
5866
6034
|
const k = this.nk(cacheKey);
|
|
5867
6035
|
if (this._redisDisabled) {
|
|
5868
6036
|
if (!this._incrementFallbackWarned) {
|
|
@@ -5904,19 +6072,38 @@ var CacheService = class _CacheService {
|
|
|
5904
6072
|
* await cache.clear('user:abc'); // flush all keys for one user
|
|
5905
6073
|
*/
|
|
5906
6074
|
async clear(prefix) {
|
|
6075
|
+
this._assertActive("clear");
|
|
5907
6076
|
const span = this._startSpan("tricache.clear");
|
|
5908
6077
|
if (this.opts.tracer) span.setAttribute("cache.prefix", prefix || "*");
|
|
5909
6078
|
try {
|
|
5910
6079
|
this.counters.deletes++;
|
|
5911
6080
|
const k = prefix ? this.nk(prefix.includes("*") ? prefix : `${prefix}*`) : void 0;
|
|
5912
6081
|
if (k) {
|
|
6082
|
+
const pfx = k.endsWith("*") ? k.slice(0, -1) : k;
|
|
6083
|
+
for (const liveKey of this.l1.liveKeys()) {
|
|
6084
|
+
if (liveKey.startsWith(pfx)) {
|
|
6085
|
+
this._bumpKeyMutation(liveKey);
|
|
6086
|
+
}
|
|
6087
|
+
}
|
|
6088
|
+
for (const inflightKey of Array.from(this.inflight.keys())) {
|
|
6089
|
+
if (inflightKey.startsWith(pfx)) {
|
|
6090
|
+
this._bumpKeyMutation(inflightKey);
|
|
6091
|
+
this.inflight.delete(inflightKey);
|
|
6092
|
+
}
|
|
6093
|
+
}
|
|
6094
|
+
this._addPatternTombstone(pfx);
|
|
5913
6095
|
this.l1.deletePattern(k);
|
|
5914
6096
|
} else {
|
|
6097
|
+
this._globalEpoch++;
|
|
6098
|
+
this.inflight.clear();
|
|
6099
|
+
this._patternTombstones.length = 0;
|
|
5915
6100
|
this.l1.clear();
|
|
5916
6101
|
if (!this._diskDisabled) this.disk.clear();
|
|
5917
6102
|
this._l1Counters.clear();
|
|
5918
6103
|
this.tagIndex.clear();
|
|
5919
6104
|
this.tagVersions.clear();
|
|
6105
|
+
this._pendingDiskDeletes.clear();
|
|
6106
|
+
this._keyMutationEpochs.clear();
|
|
5920
6107
|
}
|
|
5921
6108
|
if (!this._redisDisabled && this.opts.l2WriteMode === "read-write") {
|
|
5922
6109
|
try {
|
|
@@ -5928,13 +6115,23 @@ var CacheService = class _CacheService {
|
|
|
5928
6115
|
this.logger.debug("clear: Redis unavailable", { error: err.message });
|
|
5929
6116
|
}
|
|
5930
6117
|
}
|
|
5931
|
-
|
|
5932
|
-
|
|
5933
|
-
|
|
5934
|
-
|
|
5935
|
-
|
|
5936
|
-
|
|
5937
|
-
|
|
6118
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6119
|
+
await this.publishInvalidation(
|
|
6120
|
+
"del-glob",
|
|
6121
|
+
k ?? (this._namespace ? `${this._namespace}:*` : "*"),
|
|
6122
|
+
void 0,
|
|
6123
|
+
false,
|
|
6124
|
+
true
|
|
6125
|
+
);
|
|
6126
|
+
} else {
|
|
6127
|
+
void this.publishInvalidation(
|
|
6128
|
+
"del-glob",
|
|
6129
|
+
k ?? (this._namespace ? `${this._namespace}:*` : "*"),
|
|
6130
|
+
void 0,
|
|
6131
|
+
false,
|
|
6132
|
+
true
|
|
6133
|
+
);
|
|
6134
|
+
}
|
|
5938
6135
|
} catch (err) {
|
|
5939
6136
|
span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
|
|
5940
6137
|
span.recordException?.(err);
|
|
@@ -5949,6 +6146,7 @@ var CacheService = class _CacheService {
|
|
|
5949
6146
|
* Returns the number of entries evicted.
|
|
5950
6147
|
*/
|
|
5951
6148
|
rebalance() {
|
|
6149
|
+
this._assertActive("rebalance");
|
|
5952
6150
|
return this.l1.rebalance();
|
|
5953
6151
|
}
|
|
5954
6152
|
/**
|
|
@@ -5957,6 +6155,7 @@ var CacheService = class _CacheService {
|
|
|
5957
6155
|
* Only reflects L1 state — does not query Redis or disk.
|
|
5958
6156
|
*/
|
|
5959
6157
|
ttl(cacheKey) {
|
|
6158
|
+
this._assertActive("ttl");
|
|
5960
6159
|
return this.l1.ttl(this.nk(cacheKey));
|
|
5961
6160
|
}
|
|
5962
6161
|
/**
|
|
@@ -5964,6 +6163,7 @@ var CacheService = class _CacheService {
|
|
|
5964
6163
|
* Bloom-filter fast path — no fetch, no disk or Redis round-trip.
|
|
5965
6164
|
*/
|
|
5966
6165
|
has(cacheKey) {
|
|
6166
|
+
this._assertActive("has");
|
|
5967
6167
|
return this.l1.has(this.nk(cacheKey));
|
|
5968
6168
|
}
|
|
5969
6169
|
// ── Iteration ─────────────────────────────────────────────────────────────
|
|
@@ -5974,6 +6174,7 @@ var CacheService = class _CacheService {
|
|
|
5974
6174
|
* for (const key of cache.keys()) console.log(key);
|
|
5975
6175
|
*/
|
|
5976
6176
|
*keys() {
|
|
6177
|
+
this._assertActive("keys");
|
|
5977
6178
|
const prefix = this._namespace ? this._namespace + ":" : "";
|
|
5978
6179
|
for (const key of this.l1.liveKeys()) {
|
|
5979
6180
|
if (prefix && !key.startsWith(prefix)) continue;
|
|
@@ -5988,6 +6189,7 @@ var CacheService = class _CacheService {
|
|
|
5988
6189
|
* for (const val of cache.values<User>()) console.log(val.id);
|
|
5989
6190
|
*/
|
|
5990
6191
|
*values() {
|
|
6192
|
+
this._assertActive("values");
|
|
5991
6193
|
const prefix = this._namespace ? this._namespace + ":" : "";
|
|
5992
6194
|
if (!prefix) {
|
|
5993
6195
|
yield* this.l1.liveValues();
|
|
@@ -6006,6 +6208,7 @@ var CacheService = class _CacheService {
|
|
|
6006
6208
|
* for (const [key, val] of cache.entries<User>()) console.log(key, val.id);
|
|
6007
6209
|
*/
|
|
6008
6210
|
*entries() {
|
|
6211
|
+
this._assertActive("entries");
|
|
6009
6212
|
const prefix = this._namespace ? this._namespace + ":" : "";
|
|
6010
6213
|
for (const [key, entry] of this.l1.liveEntries()) {
|
|
6011
6214
|
if (prefix && !key.startsWith(prefix)) continue;
|
|
@@ -6033,6 +6236,7 @@ var CacheService = class _CacheService {
|
|
|
6033
6236
|
* `offset` — `rawKey.slice(offset)` gives the bare key without namespace.
|
|
6034
6237
|
*/
|
|
6035
6238
|
scan(fn) {
|
|
6239
|
+
this._assertActive("scan");
|
|
6036
6240
|
const prefixLen = this._namespace ? this._namespace.length + 1 : 0;
|
|
6037
6241
|
this.l1.scan((key, entry, pfx) => {
|
|
6038
6242
|
const value = entry.value !== void 0 ? entry.value : entry.data;
|
|
@@ -6046,6 +6250,7 @@ var CacheService = class _CacheService {
|
|
|
6046
6250
|
* @param newTtlSeconds - The new TTL from now, in seconds.
|
|
6047
6251
|
*/
|
|
6048
6252
|
async touch(cacheKey, newTtlSeconds) {
|
|
6253
|
+
this._assertActive("touch");
|
|
6049
6254
|
const k = this.nk(cacheKey);
|
|
6050
6255
|
const hit = this.l1.touch(k, newTtlSeconds * 1e3);
|
|
6051
6256
|
if (hit && !this._redisDisabled) {
|
|
@@ -6066,6 +6271,7 @@ var CacheService = class _CacheService {
|
|
|
6066
6271
|
* if (fresh !== null) return fresh; // serve from L1, no network hop
|
|
6067
6272
|
*/
|
|
6068
6273
|
getIfFresh(cacheKey) {
|
|
6274
|
+
this._assertActive("getIfFresh");
|
|
6069
6275
|
const k = this.nk(cacheKey);
|
|
6070
6276
|
const entry = this.l1.getEntry(k);
|
|
6071
6277
|
if (!entry) return null;
|
|
@@ -6083,6 +6289,7 @@ var CacheService = class _CacheService {
|
|
|
6083
6289
|
* const val = await cache.peek<User>('user:123');
|
|
6084
6290
|
*/
|
|
6085
6291
|
async peek(cacheKey) {
|
|
6292
|
+
this._assertActive("peek");
|
|
6086
6293
|
const k = this.nk(cacheKey);
|
|
6087
6294
|
const l1Hit = this.l1.get(k);
|
|
6088
6295
|
if (l1Hit !== null) {
|
|
@@ -6092,25 +6299,32 @@ var CacheService = class _CacheService {
|
|
|
6092
6299
|
if (this.opts.frozen) deepFreeze(val);
|
|
6093
6300
|
return this.opts.cloneStrategy === "structuredClone" && val != null && typeof val === "object" ? structuredClone(val) : val;
|
|
6094
6301
|
}
|
|
6095
|
-
if (!this._diskDisabled && this.
|
|
6096
|
-
|
|
6097
|
-
|
|
6098
|
-
|
|
6099
|
-
|
|
6100
|
-
|
|
6101
|
-
if (
|
|
6102
|
-
const
|
|
6103
|
-
|
|
6104
|
-
|
|
6105
|
-
);
|
|
6106
|
-
|
|
6107
|
-
|
|
6108
|
-
|
|
6109
|
-
|
|
6110
|
-
this.
|
|
6111
|
-
|
|
6112
|
-
|
|
6113
|
-
|
|
6302
|
+
if (!this._diskDisabled && !this._pendingDiskDeletes.has(k)) {
|
|
6303
|
+
if (this._isTombstonedByPattern(k)) {
|
|
6304
|
+
try {
|
|
6305
|
+
this.disk.delete(k);
|
|
6306
|
+
} catch {
|
|
6307
|
+
}
|
|
6308
|
+
} else if (this.watchdog.isDiskAllowed()) {
|
|
6309
|
+
const diskStart = performance.now();
|
|
6310
|
+
const diskHit = this.disk.load(k);
|
|
6311
|
+
const diskElapsed = performance.now() - diskStart;
|
|
6312
|
+
this.watchdog.recordDisk(diskElapsed);
|
|
6313
|
+
this.watchdog.onCanaryResult(diskHit !== null, diskElapsed);
|
|
6314
|
+
if (diskHit !== null) {
|
|
6315
|
+
const promoted = this.l1.importEntries(
|
|
6316
|
+
[{ key: k, entry: diskHit }],
|
|
6317
|
+
this.opts.forbiddenSnapshotPrefixes
|
|
6318
|
+
);
|
|
6319
|
+
if (promoted > 0) {
|
|
6320
|
+
const l1Check = this.l1.get(k);
|
|
6321
|
+
if (l1Check !== null) {
|
|
6322
|
+
this.counters.gets++;
|
|
6323
|
+
this.counters.diskHits++;
|
|
6324
|
+
const val = l1Check.value;
|
|
6325
|
+
if (this.opts.frozen) deepFreeze(val);
|
|
6326
|
+
return this.opts.cloneStrategy === "structuredClone" && val != null && typeof val === "object" ? structuredClone(val) : val;
|
|
6327
|
+
}
|
|
6114
6328
|
}
|
|
6115
6329
|
}
|
|
6116
6330
|
}
|
|
@@ -6148,6 +6362,7 @@ var CacheService = class _CacheService {
|
|
|
6148
6362
|
* `(key) => number` so heterogeneous TTLs can be batched in one call.
|
|
6149
6363
|
*/
|
|
6150
6364
|
async mget(keys, fetchFn, ttl = 300, priority) {
|
|
6365
|
+
this._assertActive("mget");
|
|
6151
6366
|
const span = this._startSpan("tricache.mget");
|
|
6152
6367
|
if (this.opts.tracer) {
|
|
6153
6368
|
span.setAttribute("cache.batch.size", keys.length);
|
|
@@ -6216,8 +6431,16 @@ var CacheService = class _CacheService {
|
|
|
6216
6431
|
}
|
|
6217
6432
|
if (!this._diskDisabled && missKeys.length > 0) {
|
|
6218
6433
|
for (let j = missKeys.length - 1; j >= 0; j--) {
|
|
6219
|
-
if (!this.watchdog.isDiskAllowed()) continue;
|
|
6220
6434
|
const k = this.nk(missKeys[j]);
|
|
6435
|
+
if (this._pendingDiskDeletes.has(k)) continue;
|
|
6436
|
+
if (this._isTombstonedByPattern(k)) {
|
|
6437
|
+
try {
|
|
6438
|
+
this.disk.delete(k);
|
|
6439
|
+
} catch {
|
|
6440
|
+
}
|
|
6441
|
+
continue;
|
|
6442
|
+
}
|
|
6443
|
+
if (!this.watchdog.isDiskAllowed()) continue;
|
|
6221
6444
|
const diskStart = performance.now();
|
|
6222
6445
|
const diskHit = this.disk.load(k);
|
|
6223
6446
|
const diskElapsed = performance.now() - diskStart;
|
|
@@ -6297,6 +6520,7 @@ var CacheService = class _CacheService {
|
|
|
6297
6520
|
* });
|
|
6298
6521
|
*/
|
|
6299
6522
|
async mset(entries) {
|
|
6523
|
+
this._assertActive("mset");
|
|
6300
6524
|
const span = this._startSpan("tricache.mset");
|
|
6301
6525
|
const keys = Object.keys(entries);
|
|
6302
6526
|
if (this.opts.tracer) {
|
|
@@ -6324,6 +6548,7 @@ var CacheService = class _CacheService {
|
|
|
6324
6548
|
* await cache.mdel(['user:1', 'user:2', 'user:3']);
|
|
6325
6549
|
*/
|
|
6326
6550
|
async mdel(keys) {
|
|
6551
|
+
this._assertActive("mdel");
|
|
6327
6552
|
const span = this._startSpan("tricache.mdel");
|
|
6328
6553
|
if (this.opts.tracer) {
|
|
6329
6554
|
span.setAttribute("cache.batch.size", keys.length);
|
|
@@ -6351,6 +6576,7 @@ var CacheService = class _CacheService {
|
|
|
6351
6576
|
* console.log(`Warmed ${loaded} keys from Redis`);
|
|
6352
6577
|
*/
|
|
6353
6578
|
async warmFromL2(pattern, opts) {
|
|
6579
|
+
this._assertActive("warmFromL2");
|
|
6354
6580
|
if (this._redisDisabled) return 0;
|
|
6355
6581
|
try {
|
|
6356
6582
|
const client = await this.getRedis();
|
|
@@ -6397,6 +6623,7 @@ var CacheService = class _CacheService {
|
|
|
6397
6623
|
* await cache.invalidateTag('catalog'); // clears product:1 and any other tagged entries
|
|
6398
6624
|
*/
|
|
6399
6625
|
async invalidateTag(tag) {
|
|
6626
|
+
this._assertActive("invalidateTag");
|
|
6400
6627
|
const span = this._startSpan("tricache.invalidate_tag");
|
|
6401
6628
|
if (this.opts.tracer) {
|
|
6402
6629
|
span.setAttribute("cache.tag", tag);
|
|
@@ -6418,15 +6645,27 @@ var CacheService = class _CacheService {
|
|
|
6418
6645
|
const current = this.tagVersions.get(tag)?.version ?? 0;
|
|
6419
6646
|
newVer = current + 1;
|
|
6420
6647
|
}
|
|
6421
|
-
this._setLocalTagVersion(tag, newVer,
|
|
6422
|
-
|
|
6648
|
+
this._setLocalTagVersion(tag, newVer, performance.now());
|
|
6649
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6650
|
+
await this.publishInvalidation("tag_incr", tag, newVer, false, true);
|
|
6651
|
+
} else {
|
|
6652
|
+
void this.publishInvalidation("tag_incr", tag, newVer, false, true);
|
|
6653
|
+
}
|
|
6423
6654
|
return;
|
|
6424
6655
|
}
|
|
6425
6656
|
const tagKey = this.nk(`_tag_:${tag}`);
|
|
6426
6657
|
const members = this.tagIndex.get(tagKey) ?? /* @__PURE__ */ new Set();
|
|
6427
6658
|
for (const k of members) {
|
|
6428
6659
|
this.l1.delete(k);
|
|
6429
|
-
|
|
6660
|
+
this._bumpKeyMutation(k);
|
|
6661
|
+
this._pendingDiskDeletes.add(k);
|
|
6662
|
+
setImmediate(() => {
|
|
6663
|
+
try {
|
|
6664
|
+
if (!this._diskDisabled) this.disk.delete(k);
|
|
6665
|
+
} finally {
|
|
6666
|
+
this._pendingDiskDeletes.delete(k);
|
|
6667
|
+
}
|
|
6668
|
+
});
|
|
6430
6669
|
}
|
|
6431
6670
|
this.tagIndex.delete(tagKey);
|
|
6432
6671
|
if (!this._redisDisabled) {
|
|
@@ -6464,13 +6703,14 @@ var CacheService = class _CacheService {
|
|
|
6464
6703
|
* await cache.invalidateTags(['case:acme', 'org:acme', 'ai-chat:acme']);
|
|
6465
6704
|
*/
|
|
6466
6705
|
async invalidateTags(tags) {
|
|
6706
|
+
this._assertActive("invalidateTags");
|
|
6467
6707
|
if (tags.length === 0) return;
|
|
6468
6708
|
if (tags.length === 1) {
|
|
6469
6709
|
await this.invalidateTag(tags[0]);
|
|
6470
6710
|
return;
|
|
6471
6711
|
}
|
|
6472
6712
|
if (this.opts.tagStrategy === "generational") {
|
|
6473
|
-
const now =
|
|
6713
|
+
const now = performance.now();
|
|
6474
6714
|
if (!this._redisDisabled) {
|
|
6475
6715
|
try {
|
|
6476
6716
|
const client = await this.getRedis();
|
|
@@ -6481,7 +6721,11 @@ var CacheService = class _CacheService {
|
|
|
6481
6721
|
const [err, newVer] = results[i] ?? [null, null];
|
|
6482
6722
|
const ver = !err && typeof newVer === "number" ? newVer : (this.tagVersions.get(tags[i])?.version ?? 0) + 1;
|
|
6483
6723
|
this._setLocalTagVersion(tags[i], ver, now);
|
|
6484
|
-
|
|
6724
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6725
|
+
await this.publishInvalidation("tag_incr", tags[i], ver, false, true);
|
|
6726
|
+
} else {
|
|
6727
|
+
void this.publishInvalidation("tag_incr", tags[i], ver, false, true);
|
|
6728
|
+
}
|
|
6485
6729
|
}
|
|
6486
6730
|
return;
|
|
6487
6731
|
} catch (err) {
|
|
@@ -6491,7 +6735,11 @@ var CacheService = class _CacheService {
|
|
|
6491
6735
|
for (const tag of tags) {
|
|
6492
6736
|
const ver = (this.tagVersions.get(tag)?.version ?? 0) + 1;
|
|
6493
6737
|
this._setLocalTagVersion(tag, ver, now);
|
|
6494
|
-
|
|
6738
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6739
|
+
await this.publishInvalidation("tag_incr", tag, ver, false, true);
|
|
6740
|
+
} else {
|
|
6741
|
+
void this.publishInvalidation("tag_incr", tag, ver, false, true);
|
|
6742
|
+
}
|
|
6495
6743
|
}
|
|
6496
6744
|
return;
|
|
6497
6745
|
}
|
|
@@ -6506,7 +6754,15 @@ var CacheService = class _CacheService {
|
|
|
6506
6754
|
}
|
|
6507
6755
|
for (const k of allMembers) {
|
|
6508
6756
|
this.l1.delete(k);
|
|
6509
|
-
this.
|
|
6757
|
+
this._bumpKeyMutation(k);
|
|
6758
|
+
this._pendingDiskDeletes.add(k);
|
|
6759
|
+
setImmediate(() => {
|
|
6760
|
+
try {
|
|
6761
|
+
if (!this._diskDisabled) this.disk.delete(k);
|
|
6762
|
+
} finally {
|
|
6763
|
+
this._pendingDiskDeletes.delete(k);
|
|
6764
|
+
}
|
|
6765
|
+
});
|
|
6510
6766
|
}
|
|
6511
6767
|
if (!this._redisDisabled) {
|
|
6512
6768
|
try {
|
|
@@ -6543,6 +6799,7 @@ var CacheService = class _CacheService {
|
|
|
6543
6799
|
* `l2` is `null` when Redis is disabled.
|
|
6544
6800
|
*/
|
|
6545
6801
|
async ping() {
|
|
6802
|
+
this._assertActive("ping");
|
|
6546
6803
|
const t0 = Date.now();
|
|
6547
6804
|
this.l1.has("__ping__");
|
|
6548
6805
|
const l1 = Date.now() - t0;
|
|
@@ -6622,6 +6879,7 @@ var CacheService = class _CacheService {
|
|
|
6622
6879
|
* Returns the number of keys written.
|
|
6623
6880
|
*/
|
|
6624
6881
|
async drainToL2() {
|
|
6882
|
+
this._assertActive("drainToL2");
|
|
6625
6883
|
if (this._redisDisabled) return 0;
|
|
6626
6884
|
try {
|
|
6627
6885
|
const client = await this.getRedis();
|
|
@@ -6665,6 +6923,7 @@ var CacheService = class _CacheService {
|
|
|
6665
6923
|
* if (!claimed) return res.status(409).json({ error: 'duplicate request' });
|
|
6666
6924
|
*/
|
|
6667
6925
|
async setIfAbsent(cacheKey, value, ttlSeconds = 300, priority) {
|
|
6926
|
+
this._assertActive("setIfAbsent");
|
|
6668
6927
|
const k = this.nk(cacheKey);
|
|
6669
6928
|
if (this.l1.has(k)) return false;
|
|
6670
6929
|
if (!this._redisDisabled) {
|
|
@@ -6683,6 +6942,8 @@ var CacheService = class _CacheService {
|
|
|
6683
6942
|
}
|
|
6684
6943
|
const ttlMs = this._jitterTtl(ttlSeconds * 1e3);
|
|
6685
6944
|
const p = priority ?? inferPriority(cacheKey);
|
|
6945
|
+
this._bumpKeyMutation(k);
|
|
6946
|
+
this._pendingDiskDeletes.delete(k);
|
|
6686
6947
|
this.l1.set(k, value, ttlMs, p);
|
|
6687
6948
|
this.counters.sets++;
|
|
6688
6949
|
return true;
|
|
@@ -6701,6 +6962,7 @@ var CacheService = class _CacheService {
|
|
|
6701
6962
|
* @param options TTL, acquire timeout, and retry polling intervals.
|
|
6702
6963
|
*/
|
|
6703
6964
|
async lock(resourceKey, fn, options) {
|
|
6965
|
+
this._assertActive("lock");
|
|
6704
6966
|
const ttlSeconds = Math.max(1, options?.ttl ?? 30);
|
|
6705
6967
|
const acquireTimeout = Math.max(0, options?.acquireTimeout ?? 5e3);
|
|
6706
6968
|
const retryInterval = Math.max(10, options?.retryInterval ?? 100);
|
|
@@ -6727,6 +6989,10 @@ var CacheService = class _CacheService {
|
|
|
6727
6989
|
}
|
|
6728
6990
|
} catch (err) {
|
|
6729
6991
|
if (err.message.startsWith("Failed to acquire lock")) throw err;
|
|
6992
|
+
const failClosed = options?.failClosedOnRedisError ?? this.opts.lockFailClosed;
|
|
6993
|
+
if (failClosed) {
|
|
6994
|
+
throw new Error(`Distributed lock acquisition failed for resource "${resourceKey}" due to Redis error: ${err.message}`);
|
|
6995
|
+
}
|
|
6730
6996
|
this.logger.debug("Distributed lock: Redis error, falling back to in-process mutex", {
|
|
6731
6997
|
resourceKey,
|
|
6732
6998
|
error: err.message
|
|
@@ -7072,6 +7338,7 @@ var CacheService = class _CacheService {
|
|
|
7072
7338
|
* the previous key for seamless fallback decryption of existing cache entries.
|
|
7073
7339
|
*/
|
|
7074
7340
|
async rotateEncryptionKey(newKeyBase64, newMode) {
|
|
7341
|
+
this._assertActive("rotateEncryptionKey");
|
|
7075
7342
|
this.enc.rotateKey(newKeyBase64, newMode);
|
|
7076
7343
|
if (this._workerPool) {
|
|
7077
7344
|
await this._workerPool.drainAndReinit(this.enc.toWorkerInit());
|
|
@@ -7081,6 +7348,8 @@ var CacheService = class _CacheService {
|
|
|
7081
7348
|
/** Close Redis connections and stop all background timers. */
|
|
7082
7349
|
async destroy() {
|
|
7083
7350
|
this._destroyed = true;
|
|
7351
|
+
this._pendingDiskDeletes.clear();
|
|
7352
|
+
this._keyMutationEpochs.clear();
|
|
7084
7353
|
const g = globalThis;
|
|
7085
7354
|
const key = _CacheService.globalKey(this.opts);
|
|
7086
7355
|
if (g[key] === this) {
|