@c9up/echo 0.1.16 → 0.2.1
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/dist/CacheManager.d.ts +52 -6
- package/dist/CacheManager.d.ts.map +1 -1
- package/dist/CacheManager.js +146 -6
- package/dist/CacheManager.js.map +1 -1
- package/dist/EchoProvider.d.ts +27 -1
- package/dist/EchoProvider.d.ts.map +1 -1
- package/dist/EchoProvider.js +31 -3
- package/dist/EchoProvider.js.map +1 -1
- package/dist/StoreManager.d.ts +25 -2
- package/dist/StoreManager.d.ts.map +1 -1
- package/dist/StoreManager.js +35 -1
- package/dist/StoreManager.js.map +1 -1
- package/dist/drivers/MemoryDriver.d.ts +1 -1
- package/dist/drivers/MemoryDriver.d.ts.map +1 -1
- package/dist/drivers/MemoryDriver.js +19 -3
- package/dist/drivers/MemoryDriver.js.map +1 -1
- package/dist/drivers/RedisDriver.d.ts +1 -1
- package/dist/drivers/RedisDriver.d.ts.map +1 -1
- package/dist/drivers/RedisDriver.js +5 -2
- package/dist/drivers/RedisDriver.js.map +1 -1
- package/dist/drivers/TieredDriver.d.ts +45 -3
- package/dist/drivers/TieredDriver.d.ts.map +1 -1
- package/dist/drivers/TieredDriver.js +77 -10
- package/dist/drivers/TieredDriver.js.map +1 -1
- package/dist/quasar.d.ts.map +1 -1
- package/dist/quasar.js +55 -11
- package/dist/quasar.js.map +1 -1
- package/dist/types.d.ts +26 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +10 -10
- package/src/CacheManager.ts +163 -12
- package/src/EchoProvider.ts +32 -3
- package/src/StoreManager.ts +52 -3
- package/src/drivers/MemoryDriver.ts +16 -3
- package/src/drivers/RedisDriver.ts +5 -2
- package/src/drivers/TieredDriver.ts +108 -11
- package/src/quasar.ts +81 -18
- package/src/types.ts +26 -1
package/dist/quasar.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quasar.js","sourceRoot":"","sources":["../src/qu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YAC9B,MAAM,MAAM,GAAG,MAAM,OAAO,EAAE,CAAC;YAC/B,qEAAqE;YACrE,mEAAmE;YACnE,yBAAyB;YACzB,MAAM,MAAM,CAAC,WAAW,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC7C,iEAAiE;YACjE,iEAAiE;YACjE,kCAAkC;YAClC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACxB,CAAC;KACD,CAAC;AACH,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -49,12 +49,37 @@ export interface CacheDriver {
|
|
|
49
49
|
get<T = unknown>(key: string): Promise<T | null>;
|
|
50
50
|
set(key: string, value: unknown, ttlSeconds?: number): Promise<void>;
|
|
51
51
|
delete(key: string): Promise<boolean>;
|
|
52
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Remove everything — or, when `prefix` is given, only the keys under it.
|
|
54
|
+
*
|
|
55
|
+
* A namespaced view shares this driver, so a `clear()` on one had to be
|
|
56
|
+
* able to say WHICH subtree it meant. Without that it flushed the whole
|
|
57
|
+
* store, and a namespace per tenant is the documented use: one tenant
|
|
58
|
+
* clearing its own cache emptied everyone else's.
|
|
59
|
+
*
|
|
60
|
+
* A driver that cannot scope the removal must REFUSE when a prefix is
|
|
61
|
+
* given. Flushing everything instead is the failure this parameter exists
|
|
62
|
+
* to prevent, and it is silent.
|
|
63
|
+
*/
|
|
64
|
+
flush(prefix?: string): Promise<void>;
|
|
53
65
|
has(key: string): Promise<boolean>;
|
|
54
66
|
/** Grace-aware read: returns the entry even if stale, or `null` if physically gone. */
|
|
55
67
|
getEntry?<T = unknown>(key: string): Promise<CacheEntry<T> | null>;
|
|
56
68
|
/** Grace-aware write. */
|
|
57
69
|
setEntry?(key: string, value: unknown, options: DriverSetOptions): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Open whatever this driver needs OUTSIDE the process — a bus subscription,
|
|
72
|
+
* a connection it owns.
|
|
73
|
+
*
|
|
74
|
+
* Separate from the constructor on purpose. Building a store is what
|
|
75
|
+
* publishing the cache service does, and that happens while the application
|
|
76
|
+
* boots, including under `ream inspect` — which never shuts anything down.
|
|
77
|
+
* A driver that opened a socket to exist left one behind every time someone
|
|
78
|
+
* listed the routes. This is called once the application is ready, and its
|
|
79
|
+
* failure is the application's to refuse: a tiered store that never
|
|
80
|
+
* subscribed serves stale L1 copies to everyone until their TTL.
|
|
81
|
+
*/
|
|
82
|
+
connect?(): Promise<void>;
|
|
58
83
|
/**
|
|
59
84
|
* Drop entries this driver knows to be expired (bentocache `prune`). Only
|
|
60
85
|
* stores that do not evict on their own need it.
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD;;;GAGG;AACH,MAAM,WAAW,UAAU,CAAC,CAAC,GAAG,OAAO;IACtC,KAAK,EAAE,CAAC,CAAC;IACT,KAAK,EAAE,OAAO,CAAC;IACf;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,gEAAgE;AAChE,MAAM,WAAW,gBAAgB;IAChC,+DAA+D;IAC/D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,yEAAyE;IACzE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,yEAAyE;IACzE,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,WAAW;IAC3B,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IACjD,GAAG,CAAC,GAA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7B,GAAG,EAAE,MAAM,CAAC;CACZ;AAED,uEAAuE;AACvE,MAAM,WAAW,aAAa;IAC7B,WAAW,EAAE;QACZ,GAAG,EAAE,MAAM,CAAC;QACZ,KAAK,EAAE,OAAO,CAAC;QACf,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,OAAO,CAAC;KAChB,CAAC;IACF,YAAY,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAC7C,eAAe,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAChE,eAAe,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAChD,eAAe,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;CACnC;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC5B;;;;;;;;;;OAUG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC;CAC/C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@c9up/echo",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Cache — pluggable cache contract with memory + Redis drivers for the Ream framework",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -29,17 +29,17 @@
|
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@c9up/
|
|
33
|
-
"@c9up/
|
|
32
|
+
"@c9up/quasar": "^0.1.0 || ^0.2.0",
|
|
33
|
+
"@c9up/ream": "^0.2.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@biomejs/biome": "^2.
|
|
37
|
-
"@c9up/quasar": "^0.1.0",
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
36
|
+
"@biomejs/biome": "^2.5.12",
|
|
37
|
+
"@c9up/quasar": "^0.1.0 || ^0.2.0",
|
|
38
|
+
"@c9up/ream": "^0.2.0",
|
|
39
|
+
"@types/node": "^22.20.1",
|
|
40
|
+
"@vitest/coverage-v8": "4.1.11",
|
|
41
|
+
"typescript": "^6.0.3",
|
|
42
|
+
"vitest": "4.1.11"
|
|
43
43
|
},
|
|
44
44
|
"engines": {
|
|
45
45
|
"node": ">=22.0.0"
|
package/src/CacheManager.ts
CHANGED
|
@@ -87,8 +87,13 @@ function withTimeout<T>(
|
|
|
87
87
|
export interface CacheConfig {
|
|
88
88
|
driver?: string;
|
|
89
89
|
prefix?: string;
|
|
90
|
-
/**
|
|
91
|
-
|
|
90
|
+
/**
|
|
91
|
+
* How long an entry stays fresh: a number of SECONDS, or a
|
|
92
|
+
* {@link Duration} — `'30s'`, `'10m'` — like every other timing option
|
|
93
|
+
* here. It was the one locked to `number`, so a config copied from the
|
|
94
|
+
* documentation failed to typecheck on the line most likely to be copied.
|
|
95
|
+
*/
|
|
96
|
+
ttl?: Duration;
|
|
92
97
|
/** Default grace period (stale-while-revalidate) as a {@link Duration}. */
|
|
93
98
|
grace?: Duration;
|
|
94
99
|
/** Default soft timeout for `getOrSet` (return stale if the factory is slower). */
|
|
@@ -120,6 +125,13 @@ interface NormalizedGetOrSet<T> {
|
|
|
120
125
|
onFactoryError?: (error: FactoryError) => void;
|
|
121
126
|
}
|
|
122
127
|
|
|
128
|
+
/** The slice of `CacheStoreManager` a store it built needs to reach back into. */
|
|
129
|
+
export interface StoreFacade {
|
|
130
|
+
use(name?: string): CacheManager;
|
|
131
|
+
connectAll(): Promise<void>;
|
|
132
|
+
disconnectAll(): Promise<void>;
|
|
133
|
+
}
|
|
134
|
+
|
|
123
135
|
export class CacheManager {
|
|
124
136
|
#driver: CacheDriver;
|
|
125
137
|
#prefix: string;
|
|
@@ -144,11 +156,24 @@ export class CacheManager {
|
|
|
144
156
|
"cache:cleared": new Set(),
|
|
145
157
|
};
|
|
146
158
|
#shared: SharedState;
|
|
159
|
+
/**
|
|
160
|
+
* The store manager this one came from, when it did.
|
|
161
|
+
*
|
|
162
|
+
* What makes `cache.use('other')` work on the object the provider
|
|
163
|
+
* publishes. Upstream's manager is ONE object that both operates on the
|
|
164
|
+
* default store and reaches the named ones; binding the default store
|
|
165
|
+
* alone made the first example in the README throw on its second line.
|
|
166
|
+
*/
|
|
167
|
+
#stores: StoreFacade | undefined;
|
|
168
|
+
/** A connect in flight, so two callers do not open two. */
|
|
169
|
+
#connecting: Promise<void> | undefined;
|
|
170
|
+
/** Whether the driver's external work is open. Reset by `disconnect()`. */
|
|
171
|
+
#connected = false;
|
|
147
172
|
|
|
148
173
|
constructor(driver: CacheDriver, config?: CacheConfig, shared?: SharedState) {
|
|
149
174
|
this.#driver = driver;
|
|
150
175
|
this.#prefix = config?.prefix ?? "";
|
|
151
|
-
this.#defaultTtl = config?.ttl
|
|
176
|
+
this.#defaultTtl = resolveTtlSeconds(config?.ttl, 3600);
|
|
152
177
|
this.#defaultGrace = config?.grace;
|
|
153
178
|
this.#defaultTimeout = config?.timeout;
|
|
154
179
|
this.#defaultHardTimeout = config?.hardTimeout;
|
|
@@ -241,6 +266,7 @@ export class CacheManager {
|
|
|
241
266
|
* A driver without the hook reports nothing pruned rather than failing.
|
|
242
267
|
*/
|
|
243
268
|
async prune(): Promise<void> {
|
|
269
|
+
await this.#whenConnected();
|
|
244
270
|
await this.#driver.prune?.();
|
|
245
271
|
}
|
|
246
272
|
|
|
@@ -251,8 +277,108 @@ export class CacheManager {
|
|
|
251
277
|
* the connection and other consumers may still be using it. Only a driver
|
|
252
278
|
* that owns its resource implements the hook.
|
|
253
279
|
*/
|
|
280
|
+
/**
|
|
281
|
+
* Reach a named store — or this one, when it is the only one.
|
|
282
|
+
*
|
|
283
|
+
* @throws when a name is given and this manager owns a single store: the
|
|
284
|
+
* configuration says which stores exist, and answering with the default
|
|
285
|
+
* would cache under a name nobody declared.
|
|
286
|
+
*/
|
|
287
|
+
use(name?: string): CacheManager {
|
|
288
|
+
if (this.#stores) return this.#stores.use(name);
|
|
289
|
+
if (name === undefined) return this;
|
|
290
|
+
throw new Error(
|
|
291
|
+
`Echo: cannot use the "${name}" store — this cache is configured with a single store. ` +
|
|
292
|
+
"Declare `{ default, stores }` in config/cache.ts to name several.",
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** @internal Called by `CacheStoreManager` on the store it builds. */
|
|
297
|
+
belongsTo(stores: StoreFacade): void {
|
|
298
|
+
this.#stores = stores;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Open what the driver needs outside the process (a bus subscription).
|
|
303
|
+
*
|
|
304
|
+
* Awaited by whoever readies the application, and its failure is theirs to
|
|
305
|
+
* refuse — a tiered store that never subscribed serves stale L1 copies to
|
|
306
|
+
* everyone until their TTL.
|
|
307
|
+
*/
|
|
308
|
+
async connect(): Promise<void> {
|
|
309
|
+
if (this.#connected) return;
|
|
310
|
+
// Once. Two callers — the provider readying, and the first operation on
|
|
311
|
+
// a store built after that — must not open two subscriptions.
|
|
312
|
+
this.#connecting ??= (async () => {
|
|
313
|
+
await this.#driver.connect?.();
|
|
314
|
+
this.#connected = true;
|
|
315
|
+
})();
|
|
316
|
+
try {
|
|
317
|
+
await this.#connecting;
|
|
318
|
+
} finally {
|
|
319
|
+
// Forgotten either way. On failure so a later call gets to try again
|
|
320
|
+
// rather than replaying the same rejection for the life of the
|
|
321
|
+
// process; on success because `#connected` is what says so, and a
|
|
322
|
+
// memo kept past a `disconnect()` made the next `connect()` a no-op
|
|
323
|
+
// on a connection that had since been closed.
|
|
324
|
+
this.#connecting = undefined;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Wait for a connect ALREADY under way, and let its failure pass.
|
|
330
|
+
*
|
|
331
|
+
* A store built after the application was ready connects on its way out of
|
|
332
|
+
* `use()`, and its first operation must not race that. The failure is not
|
|
333
|
+
* this caller's to raise: the provider refuses the boot when the bus cannot
|
|
334
|
+
* be reached, and after that an unreachable bus costs staleness — refusing
|
|
335
|
+
* every read would cost far more.
|
|
336
|
+
*/
|
|
337
|
+
async #whenConnected(): Promise<void> {
|
|
338
|
+
if (this.#connected) return;
|
|
339
|
+
// STARTED here, not merely awaited. A store built after the application
|
|
340
|
+
// was ready connects on its way out of `use()`, and a connect that
|
|
341
|
+
// failed leaves nothing in flight — so an operation that only waited
|
|
342
|
+
// went on to read and write off the bus, for good. Attempting it means
|
|
343
|
+
// a transient failure is retried by the next operation instead.
|
|
344
|
+
//
|
|
345
|
+
// The failure is not raised: the provider refuses the boot when the bus
|
|
346
|
+
// cannot be reached, and after that an unreachable bus costs staleness.
|
|
347
|
+
// Refusing every read would cost far more.
|
|
348
|
+
await this.connect().catch(() => {});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Open every store this cache can reach, the named ones included.
|
|
353
|
+
*
|
|
354
|
+
* `connect()` alone reaches the DEFAULT store, which is what the provider
|
|
355
|
+
* publishes — so a named tiered store declared beside it never subscribed,
|
|
356
|
+
* and its L1 kept serving copies of keys other instances had deleted.
|
|
357
|
+
*/
|
|
358
|
+
async connectAll(): Promise<void> {
|
|
359
|
+
if (this.#stores) return this.#stores.connectAll();
|
|
360
|
+
await this.connect();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** Release every store this cache can reach (bentocache `disconnectAll`). */
|
|
364
|
+
async disconnectAll(): Promise<void> {
|
|
365
|
+
if (this.#stores) return this.#stores.disconnectAll();
|
|
366
|
+
await this.disconnect();
|
|
367
|
+
}
|
|
368
|
+
|
|
254
369
|
async disconnect(): Promise<void> {
|
|
255
|
-
|
|
370
|
+
// A connect still in flight finishes FIRST, so its subscription is one
|
|
371
|
+
// this teardown can see. Tearing down ahead of it left the handler to
|
|
372
|
+
// land afterwards, on a store nobody was tracking any more.
|
|
373
|
+
if (this.#connecting) await this.#connecting.catch(() => {});
|
|
374
|
+
try {
|
|
375
|
+
await this.#driver.disconnect?.();
|
|
376
|
+
} finally {
|
|
377
|
+
// Reset either way: a store that refused to close is not one that
|
|
378
|
+
// should silently skip its next `connect()`.
|
|
379
|
+
this.#connected = false;
|
|
380
|
+
this.#connecting = undefined;
|
|
381
|
+
}
|
|
256
382
|
}
|
|
257
383
|
|
|
258
384
|
async #readEntry<T>(prefixed: string): Promise<CacheEntry<T> | null> {
|
|
@@ -297,11 +423,12 @@ export class CacheManager {
|
|
|
297
423
|
|
|
298
424
|
// ---- get -------------------------------------------------------------
|
|
299
425
|
|
|
300
|
-
get<T = unknown>(key: string): Promise<T |
|
|
301
|
-
get<T = unknown>(options: GetOptions<T>): Promise<T |
|
|
426
|
+
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
427
|
+
get<T = unknown>(options: GetOptions<T>): Promise<T | undefined>;
|
|
302
428
|
async get<T = unknown>(
|
|
303
429
|
keyOrOptions: string | GetOptions<T>,
|
|
304
|
-
): Promise<T |
|
|
430
|
+
): Promise<T | undefined> {
|
|
431
|
+
await this.#whenConnected();
|
|
305
432
|
const key =
|
|
306
433
|
typeof keyOrOptions === "string" ? keyOrOptions : keyOrOptions.key;
|
|
307
434
|
const graceSeconds =
|
|
@@ -335,7 +462,12 @@ export class CacheManager {
|
|
|
335
462
|
if (defaultValue !== undefined) {
|
|
336
463
|
return defaultValue instanceof Function ? defaultValue() : defaultValue;
|
|
337
464
|
}
|
|
338
|
-
|
|
465
|
+
// UNDEFINED, as upstream answers. It returned `null`, so code written
|
|
466
|
+
// against the documented shape — `if (value === undefined)` — never
|
|
467
|
+
// matched a miss, silently. The driver boundary still speaks `null`,
|
|
468
|
+
// which is where the translation happens: a driver reports "no entry",
|
|
469
|
+
// and this reports "nothing cached".
|
|
470
|
+
return undefined;
|
|
339
471
|
}
|
|
340
472
|
|
|
341
473
|
// ---- set -------------------------------------------------------------
|
|
@@ -347,6 +479,7 @@ export class CacheManager {
|
|
|
347
479
|
value?: unknown,
|
|
348
480
|
ttlSeconds?: number,
|
|
349
481
|
): Promise<void> {
|
|
482
|
+
await this.#whenConnected();
|
|
350
483
|
let key: string;
|
|
351
484
|
let val: unknown;
|
|
352
485
|
let ttl: number;
|
|
@@ -382,6 +515,7 @@ export class CacheManager {
|
|
|
382
515
|
delete(key: string): Promise<boolean>;
|
|
383
516
|
delete(options: DeleteOptions): Promise<boolean>;
|
|
384
517
|
async delete(keyOrOptions: string | DeleteOptions): Promise<boolean> {
|
|
518
|
+
await this.#whenConnected();
|
|
385
519
|
const key =
|
|
386
520
|
typeof keyOrOptions === "string" ? keyOrOptions : keyOrOptions.key;
|
|
387
521
|
const deleted = await this.#driver.delete(this.#prefixKey(key));
|
|
@@ -393,6 +527,7 @@ export class CacheManager {
|
|
|
393
527
|
async deleteMany(
|
|
394
528
|
keysOrOptions: string[] | DeleteManyOptions,
|
|
395
529
|
): Promise<boolean> {
|
|
530
|
+
await this.#whenConnected();
|
|
396
531
|
const keys = Array.isArray(keysOrOptions)
|
|
397
532
|
? keysOrOptions
|
|
398
533
|
: keysOrOptions.keys;
|
|
@@ -408,6 +543,7 @@ export class CacheManager {
|
|
|
408
543
|
has(key: string): Promise<boolean>;
|
|
409
544
|
has(options: HasOptions): Promise<boolean>;
|
|
410
545
|
async has(keyOrOptions: string | HasOptions): Promise<boolean> {
|
|
546
|
+
await this.#whenConnected();
|
|
411
547
|
const key =
|
|
412
548
|
typeof keyOrOptions === "string" ? keyOrOptions : keyOrOptions.key;
|
|
413
549
|
return this.#driver.has(this.#prefixKey(key));
|
|
@@ -421,9 +557,10 @@ export class CacheManager {
|
|
|
421
557
|
}
|
|
422
558
|
|
|
423
559
|
/** Read a key and delete it in one step (bento `pull`). Returns `null` on miss. */
|
|
424
|
-
async pull<T = unknown>(key: string): Promise<T |
|
|
560
|
+
async pull<T = unknown>(key: string): Promise<T | undefined> {
|
|
561
|
+
await this.#whenConnected();
|
|
425
562
|
const value = await this.get<T>(key);
|
|
426
|
-
if (value !==
|
|
563
|
+
if (value !== undefined) await this.delete(key);
|
|
427
564
|
return value;
|
|
428
565
|
}
|
|
429
566
|
|
|
@@ -432,6 +569,7 @@ export class CacheManager {
|
|
|
432
569
|
* window (bento `expire`). Without grace this is equivalent to a delete.
|
|
433
570
|
*/
|
|
434
571
|
async expire(keyOrOptions: string | ExpireOptions): Promise<boolean> {
|
|
572
|
+
await this.#whenConnected();
|
|
435
573
|
const key =
|
|
436
574
|
typeof keyOrOptions === "string" ? keyOrOptions : keyOrOptions.key;
|
|
437
575
|
const prefixed = this.#prefixKey(key);
|
|
@@ -452,9 +590,19 @@ export class CacheManager {
|
|
|
452
590
|
return this.#driver.delete(prefixed);
|
|
453
591
|
}
|
|
454
592
|
|
|
455
|
-
/**
|
|
593
|
+
/**
|
|
594
|
+
* Clear this cache — the whole store, or only this namespace's subtree.
|
|
595
|
+
*
|
|
596
|
+
* A namespaced view shares the driver, so this used to flush everything:
|
|
597
|
+
* one tenant clearing its own cache emptied every other tenant's, from an
|
|
598
|
+
* operation they are allowed to run. Upstream scopes it the same way, by
|
|
599
|
+
* handing the driver the prefix.
|
|
600
|
+
*/
|
|
456
601
|
async clear(): Promise<void> {
|
|
457
|
-
await this.#
|
|
602
|
+
await this.#whenConnected();
|
|
603
|
+
// The separator is part of it: `tenant-a` must not take `tenant-abc`
|
|
604
|
+
// with it, and every key under this view is written `<prefix>:<key>`.
|
|
605
|
+
await this.#driver.flush(this.#prefix ? `${this.#prefix}:` : undefined);
|
|
458
606
|
this.#emit("cache:cleared", { store: this.#name });
|
|
459
607
|
}
|
|
460
608
|
|
|
@@ -467,6 +615,7 @@ export class CacheManager {
|
|
|
467
615
|
tags: string[],
|
|
468
616
|
ttlSeconds?: number,
|
|
469
617
|
): Promise<void> {
|
|
618
|
+
await this.#whenConnected();
|
|
470
619
|
await this.#writeValue(
|
|
471
620
|
this.#prefixKey(key),
|
|
472
621
|
value,
|
|
@@ -483,6 +632,7 @@ export class CacheManager {
|
|
|
483
632
|
async deleteByTag(
|
|
484
633
|
tagsOrOptions: string[] | DeleteByTagOptions,
|
|
485
634
|
): Promise<void> {
|
|
635
|
+
await this.#whenConnected();
|
|
486
636
|
const tags = Array.isArray(tagsOrOptions)
|
|
487
637
|
? tagsOrOptions
|
|
488
638
|
: tagsOrOptions.tags;
|
|
@@ -608,6 +758,7 @@ export class CacheManager {
|
|
|
608
758
|
b?: number,
|
|
609
759
|
c?: Factory<T>,
|
|
610
760
|
): Promise<T> {
|
|
761
|
+
await this.#whenConnected();
|
|
611
762
|
const o = this.#normalizeGetOrSet<T>(a, b, c);
|
|
612
763
|
const prefixed = this.#prefixKey(o.key);
|
|
613
764
|
|
package/src/EchoProvider.ts
CHANGED
|
@@ -116,16 +116,45 @@ export default class EchoProvider {
|
|
|
116
116
|
this.app.container.singleton("cache", cache);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
/** The cache THIS provider
|
|
119
|
+
/** The cache THIS provider opened — not whatever the module singleton holds. */
|
|
120
120
|
#cache: CacheManager | undefined;
|
|
121
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Publish the cache on `services/main`.
|
|
124
|
+
*
|
|
125
|
+
* At BOOT, matching upstream, whose own accessor resolves the manager on
|
|
126
|
+
* `app.booted()`. It has to be here: the HTTP socket opens before the
|
|
127
|
+
* providers are readied, so a cache published in `ready` left a window
|
|
128
|
+
* where a request could reach a controller and the accessor would throw —
|
|
129
|
+
* and a preload that actually uses the cache would fail outright.
|
|
130
|
+
*
|
|
131
|
+
* Building it opens nothing. A driver's external work lives behind
|
|
132
|
+
* {@link CacheDriver.connect}, which {@link ready} awaits.
|
|
133
|
+
*/
|
|
122
134
|
async boot(): Promise<void> {
|
|
123
135
|
this.#cache = await this.app.container.resolve<CacheManager>(CacheManager);
|
|
124
136
|
setCache(this.#cache);
|
|
125
137
|
}
|
|
126
138
|
|
|
127
139
|
/**
|
|
128
|
-
*
|
|
140
|
+
* Open what the drivers need outside the process — a tiered store's bus.
|
|
141
|
+
*
|
|
142
|
+
* `ready` is the phase upstream reserves for operational effects, and the
|
|
143
|
+
* one an inspection never reaches: `register`, `boot` and `start` all run
|
|
144
|
+
* under `ream inspect`, and `shutdown` does not, so a subscription opened
|
|
145
|
+
* any earlier was a Redis connection left behind by a command that only
|
|
146
|
+
* meant to list the routes.
|
|
147
|
+
*
|
|
148
|
+
* A failure here fails the boot, deliberately. An instance whose bus never
|
|
149
|
+
* subscribed keeps serving its own L1 copies of keys other instances have
|
|
150
|
+
* already deleted, with nothing to say so.
|
|
151
|
+
*/
|
|
152
|
+
async ready(): Promise<void> {
|
|
153
|
+
await this.#cache?.connectAll();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Release the cache the app opened.
|
|
129
158
|
*
|
|
130
159
|
* The memory driver runs a sweep on a timer and the redis driver holds a
|
|
131
160
|
* connection. Neither is released on its own, so across a dev reload or a
|
|
@@ -133,7 +162,7 @@ export default class EchoProvider {
|
|
|
133
162
|
*/
|
|
134
163
|
async shutdown(): Promise<void> {
|
|
135
164
|
if (!this.#cache) return;
|
|
136
|
-
await this.#cache.
|
|
165
|
+
await this.#cache.disconnectAll();
|
|
137
166
|
// Two applications can share a process — parallel tests, a hot reload.
|
|
138
167
|
// The module singleton holds whichever booted last, so it is only ours
|
|
139
168
|
// to clear while it still points at the cache this provider booted.
|
package/src/StoreManager.ts
CHANGED
|
@@ -29,8 +29,15 @@ export type DriverFactory = () => CacheDriver;
|
|
|
29
29
|
export interface StoreConfig {
|
|
30
30
|
driver: DriverFactory;
|
|
31
31
|
prefix?: string;
|
|
32
|
-
/**
|
|
33
|
-
|
|
32
|
+
/**
|
|
33
|
+
* How long an entry stays fresh. A number of seconds, or a duration —
|
|
34
|
+
* `'30s'`, `'10m'`, `'1h'` — the way every other timing option here reads,
|
|
35
|
+
* and the way upstream's config is written.
|
|
36
|
+
*
|
|
37
|
+
* It was the one option locked to `number`, so a config copied from the
|
|
38
|
+
* documentation did not typecheck on the line most likely to be copied.
|
|
39
|
+
*/
|
|
40
|
+
ttl?: Duration;
|
|
34
41
|
grace?: Duration;
|
|
35
42
|
timeout?: Duration;
|
|
36
43
|
hardTimeout?: Duration;
|
|
@@ -39,6 +46,14 @@ export interface StoreConfig {
|
|
|
39
46
|
|
|
40
47
|
export interface MultiStoreConfig {
|
|
41
48
|
default: string;
|
|
49
|
+
/**
|
|
50
|
+
* The TTL every store inherits when it does not name its own.
|
|
51
|
+
*
|
|
52
|
+
* Upstream's configuration carries one, and copying a config that used it
|
|
53
|
+
* silently fell back to the built-in default instead — a cache with a
|
|
54
|
+
* different lifetime than the file says.
|
|
55
|
+
*/
|
|
56
|
+
ttl?: Duration;
|
|
42
57
|
/**
|
|
43
58
|
* The stores this application can use, by name. Each is a {@link store}
|
|
44
59
|
* builder — the shape a cache config takes — or the plain
|
|
@@ -183,6 +198,13 @@ export const drivers = {
|
|
|
183
198
|
export class CacheStoreManager {
|
|
184
199
|
#config: MultiStoreConfig;
|
|
185
200
|
#built: Map<string, CacheManager> = new Map();
|
|
201
|
+
/**
|
|
202
|
+
* Whether the application has been readied.
|
|
203
|
+
*
|
|
204
|
+
* Stores are created on first use, so one first touched by a request is
|
|
205
|
+
* created AFTER `ready()` has run — and nothing would have connected it.
|
|
206
|
+
*/
|
|
207
|
+
#connected = false;
|
|
186
208
|
// Held here as well as on each store, so a listener registered before a
|
|
187
209
|
// store is first used still reaches it.
|
|
188
210
|
readonly #listeners: {
|
|
@@ -217,7 +239,8 @@ export class CacheStoreManager {
|
|
|
217
239
|
const cfg = entryOf(declared);
|
|
218
240
|
const manager = new CacheManager(cfg.driver(), {
|
|
219
241
|
prefix: cfg.prefix,
|
|
220
|
-
|
|
242
|
+
// The store's own, or the one every store inherits.
|
|
243
|
+
ttl: cfg.ttl ?? this.#config.ttl,
|
|
221
244
|
grace: cfg.grace,
|
|
222
245
|
timeout: cfg.timeout,
|
|
223
246
|
hardTimeout: cfg.hardTimeout,
|
|
@@ -238,7 +261,17 @@ export class CacheStoreManager {
|
|
|
238
261
|
replay("cache:written");
|
|
239
262
|
replay("cache:deleted");
|
|
240
263
|
replay("cache:cleared");
|
|
264
|
+
// So `cache.use('other')` works on whichever store the provider
|
|
265
|
+
// published — upstream's manager is one object that both operates on
|
|
266
|
+
// the default store and reaches the named ones.
|
|
267
|
+
manager.belongsTo(this);
|
|
241
268
|
this.#built.set(store, manager);
|
|
269
|
+
// Built after the application was readied: connect it now rather than
|
|
270
|
+
// leave it the only store on no bus. `use()` is synchronous — upstream's
|
|
271
|
+
// is too, and the README chains off it — so the manager's own
|
|
272
|
+
// operations wait for this, and its failure costs staleness rather than
|
|
273
|
+
// every read.
|
|
274
|
+
if (this.#connected) manager.connect().catch(() => {});
|
|
242
275
|
return manager;
|
|
243
276
|
}
|
|
244
277
|
|
|
@@ -295,7 +328,23 @@ export class CacheStoreManager {
|
|
|
295
328
|
* Call it from a shutdown hook. A store on an INJECTED client leaves that
|
|
296
329
|
* client alone — echo did not open it.
|
|
297
330
|
*/
|
|
331
|
+
/**
|
|
332
|
+
* Open what every BUILT store needs outside the process.
|
|
333
|
+
*
|
|
334
|
+
* Stores are created on first use, so this reaches the ones the
|
|
335
|
+
* application has actually asked for — naming a Redis store in an
|
|
336
|
+
* environment that runs on memory still opens nothing.
|
|
337
|
+
*/
|
|
338
|
+
async connectAll(): Promise<void> {
|
|
339
|
+
// Set FIRST, so a store built while this is in flight connects itself
|
|
340
|
+
// rather than being missed by a walk that had already listed the built
|
|
341
|
+
// ones.
|
|
342
|
+
this.#connected = true;
|
|
343
|
+
await Promise.all([...this.#built.values()].map((m) => m.connect()));
|
|
344
|
+
}
|
|
345
|
+
|
|
298
346
|
async disconnectAll(): Promise<void> {
|
|
347
|
+
this.#connected = false;
|
|
299
348
|
await Promise.all([...this.#built.values()].map((m) => m.disconnect()));
|
|
300
349
|
}
|
|
301
350
|
}
|
|
@@ -208,9 +208,22 @@ export class MemoryDriver implements TaggableDriver {
|
|
|
208
208
|
return this.#store.delete(key);
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
async flush(): Promise<void> {
|
|
212
|
-
|
|
213
|
-
|
|
211
|
+
async flush(prefix?: string): Promise<void> {
|
|
212
|
+
if (prefix === undefined) {
|
|
213
|
+
this.#store.clear();
|
|
214
|
+
this.#tagIndex.clear();
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
for (const key of [...this.#store.keys()]) {
|
|
218
|
+
if (key.startsWith(prefix)) this.#store.delete(key);
|
|
219
|
+
}
|
|
220
|
+
// The tag index points at keys; the ones that are gone go with them.
|
|
221
|
+
for (const [tag, keys] of [...this.#tagIndex.entries()]) {
|
|
222
|
+
for (const key of [...keys]) {
|
|
223
|
+
if (key.startsWith(prefix)) keys.delete(key);
|
|
224
|
+
}
|
|
225
|
+
if (keys.size === 0) this.#tagIndex.delete(tag);
|
|
226
|
+
}
|
|
214
227
|
}
|
|
215
228
|
|
|
216
229
|
async has(key: string): Promise<boolean> {
|
|
@@ -220,16 +220,19 @@ export class RedisDriver implements TaggableDriver {
|
|
|
220
220
|
await this.#applyTags(key, tags, physical);
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
async flush(): Promise<void> {
|
|
223
|
+
async flush(prefix?: string): Promise<void> {
|
|
224
224
|
const client = await this.#client();
|
|
225
225
|
const scan = client.scan;
|
|
226
226
|
if (typeof scan === "function") {
|
|
227
|
+
// The driver's own prefix scopes it to this store; `prefix` scopes
|
|
228
|
+
// it further, to the namespace that asked.
|
|
229
|
+
const match = `${this.#prefix}${prefix ?? ""}`;
|
|
227
230
|
let cursor = "0";
|
|
228
231
|
do {
|
|
229
232
|
const [nextCursor, keys] = await scan(
|
|
230
233
|
cursor,
|
|
231
234
|
"MATCH",
|
|
232
|
-
`${
|
|
235
|
+
`${match}*`,
|
|
233
236
|
"COUNT",
|
|
234
237
|
100,
|
|
235
238
|
);
|