@ultimat3/cache 31.23.0 → 31.24.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 +1 -1
- package/README.md +8 -0
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/memo-per-request.ts +20 -3
package/CLAUDE.md
CHANGED
|
@@ -204,7 +204,7 @@ Tier 1. Tagged caching + THE invalidation graph.
|
|
|
204
204
|
| `memo-store.ts` | the one per-ctx memo store (WeakMap on the ALS ctx) and its `MEMO_MAX_BYTES` (8 MiB) bound, oldest evicted silently |
|
|
205
205
|
| `memo.ts` | `memoTier`: the request-memo rung over `memo-store.ts` |
|
|
206
206
|
| `memo-flights.ts` | `memoFlights(ctx, name)`: a named namespace of reads in flight in an explicit ctx's memo — `@ultimat3/query`'s request memo |
|
|
207
|
-
| `memo-per-request.ts` | `memoPerRequest(fn, key)`: the same store for a plain function; straight through outside a ctx |
|
|
207
|
+
| `memo-per-request.ts` | `memoPerRequest(fn, key)`: the same store for a plain function; straight through outside a ctx; `.forget(...args)` drops one entry and its bytes |
|
|
208
208
|
| `approx-bytes.ts` | `approxBytes`: a held value's cost from a bounded sample (8 elements, 32 members, depth 6, 512 nodes) — the memo charges every query read, so never a serialisation |
|
|
209
209
|
| `byte-length.ts` | `utf8ByteLength`: UTF-8 length counted off code units, no `TextEncoder` copy |
|
|
210
210
|
| `lru.ts` | byte-budgeted LRU (linked list + map + tag index) |
|
package/README.md
CHANGED
|
@@ -72,8 +72,16 @@ interface Org { readonly id: string; readonly name: string }
|
|
|
72
72
|
declare const db: { readonly orgs: { find(id: string): Promise<Org> } };
|
|
73
73
|
|
|
74
74
|
export const loadOrg = memoPerRequest((id: string) => db.orgs.find(id), (id) => id);
|
|
75
|
+
|
|
76
|
+
// After a write in the same request: the next loadOrg(id) reads the source again.
|
|
77
|
+
export function renamed(id: string): void {
|
|
78
|
+
loadOrg.forget(id);
|
|
79
|
+
}
|
|
75
80
|
```
|
|
76
81
|
|
|
82
|
+
**`.forget(...args)` drops one entry.** It takes the call's own arguments, so the key is still
|
|
83
|
+
derived in one place, and gives the entry's bytes back to the bound. A no-op outside a ctx.
|
|
84
|
+
|
|
77
85
|
**Cached values: JSON + `Date` + `bigint` + `Map` + `Set` — the same shape from every tier.**
|
|
78
86
|
Every tier (the memo and the LRU included) stores the value as text through one codec
|
|
79
87
|
(`value-codec.ts`) and decodes a fresh copy on each hit, so:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/cache",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.24.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.24.0"
|
|
47
47
|
}
|
|
48
48
|
}
|
package/src/index.ts
CHANGED
|
@@ -58,6 +58,7 @@ export { estimateBytes, LruCache, lruTier } from './lru';
|
|
|
58
58
|
export { MEMO_MAX_BYTES, memoTier } from './memo';
|
|
59
59
|
export type { MemoFlights } from './memo-flights';
|
|
60
60
|
export { memoFlights } from './memo-flights';
|
|
61
|
+
export type { MemoizedPerRequest } from './memo-per-request';
|
|
61
62
|
export { memoPerRequest } from './memo-per-request';
|
|
62
63
|
export type { CloudflarePurgeOptions } from './purge-cloudflare';
|
|
63
64
|
export { CLOUDFLARE_API_URL, cloudflarePurgeDriver } from './purge-cloudflare';
|
package/src/memo-per-request.ts
CHANGED
|
@@ -2,7 +2,18 @@
|
|
|
2
2
|
// and byte bound as `memoTier` — one mechanism, a second entry point, so an app stops writing its
|
|
3
3
|
// own WeakMap-on-ctx wrapper. Outside a ctx it calls straight through.
|
|
4
4
|
|
|
5
|
-
import { holdValue, storeFor } from './memo-store';
|
|
5
|
+
import { holdValue, removeEntry, storeFor } from './memo-store';
|
|
6
|
+
|
|
7
|
+
/** A request-memoized function, and the one way to drop what it holds for some arguments. */
|
|
8
|
+
export interface MemoizedPerRequest<A extends readonly unknown[], R> {
|
|
9
|
+
(...args: A): R;
|
|
10
|
+
/**
|
|
11
|
+
* Drop what this ctx holds for `args` — the same arguments the call takes, so the key is still
|
|
12
|
+
* derived in one place — and give its bytes back to the bound. The next call runs `fn` again.
|
|
13
|
+
* A no-op outside a ctx, or for arguments nothing is held for.
|
|
14
|
+
*/
|
|
15
|
+
forget(...args: A): void;
|
|
16
|
+
}
|
|
6
17
|
|
|
7
18
|
/** Wrapper identities: two wrappers handed the same key string must not answer for each other. */
|
|
8
19
|
let wrappers = 0;
|
|
@@ -20,11 +31,11 @@ let wrappers = 0;
|
|
|
20
31
|
export function memoPerRequest<A extends readonly unknown[], R>(
|
|
21
32
|
fn: (...args: A) => R,
|
|
22
33
|
key: (...args: A) => string,
|
|
23
|
-
):
|
|
34
|
+
): MemoizedPerRequest<A, R> {
|
|
24
35
|
wrappers += 1;
|
|
25
36
|
const namespace = `\u0000memo:${wrappers}\u0000`;
|
|
26
37
|
|
|
27
|
-
|
|
38
|
+
const memoized = (...args: A): R => {
|
|
28
39
|
const store = storeFor(true);
|
|
29
40
|
if (store === undefined) return fn(...args);
|
|
30
41
|
const slot = namespace + key(...args);
|
|
@@ -34,4 +45,10 @@ export function memoPerRequest<A extends readonly unknown[], R>(
|
|
|
34
45
|
holdValue(store, slot, value);
|
|
35
46
|
return value;
|
|
36
47
|
};
|
|
48
|
+
return Object.assign(memoized, {
|
|
49
|
+
forget(...args: A): void {
|
|
50
|
+
const store = storeFor(false);
|
|
51
|
+
if (store !== undefined) removeEntry(store, namespace + key(...args));
|
|
52
|
+
},
|
|
53
|
+
});
|
|
37
54
|
}
|