@c9up/echo 0.2.3 → 0.2.5
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/README.md +1 -1
- package/dist/CacheManager.d.ts +13 -2
- package/dist/CacheManager.d.ts.map +1 -1
- package/dist/CacheManager.js +309 -56
- package/dist/CacheManager.js.map +1 -1
- package/dist/EchoProvider.d.ts.map +1 -1
- package/dist/EchoProvider.js +32 -3
- package/dist/EchoProvider.js.map +1 -1
- package/dist/StoreManager.d.ts +38 -11
- package/dist/StoreManager.d.ts.map +1 -1
- package/dist/StoreManager.js +80 -22
- package/dist/StoreManager.js.map +1 -1
- package/dist/bytes.d.ts +13 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +35 -0
- package/dist/bytes.js.map +1 -0
- package/dist/claim.d.ts +15 -0
- package/dist/claim.d.ts.map +1 -0
- package/dist/claim.js +10 -0
- package/dist/claim.js.map +1 -0
- package/dist/configure.d.ts +1 -0
- package/dist/configure.d.ts.map +1 -1
- package/dist/configure.js +3 -0
- package/dist/configure.js.map +1 -1
- package/dist/console/cacheClear.d.ts +13 -0
- package/dist/console/cacheClear.d.ts.map +1 -0
- package/dist/console/cacheClear.js +69 -0
- package/dist/console/cacheClear.js.map +1 -0
- package/dist/console/cacheDelete.d.ts +11 -0
- package/dist/console/cacheDelete.d.ts.map +1 -0
- package/dist/console/cacheDelete.js +42 -0
- package/dist/console/cacheDelete.js.map +1 -0
- package/dist/console/cachePrune.d.ts +9 -0
- package/dist/console/cachePrune.d.ts.map +1 -0
- package/dist/console/cachePrune.js +35 -0
- package/dist/console/cachePrune.js.map +1 -0
- package/dist/console/contract.d.ts +64 -0
- package/dist/console/contract.d.ts.map +1 -0
- package/dist/console/contract.js +32 -0
- package/dist/console/contract.js.map +1 -0
- package/dist/console/index.d.ts +25 -0
- package/dist/console/index.d.ts.map +1 -0
- package/dist/console/index.js +38 -0
- package/dist/console/index.js.map +1 -0
- package/dist/console/store.d.ts +14 -0
- package/dist/console/store.d.ts.map +1 -0
- package/dist/console/store.js +35 -0
- package/dist/console/store.js.map +1 -0
- package/dist/drivers/MemoryDriver.d.ts +37 -1
- package/dist/drivers/MemoryDriver.d.ts.map +1 -1
- package/dist/drivers/MemoryDriver.js +170 -72
- package/dist/drivers/MemoryDriver.js.map +1 -1
- package/dist/drivers/RedisDriver.d.ts +15 -8
- package/dist/drivers/RedisDriver.d.ts.map +1 -1
- package/dist/drivers/RedisDriver.js +198 -97
- package/dist/drivers/RedisDriver.js.map +1 -1
- package/dist/drivers/TieredDriver.d.ts +42 -0
- package/dist/drivers/TieredDriver.d.ts.map +1 -1
- package/dist/drivers/TieredDriver.js +114 -9
- package/dist/drivers/TieredDriver.js.map +1 -1
- package/dist/duration.d.ts +13 -3
- package/dist/duration.d.ts.map +1 -1
- package/dist/duration.js +22 -7
- package/dist/duration.js.map +1 -1
- package/dist/errors.d.ts +60 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +81 -6
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/quasar.d.ts +1 -1
- package/dist/quasar.d.ts.map +1 -1
- package/dist/quasar.js +106 -19
- package/dist/quasar.js.map +1 -1
- package/dist/services/main.d.ts +1 -1
- package/dist/services/main.d.ts.map +1 -1
- package/dist/services/main.js +22 -5
- package/dist/services/main.js.map +1 -1
- package/dist/types.d.ts +44 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/vendor/quasarConnection.d.ts +44 -3
- package/dist/vendor/quasarConnection.d.ts.map +1 -1
- package/dist/vendor/quasarConnection.js +64 -23
- package/dist/vendor/quasarConnection.js.map +1 -1
- package/package.json +5 -1
- package/src/CacheManager.ts +367 -69
- package/src/EchoProvider.ts +45 -4
- package/src/StoreManager.ts +131 -35
- package/src/bytes.ts +38 -0
- package/src/claim.ts +22 -0
- package/src/configure.ts +4 -0
- package/src/console/cacheClear.ts +96 -0
- package/src/console/cacheDelete.ts +65 -0
- package/src/console/cachePrune.ts +57 -0
- package/src/console/contract.ts +93 -0
- package/src/console/index.ts +61 -0
- package/src/console/store.ts +47 -0
- package/src/drivers/MemoryDriver.ts +221 -69
- package/src/drivers/RedisDriver.ts +234 -104
- package/src/drivers/TieredDriver.ts +169 -9
- package/src/duration.ts +28 -7
- package/src/errors.ts +103 -6
- package/src/index.ts +17 -3
- package/src/quasar.ts +145 -30
- package/src/services/main.ts +26 -8
- package/src/types.ts +46 -7
- package/src/vendor/quasarConnection.ts +105 -22
|
@@ -3,28 +3,39 @@
|
|
|
3
3
|
* (stale-while-revalidate) and tags. Suitable as the L2 tier of
|
|
4
4
|
* {@link TieredDriver}.
|
|
5
5
|
*
|
|
6
|
-
* Requires a Redis client
|
|
7
|
-
*
|
|
6
|
+
* Requires a Redis client implementing the minimal interface below, which a
|
|
7
|
+
* raw ioredis client satisfies.
|
|
8
8
|
*
|
|
9
|
-
* STORAGE FORMAT
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
9
|
+
* STORAGE FORMAT: values live at `<prefix>v:<key>` as a JSON envelope
|
|
10
|
+
* `{ "v": <value>, "e": <logicalExpiryEpochMs> }`; tag sets at `<prefix>t:<tag>`
|
|
11
|
+
* and per-key reverse indexes at `<prefix>m:<key>`. The physical Redis TTL
|
|
12
|
+
* covers `ttl + grace` so a logically-expired value survives for the grace
|
|
13
|
+
* window; `e` records the logical expiry so reads can flag it stale.
|
|
13
14
|
*/
|
|
14
15
|
|
|
16
|
+
import { claimed } from "../claim.js";
|
|
17
|
+
import { checkTtlSeconds } from "../duration.js";
|
|
18
|
+
import { EchoError } from "../errors.js";
|
|
15
19
|
import type { CacheEntry, DriverSetOptions, TaggableDriver } from "../types.js";
|
|
20
|
+
import { hasEnded } from "../vendor/quasarConnection.js";
|
|
16
21
|
|
|
17
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Minimal Redis client interface, in ioredis' shape — a raw ioredis client
|
|
24
|
+
* satisfies it as is.
|
|
25
|
+
*/
|
|
18
26
|
export interface RedisClient {
|
|
19
27
|
get(key: string): Promise<string | null>;
|
|
20
28
|
set(key: string, value: string, ...args: unknown[]): Promise<unknown>;
|
|
21
|
-
|
|
29
|
+
/** Variadic, as ioredis declares it: a raw client then typechecks. */
|
|
30
|
+
del(...keys: string[]): Promise<number>;
|
|
22
31
|
exists(key: string): Promise<number>;
|
|
23
32
|
keys(pattern: string): Promise<string[]>;
|
|
24
33
|
sadd(key: string, ...members: string[]): Promise<number>;
|
|
25
34
|
srem(key: string, ...members: string[]): Promise<number>;
|
|
26
35
|
smembers(key: string): Promise<string[]>;
|
|
27
36
|
expire(key: string, seconds: number): Promise<number>;
|
|
37
|
+
/** Drop a key's expiry, so a tag set outlives no less than an immortal member. */
|
|
38
|
+
persist(key: string): Promise<number>;
|
|
28
39
|
ttl(key: string): Promise<number>;
|
|
29
40
|
scan?(
|
|
30
41
|
cursor: string,
|
|
@@ -38,6 +49,26 @@ export interface RedisClient {
|
|
|
38
49
|
interface Envelope {
|
|
39
50
|
v: unknown;
|
|
40
51
|
e: number;
|
|
52
|
+
/** Tags, so a tier above can promote the entry with them. */
|
|
53
|
+
t?: string[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Escape Redis MATCH glob metacharacters so the pattern matches literally. */
|
|
57
|
+
function escapeGlob(literal: string): string {
|
|
58
|
+
return literal.replace(/[*?[\]\\]/g, "\\$&");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** JSON, or `undefined` when the text is not JSON at all. */
|
|
62
|
+
function parseJson(raw: string): unknown {
|
|
63
|
+
try {
|
|
64
|
+
return JSON.parse(raw);
|
|
65
|
+
} catch {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function isStringArray(x: unknown): x is string[] {
|
|
71
|
+
return Array.isArray(x) && x.every((item) => typeof item === "string");
|
|
41
72
|
}
|
|
42
73
|
|
|
43
74
|
function isEnvelope(x: unknown): x is Envelope {
|
|
@@ -65,17 +96,32 @@ export class RedisDriver implements TaggableDriver {
|
|
|
65
96
|
#resolved: RedisClient | undefined;
|
|
66
97
|
#pending: Promise<RedisClient> | undefined;
|
|
67
98
|
#prefix: string;
|
|
99
|
+
/** Reported once per driver: the condition, not each key, is the news. */
|
|
100
|
+
#foreignReported = false;
|
|
68
101
|
|
|
69
|
-
constructor(source: RedisClientSource, prefix
|
|
102
|
+
constructor(source: RedisClientSource, prefix?: string) {
|
|
70
103
|
this.#source = source;
|
|
71
|
-
|
|
104
|
+
// `||`, not a default parameter: an empty prefix made clear() scan
|
|
105
|
+
// `MATCH *` and delete every key in the database. Upstream falls back
|
|
106
|
+
// the same way (`config.prefix || "bentocache"`).
|
|
107
|
+
this.#prefix = prefix || "cache:";
|
|
72
108
|
}
|
|
73
109
|
|
|
74
110
|
/**
|
|
75
111
|
* The client, resolved once. Two commands racing on a cold store must not
|
|
76
112
|
* each open their own connection, so the in-flight promise is shared.
|
|
113
|
+
*
|
|
114
|
+
* Resolved AGAIN once the client it holds has ended for good: quasar
|
|
115
|
+
* retires a connection whose retry strategy gave up and hands out a fresh
|
|
116
|
+
* one, and keeping the dead client made a three-second Redis blip a
|
|
117
|
+
* permanent cache outage. NAMED DEVIATION, better than upstream, whose
|
|
118
|
+
* Redis driver captures `ioConnection` once. A client handed in directly is
|
|
119
|
+
* kept whatever its state.
|
|
77
120
|
*/
|
|
78
121
|
async #client(): Promise<RedisClient> {
|
|
122
|
+
if (typeof this.#source === "function" && hasEnded(this.#resolved)) {
|
|
123
|
+
this.#resolved = undefined;
|
|
124
|
+
}
|
|
79
125
|
if (this.#resolved) return this.#resolved;
|
|
80
126
|
if (typeof this.#source !== "function") {
|
|
81
127
|
this.#resolved = this.#source;
|
|
@@ -99,35 +145,47 @@ export class RedisDriver implements TaggableDriver {
|
|
|
99
145
|
return this.#pending;
|
|
100
146
|
}
|
|
101
147
|
|
|
102
|
-
|
|
103
|
-
|
|
148
|
+
/*
|
|
149
|
+
* Three disjoint sub-spaces under the store prefix: values (`v:`), tag
|
|
150
|
+
* sets (`t:`) and per-key reverse indexes (`m:`). They shared one keyspace
|
|
151
|
+
* before, so a cache key `tag:<t>` overwrote a tag SET with a string and a
|
|
152
|
+
* root key `meta:tags:<k>` sat in another entry's index slot: every
|
|
153
|
+
* invalidation through them then failed with WRONGTYPE, and the entry it
|
|
154
|
+
* was meant to drop kept being served. Any user key lands under `v:`, so
|
|
155
|
+
* none can reach the bookkeeping.
|
|
156
|
+
*/
|
|
157
|
+
|
|
158
|
+
#valueKey(key: string): string {
|
|
159
|
+
return `${this.#prefix}v:${key}`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
#tagKey(tag: string): string {
|
|
163
|
+
return `${this.#prefix}t:${tag}`;
|
|
104
164
|
}
|
|
105
165
|
|
|
106
166
|
/**
|
|
107
|
-
* Reverse
|
|
108
|
-
* memberships
|
|
109
|
-
* belongs to.
|
|
167
|
+
* Reverse index of the tags `key` carries. Lets a retag clean stale
|
|
168
|
+
* memberships, and `delete()` drop the key from every tag set it is in.
|
|
110
169
|
*/
|
|
111
|
-
#metaKey(
|
|
112
|
-
return `${this.#prefix}
|
|
170
|
+
#metaKey(key: string): string {
|
|
171
|
+
return `${this.#prefix}m:${key}`;
|
|
113
172
|
}
|
|
114
173
|
|
|
115
174
|
async delete(key: string): Promise<boolean> {
|
|
116
175
|
const client = await this.#client();
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
const tags = await client.smembers(metaKey);
|
|
120
|
-
for (const tag of tags) {
|
|
121
|
-
const tagKey = `${this.#prefix}tag:${tag}`;
|
|
122
|
-
await client.srem(tagKey, fullKey);
|
|
123
|
-
}
|
|
124
|
-
if (tags.length > 0) {
|
|
125
|
-
await client.del(metaKey);
|
|
126
|
-
}
|
|
127
|
-
const count = await client.del(fullKey);
|
|
176
|
+
await this.#dropTags(key);
|
|
177
|
+
const count = await client.del(this.#valueKey(key));
|
|
128
178
|
return count > 0;
|
|
129
179
|
}
|
|
130
180
|
|
|
181
|
+
#reportForeign(): void {
|
|
182
|
+
if (this.#foreignReported) return;
|
|
183
|
+
this.#foreignReported = true;
|
|
184
|
+
process.stderr.write(
|
|
185
|
+
`[echo] a value under the Redis prefix "${this.#prefix}" is not one echo wrote; it reads as a miss and the next write replaces it\n`,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
131
189
|
async get<T = unknown>(key: string): Promise<T | null> {
|
|
132
190
|
const entry = await this.getEntry<T>(key);
|
|
133
191
|
if (entry === null || entry.stale) return null;
|
|
@@ -136,25 +194,36 @@ export class RedisDriver implements TaggableDriver {
|
|
|
136
194
|
|
|
137
195
|
async getEntry<T = unknown>(key: string): Promise<CacheEntry<T> | null> {
|
|
138
196
|
const client = await this.#client();
|
|
139
|
-
const raw = await client.get(this.#
|
|
197
|
+
const raw = await client.get(this.#valueKey(key));
|
|
140
198
|
if (raw === null) return null;
|
|
141
|
-
const parsed
|
|
142
|
-
if (!isEnvelope(parsed))
|
|
199
|
+
const parsed = parseJson(raw);
|
|
200
|
+
if (!isEnvelope(parsed)) {
|
|
201
|
+
// Not something this driver wrote — another application on the
|
|
202
|
+
// prefix, a manual SET, a torn write. A miss, so getOrSet computes
|
|
203
|
+
// and overwrites it; it used to throw on every read of the key,
|
|
204
|
+
// and getOrSet reads before it computes, so it never healed.
|
|
205
|
+
this.#reportForeign();
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
143
208
|
const stale = parsed.e > 0 && parsed.e < Date.now();
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
209
|
+
const value = claimed<T>(parsed.v);
|
|
210
|
+
const tags = Reflect.get(parsed, "t");
|
|
211
|
+
return {
|
|
212
|
+
value,
|
|
213
|
+
stale,
|
|
214
|
+
expiresAt: parsed.e,
|
|
215
|
+
...(isStringArray(tags) && tags.length > 0 ? { tags } : {}),
|
|
216
|
+
};
|
|
149
217
|
}
|
|
150
218
|
|
|
151
219
|
/** Write the value envelope with a physical (grace-inclusive) TTL in seconds. */
|
|
152
220
|
async #writeEnvelope(
|
|
153
|
-
|
|
221
|
+
key: string,
|
|
154
222
|
value: unknown,
|
|
155
223
|
logicalTtlSeconds: number,
|
|
156
224
|
physicalTtlSeconds: number,
|
|
157
225
|
logicalExpiresAtOverride?: number,
|
|
226
|
+
tags: string[] = [],
|
|
158
227
|
): Promise<void> {
|
|
159
228
|
const client = await this.#client();
|
|
160
229
|
// An absolute override (e.g. `expire()` marking stale-now) wins over the
|
|
@@ -165,31 +234,38 @@ export class RedisDriver implements TaggableDriver {
|
|
|
165
234
|
: logicalTtlSeconds > 0
|
|
166
235
|
? Date.now() + logicalTtlSeconds * 1000
|
|
167
236
|
: 0;
|
|
168
|
-
const envelope: Envelope =
|
|
237
|
+
const envelope: Envelope =
|
|
238
|
+
tags.length > 0
|
|
239
|
+
? { v: value, e: expiresAt, t: tags }
|
|
240
|
+
: { v: value, e: expiresAt };
|
|
169
241
|
const serialized = JSON.stringify(envelope);
|
|
170
242
|
if (physicalTtlSeconds > 0) {
|
|
171
|
-
|
|
243
|
+
// Milliseconds, as upstream writes them (`PX`): `EX` takes whole
|
|
244
|
+
// seconds, so a '500ms' or '1.5s' TTL failed every write.
|
|
245
|
+
await client.set(
|
|
246
|
+
this.#valueKey(key),
|
|
247
|
+
serialized,
|
|
248
|
+
"PX",
|
|
249
|
+
Math.ceil(physicalTtlSeconds * 1000),
|
|
250
|
+
);
|
|
172
251
|
} else {
|
|
173
|
-
await client.set(
|
|
252
|
+
await client.set(this.#valueKey(key), serialized);
|
|
174
253
|
}
|
|
175
254
|
}
|
|
176
255
|
|
|
177
|
-
/** Drop a key from
|
|
178
|
-
async #dropTags(
|
|
256
|
+
/** Drop a key from every tag set it is in, and its reverse index. */
|
|
257
|
+
async #dropTags(key: string): Promise<void> {
|
|
179
258
|
const client = await this.#client();
|
|
259
|
+
const metaKey = this.#metaKey(key);
|
|
180
260
|
const prevTags = await client.smembers(metaKey);
|
|
181
261
|
for (const tag of prevTags) {
|
|
182
|
-
await client.srem(
|
|
262
|
+
await client.srem(this.#tagKey(tag), key);
|
|
183
263
|
}
|
|
184
264
|
if (prevTags.length > 0) await client.del(metaKey);
|
|
185
|
-
return prevTags;
|
|
186
265
|
}
|
|
187
266
|
|
|
188
267
|
async set(key: string, value: unknown, ttlSeconds?: number): Promise<void> {
|
|
189
|
-
|
|
190
|
-
await this.#dropTags(fullKey, this.#metaKey(key));
|
|
191
|
-
const ttl = ttlSeconds && ttlSeconds > 0 ? ttlSeconds : 0;
|
|
192
|
-
await this.#writeEnvelope(fullKey, value, ttl, ttl);
|
|
268
|
+
await this.setEntry(key, value, { ttlSeconds });
|
|
193
269
|
}
|
|
194
270
|
|
|
195
271
|
async setEntry(
|
|
@@ -197,10 +273,10 @@ export class RedisDriver implements TaggableDriver {
|
|
|
197
273
|
value: unknown,
|
|
198
274
|
options: DriverSetOptions,
|
|
199
275
|
): Promise<void> {
|
|
200
|
-
const fullKey = this.#key(key);
|
|
201
|
-
const metaKey = this.#metaKey(key);
|
|
202
276
|
const ttl =
|
|
203
|
-
options.ttlSeconds
|
|
277
|
+
options.ttlSeconds === undefined
|
|
278
|
+
? 0
|
|
279
|
+
: checkTtlSeconds(options.ttlSeconds);
|
|
204
280
|
const grace =
|
|
205
281
|
options.graceSeconds && options.graceSeconds > 0
|
|
206
282
|
? options.graceSeconds
|
|
@@ -208,44 +284,95 @@ export class RedisDriver implements TaggableDriver {
|
|
|
208
284
|
// With an absolute logical expiry (e.g. `expire()` marking stale-now), the
|
|
209
285
|
// ttl no longer drives physical retention — keep the value for the grace
|
|
210
286
|
// window measured from now so a stale-but-graced read still finds it.
|
|
211
|
-
|
|
287
|
+
// A negative TTL is the same thing: already expired, never immortal.
|
|
288
|
+
const override =
|
|
289
|
+
options.expiresAt ?? (ttl < 0 ? Date.now() - 1 : undefined);
|
|
212
290
|
const physical = override !== undefined ? grace : ttl > 0 ? ttl + grace : 0;
|
|
291
|
+
if (override !== undefined && physical === 0 && override <= Date.now()) {
|
|
292
|
+
// Expired, with no grace to keep it for. Written without a PX it
|
|
293
|
+
// would never leave; and the previous value must not survive it.
|
|
294
|
+
await this.delete(key);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
213
297
|
const tags = options.tags ?? [];
|
|
214
298
|
if (tags.length === 0) {
|
|
215
|
-
await this.#dropTags(
|
|
216
|
-
await this.#writeEnvelope(
|
|
299
|
+
await this.#dropTags(key);
|
|
300
|
+
await this.#writeEnvelope(key, value, ttl, physical, override);
|
|
217
301
|
return;
|
|
218
302
|
}
|
|
219
|
-
await this.#writeEnvelope(
|
|
303
|
+
await this.#writeEnvelope(key, value, ttl, physical, override, tags);
|
|
220
304
|
await this.#applyTags(key, tags, physical);
|
|
221
305
|
}
|
|
222
306
|
|
|
223
307
|
async flush(prefix?: string): Promise<void> {
|
|
308
|
+
if (prefix === undefined) {
|
|
309
|
+
// The whole store: everything under its prefix, bookkeeping included.
|
|
310
|
+
await this.#deleteUnder(this.#prefix);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
// A namespace: its keys leave the tag sets they are in first — those
|
|
314
|
+
// sets are shared with whatever else carries the same tag — then its
|
|
315
|
+
// values and reverse indexes go. Scanning values alone left the
|
|
316
|
+
// indexes behind, immortal when the entries were.
|
|
224
317
|
const client = await this.#client();
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
do {
|
|
232
|
-
const [nextCursor, keys] = await scan(
|
|
233
|
-
cursor,
|
|
234
|
-
"MATCH",
|
|
235
|
-
`${match}*`,
|
|
236
|
-
"COUNT",
|
|
237
|
-
100,
|
|
238
|
-
);
|
|
239
|
-
cursor = nextCursor;
|
|
240
|
-
if (keys.length > 0) {
|
|
241
|
-
await client.del(keys);
|
|
318
|
+
const metaPrefix = this.#metaKey(prefix);
|
|
319
|
+
await this.#scan(metaPrefix, async (metaKeys) => {
|
|
320
|
+
for (const metaKey of metaKeys) {
|
|
321
|
+
const key = metaKey.slice(this.#metaKey("").length);
|
|
322
|
+
for (const tag of await client.smembers(metaKey)) {
|
|
323
|
+
await client.srem(this.#tagKey(tag), key);
|
|
242
324
|
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
325
|
+
}
|
|
326
|
+
await client.del(...metaKeys);
|
|
327
|
+
});
|
|
328
|
+
await this.#deleteUnder(this.#valueKey(prefix));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Delete every key that starts with `literal`, walking a SCAN cursor.
|
|
333
|
+
*
|
|
334
|
+
* The prefix is escaped before it becomes a MATCH pattern: MATCH is a glob,
|
|
335
|
+
* so a namespace like `tenant-*` matched every tenant and a clear of it
|
|
336
|
+
* wiped them all. Each returned key is checked against the literal prefix
|
|
337
|
+
* as well, so a pattern the server reads differently still deletes nothing
|
|
338
|
+
* outside it. Upstream interpolates the prefix unescaped; this is stricter
|
|
339
|
+
* on purpose.
|
|
340
|
+
*/
|
|
341
|
+
async #deleteUnder(literal: string): Promise<void> {
|
|
342
|
+
const client = await this.#client();
|
|
343
|
+
await this.#scan(literal, async (keys) => {
|
|
344
|
+
await client.del(...keys);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Walk a SCAN cursor over the keys starting with `literal`, a page at a time. */
|
|
349
|
+
async #scan(
|
|
350
|
+
literal: string,
|
|
351
|
+
onPage: (keys: string[]) => Promise<void>,
|
|
352
|
+
): Promise<void> {
|
|
353
|
+
const client = await this.#client();
|
|
354
|
+
if (typeof client.scan !== "function") {
|
|
355
|
+
throw new EchoError(
|
|
356
|
+
"E_ECHO_SCAN_UNSUPPORTED",
|
|
246
357
|
"Echo: RedisDriver.flush() requires a client with scan() support. KEYS is not safe for production use.",
|
|
247
358
|
);
|
|
248
359
|
}
|
|
360
|
+
const pattern = `${escapeGlob(literal)}*`;
|
|
361
|
+
let cursor = "0";
|
|
362
|
+
do {
|
|
363
|
+
// Called ON the client: ioredis reads `this` inside scan(), so a
|
|
364
|
+
// detached reference threw and left every key in place.
|
|
365
|
+
const [nextCursor, keys] = await client.scan(
|
|
366
|
+
cursor,
|
|
367
|
+
"MATCH",
|
|
368
|
+
pattern,
|
|
369
|
+
"COUNT",
|
|
370
|
+
100,
|
|
371
|
+
);
|
|
372
|
+
cursor = nextCursor;
|
|
373
|
+
const owned = keys.filter((key) => key.startsWith(literal));
|
|
374
|
+
if (owned.length > 0) await onPage(owned);
|
|
375
|
+
} while (cursor !== "0");
|
|
249
376
|
}
|
|
250
377
|
|
|
251
378
|
async has(key: string): Promise<boolean> {
|
|
@@ -262,7 +389,9 @@ export class RedisDriver implements TaggableDriver {
|
|
|
262
389
|
physicalTtlSeconds: number,
|
|
263
390
|
): Promise<void> {
|
|
264
391
|
const client = await this.#client();
|
|
265
|
-
|
|
392
|
+
// EXPIRE takes whole seconds. Rounded UP, so an index never dies
|
|
393
|
+
// before the entry it points at.
|
|
394
|
+
const indexTtl = Math.ceil(physicalTtlSeconds);
|
|
266
395
|
const metaKey = this.#metaKey(key);
|
|
267
396
|
const oldTags = await client.smembers(metaKey);
|
|
268
397
|
const newTagSet = new Set(tags);
|
|
@@ -271,19 +400,32 @@ export class RedisDriver implements TaggableDriver {
|
|
|
271
400
|
const addedTags = tags.filter((t) => !oldTagSet.has(t));
|
|
272
401
|
|
|
273
402
|
for (const tag of removedTags) {
|
|
274
|
-
await client.srem(
|
|
403
|
+
await client.srem(this.#tagKey(tag), key);
|
|
275
404
|
}
|
|
276
405
|
|
|
277
406
|
for (const tag of tags) {
|
|
278
|
-
const tagKey =
|
|
407
|
+
const tagKey = this.#tagKey(tag);
|
|
408
|
+
// Read BEFORE the SADD. Redis answers -2 for no such key and -1 for
|
|
409
|
+
// a key with no expiry, and a set the SADD has just created reads
|
|
410
|
+
// -1 too: only this order tells a new set from a persistent one.
|
|
411
|
+
const currentTtl = await client.ttl(tagKey);
|
|
279
412
|
if (addedTags.includes(tag)) {
|
|
280
|
-
await client.sadd(tagKey,
|
|
413
|
+
await client.sadd(tagKey, key);
|
|
281
414
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
415
|
+
// A tag set must outlive every member it indexes. It took the TTL
|
|
416
|
+
// of the last member written with one, and an immortal member never
|
|
417
|
+
// cleared that TTL: once the set lapsed, deleteByTag found nobody
|
|
418
|
+
// and the immortal entry kept being served. So an immortal member
|
|
419
|
+
// makes the set persistent, and a finite member never puts an
|
|
420
|
+
// expiry back on a persistent set. A persistent set left holding
|
|
421
|
+
// only lapsed members is emptied by the next deleteByTag of its tag.
|
|
422
|
+
if (physicalTtlSeconds === 0) {
|
|
423
|
+
if (currentTtl >= 0) await client.persist(tagKey);
|
|
424
|
+
} else if (
|
|
425
|
+
currentTtl === -2 ||
|
|
426
|
+
(currentTtl >= 0 && indexTtl > currentTtl)
|
|
427
|
+
) {
|
|
428
|
+
await client.expire(tagKey, indexTtl);
|
|
287
429
|
}
|
|
288
430
|
}
|
|
289
431
|
|
|
@@ -293,7 +435,7 @@ export class RedisDriver implements TaggableDriver {
|
|
|
293
435
|
if (tags.length > 0) {
|
|
294
436
|
await client.sadd(metaKey, ...tags);
|
|
295
437
|
if (physicalTtlSeconds > 0) {
|
|
296
|
-
await client.expire(metaKey,
|
|
438
|
+
await client.expire(metaKey, indexTtl);
|
|
297
439
|
}
|
|
298
440
|
}
|
|
299
441
|
}
|
|
@@ -305,9 +447,7 @@ export class RedisDriver implements TaggableDriver {
|
|
|
305
447
|
tags: string[],
|
|
306
448
|
ttlSeconds?: number,
|
|
307
449
|
): Promise<void> {
|
|
308
|
-
|
|
309
|
-
await this.#writeEnvelope(this.#key(key), value, ttl, ttl);
|
|
310
|
-
await this.#applyTags(key, tags, ttl);
|
|
450
|
+
await this.setEntry(key, value, { ttlSeconds, tags });
|
|
311
451
|
}
|
|
312
452
|
|
|
313
453
|
/**
|
|
@@ -318,25 +458,15 @@ export class RedisDriver implements TaggableDriver {
|
|
|
318
458
|
async deleteByTag(tags: string[]): Promise<void> {
|
|
319
459
|
const client = await this.#client();
|
|
320
460
|
for (const tag of tags) {
|
|
321
|
-
const tagKey =
|
|
461
|
+
const tagKey = this.#tagKey(tag);
|
|
322
462
|
const members = await client.smembers(tagKey);
|
|
323
|
-
for (const
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
: fullKey;
|
|
327
|
-
const metaKey = this.#metaKey(userKey);
|
|
328
|
-
const allTags = await client.smembers(metaKey);
|
|
329
|
-
for (const otherTag of allTags) {
|
|
463
|
+
for (const key of members) {
|
|
464
|
+
const metaKey = this.#metaKey(key);
|
|
465
|
+
for (const otherTag of await client.smembers(metaKey)) {
|
|
330
466
|
if (otherTag === tag) continue;
|
|
331
|
-
|
|
332
|
-
await client.srem(otherTagKey, fullKey);
|
|
467
|
+
await client.srem(this.#tagKey(otherTag), key);
|
|
333
468
|
}
|
|
334
|
-
|
|
335
|
-
await client.del(metaKey);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
if (members.length > 0) {
|
|
339
|
-
await client.del(members);
|
|
469
|
+
await client.del(metaKey, this.#valueKey(key));
|
|
340
470
|
}
|
|
341
471
|
await client.del(tagKey);
|
|
342
472
|
}
|