@ultimat3/cache 30.8.0 → 31.0.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/CLAUDE.md +59 -35
- package/README.md +97 -66
- package/package.json +4 -3
- package/src/adapter.ts +17 -0
- package/src/cdn.ts +1 -1
- package/src/errors.ts +2 -1
- package/src/index.ts +25 -32
- package/src/invalidate.ts +82 -123
- package/src/redis-buckets.ts +203 -0
- package/src/redis-pubsub.ts +215 -0
- package/src/redis.ts +77 -195
- package/src/tags.ts +1 -2
- package/src/tier-failures.ts +9 -51
- package/src/tiers.ts +13 -4
package/src/tier-failures.ts
CHANGED
|
@@ -1,57 +1,14 @@
|
|
|
1
1
|
// A cache tier is best-effort infrastructure: a refusal must never reach the caller of a
|
|
2
2
|
// business read or write, the same rule `invalidateTags` already keeps. `read()` has no report
|
|
3
|
-
// to return, so every swallowed refusal
|
|
4
|
-
// running degraded stays answerable instead of merely looking slow.
|
|
3
|
+
// to return, so every swallowed refusal is one `cache.tier_failure` event plus one `warn` — a
|
|
4
|
+
// stack running degraded stays answerable instead of merely looking slow.
|
|
5
5
|
|
|
6
|
-
import { logger, renderThrowable,
|
|
6
|
+
import { instrument, logger, renderThrowable, UltimateError } from '@ultimat3/core';
|
|
7
7
|
import type { TierLabel } from './tiers';
|
|
8
8
|
|
|
9
9
|
/** The three tier calls a stack makes on the value path. `invalidateTags` reports its own. */
|
|
10
10
|
export type TierOperation = 'get' | 'set' | 'del';
|
|
11
11
|
|
|
12
|
-
export interface TierFailure {
|
|
13
|
-
/** ISO-8601, from core's `systemClock` — never `new Date()`. */
|
|
14
|
-
readonly at: string;
|
|
15
|
-
readonly tier: TierLabel;
|
|
16
|
-
readonly op: TierOperation;
|
|
17
|
-
readonly key: string;
|
|
18
|
-
/** The `X_*` code when the tier threw an `UltimateError`; absent for anything else. */
|
|
19
|
-
readonly code?: string;
|
|
20
|
-
readonly message: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// A dev log, not an audit trail — capped so a long-lived process cannot grow it forever.
|
|
24
|
-
const MAX_TIER_FAILURES = 100;
|
|
25
|
-
|
|
26
|
-
/** Newest first. Module-private; read it through `recentTierFailures()`. */
|
|
27
|
-
const failureLog: TierFailure[] = [];
|
|
28
|
-
|
|
29
|
-
/** What a "is the cache degraded?" question reads: newest first, capped, a copy of the log. */
|
|
30
|
-
export function recentTierFailures(): readonly TierFailure[] {
|
|
31
|
-
return [...failureLog];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Test seam, and `resetTiers()`'s: drops every recorded failure. */
|
|
35
|
-
export function resetTierFailures(): void {
|
|
36
|
-
failureLog.length = 0;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* `isolateDeclaredTags()`'s contract over this log — `tags.ts` carries the why. It lives here
|
|
41
|
-
* rather than in a test file because the log has a reader and no writer: from outside this module
|
|
42
|
-
* the entries `resetTierFailures()` drops cannot be put back at all.
|
|
43
|
-
*
|
|
44
|
-
* const restoreFailures = isolateTierFailures();
|
|
45
|
-
* afterAll(restoreFailures);
|
|
46
|
-
*/
|
|
47
|
-
export function isolateTierFailures(): () => void {
|
|
48
|
-
const captured = [...failureLog];
|
|
49
|
-
return () => {
|
|
50
|
-
failureLog.length = 0;
|
|
51
|
-
failureLog.push(...captured);
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
12
|
/**
|
|
56
13
|
* Runs one tier call and absorbs its refusal. `undefined` back means the tier declined, which a
|
|
57
14
|
* `get` already reads as a miss and a `set`/`del` as "that tier is unchanged" — so the caller
|
|
@@ -60,7 +17,7 @@ export function isolateTierFailures(): () => void {
|
|
|
60
17
|
*
|
|
61
18
|
* Public, and the only sanctioned way to swallow a cache refusal: a store outside this package
|
|
62
19
|
* (`@ultimat3/query`'s read cache) that wrapped its own `try/catch` would degrade invisibly, and
|
|
63
|
-
* a second failure
|
|
20
|
+
* a second failure stream nobody subscribes to is what this one exists to prevent. Pass the store's
|
|
64
21
|
* `TierLabel` — it is closed for that reason.
|
|
65
22
|
*/
|
|
66
23
|
export async function bestEffort<T>(
|
|
@@ -97,8 +54,8 @@ function ultimateCode(error: unknown): string | undefined {
|
|
|
97
54
|
|
|
98
55
|
function record(tier: TierLabel, op: TierOperation, key: string, error: unknown): void {
|
|
99
56
|
const code = ultimateCode(error);
|
|
100
|
-
|
|
101
|
-
|
|
57
|
+
// `CacheTierFailureEvent`, core's shape: no `at` — a subscriber that keeps a log reads its clock.
|
|
58
|
+
const failure = {
|
|
102
59
|
tier,
|
|
103
60
|
op,
|
|
104
61
|
key,
|
|
@@ -107,7 +64,8 @@ function record(tier: TierLabel, op: TierOperation, key: string, error: unknown)
|
|
|
107
64
|
// `TypeError` on the business read this function exists to keep alive.
|
|
108
65
|
message: renderThrowable(error),
|
|
109
66
|
};
|
|
110
|
-
failureLog.unshift(failure);
|
|
111
|
-
failureLog.length = Math.min(failureLog.length, MAX_TIER_FAILURES);
|
|
112
67
|
logger.warn('cache.tier.failed', { ...failure });
|
|
68
|
+
// A subscriber's throw is contained by the bus (logged once as `instrument.subscriber_failed`):
|
|
69
|
+
// observability can never turn a degraded cache into a failed business read.
|
|
70
|
+
instrument('cache.tier_failure', failure);
|
|
113
71
|
}
|
package/src/tiers.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// sites. Order is data, not control flow.
|
|
5
5
|
|
|
6
6
|
import type { CacheTierName, Clock, Scheduler } from '@ultimat3/core';
|
|
7
|
-
import { CACHE_TIERS, singleFlight, systemClock, tryUseContext } from '@ultimat3/core';
|
|
7
|
+
import { CACHE_TIERS, instrument, singleFlight, systemClock, tryUseContext } from '@ultimat3/core';
|
|
8
8
|
import { CacheJitterInvalidError, CacheLimitInvalidError, CacheTtlInvalidError } from './errors';
|
|
9
9
|
import type { CacheFence, FenceScope } from './fence';
|
|
10
10
|
import { markInvalidated, sampleFence } from './fence';
|
|
@@ -28,7 +28,7 @@ export type TierName = CacheTierName;
|
|
|
28
28
|
// Imported, never aliased under a second name here (`X_HELPER_COPY`).
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
* Who a swallowed refusal is attributed to
|
|
31
|
+
* Who a swallowed refusal is attributed to on its `cache.tier_failure` event and in the `/_x` panel: every
|
|
32
32
|
* rung of the ladder, plus a store that degrades the same way without being on it.
|
|
33
33
|
*
|
|
34
34
|
* Closed rather than a free-form string, and NOT a widening of `TierName`: `CACHE_TIERS` is the
|
|
@@ -360,11 +360,20 @@ export function cacheStack(
|
|
|
360
360
|
const tier = ordered[i];
|
|
361
361
|
if (tier === undefined) continue;
|
|
362
362
|
const hit = await bestEffort(tier.name, 'get', key, () => tier.get<T>(key));
|
|
363
|
-
|
|
363
|
+
// Every rung asked is one `cache.hit`/`cache.miss` — a refused `get` and an expired entry
|
|
364
|
+
// are misses, as the read sees them.
|
|
365
|
+
if (hit === undefined) {
|
|
366
|
+
instrument('cache.miss', { tier: tier.name, key });
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
364
369
|
const now = nowMs(clock);
|
|
365
370
|
// A tier may answer with an entry it has not reaped yet; expiry is decided here, once,
|
|
366
371
|
// by the predicate this module already exported and nothing had ever called.
|
|
367
|
-
if (isExpired(hit, now))
|
|
372
|
+
if (isExpired(hit, now)) {
|
|
373
|
+
instrument('cache.miss', { tier: tier.name, key });
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
instrument('cache.hit', { tier: tier.name, key });
|
|
368
377
|
// Populate every tier we walked past, closest-first on the next read — carrying the
|
|
369
378
|
// entry's REMAINING life, never the caller's original ttlMs. Re-leasing a value one
|
|
370
379
|
// second from expiry for a fresh five minutes on every read is a hot key that never
|