@ultimat3/cache 31.47.1 → 31.48.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 +5 -1
- package/README.md +4 -1
- package/package.json +2 -2
- package/src/errors.ts +25 -12
- package/src/index.ts +1 -1
- package/src/lru.ts +10 -1
- package/src/redis.ts +23 -2
- package/src/tiers.ts +14 -5
package/CLAUDE.md
CHANGED
|
@@ -82,7 +82,11 @@ Tier 1. Tagged caching + THE invalidation graph.
|
|
|
82
82
|
included (scope `'semantic'`, `jitterFraction: 0`). `X_CACHE_TTL_INVALID`, never a resolution.
|
|
83
83
|
- **`assertTtl` also SPREADS the lease it validated.** `rng` is injected (`LruOptions.rng`,
|
|
84
84
|
`RedisTierOptions.rng`) — never `Math.random()` at a call site; `rng: () => 0` is the full lease.
|
|
85
|
-
`jitterFraction` outside `[0, 1)` is `X_CACHE_JITTER_INVALID`, refused, not clamped
|
|
85
|
+
`jitterFraction` outside `[0, 1)` is `X_CACHE_JITTER_INVALID`, refused, not clamped, when the
|
|
86
|
+
tier is built (`assertJitterFraction`) and again at each write.
|
|
87
|
+
- **A rung's limits are constructor options, set through `runtime.ts`'s `cacheTiers`** (`As of
|
|
88
|
+
2026-11`, #1178). The boot builds `lru` and `redis` with defaults; no `app.config.ts` key exists,
|
|
89
|
+
because it would be a second spelling of that replacement. Every limit `fix:` names the line.
|
|
86
90
|
- **`cacheStack` is the production read path, and `@ultimat3/query`'s `readThrough` is its
|
|
87
91
|
caller** — it is what makes an action's `cache.invalidates` reach a `cache:` query.
|
|
88
92
|
- **`cacheStack` shares one in-flight `load()` per key** through `@ultimat3/core`'s
|
package/README.md
CHANGED
|
@@ -611,7 +611,10 @@ a job boundary the class is gone and the `code` is what survives — match on th
|
|
|
611
611
|
(`CacheDriverUnavailableError`, `CachePurgeFailedError`). An app hands its own to the boot in
|
|
612
612
|
`apps/web/runtime.ts` — `cacheTiers: [myRedisTier]` replaces the rung of that name (`cache.tiers`
|
|
613
613
|
still decides which rungs exist; a supplied rung it does not name is logged `cache.tier.unnamed`
|
|
614
|
-
and unused)
|
|
614
|
+
and unused). It is also the one way to set a built-in rung's limits: `cacheTiers: [lruTier({
|
|
615
|
+
maxBytes })]`, never an `app.config.ts` key. A `redisTier` with no `client` and no `REDIS_URL` or
|
|
616
|
+
`VALKEY_URL` dials the local server, and the boot logs `cache.tier.default-redis`
|
|
617
|
+
(`usesDefaultRedisClient`). `purge: myPurgeDriver` replaces the `CLOUDFLARE_*`/`FASTLY_*` selection. The
|
|
615
618
|
conformance suites are `@ultimat3/testing/conformance`'s `cacheTierConformance` and
|
|
616
619
|
`purgeDriverConformance`.
|
|
617
620
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/cache",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.48.0",
|
|
4
4
|
"description": "Tagged caching: request memo, LRU, Redis, CDN — one invalidation graph",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -43,6 +43,6 @@
|
|
|
43
43
|
"test": "bun test"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@ultimat3/core": "31.
|
|
46
|
+
"@ultimat3/core": "31.48.0"
|
|
47
47
|
}
|
|
48
48
|
}
|
package/src/errors.ts
CHANGED
|
@@ -67,11 +67,17 @@ export class CacheTagUnknownError extends UltimateError {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Where an app tunes a rung. The boot builds `lru` and `redis` with their defaults and takes a
|
|
72
|
+
* replacement from `runtime.ts`'s `cacheTiers`: the one way, so every fix below names it. Limits
|
|
73
|
+
* are not `app.config.ts` keys — they would be a second spelling of that same replacement.
|
|
74
|
+
*/
|
|
75
|
+
const RUNG_FILE = 'apps/web/runtime.ts';
|
|
76
|
+
|
|
70
77
|
/** A single entry cannot fit the tier's byte budget, so caching it would evict everything. */
|
|
71
78
|
export class CacheTooLargeError extends UltimateError {
|
|
72
79
|
constructor(input: { key: string; bytes: number; maxBytes: number; tier: string }) {
|
|
73
80
|
const bytes = String(input.bytes);
|
|
74
|
-
// The cap is a constructor option on every tier, never an `app.config.ts` key.
|
|
75
81
|
const raise =
|
|
76
82
|
input.tier === 'redis'
|
|
77
83
|
? `redisTier({ maxValueBytes: ${bytes} })`
|
|
@@ -79,7 +85,7 @@ export class CacheTooLargeError extends UltimateError {
|
|
|
79
85
|
super({
|
|
80
86
|
code: 'X_CACHE_TOO_LARGE',
|
|
81
87
|
cause: `entry "${input.key}" is ${input.bytes}B, over the ${input.tier} budget of ${input.maxBytes}B`,
|
|
82
|
-
fix:
|
|
88
|
+
fix: `cacheTiers: [${raise}] // in ${RUNG_FILE}, replacing the rung the boot builds — or cache a projection instead of the row`,
|
|
83
89
|
});
|
|
84
90
|
}
|
|
85
91
|
}
|
|
@@ -128,10 +134,7 @@ const limitEffect = (value: number): string =>
|
|
|
128
134
|
? ', and every comparison against NaN is false, so the limit stops being enforced rather than being enforced wrongly — NaN is what Number(process.env.…) answers for an unset variable, and it is not nullish, so `??` does not catch it'
|
|
129
135
|
: ', and it is applied as given, so the tier is bounded at a value nobody chose';
|
|
130
136
|
|
|
131
|
-
/**
|
|
132
|
-
* The factory each scope's limits are constructor options of — the fix names where the corrected
|
|
133
|
-
* option goes. None is an `app.config.ts` key: the runtime builds every tier with defaults.
|
|
134
|
-
*/
|
|
137
|
+
/** The factory each scope's limits are constructor options of: where the corrected one goes. */
|
|
135
138
|
const LIMIT_OWNER: Readonly<Record<string, string>> = {
|
|
136
139
|
lru: 'lruTier',
|
|
137
140
|
redis: 'redisTier',
|
|
@@ -141,6 +144,20 @@ const LIMIT_OWNER: Readonly<Record<string, string>> = {
|
|
|
141
144
|
ladder: 'cacheStack',
|
|
142
145
|
};
|
|
143
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Where a corrected tier option goes. `lru` and `redis` are the rungs a boot builds, so theirs also
|
|
149
|
+
* names the `cacheTiers` line: an app on the defaults has no `lruTier(...)` call of its own to edit.
|
|
150
|
+
*/
|
|
151
|
+
function optionHome(tier: string, option: string, example: string): string {
|
|
152
|
+
// `hasOwn`: `tier` is a caller's string, and `constructor` must not read the prototype.
|
|
153
|
+
const owner = Object.hasOwn(LIMIT_OWNER, tier) ? LIMIT_OWNER[tier] : tier;
|
|
154
|
+
const rung =
|
|
155
|
+
tier === 'lru' || tier === 'redis'
|
|
156
|
+
? `; for the rung a boot builds, cacheTiers: [${owner}({ ${option}: ${example} })] in ${RUNG_FILE}`
|
|
157
|
+
: '';
|
|
158
|
+
return `${owner}(...)${rung} — a constructor option, never an app.config.ts key`;
|
|
159
|
+
}
|
|
160
|
+
|
|
144
161
|
export class CacheLimitInvalidError extends UltimateError {
|
|
145
162
|
constructor(input: {
|
|
146
163
|
option: string;
|
|
@@ -155,9 +172,7 @@ export class CacheLimitInvalidError extends UltimateError {
|
|
|
155
172
|
*/
|
|
156
173
|
source?: string | undefined;
|
|
157
174
|
}) {
|
|
158
|
-
const where =
|
|
159
|
-
input.source ??
|
|
160
|
-
`${LIMIT_OWNER[input.tier] ?? input.tier}(...) — a constructor option, never an app.config.ts key`;
|
|
175
|
+
const where = input.source ?? optionHome(input.tier, input.option, input.example);
|
|
161
176
|
super({
|
|
162
177
|
code: 'X_CACHE_LIMIT_INVALID',
|
|
163
178
|
cause: `the ${input.tier} tier was given ${input.option}=${String(input.value)}; it must be ${input.expected}${limitEffect(input.value)}`,
|
|
@@ -205,9 +220,7 @@ export class CacheJitterInvalidError extends UltimateError {
|
|
|
205
220
|
cause: `the ${input.tier} tier was configured with jitterFraction=${String(
|
|
206
221
|
input.jitterFraction,
|
|
207
222
|
)}; a jitter fraction is a finite number in [0, 1)`,
|
|
208
|
-
|
|
209
|
-
// tiers with none (`runtime-cache.ts`), so the config path this used to name did not exist.
|
|
210
|
-
fix: `{ jitterFraction: 0.05 } // in the ${input.tier} tier's lruTier({ ... }) or redisTier({ ... }): a value in [0, 1) — 0.05 is the default, 0 disables jitter`,
|
|
223
|
+
fix: `{ jitterFraction: 0.05 } // a value in [0, 1) — 0.05 is the default, 0 disables jitter — in ${optionHome(input.tier, 'jitterFraction', '0.05')}`,
|
|
211
224
|
meta: { tier: input.tier, jitterFraction: input.jitterFraction },
|
|
212
225
|
});
|
|
213
226
|
}
|
package/src/index.ts
CHANGED
|
@@ -68,7 +68,7 @@ export type { FastlyPurgeOptions } from './purge-fastly';
|
|
|
68
68
|
export { fastlyPurgeDriver } from './purge-fastly';
|
|
69
69
|
export type { PurgeFetch } from './purge-http';
|
|
70
70
|
export type { RedisLike, RedisTierOptions } from './redis';
|
|
71
|
-
export { namespaceFor, redisTier } from './redis';
|
|
71
|
+
export { namespaceFor, redisTier, usesDefaultRedisClient } from './redis';
|
|
72
72
|
export type {
|
|
73
73
|
InvalidationBus,
|
|
74
74
|
InvalidationSubscription,
|
package/src/lru.ts
CHANGED
|
@@ -17,7 +17,13 @@ import type {
|
|
|
17
17
|
TierInvalidation,
|
|
18
18
|
TtlJitter,
|
|
19
19
|
} from './tiers';
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
assertFiniteCapacity,
|
|
22
|
+
assertFiniteDurationMs,
|
|
23
|
+
assertJitterFraction,
|
|
24
|
+
assertTtl,
|
|
25
|
+
nowMs,
|
|
26
|
+
} from './tiers';
|
|
21
27
|
import { decodeCacheValue, encodeCacheValue } from './value-codec';
|
|
22
28
|
|
|
23
29
|
export interface LruOptions {
|
|
@@ -130,6 +136,9 @@ export class LruCache {
|
|
|
130
136
|
options.defaultTtlMs ?? 60_000,
|
|
131
137
|
);
|
|
132
138
|
this.clock = options.clock ?? systemClock;
|
|
139
|
+
if (options.jitterFraction !== undefined) {
|
|
140
|
+
assertJitterFraction('lru', options.jitterFraction);
|
|
141
|
+
}
|
|
133
142
|
this.jitter = {
|
|
134
143
|
...(options.jitterFraction === undefined ? {} : { jitterFraction: options.jitterFraction }),
|
|
135
144
|
...(options.rng === undefined ? {} : { rng: options.rng }),
|
package/src/redis.ts
CHANGED
|
@@ -30,7 +30,13 @@ import type {
|
|
|
30
30
|
TierInvalidation,
|
|
31
31
|
TtlJitter,
|
|
32
32
|
} from './tiers';
|
|
33
|
-
import {
|
|
33
|
+
import {
|
|
34
|
+
assertFiniteCapacity,
|
|
35
|
+
assertFiniteDurationMs,
|
|
36
|
+
assertJitterFraction,
|
|
37
|
+
assertTtl,
|
|
38
|
+
nowMs,
|
|
39
|
+
} from './tiers';
|
|
34
40
|
import { decodeCacheValue, encodeCacheValue } from './value-codec';
|
|
35
41
|
|
|
36
42
|
/** The slice of Bun's Redis client this tier uses. Narrow on purpose: easy to fake in tests. */
|
|
@@ -144,6 +150,16 @@ function raiseSweepFailure(failures: readonly unknown[], attempted: number): nev
|
|
|
144
150
|
});
|
|
145
151
|
}
|
|
146
152
|
|
|
153
|
+
/** The tiers `redisTier` built with no `client`: the ones core's `redisClient()` will dial for. */
|
|
154
|
+
const onDefaultClient = new WeakSet<CacheTier>();
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Whether `tier` is a `redisTier` left to core's default client, which reads `REDIS_URL`, then
|
|
158
|
+
* `VALKEY_URL`, then the local server. A tier's options are closed over, so the boot asks here: a
|
|
159
|
+
* rung it did not build still owes an operator a word when no URL is set (`runtime-cache.ts`).
|
|
160
|
+
*/
|
|
161
|
+
export const usesDefaultRedisClient = (tier: CacheTier): boolean => onDefaultClient.has(tier);
|
|
162
|
+
|
|
147
163
|
export function redisTier(options: RedisTierOptions = {}): CacheTier {
|
|
148
164
|
const ns = namespaceFor(options.prefix ?? 'x', options.buildId);
|
|
149
165
|
const defaultTtlMs = assertFiniteDurationMs(
|
|
@@ -157,6 +173,9 @@ export function redisTier(options: RedisTierOptions = {}): CacheTier {
|
|
|
157
173
|
'maxValueBytes',
|
|
158
174
|
options.maxValueBytes ?? DEFAULT_REDIS_MAX_VALUE_BYTES,
|
|
159
175
|
);
|
|
176
|
+
if (options.jitterFraction !== undefined) {
|
|
177
|
+
assertJitterFraction('redis', options.jitterFraction);
|
|
178
|
+
}
|
|
160
179
|
const jitter: TtlJitter = {
|
|
161
180
|
...(options.jitterFraction === undefined ? {} : { jitterFraction: options.jitterFraction }),
|
|
162
181
|
...(options.rng === undefined ? {} : { rng: options.rng }),
|
|
@@ -185,7 +204,7 @@ export function redisTier(options: RedisTierOptions = {}): CacheTier {
|
|
|
185
204
|
|
|
186
205
|
const { valueKey, writeBucketsFor, bustBucketsFor, sweepBucketsFor } = bucketLayout(ns);
|
|
187
206
|
|
|
188
|
-
|
|
207
|
+
const tier: CacheTier = {
|
|
189
208
|
name: 'redis',
|
|
190
209
|
|
|
191
210
|
/**
|
|
@@ -315,4 +334,6 @@ export function redisTier(options: RedisTierOptions = {}): CacheTier {
|
|
|
315
334
|
return { tier: 'redis', keys: stripped };
|
|
316
335
|
},
|
|
317
336
|
};
|
|
337
|
+
if (options.client === undefined) onDefaultClient.add(tier);
|
|
338
|
+
return tier;
|
|
318
339
|
}
|
package/src/tiers.ts
CHANGED
|
@@ -103,7 +103,7 @@ export type TtlScope = TierName | 'semantic';
|
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
105
|
* A tier's ceiling — bytes or entries — screened where it is still nameable. The same rule as
|
|
106
|
-
* `assertTtl` beside it, over the other knob
|
|
106
|
+
* `assertTtl` beside it, over the other knob a tier's constructor takes. A SEPARATE refusal from
|
|
107
107
|
* `X_CACHE_TOO_LARGE`, which is one entry against a valid ceiling: this one is the ceiling itself,
|
|
108
108
|
* and it needs a screen because a bad ceiling refuses nothing and evicts nothing rather than
|
|
109
109
|
* refusing everything. `NaN > x` and `x > NaN` are both false.
|
|
@@ -165,6 +165,15 @@ export function assertFiniteSimilarityFloor(
|
|
|
165
165
|
});
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
+
/**
|
|
169
|
+
* A tier's TTL spread, screened when the tier is BUILT as well as at each write: found only at the
|
|
170
|
+
* first `set`, a bad fraction refused every write of a tier that had booted healthy.
|
|
171
|
+
*/
|
|
172
|
+
export function assertJitterFraction(tier: TtlScope, fraction: number): number {
|
|
173
|
+
if (Number.isFinite(fraction) && fraction >= 0 && fraction < 1) return fraction;
|
|
174
|
+
throw new CacheJitterInvalidError({ tier, jitterFraction: fraction });
|
|
175
|
+
}
|
|
176
|
+
|
|
168
177
|
export function assertTtl(
|
|
169
178
|
key: string,
|
|
170
179
|
ttlMs: number,
|
|
@@ -174,10 +183,10 @@ export function assertTtl(
|
|
|
174
183
|
if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
|
|
175
184
|
throw new CacheTtlInvalidError({ key, ttlMs, tier });
|
|
176
185
|
}
|
|
177
|
-
const fraction =
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
186
|
+
const fraction =
|
|
187
|
+
jitter.jitterFraction === undefined
|
|
188
|
+
? DEFAULT_TTL_JITTER_FRACTION
|
|
189
|
+
: assertJitterFraction(tier, jitter.jitterFraction);
|
|
181
190
|
if (fraction === 0) return ttlMs;
|
|
182
191
|
// Clamped rather than trusted: an `rng` outside [0, 1) would EXTEND the lease past what the
|
|
183
192
|
// caller asked for, which is a stale read no reader can explain.
|