@postgresx/noredis 0.1.0 → 0.6.0
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 +625 -25
- package/dist/adapters/ioredis.d.ts +130 -0
- package/dist/adapters/ioredis.js +141 -0
- package/dist/adapters/node.d.ts +1 -1
- package/dist/adapters/redis.d.ts +143 -0
- package/dist/adapters/redis.js +118 -0
- package/dist/adapters/web.d.ts +30 -0
- package/dist/adapters/web.js +64 -0
- package/dist/chunk-3DNTNHVD.js +295 -0
- package/dist/chunk-5ZGPCW5N.js +288 -0
- package/dist/chunk-HW3WONAC.js +239 -0
- package/dist/chunk-QI73S2FT.js +101 -0
- package/dist/chunk-RWRM5RVO.js +30 -0
- package/dist/{chunk-GBFG2TOL.js → chunk-VLFUE33D.js} +264 -16
- package/dist/chunk-WRMEEA6T.js +246 -0
- package/dist/chunk-YSWHEFPX.js +328 -0
- package/dist/client-DP3ssToT.d.ts +509 -0
- package/dist/hash-index.d.ts +43 -0
- package/dist/hash-index.js +9 -0
- package/dist/index.d.ts +16 -403
- package/dist/index.js +448 -988
- package/dist/kv-cache.d.ts +38 -2
- package/dist/kv-cache.js +1 -1
- package/dist/kv.d.ts +1 -0
- package/dist/kv.js +8 -0
- package/dist/list-index.d.ts +53 -0
- package/dist/list-index.js +9 -0
- package/dist/{pubsub-CHGs5dXU.d.ts → pubsub-index.d.ts} +1 -1
- package/dist/pubsub-index.js +12 -0
- package/dist/set-index.d.ts +42 -0
- package/dist/set-index.js +9 -0
- package/dist/sorted-set-index.d.ts +58 -0
- package/dist/sorted-set-index.js +9 -0
- package/package.json +54 -4
|
@@ -52,6 +52,15 @@ function normalizeTtlMs(ttlMs) {
|
|
|
52
52
|
function escapeLike(value) {
|
|
53
53
|
return value.replace(/[\\%_]/g, (match) => `\\${match}`);
|
|
54
54
|
}
|
|
55
|
+
function redisPatternToSqlLike(pattern) {
|
|
56
|
+
let sqlPattern = "";
|
|
57
|
+
for (const char of pattern) {
|
|
58
|
+
if (char === "*") sqlPattern += "%";
|
|
59
|
+
else if (char === "?") sqlPattern += "_";
|
|
60
|
+
else sqlPattern += escapeLike(char);
|
|
61
|
+
}
|
|
62
|
+
return sqlPattern;
|
|
63
|
+
}
|
|
55
64
|
var PgKvCache = class {
|
|
56
65
|
namespace;
|
|
57
66
|
tableName;
|
|
@@ -64,6 +73,7 @@ var PgKvCache = class {
|
|
|
64
73
|
l1Max;
|
|
65
74
|
l1TtlMs;
|
|
66
75
|
notifyEnabled;
|
|
76
|
+
serializer;
|
|
67
77
|
l1 = /* @__PURE__ */ new Map();
|
|
68
78
|
constructor(options) {
|
|
69
79
|
this.sql = options.sql;
|
|
@@ -77,6 +87,10 @@ var PgKvCache = class {
|
|
|
77
87
|
this.l1TtlMs = options.l1 && options.l1.ttlMs !== void 0 ? options.l1.ttlMs : DEFAULT_L1_TTL_MS;
|
|
78
88
|
this.notifyEnabled = options.notify !== false && options.notify?.enabled !== false;
|
|
79
89
|
this.notifyChannel = options.notify && options.notify.channel ? options.notify.channel : DEFAULT_NOTIFY_CHANNEL;
|
|
90
|
+
this.serializer = options.serializer ?? {
|
|
91
|
+
serialize: (value) => value,
|
|
92
|
+
deserialize: (value) => value
|
|
93
|
+
};
|
|
80
94
|
}
|
|
81
95
|
async ensureSchema(options = {}) {
|
|
82
96
|
const persistence = options.unlogged === false ? "" : "UNLOGGED ";
|
|
@@ -117,7 +131,7 @@ var PgKvCache = class {
|
|
|
117
131
|
this.deleteL1(key);
|
|
118
132
|
return null;
|
|
119
133
|
}
|
|
120
|
-
const value =
|
|
134
|
+
const value = this.deserialize(row.value);
|
|
121
135
|
this.setL1(key, value, rowExpiresAt(row));
|
|
122
136
|
return value;
|
|
123
137
|
}
|
|
@@ -144,7 +158,7 @@ var PgKvCache = class {
|
|
|
144
158
|
);
|
|
145
159
|
for (const row of rows) {
|
|
146
160
|
if (!row.key) continue;
|
|
147
|
-
const value =
|
|
161
|
+
const value = this.deserialize(row.value);
|
|
148
162
|
result.set(row.key, value);
|
|
149
163
|
this.setL1(row.key, value, rowExpiresAt(row));
|
|
150
164
|
}
|
|
@@ -155,23 +169,43 @@ var PgKvCache = class {
|
|
|
155
169
|
}
|
|
156
170
|
async set(key, value, options = {}) {
|
|
157
171
|
const ttlMs = normalizeTtlMs(options.ttlMs);
|
|
158
|
-
|
|
172
|
+
const mode = this.resolveSetMode(options);
|
|
173
|
+
const serialized = jsonValue(this.serializer.serialize(value));
|
|
174
|
+
const rows = mode === "xx" ? await this.sql.unsafe(
|
|
175
|
+
`UPDATE ${this.quotedTableName}
|
|
176
|
+
SET value = $3::jsonb,
|
|
177
|
+
expires_at = CASE WHEN $4::bigint IS NULL THEN NULL ELSE NOW() + ($4::bigint * INTERVAL '1 millisecond') END,
|
|
178
|
+
updated_at = NOW()
|
|
179
|
+
WHERE namespace = $1
|
|
180
|
+
AND key = $2
|
|
181
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
182
|
+
RETURNING key`,
|
|
183
|
+
[this.namespace, key, serialized, ttlMs]
|
|
184
|
+
) : await this.sql.unsafe(
|
|
159
185
|
`INSERT INTO ${this.quotedTableName} (namespace, key, value, expires_at, updated_at)
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
186
|
+
VALUES (
|
|
187
|
+
$1,
|
|
188
|
+
$2,
|
|
189
|
+
$3::jsonb,
|
|
190
|
+
CASE WHEN $4::bigint IS NULL THEN NULL ELSE NOW() + ($4::bigint * INTERVAL '1 millisecond') END,
|
|
191
|
+
NOW()
|
|
192
|
+
)
|
|
193
|
+
ON CONFLICT (namespace, key) DO UPDATE
|
|
194
|
+
SET value = EXCLUDED.value,
|
|
195
|
+
expires_at = EXCLUDED.expires_at,
|
|
196
|
+
updated_at = NOW()
|
|
197
|
+
${mode === "nx" ? `WHERE ${this.quotedTableName}.expires_at IS NOT NULL AND ${this.quotedTableName}.expires_at <= NOW()` : ""}
|
|
198
|
+
RETURNING key`,
|
|
199
|
+
[this.namespace, key, serialized, ttlMs]
|
|
172
200
|
);
|
|
201
|
+
const written = rows.length > 0;
|
|
202
|
+
if (!written) {
|
|
203
|
+
if (mode === "xx") this.deleteL1(key);
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
173
206
|
this.setL1(key, value, ttlMs === null ? null : this.now() + ttlMs);
|
|
174
207
|
if (options.notify !== false) await this.publish({ op: "set", key });
|
|
208
|
+
return true;
|
|
175
209
|
}
|
|
176
210
|
async mset(entries, options = {}) {
|
|
177
211
|
const values = Array.from(entries);
|
|
@@ -180,7 +214,7 @@ var PgKvCache = class {
|
|
|
180
214
|
const params = [];
|
|
181
215
|
const groups = values.map(([key, value], index) => {
|
|
182
216
|
const base = index * 4;
|
|
183
|
-
params.push(this.namespace, key, jsonValue(value), ttlMs);
|
|
217
|
+
params.push(this.namespace, key, jsonValue(this.serializer.serialize(value)), ttlMs);
|
|
184
218
|
return `($${base + 1}, $${base + 2}, $${base + 3}::jsonb, CASE WHEN $${base + 4}::bigint IS NULL THEN NULL ELSE NOW() + ($${base + 4}::bigint * INTERVAL '1 millisecond') END, NOW())`;
|
|
185
219
|
}).join(", ");
|
|
186
220
|
await this.sql.unsafe(
|
|
@@ -249,6 +283,116 @@ var PgKvCache = class {
|
|
|
249
283
|
this.cleanupL1();
|
|
250
284
|
return rows.length;
|
|
251
285
|
}
|
|
286
|
+
async compareAndSwap(key, expected, next, options = {}) {
|
|
287
|
+
const ttlMs = normalizeTtlMs(options.ttlMs);
|
|
288
|
+
const nextValue = jsonValue(this.serializer.serialize(next));
|
|
289
|
+
const expectedValue = expected === null ? null : jsonValue(this.serializer.serialize(expected));
|
|
290
|
+
const expectedMissing = options.expectedMissing ?? expected === null;
|
|
291
|
+
const rows = expectedMissing ? await this.sql.unsafe(
|
|
292
|
+
`INSERT INTO ${this.quotedTableName} (namespace, key, value, expires_at, updated_at)
|
|
293
|
+
VALUES (
|
|
294
|
+
$1,
|
|
295
|
+
$2,
|
|
296
|
+
$3::jsonb,
|
|
297
|
+
CASE WHEN $4::bigint IS NULL THEN NULL ELSE NOW() + ($4::bigint * INTERVAL '1 millisecond') END,
|
|
298
|
+
NOW()
|
|
299
|
+
)
|
|
300
|
+
ON CONFLICT (namespace, key) DO UPDATE
|
|
301
|
+
SET value = EXCLUDED.value,
|
|
302
|
+
expires_at = EXCLUDED.expires_at,
|
|
303
|
+
updated_at = NOW()
|
|
304
|
+
WHERE ${this.quotedTableName}.expires_at IS NOT NULL AND ${this.quotedTableName}.expires_at <= NOW()
|
|
305
|
+
RETURNING key`,
|
|
306
|
+
[this.namespace, key, nextValue, ttlMs]
|
|
307
|
+
) : await this.sql.unsafe(
|
|
308
|
+
`UPDATE ${this.quotedTableName}
|
|
309
|
+
SET value = $4::jsonb,
|
|
310
|
+
expires_at = CASE WHEN $5::bigint IS NULL THEN NULL ELSE NOW() + ($5::bigint * INTERVAL '1 millisecond') END,
|
|
311
|
+
updated_at = NOW()
|
|
312
|
+
WHERE namespace = $1
|
|
313
|
+
AND key = $2
|
|
314
|
+
AND value = $3::jsonb
|
|
315
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
316
|
+
RETURNING key`,
|
|
317
|
+
[this.namespace, key, expectedValue, nextValue, ttlMs]
|
|
318
|
+
);
|
|
319
|
+
const written = rows.length > 0;
|
|
320
|
+
if (!written) {
|
|
321
|
+
this.deleteL1(key);
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
this.setL1(key, next, ttlMs === null ? null : this.now() + ttlMs);
|
|
325
|
+
if (options.notify !== false) await this.publish({ op: "set", key });
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
async expire(key, ttlMs, options = {}) {
|
|
329
|
+
const normalizedTtlMs = normalizeTtlMs(ttlMs) ?? 0;
|
|
330
|
+
const rows = await this.sql.unsafe(
|
|
331
|
+
`UPDATE ${this.quotedTableName}
|
|
332
|
+
SET expires_at = NOW() + ($3::bigint * INTERVAL '1 millisecond'),
|
|
333
|
+
updated_at = NOW()
|
|
334
|
+
WHERE namespace = $1
|
|
335
|
+
AND key = $2
|
|
336
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
337
|
+
RETURNING key, value, expires_at`,
|
|
338
|
+
[this.namespace, key, normalizedTtlMs]
|
|
339
|
+
);
|
|
340
|
+
const row = rows[0];
|
|
341
|
+
if (!row) {
|
|
342
|
+
this.deleteL1(key);
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
this.setL1(key, this.deserialize(row.value), rowExpiresAt(row));
|
|
346
|
+
if (options.notify !== false) await this.publish({ op: "set", key });
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
async persist(key, options = {}) {
|
|
350
|
+
const rows = await this.sql.unsafe(
|
|
351
|
+
`UPDATE ${this.quotedTableName}
|
|
352
|
+
SET expires_at = NULL,
|
|
353
|
+
updated_at = NOW()
|
|
354
|
+
WHERE namespace = $1
|
|
355
|
+
AND key = $2
|
|
356
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
357
|
+
RETURNING key, value, expires_at`,
|
|
358
|
+
[this.namespace, key]
|
|
359
|
+
);
|
|
360
|
+
const row = rows[0];
|
|
361
|
+
if (!row) {
|
|
362
|
+
this.deleteL1(key);
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
this.setL1(key, this.deserialize(row.value), null);
|
|
366
|
+
if (options.notify !== false) await this.publish({ op: "set", key });
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
async touch(key) {
|
|
370
|
+
const rows = await this.sql.unsafe(
|
|
371
|
+
`UPDATE ${this.quotedTableName}
|
|
372
|
+
SET updated_at = NOW()
|
|
373
|
+
WHERE namespace = $1
|
|
374
|
+
AND key = $2
|
|
375
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
376
|
+
RETURNING key`,
|
|
377
|
+
[this.namespace, key]
|
|
378
|
+
);
|
|
379
|
+
return rows.length > 0;
|
|
380
|
+
}
|
|
381
|
+
async ttl(key) {
|
|
382
|
+
const rows = await this.sql.unsafe(
|
|
383
|
+
`SELECT CASE
|
|
384
|
+
WHEN expires_at IS NULL THEN NULL
|
|
385
|
+
ELSE GREATEST(0, FLOOR(EXTRACT(EPOCH FROM (expires_at - NOW())) * 1000))::bigint
|
|
386
|
+
END AS ttl_ms
|
|
387
|
+
FROM ${this.quotedTableName}
|
|
388
|
+
WHERE namespace = $1
|
|
389
|
+
AND key = $2
|
|
390
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
391
|
+
LIMIT 1`,
|
|
392
|
+
[this.namespace, key]
|
|
393
|
+
);
|
|
394
|
+
return rows[0]?.ttl_ms === null || rows[0]?.ttl_ms === void 0 ? null : Number(rows[0].ttl_ms);
|
|
395
|
+
}
|
|
252
396
|
invalidate(key) {
|
|
253
397
|
this.deleteL1(key);
|
|
254
398
|
}
|
|
@@ -272,6 +416,101 @@ var PgKvCache = class {
|
|
|
272
416
|
}
|
|
273
417
|
return false;
|
|
274
418
|
}
|
|
419
|
+
async keys(pattern = "*", limit = 1e3) {
|
|
420
|
+
const like = redisPatternToSqlLike(pattern);
|
|
421
|
+
const rows = await this.sql.unsafe(
|
|
422
|
+
`SELECT key FROM ${this.quotedTableName}
|
|
423
|
+
WHERE namespace = $1 AND key LIKE $2 ESCAPE '\\'
|
|
424
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
425
|
+
ORDER BY key ASC LIMIT $3`,
|
|
426
|
+
[this.namespace, like, Math.max(1, Math.floor(limit))]
|
|
427
|
+
);
|
|
428
|
+
return rows.map((r) => r.key);
|
|
429
|
+
}
|
|
430
|
+
async scan(cursor = null, count = 100, pattern = "*") {
|
|
431
|
+
const like = redisPatternToSqlLike(pattern);
|
|
432
|
+
const limit = Math.max(1, Math.floor(count));
|
|
433
|
+
const rows = await this.sql.unsafe(
|
|
434
|
+
`SELECT key FROM ${this.quotedTableName}
|
|
435
|
+
WHERE namespace = $1 AND key LIKE $2 ESCAPE '\\'
|
|
436
|
+
AND key > COALESCE($3::text, '')
|
|
437
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
438
|
+
ORDER BY key ASC LIMIT $4`,
|
|
439
|
+
[this.namespace, like, cursor, limit]
|
|
440
|
+
);
|
|
441
|
+
return {
|
|
442
|
+
cursor: rows.length === limit ? rows[rows.length - 1].key : null,
|
|
443
|
+
keys: rows.map((r) => r.key)
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
async rename(key, newKey) {
|
|
447
|
+
if (key === newKey) return await this.get(key) !== null;
|
|
448
|
+
const rows = await this.sql.unsafe(
|
|
449
|
+
`WITH source AS (
|
|
450
|
+
SELECT key
|
|
451
|
+
FROM ${this.quotedTableName}
|
|
452
|
+
WHERE namespace = $1
|
|
453
|
+
AND key = $2
|
|
454
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
455
|
+
LIMIT 1
|
|
456
|
+
), deleted_target AS (
|
|
457
|
+
DELETE FROM ${this.quotedTableName}
|
|
458
|
+
WHERE namespace = $1
|
|
459
|
+
AND key = $3
|
|
460
|
+
AND EXISTS (SELECT 1 FROM source)
|
|
461
|
+
)
|
|
462
|
+
UPDATE ${this.quotedTableName}
|
|
463
|
+
SET key = $3,
|
|
464
|
+
updated_at = NOW()
|
|
465
|
+
WHERE namespace = $1
|
|
466
|
+
AND key = $2
|
|
467
|
+
AND EXISTS (SELECT 1 FROM source)
|
|
468
|
+
RETURNING key`,
|
|
469
|
+
[this.namespace, key, newKey]
|
|
470
|
+
);
|
|
471
|
+
if (rows.length === 0) return false;
|
|
472
|
+
this.deleteL1(key);
|
|
473
|
+
this.deleteL1(newKey);
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
async type(key) {
|
|
477
|
+
const value = await this.get(key);
|
|
478
|
+
return value === null ? "none" : "string";
|
|
479
|
+
}
|
|
480
|
+
async unlink(...keys) {
|
|
481
|
+
if (keys.length === 0) return 0;
|
|
482
|
+
const placeholders = keys.map((_, i) => `$${i + 2}`).join(", ");
|
|
483
|
+
const rows = await this.sql.unsafe(
|
|
484
|
+
`DELETE FROM ${this.quotedTableName}
|
|
485
|
+
WHERE namespace = $1 AND key IN (${placeholders})
|
|
486
|
+
RETURNING key`,
|
|
487
|
+
[this.namespace, ...keys]
|
|
488
|
+
);
|
|
489
|
+
for (const r of rows) this.deleteL1(r.key);
|
|
490
|
+
return rows.length;
|
|
491
|
+
}
|
|
492
|
+
async setex(key, seconds, value) {
|
|
493
|
+
await this.set(key, value, { ttlMs: seconds * 1e3 });
|
|
494
|
+
return "OK";
|
|
495
|
+
}
|
|
496
|
+
async psetex(key, milliseconds, value) {
|
|
497
|
+
await this.set(key, value, { ttlMs: milliseconds });
|
|
498
|
+
return "OK";
|
|
499
|
+
}
|
|
500
|
+
async setnx(key, value) {
|
|
501
|
+
const written = await this.set(key, value, { nx: true });
|
|
502
|
+
return written ? 1 : 0;
|
|
503
|
+
}
|
|
504
|
+
async getset(key, value) {
|
|
505
|
+
const old = await this.get(key);
|
|
506
|
+
await this.set(key, value);
|
|
507
|
+
return old;
|
|
508
|
+
}
|
|
509
|
+
async getdel(key) {
|
|
510
|
+
const value = await this.get(key);
|
|
511
|
+
if (value !== null) await this.delete(key);
|
|
512
|
+
return value;
|
|
513
|
+
}
|
|
275
514
|
stats() {
|
|
276
515
|
this.cleanupL1();
|
|
277
516
|
return {
|
|
@@ -290,6 +529,15 @@ var PgKvCache = class {
|
|
|
290
529
|
};
|
|
291
530
|
await this.sql.unsafe("SELECT pg_notify($1, $2)", [this.notifyChannel, JSON.stringify(payload)]);
|
|
292
531
|
}
|
|
532
|
+
deserialize(value) {
|
|
533
|
+
return this.serializer.deserialize(parseValue(value));
|
|
534
|
+
}
|
|
535
|
+
resolveSetMode(options) {
|
|
536
|
+
if (options.nx && options.xx) throw new Error("PgKvCache.set cannot use both nx and xx");
|
|
537
|
+
if (options.nx) return "nx";
|
|
538
|
+
if (options.xx) return "xx";
|
|
539
|
+
return options.mode ?? "always";
|
|
540
|
+
}
|
|
293
541
|
parseNotification(payload) {
|
|
294
542
|
try {
|
|
295
543
|
const value = JSON.parse(payload);
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var UnsupportedCommandError = class extends Error {
|
|
3
|
+
command;
|
|
4
|
+
constructor(command) {
|
|
5
|
+
super(`Unsupported Redis command: ${command}. This command is not available in @postgresx/noredis.`);
|
|
6
|
+
this.name = "UnsupportedCommandError";
|
|
7
|
+
this.command = command;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/redis-aliases.ts
|
|
12
|
+
function entriesFromRecord(entries) {
|
|
13
|
+
return Symbol.iterator in Object(entries) ? Array.from(entries) : Object.entries(entries);
|
|
14
|
+
}
|
|
15
|
+
function ttlResult(ttlMs) {
|
|
16
|
+
return ttlMs === null ? -1 : Math.ceil(ttlMs / 1e3);
|
|
17
|
+
}
|
|
18
|
+
function createPipelineFacade(client) {
|
|
19
|
+
const queue = [];
|
|
20
|
+
const aliases = createPgredisMigrationAliases(client);
|
|
21
|
+
const facade = {
|
|
22
|
+
queue,
|
|
23
|
+
add(command, ...args) {
|
|
24
|
+
queue.push({ command, args });
|
|
25
|
+
return facade;
|
|
26
|
+
},
|
|
27
|
+
async exec() {
|
|
28
|
+
const results = [];
|
|
29
|
+
for (const cmd of queue) {
|
|
30
|
+
try {
|
|
31
|
+
const fn = aliases[cmd.command];
|
|
32
|
+
if (typeof fn === "function") {
|
|
33
|
+
results.push([null, await fn.apply(aliases, cmd.args)]);
|
|
34
|
+
} else {
|
|
35
|
+
results.push([new UnsupportedCommandError(cmd.command), null]);
|
|
36
|
+
}
|
|
37
|
+
} catch (err) {
|
|
38
|
+
results.push([err instanceof Error ? err : new Error(String(err)), null]);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
queue.length = 0;
|
|
42
|
+
return results;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
return facade;
|
|
46
|
+
}
|
|
47
|
+
function createPgredisMigrationAliases(client) {
|
|
48
|
+
const noopHandlers = [];
|
|
49
|
+
return {
|
|
50
|
+
// KV 基础
|
|
51
|
+
get: (key) => client.cache.get(key),
|
|
52
|
+
async set(key, value, options = {}) {
|
|
53
|
+
const ttlMs = options.PX ?? (options.EX === void 0 ? void 0 : options.EX * 1e3);
|
|
54
|
+
const written = await client.cache.set(key, value, {
|
|
55
|
+
ttlMs,
|
|
56
|
+
nx: options.NX,
|
|
57
|
+
xx: options.XX
|
|
58
|
+
});
|
|
59
|
+
return written ? "OK" : null;
|
|
60
|
+
},
|
|
61
|
+
async del(...keys) {
|
|
62
|
+
const results = await Promise.all(keys.map((key) => client.cache.delete(key)));
|
|
63
|
+
return results.filter(Boolean).length;
|
|
64
|
+
},
|
|
65
|
+
async unlink(...keys) {
|
|
66
|
+
return client.cache.unlink(...keys);
|
|
67
|
+
},
|
|
68
|
+
async exists(...keys) {
|
|
69
|
+
const values = await client.cache.mget(keys);
|
|
70
|
+
return keys.filter((key) => values.has(key)).length;
|
|
71
|
+
},
|
|
72
|
+
type: (key) => client.cache.type(key),
|
|
73
|
+
// KV 过期
|
|
74
|
+
async expire(key, seconds) {
|
|
75
|
+
return await client.cache.expire(key, seconds * 1e3) ? 1 : 0;
|
|
76
|
+
},
|
|
77
|
+
async pexpire(key, milliseconds) {
|
|
78
|
+
return await client.cache.expire(key, milliseconds) ? 1 : 0;
|
|
79
|
+
},
|
|
80
|
+
async ttl(key) {
|
|
81
|
+
const value = await client.cache.get(key);
|
|
82
|
+
if (value === null) return -2;
|
|
83
|
+
return ttlResult(await client.cache.ttl(key));
|
|
84
|
+
},
|
|
85
|
+
async pttl(key) {
|
|
86
|
+
const value = await client.cache.get(key);
|
|
87
|
+
if (value === null) return -2;
|
|
88
|
+
return await client.cache.ttl(key) ?? -1;
|
|
89
|
+
},
|
|
90
|
+
async persist(key) {
|
|
91
|
+
return await client.cache.persist(key) ? 1 : 0;
|
|
92
|
+
},
|
|
93
|
+
// KV 条件写入
|
|
94
|
+
setex: (key, seconds, value) => client.cache.setex(key, seconds, value),
|
|
95
|
+
psetex: (key, milliseconds, value) => client.cache.psetex(key, milliseconds, value),
|
|
96
|
+
setnx: (key, value) => client.cache.setnx(key, value),
|
|
97
|
+
getset: (key, value) => client.cache.getset(key, value),
|
|
98
|
+
getdel: (key) => client.cache.getdel(key),
|
|
99
|
+
// KV 批量
|
|
100
|
+
async mget(...keys) {
|
|
101
|
+
const values = await client.cache.mget(keys);
|
|
102
|
+
return keys.map((key) => values.get(key) ?? null);
|
|
103
|
+
},
|
|
104
|
+
async mset(entries) {
|
|
105
|
+
await client.cache.mset(entriesFromRecord(entries));
|
|
106
|
+
return "OK";
|
|
107
|
+
},
|
|
108
|
+
// KV 扫描
|
|
109
|
+
keys: (pattern) => client.cache.keys(pattern),
|
|
110
|
+
scan: (cursor, count, pattern) => client.cache.scan(cursor, count, pattern),
|
|
111
|
+
rename: (key, newKey) => client.cache.rename(key, newKey),
|
|
112
|
+
// 计数器
|
|
113
|
+
incr: (key) => client.counter.incr(key),
|
|
114
|
+
incrby: (key, amount) => client.counter.incr(key, amount),
|
|
115
|
+
decr: (key) => client.counter.decr(key),
|
|
116
|
+
decrby: (key, amount) => client.counter.decr(key, amount),
|
|
117
|
+
// Hash
|
|
118
|
+
hget: (key, field) => client.hash.hget(key, field),
|
|
119
|
+
async hset(key, field, value) {
|
|
120
|
+
await client.hash.hset(key, field, value);
|
|
121
|
+
return 1;
|
|
122
|
+
},
|
|
123
|
+
async hmset(key, entries) {
|
|
124
|
+
await client.hash.hmset(key, entriesFromRecord(entries));
|
|
125
|
+
return "OK";
|
|
126
|
+
},
|
|
127
|
+
hmget: (key, ...fields) => client.hash.hmget(key, ...fields),
|
|
128
|
+
hgetall: (key) => client.hash.hgetall(key),
|
|
129
|
+
async hdel(key, ...fields) {
|
|
130
|
+
const results = await Promise.all(fields.map((f) => client.hash.hdel(key, f)));
|
|
131
|
+
return results.filter(Boolean).length;
|
|
132
|
+
},
|
|
133
|
+
async hexists(key, field) {
|
|
134
|
+
return await client.hash.hexists(key, field) ? 1 : 0;
|
|
135
|
+
},
|
|
136
|
+
hlen: (key) => client.hash.hlen(key),
|
|
137
|
+
hincrby: (key, field, amount) => client.hash.hincrby(key, field, amount),
|
|
138
|
+
hkeys: (key) => client.hash.hkeys(key),
|
|
139
|
+
hvals: (key) => client.hash.hvals(key),
|
|
140
|
+
hstrlen: (key, field) => client.hash.hstrlen(key, field),
|
|
141
|
+
hscan: (key, cursor, count) => client.hash.hscan(key, cursor, count),
|
|
142
|
+
// List
|
|
143
|
+
lpush: (key, ...values) => client.list.lpush(key, ...values),
|
|
144
|
+
rpush: (key, ...values) => client.list.rpush(key, ...values),
|
|
145
|
+
async lpop(key) {
|
|
146
|
+
return (await client.list.lpop(key, 1))[0] ?? null;
|
|
147
|
+
},
|
|
148
|
+
async rpop(key) {
|
|
149
|
+
return (await client.list.rpop(key, 1))[0] ?? null;
|
|
150
|
+
},
|
|
151
|
+
async blpop(key, timeoutSeconds = 0) {
|
|
152
|
+
const item = await client.list.blpop(key, { timeoutMs: timeoutSeconds * 1e3 });
|
|
153
|
+
return item ? [item.key, item.value] : null;
|
|
154
|
+
},
|
|
155
|
+
async brpop(key, timeoutSeconds = 0) {
|
|
156
|
+
const item = await client.list.brpop(key, { timeoutMs: timeoutSeconds * 1e3 });
|
|
157
|
+
return item ? [item.key, item.value] : null;
|
|
158
|
+
},
|
|
159
|
+
llen: (key) => client.list.llen(key),
|
|
160
|
+
lrange: (key, start, stop) => client.list.lrange(key, start, stop),
|
|
161
|
+
async lrem() {
|
|
162
|
+
throw new UnsupportedCommandError("lrem");
|
|
163
|
+
},
|
|
164
|
+
// Set
|
|
165
|
+
sadd: (key, ...members) => client.set.sadd(key, ...members),
|
|
166
|
+
srem: (key, ...members) => client.set.srem(key, ...members),
|
|
167
|
+
smembers: (key) => client.set.smembers(key),
|
|
168
|
+
async sismember(key, member) {
|
|
169
|
+
return await client.set.sismember(key, member) ? 1 : 0;
|
|
170
|
+
},
|
|
171
|
+
scard: (key) => client.set.scard(key),
|
|
172
|
+
sinter: (...keys) => client.set.sinter(...keys),
|
|
173
|
+
sunion: (...keys) => client.set.sunion(...keys),
|
|
174
|
+
sdiff: (key, ...otherKeys) => client.set.sdiff(key, ...otherKeys),
|
|
175
|
+
spop: (key, count) => client.set.spop(key, count),
|
|
176
|
+
srandmember: (key, count) => client.set.srandmember(key, count),
|
|
177
|
+
async smove(source, destination, member) {
|
|
178
|
+
return await client.set.smove(source, destination, member) ? 1 : 0;
|
|
179
|
+
},
|
|
180
|
+
sscan: (key, cursor, count) => client.set.sscan(key, cursor, count),
|
|
181
|
+
// Sorted Set
|
|
182
|
+
async zadd(key, ...args) {
|
|
183
|
+
if (args.length < 2) throw new Error("zadd requires at least score and member");
|
|
184
|
+
let added = 0;
|
|
185
|
+
for (let i = 0; i < args.length - 1; i += 2) {
|
|
186
|
+
const score = Number(args[i]);
|
|
187
|
+
const member = String(args[i + 1]);
|
|
188
|
+
if (await client.sortedSet.zadd(key, score, member)) added++;
|
|
189
|
+
}
|
|
190
|
+
return added;
|
|
191
|
+
},
|
|
192
|
+
zscore: (key, member) => client.sortedSet.zscore(key, member),
|
|
193
|
+
async zrange(key, start, stop) {
|
|
194
|
+
return await client.sortedSet.zrange(key, start, stop, {});
|
|
195
|
+
},
|
|
196
|
+
async zrevrange(key, start, stop) {
|
|
197
|
+
return await client.sortedSet.zrange(key, start, stop, { desc: true });
|
|
198
|
+
},
|
|
199
|
+
async zrangeByScore(key, min, max) {
|
|
200
|
+
return await client.sortedSet.zrangeByScore(key, min, max, {});
|
|
201
|
+
},
|
|
202
|
+
zrank: (key, member) => client.sortedSet.zrank(key, member),
|
|
203
|
+
zcount: (key, min, max) => client.sortedSet.zcount(key, min, max),
|
|
204
|
+
zrem: (key, ...members) => client.sortedSet.zrem(key, ...members),
|
|
205
|
+
zcard: (key) => client.sortedSet.zcard(key),
|
|
206
|
+
zincrby: (key, amount, member) => client.sortedSet.zincrby(key, amount, member),
|
|
207
|
+
zpopmin: (key, count) => client.sortedSet.zpopmin(key, count),
|
|
208
|
+
zpopmax: (key, count) => client.sortedSet.zpopmax(key, count),
|
|
209
|
+
zscan: (key, cursor, count) => client.sortedSet.zscan(key, cursor, count),
|
|
210
|
+
// Pub/Sub
|
|
211
|
+
async publish(channel, payload) {
|
|
212
|
+
await client.pubsub.publish(channel, payload);
|
|
213
|
+
return 1;
|
|
214
|
+
},
|
|
215
|
+
// 连接生命周期
|
|
216
|
+
async connect() {
|
|
217
|
+
},
|
|
218
|
+
async quit() {
|
|
219
|
+
},
|
|
220
|
+
disconnect() {
|
|
221
|
+
},
|
|
222
|
+
duplicate() {
|
|
223
|
+
return createPgredisMigrationAliases(client);
|
|
224
|
+
},
|
|
225
|
+
on(event, handler) {
|
|
226
|
+
noopHandlers.push({ event, handler });
|
|
227
|
+
return this;
|
|
228
|
+
},
|
|
229
|
+
// Pipeline / Multi
|
|
230
|
+
pipeline() {
|
|
231
|
+
return createPipelineFacade(client);
|
|
232
|
+
},
|
|
233
|
+
multi() {
|
|
234
|
+
return createPipelineFacade(client);
|
|
235
|
+
},
|
|
236
|
+
// 不支持命令
|
|
237
|
+
unsupported(command) {
|
|
238
|
+
throw new UnsupportedCommandError(command);
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export {
|
|
244
|
+
UnsupportedCommandError,
|
|
245
|
+
createPgredisMigrationAliases
|
|
246
|
+
};
|