@c9up/echo 0.2.2 → 0.2.4
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 +2 -0
- package/dist/CacheManager.d.ts.map +1 -1
- package/dist/CacheManager.js +92 -32
- 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 +28 -9
- package/dist/StoreManager.d.ts.map +1 -1
- package/dist/StoreManager.js +42 -18
- 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 +7 -0
- package/dist/configure.d.ts.map +1 -1
- package/dist/configure.js +5 -18
- 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 +36 -1
- package/dist/drivers/MemoryDriver.d.ts.map +1 -1
- package/dist/drivers/MemoryDriver.js +157 -62
- package/dist/drivers/MemoryDriver.js.map +1 -1
- package/dist/drivers/RedisDriver.d.ts +13 -8
- package/dist/drivers/RedisDriver.d.ts.map +1 -1
- package/dist/drivers/RedisDriver.js +171 -94
- 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 +27 -6
- 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/stubs.d.ts +14 -0
- package/dist/stubs.d.ts.map +1 -0
- package/dist/stubs.js +15 -0
- package/dist/stubs.js.map +1 -0
- package/dist/types.d.ts +12 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/vendor/quasarConnection.d.ts +26 -3
- package/dist/vendor/quasarConnection.d.ts.map +1 -1
- package/dist/vendor/quasarConnection.js +44 -23
- package/dist/vendor/quasarConnection.js.map +1 -1
- package/package.json +8 -3
- package/src/CacheManager.ts +117 -41
- package/src/EchoProvider.ts +45 -4
- package/src/StoreManager.ts +86 -31
- package/src/bytes.ts +38 -0
- package/src/claim.ts +22 -0
- package/src/configure.ts +13 -21
- 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 +205 -60
- package/src/drivers/RedisDriver.ts +205 -101
- 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 +44 -8
- package/src/services/main.ts +26 -8
- package/src/stubs.ts +16 -0
- package/src/types.ts +13 -2
- package/src/vendor/quasarConnection.ts +84 -22
- package/stubs/config/cache.stub +21 -0
|
@@ -3,22 +3,30 @@
|
|
|
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";
|
|
16
20
|
|
|
17
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Minimal Redis client interface, in ioredis' shape — a raw ioredis client
|
|
23
|
+
* satisfies it as is.
|
|
24
|
+
*/
|
|
18
25
|
export interface RedisClient {
|
|
19
26
|
get(key: string): Promise<string | null>;
|
|
20
27
|
set(key: string, value: string, ...args: unknown[]): Promise<unknown>;
|
|
21
|
-
|
|
28
|
+
/** Variadic, as ioredis declares it: a raw client then typechecks. */
|
|
29
|
+
del(...keys: string[]): Promise<number>;
|
|
22
30
|
exists(key: string): Promise<number>;
|
|
23
31
|
keys(pattern: string): Promise<string[]>;
|
|
24
32
|
sadd(key: string, ...members: string[]): Promise<number>;
|
|
@@ -38,6 +46,26 @@ export interface RedisClient {
|
|
|
38
46
|
interface Envelope {
|
|
39
47
|
v: unknown;
|
|
40
48
|
e: number;
|
|
49
|
+
/** Tags, so a tier above can promote the entry with them. */
|
|
50
|
+
t?: string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Escape Redis MATCH glob metacharacters so the pattern matches literally. */
|
|
54
|
+
function escapeGlob(literal: string): string {
|
|
55
|
+
return literal.replace(/[*?[\]\\]/g, "\\$&");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** JSON, or `undefined` when the text is not JSON at all. */
|
|
59
|
+
function parseJson(raw: string): unknown {
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(raw);
|
|
62
|
+
} catch {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isStringArray(x: unknown): x is string[] {
|
|
68
|
+
return Array.isArray(x) && x.every((item) => typeof item === "string");
|
|
41
69
|
}
|
|
42
70
|
|
|
43
71
|
function isEnvelope(x: unknown): x is Envelope {
|
|
@@ -65,10 +93,15 @@ export class RedisDriver implements TaggableDriver {
|
|
|
65
93
|
#resolved: RedisClient | undefined;
|
|
66
94
|
#pending: Promise<RedisClient> | undefined;
|
|
67
95
|
#prefix: string;
|
|
96
|
+
/** Reported once per driver: the condition, not each key, is the news. */
|
|
97
|
+
#foreignReported = false;
|
|
68
98
|
|
|
69
|
-
constructor(source: RedisClientSource, prefix
|
|
99
|
+
constructor(source: RedisClientSource, prefix?: string) {
|
|
70
100
|
this.#source = source;
|
|
71
|
-
|
|
101
|
+
// `||`, not a default parameter: an empty prefix made clear() scan
|
|
102
|
+
// `MATCH *` and delete every key in the database. Upstream falls back
|
|
103
|
+
// the same way (`config.prefix || "bentocache"`).
|
|
104
|
+
this.#prefix = prefix || "cache:";
|
|
72
105
|
}
|
|
73
106
|
|
|
74
107
|
/**
|
|
@@ -99,35 +132,47 @@ export class RedisDriver implements TaggableDriver {
|
|
|
99
132
|
return this.#pending;
|
|
100
133
|
}
|
|
101
134
|
|
|
102
|
-
|
|
103
|
-
|
|
135
|
+
/*
|
|
136
|
+
* Three disjoint sub-spaces under the store prefix: values (`v:`), tag
|
|
137
|
+
* sets (`t:`) and per-key reverse indexes (`m:`). They shared one keyspace
|
|
138
|
+
* before, so a cache key `tag:<t>` overwrote a tag SET with a string and a
|
|
139
|
+
* root key `meta:tags:<k>` sat in another entry's index slot: every
|
|
140
|
+
* invalidation through them then failed with WRONGTYPE, and the entry it
|
|
141
|
+
* was meant to drop kept being served. Any user key lands under `v:`, so
|
|
142
|
+
* none can reach the bookkeeping.
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
#valueKey(key: string): string {
|
|
146
|
+
return `${this.#prefix}v:${key}`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
#tagKey(tag: string): string {
|
|
150
|
+
return `${this.#prefix}t:${tag}`;
|
|
104
151
|
}
|
|
105
152
|
|
|
106
153
|
/**
|
|
107
|
-
* Reverse
|
|
108
|
-
* memberships
|
|
109
|
-
* belongs to.
|
|
154
|
+
* Reverse index of the tags `key` carries. Lets a retag clean stale
|
|
155
|
+
* memberships, and `delete()` drop the key from every tag set it is in.
|
|
110
156
|
*/
|
|
111
|
-
#metaKey(
|
|
112
|
-
return `${this.#prefix}
|
|
157
|
+
#metaKey(key: string): string {
|
|
158
|
+
return `${this.#prefix}m:${key}`;
|
|
113
159
|
}
|
|
114
160
|
|
|
115
161
|
async delete(key: string): Promise<boolean> {
|
|
116
162
|
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);
|
|
163
|
+
await this.#dropTags(key);
|
|
164
|
+
const count = await client.del(this.#valueKey(key));
|
|
128
165
|
return count > 0;
|
|
129
166
|
}
|
|
130
167
|
|
|
168
|
+
#reportForeign(): void {
|
|
169
|
+
if (this.#foreignReported) return;
|
|
170
|
+
this.#foreignReported = true;
|
|
171
|
+
process.stderr.write(
|
|
172
|
+
`[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`,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
131
176
|
async get<T = unknown>(key: string): Promise<T | null> {
|
|
132
177
|
const entry = await this.getEntry<T>(key);
|
|
133
178
|
if (entry === null || entry.stale) return null;
|
|
@@ -136,25 +181,36 @@ export class RedisDriver implements TaggableDriver {
|
|
|
136
181
|
|
|
137
182
|
async getEntry<T = unknown>(key: string): Promise<CacheEntry<T> | null> {
|
|
138
183
|
const client = await this.#client();
|
|
139
|
-
const raw = await client.get(this.#
|
|
184
|
+
const raw = await client.get(this.#valueKey(key));
|
|
140
185
|
if (raw === null) return null;
|
|
141
|
-
const parsed
|
|
142
|
-
if (!isEnvelope(parsed))
|
|
186
|
+
const parsed = parseJson(raw);
|
|
187
|
+
if (!isEnvelope(parsed)) {
|
|
188
|
+
// Not something this driver wrote — another application on the
|
|
189
|
+
// prefix, a manual SET, a torn write. A miss, so getOrSet computes
|
|
190
|
+
// and overwrites it; it used to throw on every read of the key,
|
|
191
|
+
// and getOrSet reads before it computes, so it never healed.
|
|
192
|
+
this.#reportForeign();
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
143
195
|
const stale = parsed.e > 0 && parsed.e < Date.now();
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
196
|
+
const value = claimed<T>(parsed.v);
|
|
197
|
+
const tags = Reflect.get(parsed, "t");
|
|
198
|
+
return {
|
|
199
|
+
value,
|
|
200
|
+
stale,
|
|
201
|
+
expiresAt: parsed.e,
|
|
202
|
+
...(isStringArray(tags) && tags.length > 0 ? { tags } : {}),
|
|
203
|
+
};
|
|
149
204
|
}
|
|
150
205
|
|
|
151
206
|
/** Write the value envelope with a physical (grace-inclusive) TTL in seconds. */
|
|
152
207
|
async #writeEnvelope(
|
|
153
|
-
|
|
208
|
+
key: string,
|
|
154
209
|
value: unknown,
|
|
155
210
|
logicalTtlSeconds: number,
|
|
156
211
|
physicalTtlSeconds: number,
|
|
157
212
|
logicalExpiresAtOverride?: number,
|
|
213
|
+
tags: string[] = [],
|
|
158
214
|
): Promise<void> {
|
|
159
215
|
const client = await this.#client();
|
|
160
216
|
// An absolute override (e.g. `expire()` marking stale-now) wins over the
|
|
@@ -165,31 +221,38 @@ export class RedisDriver implements TaggableDriver {
|
|
|
165
221
|
: logicalTtlSeconds > 0
|
|
166
222
|
? Date.now() + logicalTtlSeconds * 1000
|
|
167
223
|
: 0;
|
|
168
|
-
const envelope: Envelope =
|
|
224
|
+
const envelope: Envelope =
|
|
225
|
+
tags.length > 0
|
|
226
|
+
? { v: value, e: expiresAt, t: tags }
|
|
227
|
+
: { v: value, e: expiresAt };
|
|
169
228
|
const serialized = JSON.stringify(envelope);
|
|
170
229
|
if (physicalTtlSeconds > 0) {
|
|
171
|
-
|
|
230
|
+
// Milliseconds, as upstream writes them (`PX`): `EX` takes whole
|
|
231
|
+
// seconds, so a '500ms' or '1.5s' TTL failed every write.
|
|
232
|
+
await client.set(
|
|
233
|
+
this.#valueKey(key),
|
|
234
|
+
serialized,
|
|
235
|
+
"PX",
|
|
236
|
+
Math.ceil(physicalTtlSeconds * 1000),
|
|
237
|
+
);
|
|
172
238
|
} else {
|
|
173
|
-
await client.set(
|
|
239
|
+
await client.set(this.#valueKey(key), serialized);
|
|
174
240
|
}
|
|
175
241
|
}
|
|
176
242
|
|
|
177
|
-
/** Drop a key from
|
|
178
|
-
async #dropTags(
|
|
243
|
+
/** Drop a key from every tag set it is in, and its reverse index. */
|
|
244
|
+
async #dropTags(key: string): Promise<void> {
|
|
179
245
|
const client = await this.#client();
|
|
246
|
+
const metaKey = this.#metaKey(key);
|
|
180
247
|
const prevTags = await client.smembers(metaKey);
|
|
181
248
|
for (const tag of prevTags) {
|
|
182
|
-
await client.srem(
|
|
249
|
+
await client.srem(this.#tagKey(tag), key);
|
|
183
250
|
}
|
|
184
251
|
if (prevTags.length > 0) await client.del(metaKey);
|
|
185
|
-
return prevTags;
|
|
186
252
|
}
|
|
187
253
|
|
|
188
254
|
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);
|
|
255
|
+
await this.setEntry(key, value, { ttlSeconds });
|
|
193
256
|
}
|
|
194
257
|
|
|
195
258
|
async setEntry(
|
|
@@ -197,10 +260,10 @@ export class RedisDriver implements TaggableDriver {
|
|
|
197
260
|
value: unknown,
|
|
198
261
|
options: DriverSetOptions,
|
|
199
262
|
): Promise<void> {
|
|
200
|
-
const fullKey = this.#key(key);
|
|
201
|
-
const metaKey = this.#metaKey(key);
|
|
202
263
|
const ttl =
|
|
203
|
-
options.ttlSeconds
|
|
264
|
+
options.ttlSeconds === undefined
|
|
265
|
+
? 0
|
|
266
|
+
: checkTtlSeconds(options.ttlSeconds);
|
|
204
267
|
const grace =
|
|
205
268
|
options.graceSeconds && options.graceSeconds > 0
|
|
206
269
|
? options.graceSeconds
|
|
@@ -208,44 +271,95 @@ export class RedisDriver implements TaggableDriver {
|
|
|
208
271
|
// With an absolute logical expiry (e.g. `expire()` marking stale-now), the
|
|
209
272
|
// ttl no longer drives physical retention — keep the value for the grace
|
|
210
273
|
// window measured from now so a stale-but-graced read still finds it.
|
|
211
|
-
|
|
274
|
+
// A negative TTL is the same thing: already expired, never immortal.
|
|
275
|
+
const override =
|
|
276
|
+
options.expiresAt ?? (ttl < 0 ? Date.now() - 1 : undefined);
|
|
212
277
|
const physical = override !== undefined ? grace : ttl > 0 ? ttl + grace : 0;
|
|
278
|
+
if (override !== undefined && physical === 0 && override <= Date.now()) {
|
|
279
|
+
// Expired, with no grace to keep it for. Written without a PX it
|
|
280
|
+
// would never leave; and the previous value must not survive it.
|
|
281
|
+
await this.delete(key);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
213
284
|
const tags = options.tags ?? [];
|
|
214
285
|
if (tags.length === 0) {
|
|
215
|
-
await this.#dropTags(
|
|
216
|
-
await this.#writeEnvelope(
|
|
286
|
+
await this.#dropTags(key);
|
|
287
|
+
await this.#writeEnvelope(key, value, ttl, physical, override);
|
|
217
288
|
return;
|
|
218
289
|
}
|
|
219
|
-
await this.#writeEnvelope(
|
|
290
|
+
await this.#writeEnvelope(key, value, ttl, physical, override, tags);
|
|
220
291
|
await this.#applyTags(key, tags, physical);
|
|
221
292
|
}
|
|
222
293
|
|
|
223
294
|
async flush(prefix?: string): Promise<void> {
|
|
295
|
+
if (prefix === undefined) {
|
|
296
|
+
// The whole store: everything under its prefix, bookkeeping included.
|
|
297
|
+
await this.#deleteUnder(this.#prefix);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
// A namespace: its keys leave the tag sets they are in first — those
|
|
301
|
+
// sets are shared with whatever else carries the same tag — then its
|
|
302
|
+
// values and reverse indexes go. Scanning values alone left the
|
|
303
|
+
// indexes behind, immortal when the entries were.
|
|
224
304
|
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);
|
|
305
|
+
const metaPrefix = this.#metaKey(prefix);
|
|
306
|
+
await this.#scan(metaPrefix, async (metaKeys) => {
|
|
307
|
+
for (const metaKey of metaKeys) {
|
|
308
|
+
const key = metaKey.slice(this.#metaKey("").length);
|
|
309
|
+
for (const tag of await client.smembers(metaKey)) {
|
|
310
|
+
await client.srem(this.#tagKey(tag), key);
|
|
242
311
|
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
312
|
+
}
|
|
313
|
+
await client.del(...metaKeys);
|
|
314
|
+
});
|
|
315
|
+
await this.#deleteUnder(this.#valueKey(prefix));
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Delete every key that starts with `literal`, walking a SCAN cursor.
|
|
320
|
+
*
|
|
321
|
+
* The prefix is escaped before it becomes a MATCH pattern: MATCH is a glob,
|
|
322
|
+
* so a namespace like `tenant-*` matched every tenant and a clear of it
|
|
323
|
+
* wiped them all. Each returned key is checked against the literal prefix
|
|
324
|
+
* as well, so a pattern the server reads differently still deletes nothing
|
|
325
|
+
* outside it. Upstream interpolates the prefix unescaped; this is stricter
|
|
326
|
+
* on purpose.
|
|
327
|
+
*/
|
|
328
|
+
async #deleteUnder(literal: string): Promise<void> {
|
|
329
|
+
const client = await this.#client();
|
|
330
|
+
await this.#scan(literal, async (keys) => {
|
|
331
|
+
await client.del(...keys);
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** Walk a SCAN cursor over the keys starting with `literal`, a page at a time. */
|
|
336
|
+
async #scan(
|
|
337
|
+
literal: string,
|
|
338
|
+
onPage: (keys: string[]) => Promise<void>,
|
|
339
|
+
): Promise<void> {
|
|
340
|
+
const client = await this.#client();
|
|
341
|
+
if (typeof client.scan !== "function") {
|
|
342
|
+
throw new EchoError(
|
|
343
|
+
"E_ECHO_SCAN_UNSUPPORTED",
|
|
246
344
|
"Echo: RedisDriver.flush() requires a client with scan() support. KEYS is not safe for production use.",
|
|
247
345
|
);
|
|
248
346
|
}
|
|
347
|
+
const pattern = `${escapeGlob(literal)}*`;
|
|
348
|
+
let cursor = "0";
|
|
349
|
+
do {
|
|
350
|
+
// Called ON the client: ioredis reads `this` inside scan(), so a
|
|
351
|
+
// detached reference threw and left every key in place.
|
|
352
|
+
const [nextCursor, keys] = await client.scan(
|
|
353
|
+
cursor,
|
|
354
|
+
"MATCH",
|
|
355
|
+
pattern,
|
|
356
|
+
"COUNT",
|
|
357
|
+
100,
|
|
358
|
+
);
|
|
359
|
+
cursor = nextCursor;
|
|
360
|
+
const owned = keys.filter((key) => key.startsWith(literal));
|
|
361
|
+
if (owned.length > 0) await onPage(owned);
|
|
362
|
+
} while (cursor !== "0");
|
|
249
363
|
}
|
|
250
364
|
|
|
251
365
|
async has(key: string): Promise<boolean> {
|
|
@@ -262,7 +376,9 @@ export class RedisDriver implements TaggableDriver {
|
|
|
262
376
|
physicalTtlSeconds: number,
|
|
263
377
|
): Promise<void> {
|
|
264
378
|
const client = await this.#client();
|
|
265
|
-
|
|
379
|
+
// EXPIRE takes whole seconds. Rounded UP, so an index never dies
|
|
380
|
+
// before the entry it points at.
|
|
381
|
+
const indexTtl = Math.ceil(physicalTtlSeconds);
|
|
266
382
|
const metaKey = this.#metaKey(key);
|
|
267
383
|
const oldTags = await client.smembers(metaKey);
|
|
268
384
|
const newTagSet = new Set(tags);
|
|
@@ -271,18 +387,18 @@ export class RedisDriver implements TaggableDriver {
|
|
|
271
387
|
const addedTags = tags.filter((t) => !oldTagSet.has(t));
|
|
272
388
|
|
|
273
389
|
for (const tag of removedTags) {
|
|
274
|
-
await client.srem(
|
|
390
|
+
await client.srem(this.#tagKey(tag), key);
|
|
275
391
|
}
|
|
276
392
|
|
|
277
393
|
for (const tag of tags) {
|
|
278
|
-
const tagKey =
|
|
394
|
+
const tagKey = this.#tagKey(tag);
|
|
279
395
|
if (addedTags.includes(tag)) {
|
|
280
|
-
await client.sadd(tagKey,
|
|
396
|
+
await client.sadd(tagKey, key);
|
|
281
397
|
}
|
|
282
398
|
if (physicalTtlSeconds > 0) {
|
|
283
399
|
const currentTtl = await client.ttl(tagKey);
|
|
284
|
-
if (currentTtl < 0 ||
|
|
285
|
-
await client.expire(tagKey,
|
|
400
|
+
if (currentTtl < 0 || indexTtl > currentTtl) {
|
|
401
|
+
await client.expire(tagKey, indexTtl);
|
|
286
402
|
}
|
|
287
403
|
}
|
|
288
404
|
}
|
|
@@ -293,7 +409,7 @@ export class RedisDriver implements TaggableDriver {
|
|
|
293
409
|
if (tags.length > 0) {
|
|
294
410
|
await client.sadd(metaKey, ...tags);
|
|
295
411
|
if (physicalTtlSeconds > 0) {
|
|
296
|
-
await client.expire(metaKey,
|
|
412
|
+
await client.expire(metaKey, indexTtl);
|
|
297
413
|
}
|
|
298
414
|
}
|
|
299
415
|
}
|
|
@@ -305,9 +421,7 @@ export class RedisDriver implements TaggableDriver {
|
|
|
305
421
|
tags: string[],
|
|
306
422
|
ttlSeconds?: number,
|
|
307
423
|
): Promise<void> {
|
|
308
|
-
|
|
309
|
-
await this.#writeEnvelope(this.#key(key), value, ttl, ttl);
|
|
310
|
-
await this.#applyTags(key, tags, ttl);
|
|
424
|
+
await this.setEntry(key, value, { ttlSeconds, tags });
|
|
311
425
|
}
|
|
312
426
|
|
|
313
427
|
/**
|
|
@@ -318,25 +432,15 @@ export class RedisDriver implements TaggableDriver {
|
|
|
318
432
|
async deleteByTag(tags: string[]): Promise<void> {
|
|
319
433
|
const client = await this.#client();
|
|
320
434
|
for (const tag of tags) {
|
|
321
|
-
const tagKey =
|
|
435
|
+
const tagKey = this.#tagKey(tag);
|
|
322
436
|
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) {
|
|
437
|
+
for (const key of members) {
|
|
438
|
+
const metaKey = this.#metaKey(key);
|
|
439
|
+
for (const otherTag of await client.smembers(metaKey)) {
|
|
330
440
|
if (otherTag === tag) continue;
|
|
331
|
-
|
|
332
|
-
await client.srem(otherTagKey, fullKey);
|
|
441
|
+
await client.srem(this.#tagKey(otherTag), key);
|
|
333
442
|
}
|
|
334
|
-
|
|
335
|
-
await client.del(metaKey);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
if (members.length > 0) {
|
|
339
|
-
await client.del(members);
|
|
443
|
+
await client.del(metaKey, this.#valueKey(key));
|
|
340
444
|
}
|
|
341
445
|
await client.del(tagKey);
|
|
342
446
|
}
|