@coasys/ad4m 0.13.1-staging.13 → 0.13.1-staging.15
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/lib/index.cjs +571 -480
- package/lib/index.js +571 -476
- package/lib/src/agent/AgentClient.d.ts +9 -8
- package/lib/src/agent/AgentClient.js +46 -67
- package/lib/src/cache/KeyedCache.d.ts +51 -0
- package/lib/src/cache/KeyedCache.js +142 -0
- package/lib/src/expression/ExpressionClient.d.ts +14 -3
- package/lib/src/expression/ExpressionClient.js +49 -111
- package/lib/src/generated/api/ExpressionLookupError.d.ts +7 -0
- package/lib/src/generated/api/ExpressionLookupError.js +2 -0
- package/lib/src/generated/api/ExpressionManyEntry.d.ts +6 -0
- package/lib/src/generated/api/ExpressionManyEntry.js +1 -0
- package/lib/src/generated/api/FlowFiredEntry.d.ts +11 -0
- package/lib/src/generated/api/FlowFiredEntry.js +2 -0
- package/lib/src/generated/api/PerspectiveFlowFiredParams.d.ts +3 -3
- package/lib/src/generated/api/RpcMethods.d.ts +4 -2
- package/lib/src/model/Ad4mModel.d.ts +26 -2
- package/lib/src/model/Ad4mModel.js +127 -83
- package/lib/src/model/ModelQueryBuilder.d.ts +3 -0
- package/lib/src/model/ModelQueryBuilder.js +42 -14
- package/lib/src/model/index.d.ts +0 -1
- package/lib/src/model/index.js +0 -1
- package/lib/src/model/query-common.d.ts +8 -0
- package/lib/src/model/query-common.js +12 -0
- package/lib/src/model/shacl-gen.js +2 -0
- package/lib/src/model/types.d.ts +12 -3
- package/lib/src/neighbourhood/SfuManager.d.ts +8 -1
- package/lib/src/neighbourhood/SfuManager.js +9 -2
- package/lib/src/perspectives/FlowModels.js +1 -1
- package/lib/src/perspectives/LiveQuery.d.ts +1 -1
- package/lib/src/perspectives/PerspectiveClient.d.ts +17 -6
- package/lib/src/perspectives/PerspectiveClient.js +83 -44
- package/lib/src/perspectives/PerspectiveProxy.d.ts +19 -10
- package/lib/src/perspectives/PerspectiveProxy.js +31 -69
- package/lib/src/shacl/SHACLShape.d.ts +4 -0
- package/lib/src/shacl/SHACLShape.js +10 -0
- package/package.json +2 -2
- package/src/Ad4mClient.test.ts +17 -6
- package/src/agent/AgentClient.ts +46 -74
- package/src/cache/KeyedCache.test.ts +162 -0
- package/src/cache/KeyedCache.ts +179 -0
- package/src/cache/cache.test.ts +294 -71
- package/src/expression/ExpressionClient.ts +46 -131
- package/src/generated/api/ExpressionLookupError.ts +6 -0
- package/src/generated/api/ExpressionManyEntry.ts +8 -0
- package/src/generated/api/FlowFiredEntry.ts +11 -0
- package/src/generated/api/PerspectiveFlowFiredParams.ts +3 -3
- package/src/generated/api/RpcMethods.ts +4 -2
- package/src/model/Ad4mModel.test.ts +237 -43
- package/src/model/Ad4mModel.ts +134 -99
- package/src/model/ModelQueryBuilder.ts +41 -15
- package/src/model/count-result-type.test.ts +72 -0
- package/src/model/index.ts +0 -1
- package/src/model/query-common.ts +13 -0
- package/src/model/relation-filtering.test.ts +3 -5
- package/src/model/shacl-gen.ts +1 -0
- package/src/model/shacl-roundtrip.test.ts +28 -0
- package/src/model/types.ts +9 -3
- package/src/model/write-round-trips.test.ts +30 -6
- package/src/neighbourhood/SfuManager.test.ts +57 -5
- package/src/neighbourhood/SfuManager.ts +10 -2
- package/src/perspectives/FlowModels.ts +1 -1
- package/src/perspectives/LiveQuery.ts +1 -1
- package/src/perspectives/PerspectiveClient.test.ts +287 -0
- package/src/perspectives/PerspectiveClient.ts +101 -66
- package/src/perspectives/PerspectiveProxy.test.ts +6 -14
- package/src/perspectives/PerspectiveProxy.ts +33 -67
- package/src/perspectives/TextContent.test.ts +0 -16
- package/src/perspectives/shacl-rpc.test.ts +1 -1
- package/src/shacl/SHACLShape.test.ts +21 -0
- package/src/shacl/SHACLShape.ts +16 -0
- package/lib/src/model/query-cache.d.ts +0 -30
- package/lib/src/model/query-cache.js +0 -66
- package/src/model/query-cache.test.ts +0 -54
- package/src/model/query-cache.ts +0 -79
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { KeyedCache, StoredEntry } from './KeyedCache';
|
|
2
|
+
import { PersistentCache } from './PersistentCache';
|
|
3
|
+
|
|
4
|
+
function deferred<T>() {
|
|
5
|
+
let resolve!: (v: T) => void;
|
|
6
|
+
const promise = new Promise<T>(r => { resolve = r; });
|
|
7
|
+
return { promise, resolve };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
class MapStore<V> implements PersistentCache<StoredEntry<V>> {
|
|
11
|
+
entries = new Map<string, StoredEntry<V>>();
|
|
12
|
+
puts: string[] = [];
|
|
13
|
+
async get(key: string) { return this.entries.get(key); }
|
|
14
|
+
async put(key: string, value: StoredEntry<V>) { this.puts.push(key); this.entries.set(key, value); }
|
|
15
|
+
async delete(key: string) { this.entries.delete(key); }
|
|
16
|
+
async clear() { this.entries.clear(); }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe('KeyedCache', () => {
|
|
20
|
+
test('get() and getMany() misses of one tick share one loadMany; a later read is a hit', async () => {
|
|
21
|
+
const loadMany = jest.fn(async (keys: string[]) => keys.map(k => k.toUpperCase()));
|
|
22
|
+
const cache = new KeyedCache<string>({ loadMany });
|
|
23
|
+
const [a, many] = await Promise.all([cache.get('a'), cache.getMany(['b', 'a'])]);
|
|
24
|
+
expect(a).toBe('A');
|
|
25
|
+
expect(many).toEqual(['B', 'A']);
|
|
26
|
+
expect(loadMany.mock.calls).toEqual([[['a', 'b']]]);
|
|
27
|
+
expect(await cache.get('b')).toBe('B');
|
|
28
|
+
expect(loadMany).toHaveBeenCalledTimes(1);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('with load, a get() miss is read alone and settles without waiting for a slower key', async () => {
|
|
32
|
+
const slow = deferred<string>();
|
|
33
|
+
const load = jest.fn((key: string) => key === 'slow' ? slow.promise : Promise.resolve(key.toUpperCase()));
|
|
34
|
+
const loadMany = jest.fn(async (keys: string[]) => keys.map(k => k.toUpperCase()));
|
|
35
|
+
const cache = new KeyedCache<string>({ load, loadMany });
|
|
36
|
+
const slowRead = cache.get('slow');
|
|
37
|
+
expect(await cache.get('fast')).toBe('FAST');
|
|
38
|
+
// A getMany() shares the get() in flight, and only its other misses go to loadMany.
|
|
39
|
+
const many = cache.getMany(['fast', 'slow', 'b']);
|
|
40
|
+
slow.resolve('SLOW');
|
|
41
|
+
expect(await slowRead).toBe('SLOW');
|
|
42
|
+
expect(await many).toEqual(['FAST', 'SLOW', 'B']);
|
|
43
|
+
expect(load.mock.calls).toEqual([['slow'], ['fast']]);
|
|
44
|
+
expect(loadMany.mock.calls).toEqual([[['b']]]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('with load, a get() miss is served from the persistent store before load', async () => {
|
|
48
|
+
const persistent = new MapStore<string>();
|
|
49
|
+
persistent.entries.set('a', { value: 'stored', ts: Date.now() });
|
|
50
|
+
const load = jest.fn(async (key: string) => key);
|
|
51
|
+
const cache = new KeyedCache<string>({ load, loadMany: async keys => keys, persistent });
|
|
52
|
+
expect(await cache.get('a')).toBe('stored');
|
|
53
|
+
expect(load).not.toHaveBeenCalled();
|
|
54
|
+
expect(persistent.puts).toEqual([]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('with load, a failed get() rejects that read only, and the next get() retries', async () => {
|
|
58
|
+
const load = jest.fn().mockRejectedValueOnce(new Error('boom')).mockResolvedValue('ok');
|
|
59
|
+
const cache = new KeyedCache<string>({ load, loadMany: async keys => keys });
|
|
60
|
+
await expect(cache.get('a')).rejects.toThrow('boom');
|
|
61
|
+
expect(await cache.get('a')).toBe('ok');
|
|
62
|
+
expect(load).toHaveBeenCalledTimes(2);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('maxBatch splits one tick of misses into calls of at most that size', async () => {
|
|
66
|
+
const loadMany = jest.fn(async (keys: string[]) => keys);
|
|
67
|
+
const cache = new KeyedCache<string>({ loadMany, maxBatch: 2 });
|
|
68
|
+
expect(await cache.getMany(['a', 'b', 'c', 'd', 'e'])).toEqual(['a', 'b', 'c', 'd', 'e']);
|
|
69
|
+
expect(loadMany.mock.calls.map(([keys]) => keys)).toEqual([['a', 'b'], ['c', 'd'], ['e']]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('null (not found) is returned but not kept', async () => {
|
|
73
|
+
const loadMany = jest.fn(async (keys: string[]) => keys.map(() => null));
|
|
74
|
+
const cache = new KeyedCache<string>({ loadMany });
|
|
75
|
+
expect(await cache.get('x')).toBeNull();
|
|
76
|
+
expect(await cache.get('x')).toBeNull();
|
|
77
|
+
expect(loadMany).toHaveBeenCalledTimes(2);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('a set() while a read is in flight wins over the read', async () => {
|
|
81
|
+
const reply = deferred<string[]>();
|
|
82
|
+
const cache = new KeyedCache<string>({ loadMany: () => reply.promise });
|
|
83
|
+
const reading = cache.get('a');
|
|
84
|
+
await Promise.resolve();
|
|
85
|
+
cache.set('a', 'fresh');
|
|
86
|
+
reply.resolve(['stale']);
|
|
87
|
+
expect(await reading).toBe('stale');
|
|
88
|
+
expect(await cache.get('a')).toBe('fresh');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('a delete() while a read is in flight keeps the read out of the cache', async () => {
|
|
92
|
+
const loadMany = jest.fn(async (keys: string[]) => keys);
|
|
93
|
+
const cache = new KeyedCache<string>({ loadMany });
|
|
94
|
+
const reading = cache.get('a');
|
|
95
|
+
cache.delete('a');
|
|
96
|
+
await reading;
|
|
97
|
+
await cache.get('a');
|
|
98
|
+
expect(loadMany).toHaveBeenCalledTimes(2);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('a clear() while a read is in flight keeps the read out of the cache', async () => {
|
|
102
|
+
const loadMany = jest.fn(async (keys: string[]) => keys);
|
|
103
|
+
const cache = new KeyedCache<string>({ loadMany });
|
|
104
|
+
const reading = cache.get('a');
|
|
105
|
+
cache.clear();
|
|
106
|
+
await reading;
|
|
107
|
+
await cache.get('a');
|
|
108
|
+
expect(loadMany).toHaveBeenCalledTimes(2);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('maxAgeMs expires a value per key', async () => {
|
|
112
|
+
const loadMany = jest.fn(async (keys: string[]) => keys);
|
|
113
|
+
const cache = new KeyedCache<string>({ loadMany, maxAgeMs: k => (k === 'keep' ? Infinity : 0) });
|
|
114
|
+
await cache.getMany(['keep', 'drop']);
|
|
115
|
+
await cache.getMany(['keep', 'drop']);
|
|
116
|
+
expect(loadMany.mock.calls.map(([keys]) => keys)).toEqual([['keep', 'drop'], ['drop']]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('limits evict the oldest values first, by count and by JSON size', async () => {
|
|
120
|
+
let limits = { entries: 2, bytes: Infinity };
|
|
121
|
+
const loadMany = jest.fn(async (keys: string[]) => keys);
|
|
122
|
+
const cache = new KeyedCache<string>({ loadMany, limits: () => limits });
|
|
123
|
+
await cache.getMany(['a', 'b']);
|
|
124
|
+
await cache.get('c'); // evicts a
|
|
125
|
+
await cache.getMany(['b', 'c']);
|
|
126
|
+
expect(loadMany).toHaveBeenCalledTimes(2);
|
|
127
|
+
await cache.get('a');
|
|
128
|
+
expect(loadMany).toHaveBeenCalledTimes(3);
|
|
129
|
+
|
|
130
|
+
limits = { entries: 100, bytes: 6 }; // each value is 3 bytes of JSON ("x")
|
|
131
|
+
cache.clear();
|
|
132
|
+
await cache.getMany(['x', 'y']);
|
|
133
|
+
await cache.get('z'); // evicts x
|
|
134
|
+
await cache.get('x');
|
|
135
|
+
expect(loadMany.mock.calls.slice(3).map(([keys]) => keys)).toEqual([['x', 'y'], ['z'], ['x']]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('the persistent tier serves fresh entries without a load and is not rewritten by them', async () => {
|
|
139
|
+
const store = new MapStore<string>();
|
|
140
|
+
store.entries.set('fresh', { value: 'stored', ts: Date.now() });
|
|
141
|
+
store.entries.set('stale', { value: 'old', ts: Date.now() - 10_000 });
|
|
142
|
+
store.entries.set('legacy', { agent: 'old format' } as any);
|
|
143
|
+
const loadMany = jest.fn(async (keys: string[]) => keys.map(k => `loaded:${k}`));
|
|
144
|
+
const cache = new KeyedCache<string>({ loadMany, persistent: store, persistentMaxAgeMs: () => 5_000 });
|
|
145
|
+
|
|
146
|
+
expect(await cache.getMany(['fresh', 'stale', 'legacy'])).toEqual(['stored', 'loaded:stale', 'loaded:legacy']);
|
|
147
|
+
expect(loadMany.mock.calls).toEqual([[['stale', 'legacy']]]);
|
|
148
|
+
expect(store.puts.sort()).toEqual(['legacy', 'stale']);
|
|
149
|
+
expect(store.entries.get('stale')!.value).toBe('loaded:stale');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('clear() keeps the persistent tier unless asked', async () => {
|
|
153
|
+
const store = new MapStore<string>();
|
|
154
|
+
const cache = new KeyedCache<string>({ loadMany: async keys => keys, persistent: store });
|
|
155
|
+
await cache.get('a');
|
|
156
|
+
await new Promise(r => setTimeout(r, 0));
|
|
157
|
+
cache.clear();
|
|
158
|
+
expect(store.entries.has('a')).toBe(true);
|
|
159
|
+
cache.clear(true);
|
|
160
|
+
expect(store.entries.has('a')).toBe(false);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type { PersistentCache } from "./PersistentCache";
|
|
2
|
+
|
|
3
|
+
/** What a {@link KeyedCache} writes to its persistent store. */
|
|
4
|
+
export interface StoredEntry<V> {
|
|
5
|
+
value: V;
|
|
6
|
+
ts: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface KeyedCacheOptions<V> {
|
|
10
|
+
/**
|
|
11
|
+
* Reads many keys in one call: one entry per key, in order, `null` when
|
|
12
|
+
* not found, an `Error` when that key's read failed (it rejects that key's
|
|
13
|
+
* readers only).
|
|
14
|
+
*/
|
|
15
|
+
loadMany(keys: string[]): Promise<Array<V | null | Error>>;
|
|
16
|
+
/**
|
|
17
|
+
* Reads one key (`null` when not found; rejects when the read failed).
|
|
18
|
+
* When set, a miss of {@link KeyedCache.get} is read alone with it, so it
|
|
19
|
+
* waits for no other key; only {@link KeyedCache.getMany} uses `loadMany`.
|
|
20
|
+
*/
|
|
21
|
+
load?(key: string): Promise<V | null>;
|
|
22
|
+
/** Most keys per `loadMany` call. Default: no limit. */
|
|
23
|
+
maxBatch?: number;
|
|
24
|
+
/** Second tier, read on a memory miss (IndexedDB in browsers). */
|
|
25
|
+
persistent?: PersistentCache<StoredEntry<V>>;
|
|
26
|
+
/** How long a value stays fresh in memory, per key. Default: until evicted. */
|
|
27
|
+
maxAgeMs?(key: string): number;
|
|
28
|
+
/** How long a stored value stays fresh. Default: forever. */
|
|
29
|
+
persistentMaxAgeMs?(): number;
|
|
30
|
+
/** Memory limits; the oldest values go first. Default: none. */
|
|
31
|
+
limits?(): { entries: number; bytes: number };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface Loaded<V> {
|
|
35
|
+
value: V | null;
|
|
36
|
+
/** Came from the persistent store, so it is not written back. */
|
|
37
|
+
stored: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Keyed read cache shared by profile and expression lookups. Concurrent reads
|
|
42
|
+
* of one key share one request; misses of one tick go out in one `loadMany`
|
|
43
|
+
* call, except a `get()` miss when the options have `load`. A `null` (not
|
|
44
|
+
* found) is returned but not kept.
|
|
45
|
+
*/
|
|
46
|
+
export class KeyedCache<V> {
|
|
47
|
+
#opts: KeyedCacheOptions<V>;
|
|
48
|
+
#values = new Map<string, { value: V; ts: number; size: number }>();
|
|
49
|
+
#bytes = 0;
|
|
50
|
+
#pending = new Map<string, Promise<V | null>>();
|
|
51
|
+
#batch: { keys: string[]; done: Promise<Map<string, Loaded<V | Error>>> } | null = null;
|
|
52
|
+
|
|
53
|
+
constructor(opts: KeyedCacheOptions<V>) {
|
|
54
|
+
this.#opts = opts;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get(key: string): Promise<V | null> {
|
|
58
|
+
const load = this.#opts.load;
|
|
59
|
+
return this.#read(key, load ? k => this.#loadOne(k, load) : k => this.#enqueue(k));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getMany(keys: string[]): Promise<Array<V | null>> {
|
|
63
|
+
return Promise.all(keys.map(key => this.#read(key, k => this.#enqueue(k))));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Stores a value known to be current; a read in flight for the key is not kept. */
|
|
67
|
+
set(key: string, value: V): void {
|
|
68
|
+
this.#pending.delete(key);
|
|
69
|
+
this.#remember(key, value, true);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Forgets a key in memory and in the persistent store. */
|
|
73
|
+
delete(key: string): void {
|
|
74
|
+
this.#pending.delete(key);
|
|
75
|
+
this.#forget(key);
|
|
76
|
+
this.#opts.persistent?.delete(key); // fire-and-forget
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Forgets every key in memory, and in the persistent store when `persistent` is set. */
|
|
80
|
+
clear(persistent = false): void {
|
|
81
|
+
this.#pending.clear();
|
|
82
|
+
this.#values.clear();
|
|
83
|
+
this.#bytes = 0;
|
|
84
|
+
if (persistent) this.#opts.persistent?.clear(); // fire-and-forget
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
#read(key: string, fetch: (key: string) => Promise<Loaded<V>>): Promise<V | null> {
|
|
88
|
+
const hit = this.#values.get(key);
|
|
89
|
+
if (hit && Date.now() - hit.ts < (this.#opts.maxAgeMs?.(key) ?? Infinity)) return Promise.resolve(hit.value);
|
|
90
|
+
const inFlight = this.#pending.get(key);
|
|
91
|
+
if (inFlight) return inFlight;
|
|
92
|
+
|
|
93
|
+
const loading = fetch(key);
|
|
94
|
+
const promise: Promise<V | null> = loading.then(
|
|
95
|
+
({ value, stored }) => {
|
|
96
|
+
// A set(), delete() or clear() since the read started wins over its result.
|
|
97
|
+
if (this.#pending.get(key) === promise) {
|
|
98
|
+
this.#pending.delete(key);
|
|
99
|
+
if (value !== null) this.#remember(key, value, !stored);
|
|
100
|
+
}
|
|
101
|
+
return value;
|
|
102
|
+
},
|
|
103
|
+
(error) => {
|
|
104
|
+
if (this.#pending.get(key) === promise) this.#pending.delete(key);
|
|
105
|
+
throw error;
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
this.#pending.set(key, promise);
|
|
109
|
+
return promise;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
#enqueue(key: string): Promise<Loaded<V>> {
|
|
113
|
+
if (!this.#batch) {
|
|
114
|
+
const keys: string[] = [];
|
|
115
|
+
const done = Promise.resolve().then(() => {
|
|
116
|
+
this.#batch = null;
|
|
117
|
+
return this.#loadBatch(keys);
|
|
118
|
+
});
|
|
119
|
+
this.#batch = { keys, done };
|
|
120
|
+
}
|
|
121
|
+
this.#batch.keys.push(key);
|
|
122
|
+
return this.#batch.done.then(loaded => {
|
|
123
|
+
const entry = loaded.get(key)!;
|
|
124
|
+
if (entry.value instanceof Error) throw entry.value;
|
|
125
|
+
return entry as Loaded<V>;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async #loadOne(key: string, load: (key: string) => Promise<V | null>): Promise<Loaded<V>> {
|
|
130
|
+
return (await this.#stored(key)) ?? { value: await load(key), stored: false };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async #loadBatch(keys: string[]): Promise<Map<string, Loaded<V | Error>>> {
|
|
134
|
+
const loaded = new Map<string, Loaded<V | Error>>();
|
|
135
|
+
const stored = await Promise.all(keys.map(key => this.#stored(key)));
|
|
136
|
+
const misses = keys.filter((key, i) => {
|
|
137
|
+
if (stored[i]) loaded.set(key, stored[i]!);
|
|
138
|
+
return !stored[i];
|
|
139
|
+
});
|
|
140
|
+
const size = this.#opts.maxBatch ?? Infinity;
|
|
141
|
+
const chunks: string[][] = [];
|
|
142
|
+
for (let i = 0; i < misses.length; i += size) chunks.push(misses.slice(i, i + size));
|
|
143
|
+
await Promise.all(chunks.map(async chunk => {
|
|
144
|
+
const values = await this.#opts.loadMany(chunk);
|
|
145
|
+
chunk.forEach((key, i) => loaded.set(key, { value: values[i] ?? null, stored: false }));
|
|
146
|
+
}));
|
|
147
|
+
return loaded;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async #stored(key: string): Promise<Loaded<V> | null> {
|
|
151
|
+
const entry = await this.#opts.persistent?.get(key);
|
|
152
|
+
// An entry without `value` was written in an older format: a miss.
|
|
153
|
+
if (entry?.value == null || !(Date.now() - (entry.ts ?? 0) < (this.#opts.persistentMaxAgeMs?.() ?? Infinity))) return null;
|
|
154
|
+
return { value: entry.value, stored: true };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
#remember(key: string, value: V, persist: boolean): void {
|
|
158
|
+
const limits = this.#opts.limits?.();
|
|
159
|
+
const size = limits ? JSON.stringify(value).length : 0;
|
|
160
|
+
this.#forget(key);
|
|
161
|
+
if (limits) {
|
|
162
|
+
for (const oldest of this.#values.keys()) {
|
|
163
|
+
if (this.#values.size < limits.entries && this.#bytes + size <= limits.bytes) break;
|
|
164
|
+
this.#forget(oldest);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const ts = Date.now();
|
|
168
|
+
this.#values.set(key, { value, ts, size });
|
|
169
|
+
this.#bytes += size;
|
|
170
|
+
if (persist) this.#opts.persistent?.put(key, { value, ts }); // fire-and-forget
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
#forget(key: string): void {
|
|
174
|
+
const entry = this.#values.get(key);
|
|
175
|
+
if (!entry) return;
|
|
176
|
+
this.#bytes -= entry.size;
|
|
177
|
+
this.#values.delete(key);
|
|
178
|
+
}
|
|
179
|
+
}
|