@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.
Files changed (109) hide show
  1. package/README.md +1 -1
  2. package/dist/CacheManager.d.ts +13 -2
  3. package/dist/CacheManager.d.ts.map +1 -1
  4. package/dist/CacheManager.js +309 -56
  5. package/dist/CacheManager.js.map +1 -1
  6. package/dist/EchoProvider.d.ts.map +1 -1
  7. package/dist/EchoProvider.js +32 -3
  8. package/dist/EchoProvider.js.map +1 -1
  9. package/dist/StoreManager.d.ts +38 -11
  10. package/dist/StoreManager.d.ts.map +1 -1
  11. package/dist/StoreManager.js +80 -22
  12. package/dist/StoreManager.js.map +1 -1
  13. package/dist/bytes.d.ts +13 -0
  14. package/dist/bytes.d.ts.map +1 -0
  15. package/dist/bytes.js +35 -0
  16. package/dist/bytes.js.map +1 -0
  17. package/dist/claim.d.ts +15 -0
  18. package/dist/claim.d.ts.map +1 -0
  19. package/dist/claim.js +10 -0
  20. package/dist/claim.js.map +1 -0
  21. package/dist/configure.d.ts +1 -0
  22. package/dist/configure.d.ts.map +1 -1
  23. package/dist/configure.js +3 -0
  24. package/dist/configure.js.map +1 -1
  25. package/dist/console/cacheClear.d.ts +13 -0
  26. package/dist/console/cacheClear.d.ts.map +1 -0
  27. package/dist/console/cacheClear.js +69 -0
  28. package/dist/console/cacheClear.js.map +1 -0
  29. package/dist/console/cacheDelete.d.ts +11 -0
  30. package/dist/console/cacheDelete.d.ts.map +1 -0
  31. package/dist/console/cacheDelete.js +42 -0
  32. package/dist/console/cacheDelete.js.map +1 -0
  33. package/dist/console/cachePrune.d.ts +9 -0
  34. package/dist/console/cachePrune.d.ts.map +1 -0
  35. package/dist/console/cachePrune.js +35 -0
  36. package/dist/console/cachePrune.js.map +1 -0
  37. package/dist/console/contract.d.ts +64 -0
  38. package/dist/console/contract.d.ts.map +1 -0
  39. package/dist/console/contract.js +32 -0
  40. package/dist/console/contract.js.map +1 -0
  41. package/dist/console/index.d.ts +25 -0
  42. package/dist/console/index.d.ts.map +1 -0
  43. package/dist/console/index.js +38 -0
  44. package/dist/console/index.js.map +1 -0
  45. package/dist/console/store.d.ts +14 -0
  46. package/dist/console/store.d.ts.map +1 -0
  47. package/dist/console/store.js +35 -0
  48. package/dist/console/store.js.map +1 -0
  49. package/dist/drivers/MemoryDriver.d.ts +37 -1
  50. package/dist/drivers/MemoryDriver.d.ts.map +1 -1
  51. package/dist/drivers/MemoryDriver.js +170 -72
  52. package/dist/drivers/MemoryDriver.js.map +1 -1
  53. package/dist/drivers/RedisDriver.d.ts +15 -8
  54. package/dist/drivers/RedisDriver.d.ts.map +1 -1
  55. package/dist/drivers/RedisDriver.js +198 -97
  56. package/dist/drivers/RedisDriver.js.map +1 -1
  57. package/dist/drivers/TieredDriver.d.ts +42 -0
  58. package/dist/drivers/TieredDriver.d.ts.map +1 -1
  59. package/dist/drivers/TieredDriver.js +114 -9
  60. package/dist/drivers/TieredDriver.js.map +1 -1
  61. package/dist/duration.d.ts +13 -3
  62. package/dist/duration.d.ts.map +1 -1
  63. package/dist/duration.js +22 -7
  64. package/dist/duration.js.map +1 -1
  65. package/dist/errors.d.ts +60 -2
  66. package/dist/errors.d.ts.map +1 -1
  67. package/dist/errors.js +81 -6
  68. package/dist/errors.js.map +1 -1
  69. package/dist/index.d.ts +4 -4
  70. package/dist/index.d.ts.map +1 -1
  71. package/dist/index.js +3 -3
  72. package/dist/index.js.map +1 -1
  73. package/dist/quasar.d.ts +1 -1
  74. package/dist/quasar.d.ts.map +1 -1
  75. package/dist/quasar.js +106 -19
  76. package/dist/quasar.js.map +1 -1
  77. package/dist/services/main.d.ts +1 -1
  78. package/dist/services/main.d.ts.map +1 -1
  79. package/dist/services/main.js +22 -5
  80. package/dist/services/main.js.map +1 -1
  81. package/dist/types.d.ts +44 -2
  82. package/dist/types.d.ts.map +1 -1
  83. package/dist/vendor/quasarConnection.d.ts +44 -3
  84. package/dist/vendor/quasarConnection.d.ts.map +1 -1
  85. package/dist/vendor/quasarConnection.js +64 -23
  86. package/dist/vendor/quasarConnection.js.map +1 -1
  87. package/package.json +5 -1
  88. package/src/CacheManager.ts +367 -69
  89. package/src/EchoProvider.ts +45 -4
  90. package/src/StoreManager.ts +131 -35
  91. package/src/bytes.ts +38 -0
  92. package/src/claim.ts +22 -0
  93. package/src/configure.ts +4 -0
  94. package/src/console/cacheClear.ts +96 -0
  95. package/src/console/cacheDelete.ts +65 -0
  96. package/src/console/cachePrune.ts +57 -0
  97. package/src/console/contract.ts +93 -0
  98. package/src/console/index.ts +61 -0
  99. package/src/console/store.ts +47 -0
  100. package/src/drivers/MemoryDriver.ts +221 -69
  101. package/src/drivers/RedisDriver.ts +234 -104
  102. package/src/drivers/TieredDriver.ts +169 -9
  103. package/src/duration.ts +28 -7
  104. package/src/errors.ts +103 -6
  105. package/src/index.ts +17 -3
  106. package/src/quasar.ts +145 -30
  107. package/src/services/main.ts +26 -8
  108. package/src/types.ts +46 -7
  109. 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 instance implementing the minimal interface below.
7
- * Compatible with ioredis and redis (node-redis) clients.
6
+ * Requires a Redis client implementing the minimal interface below, which a
7
+ * raw ioredis client satisfies.
8
8
  *
9
- * STORAGE FORMAT (breaking vs echo <=0.1.5): values are stored as a JSON
10
- * envelope `{ "v": <value>, "e": <logicalExpiryEpochMs> }`. The physical Redis
11
- * TTL (`EX`) covers `ttl + grace` so a logically-expired value survives for the
12
- * grace window; `e` records the logical expiry so reads can flag it stale.
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
- /** Minimal Redis client interface — compatible with ioredis and node-redis. */
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
- del(key: string | string[]): Promise<number>;
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 = "cache:") {
102
+ constructor(source: RedisClientSource, prefix?: string) {
70
103
  this.#source = source;
71
- this.#prefix = prefix;
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
- #key(k: string): string {
103
- return `${this.#prefix}${k}`;
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-index for per-key tag membership. Lets tag writes clean stale
108
- * memberships on retag, and `delete()` drop the key from every tag-set it
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(k: string): string {
112
- return `${this.#prefix}meta:tags:${k}`;
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
- const fullKey = this.#key(key);
118
- const metaKey = this.#metaKey(key);
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.#key(key));
197
+ const raw = await client.get(this.#valueKey(key));
140
198
  if (raw === null) return null;
141
- const parsed: unknown = JSON.parse(raw);
142
- if (!isEnvelope(parsed)) return null;
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
- // Deserialize boundary: the on-the-wire value is genuinely `unknown`; the
145
- // caller's generic `T` is the assertion. This is the single unavoidable
146
- // cast site (mirrors echo <=0.1.5 `JSON.parse(raw) as T`).
147
- const value = parsed.v as T;
148
- return { value, stale, expiresAt: parsed.e };
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
- fullKey: string,
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 = { v: value, e: expiresAt };
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
- await client.set(fullKey, serialized, "EX", physicalTtlSeconds);
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(fullKey, serialized);
252
+ await client.set(this.#valueKey(key), serialized);
174
253
  }
175
254
  }
176
255
 
177
- /** Drop a key from its previous tag memberships (reconcile before re-tagging). */
178
- async #dropTags(fullKey: string, metaKey: string): Promise<string[]> {
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(`${this.#prefix}tag:${tag}`, fullKey);
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
- const fullKey = this.#key(key);
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 && options.ttlSeconds > 0 ? options.ttlSeconds : 0;
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
- const override = options.expiresAt;
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(fullKey, metaKey);
216
- await this.#writeEnvelope(fullKey, value, ttl, physical, override);
299
+ await this.#dropTags(key);
300
+ await this.#writeEnvelope(key, value, ttl, physical, override);
217
301
  return;
218
302
  }
219
- await this.#writeEnvelope(fullKey, value, ttl, physical, override);
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 scan = client.scan;
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 ?? ""}`;
230
- let cursor = "0";
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
- } while (cursor !== "0");
244
- } else {
245
- throw new Error(
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
- const fullKey = this.#key(key);
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(`${this.#prefix}tag:${tag}`, fullKey);
403
+ await client.srem(this.#tagKey(tag), key);
275
404
  }
276
405
 
277
406
  for (const tag of tags) {
278
- const tagKey = `${this.#prefix}tag:${tag}`;
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, fullKey);
413
+ await client.sadd(tagKey, key);
281
414
  }
282
- if (physicalTtlSeconds > 0) {
283
- const currentTtl = await client.ttl(tagKey);
284
- if (currentTtl < 0 || physicalTtlSeconds > currentTtl) {
285
- await client.expire(tagKey, physicalTtlSeconds);
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, physicalTtlSeconds);
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
- const ttl = ttlSeconds && ttlSeconds > 0 ? ttlSeconds : 0;
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 = `${this.#prefix}tag:${tag}`;
461
+ const tagKey = this.#tagKey(tag);
322
462
  const members = await client.smembers(tagKey);
323
- for (const fullKey of members) {
324
- const userKey = fullKey.startsWith(this.#prefix)
325
- ? fullKey.slice(this.#prefix.length)
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
- const otherTagKey = `${this.#prefix}tag:${otherTag}`;
332
- await client.srem(otherTagKey, fullKey);
467
+ await client.srem(this.#tagKey(otherTag), key);
333
468
  }
334
- if (allTags.length > 0) {
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
  }