@h-ai/cache 1.0.0-beta.3 → 1.0.0-beta.30
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.en.md +202 -35
- package/README.ja.md +233 -68
- package/README.md +234 -69
- package/dist/index.d.ts +2 -1
- package/dist/index.js +290 -95
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -170,7 +170,12 @@ function validateSetOptions(options) {
|
|
|
170
170
|
const expiresAt = mode === "ex" || mode === "px" ? Date.now() + milliseconds : milliseconds;
|
|
171
171
|
if (!Number.isSafeInteger(expiresAt))
|
|
172
172
|
return invalid();
|
|
173
|
-
const command = {
|
|
173
|
+
const command = {
|
|
174
|
+
ex: "EX",
|
|
175
|
+
px: "PX",
|
|
176
|
+
exat: "EXAT",
|
|
177
|
+
pxat: "PXAT"
|
|
178
|
+
};
|
|
174
179
|
result.expiry = [command[mode], value];
|
|
175
180
|
result.expiresAt = expiresAt;
|
|
176
181
|
}
|
|
@@ -198,6 +203,89 @@ function createMemoryProvider() {
|
|
|
198
203
|
const listStore = /* @__PURE__ */ new Map();
|
|
199
204
|
const setStore = /* @__PURE__ */ new Map();
|
|
200
205
|
const zsetStore = /* @__PURE__ */ new Map();
|
|
206
|
+
const structureExpiry = /* @__PURE__ */ new Map();
|
|
207
|
+
function deleteKey(key) {
|
|
208
|
+
const existed = store.has(key) || Boolean(hashStore.get(key)?.size || listStore.get(key)?.length || setStore.get(key)?.size || zsetStore.get(key)?.size);
|
|
209
|
+
store.delete(key);
|
|
210
|
+
hashStore.delete(key);
|
|
211
|
+
listStore.delete(key);
|
|
212
|
+
setStore.delete(key);
|
|
213
|
+
zsetStore.delete(key);
|
|
214
|
+
structureExpiry.delete(key);
|
|
215
|
+
return existed;
|
|
216
|
+
}
|
|
217
|
+
function expireKey(key) {
|
|
218
|
+
const expiresAt = store.get(key)?.expiresAt ?? structureExpiry.get(key);
|
|
219
|
+
if (expiresAt !== void 0 && expiresAt <= Date.now())
|
|
220
|
+
deleteKey(key);
|
|
221
|
+
}
|
|
222
|
+
function keyType(key) {
|
|
223
|
+
expireKey(key);
|
|
224
|
+
if (store.has(key))
|
|
225
|
+
return "string";
|
|
226
|
+
if (hashStore.get(key)?.size)
|
|
227
|
+
return "hash";
|
|
228
|
+
if (listStore.get(key)?.length)
|
|
229
|
+
return "list";
|
|
230
|
+
if (setStore.get(key)?.size)
|
|
231
|
+
return "set";
|
|
232
|
+
if (zsetStore.get(key)?.size)
|
|
233
|
+
return "zset";
|
|
234
|
+
return "none";
|
|
235
|
+
}
|
|
236
|
+
function keyExpiry(key) {
|
|
237
|
+
return store.get(key)?.expiresAt ?? structureExpiry.get(key);
|
|
238
|
+
}
|
|
239
|
+
function setKeyExpiry(key, expiresAt) {
|
|
240
|
+
const entry = store.get(key);
|
|
241
|
+
if (entry)
|
|
242
|
+
entry.expiresAt = expiresAt;
|
|
243
|
+
else structureExpiry.set(key, expiresAt);
|
|
244
|
+
expireKey(key);
|
|
245
|
+
}
|
|
246
|
+
function allKeys() {
|
|
247
|
+
return [
|
|
248
|
+
.../* @__PURE__ */ new Set([
|
|
249
|
+
...store.keys(),
|
|
250
|
+
...hashStore.keys(),
|
|
251
|
+
...listStore.keys(),
|
|
252
|
+
...setStore.keys(),
|
|
253
|
+
...zsetStore.keys()
|
|
254
|
+
])
|
|
255
|
+
].filter((key) => keyType(key) !== "none");
|
|
256
|
+
}
|
|
257
|
+
function guardStructure(operations, type) {
|
|
258
|
+
return new Proxy(operations, {
|
|
259
|
+
get(target, property, receiver) {
|
|
260
|
+
const operation = Reflect.get(target, property, receiver);
|
|
261
|
+
if (typeof operation !== "function")
|
|
262
|
+
return operation;
|
|
263
|
+
return (...args) => {
|
|
264
|
+
const keys = ([
|
|
265
|
+
"sinter",
|
|
266
|
+
"sunion",
|
|
267
|
+
"sdiff",
|
|
268
|
+
"blpop",
|
|
269
|
+
"brpop"
|
|
270
|
+
].includes(String(property)) ? args : args.slice(0, 1)).filter((key) => typeof key === "string");
|
|
271
|
+
for (const key of keys) {
|
|
272
|
+
const actual = keyType(key);
|
|
273
|
+
if (actual !== "none" && actual !== type) {
|
|
274
|
+
return Promise.resolve(err(HaiCacheError.OPERATION_FAILED, cacheM("cache_operationFailed", {
|
|
275
|
+
params: { error: "Key contains another data type" }
|
|
276
|
+
})));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return Promise.resolve(Reflect.apply(operation, target, args)).finally(() => {
|
|
280
|
+
for (const key of keys) {
|
|
281
|
+
if (keyType(key) === "none")
|
|
282
|
+
deleteKey(key);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
}
|
|
201
289
|
let connected = false;
|
|
202
290
|
let cleanupTimer = null;
|
|
203
291
|
function isExpired(entry) {
|
|
@@ -207,11 +295,14 @@ function createMemoryProvider() {
|
|
|
207
295
|
const now = Date.now();
|
|
208
296
|
for (const [key, entry] of store.entries()) {
|
|
209
297
|
if (entry.expiresAt != null && entry.expiresAt <= now) {
|
|
210
|
-
|
|
298
|
+
deleteKey(key);
|
|
211
299
|
}
|
|
212
300
|
}
|
|
301
|
+
for (const key of structureExpiry.keys())
|
|
302
|
+
expireKey(key);
|
|
213
303
|
}
|
|
214
304
|
function getValidEntry(key) {
|
|
305
|
+
expireKey(key);
|
|
215
306
|
const entry = store.get(key);
|
|
216
307
|
if (!entry)
|
|
217
308
|
return null;
|
|
@@ -226,27 +317,44 @@ function createMemoryProvider() {
|
|
|
226
317
|
try {
|
|
227
318
|
if (options?.px !== void 0 && (!Number.isSafeInteger(options.px) || options.px <= 0) || options?.px !== void 0 && options.keepTtl)
|
|
228
319
|
throw new TypeError("Invalid compare-and-swap expiry");
|
|
320
|
+
const type = keyType(key);
|
|
321
|
+
if (type !== "none" && type !== "string")
|
|
322
|
+
throw new TypeError("Key contains another data type");
|
|
229
323
|
const entry = getValidEntry(key);
|
|
230
324
|
const actual = entry ? serializeValue(entry.value) : void 0;
|
|
231
|
-
if (actual !== (expected === void 0 ? void 0 : serializeValue(expected)))
|
|
325
|
+
if (actual !== (expected === void 0 ? void 0 : serializeValue(expected))) {
|
|
232
326
|
return ok(false);
|
|
327
|
+
}
|
|
233
328
|
if (value === void 0) {
|
|
234
329
|
store.delete(key);
|
|
235
330
|
} else {
|
|
236
331
|
const expiresAt = options?.px !== void 0 ? Date.now() + options.px : options?.keepTtl ? entry?.expiresAt : void 0;
|
|
237
|
-
store.set(key, {
|
|
332
|
+
store.set(key, {
|
|
333
|
+
value: deserializeValue(serializeValue(value)),
|
|
334
|
+
expiresAt
|
|
335
|
+
});
|
|
238
336
|
}
|
|
239
337
|
return ok(true);
|
|
240
338
|
} catch (error) {
|
|
241
|
-
return err(HaiCacheError.OPERATION_FAILED, cacheM("cache_operationFailed", {
|
|
339
|
+
return err(HaiCacheError.OPERATION_FAILED, cacheM("cache_operationFailed", {
|
|
340
|
+
params: { error: "Compare-and-swap failed" }
|
|
341
|
+
}), error);
|
|
242
342
|
}
|
|
243
343
|
},
|
|
244
344
|
async get(key) {
|
|
245
345
|
try {
|
|
346
|
+
const type = keyType(key);
|
|
347
|
+
if (type !== "none" && type !== "string") {
|
|
348
|
+
return err(HaiCacheError.OPERATION_FAILED, cacheM("cache_operationFailed", {
|
|
349
|
+
params: { error: "Key contains another data type" }
|
|
350
|
+
}));
|
|
351
|
+
}
|
|
246
352
|
const entry = getValidEntry(key);
|
|
247
353
|
return ok(entry ? deserializeValue(serializeValue(entry.value)) : null);
|
|
248
354
|
} catch (error) {
|
|
249
|
-
return err(HaiCacheError.SERIALIZATION_FAILED, cacheM("cache_operationFailed", {
|
|
355
|
+
return err(HaiCacheError.SERIALIZATION_FAILED, cacheM("cache_operationFailed", {
|
|
356
|
+
params: { error: "JSON snapshot failed" }
|
|
357
|
+
}), error);
|
|
250
358
|
}
|
|
251
359
|
},
|
|
252
360
|
async set(key, value, options) {
|
|
@@ -254,17 +362,13 @@ function createMemoryProvider() {
|
|
|
254
362
|
if (!validated.success)
|
|
255
363
|
return validated;
|
|
256
364
|
const policy = validated.data;
|
|
257
|
-
const
|
|
258
|
-
const existing = currentEntry !== null || Boolean(hashStore.get(key)?.size || listStore.get(key)?.length || setStore.get(key)?.size || zsetStore.get(key)?.size);
|
|
365
|
+
const existing = keyType(key) !== "none";
|
|
259
366
|
if (policy.nx && existing || policy.xx && !existing)
|
|
260
367
|
return ok(false);
|
|
261
368
|
try {
|
|
262
369
|
const snapshot = deserializeValue(serializeValue(value));
|
|
263
|
-
const expiresAt = policy.keepTtl ?
|
|
264
|
-
|
|
265
|
-
listStore.delete(key);
|
|
266
|
-
setStore.delete(key);
|
|
267
|
-
zsetStore.delete(key);
|
|
370
|
+
const expiresAt = policy.keepTtl ? keyExpiry(key) : policy.expiresAt;
|
|
371
|
+
deleteKey(key);
|
|
268
372
|
if (expiresAt !== void 0 && expiresAt <= Date.now())
|
|
269
373
|
store.delete(key);
|
|
270
374
|
else
|
|
@@ -277,56 +381,56 @@ function createMemoryProvider() {
|
|
|
277
381
|
async del(...keys) {
|
|
278
382
|
let count = 0;
|
|
279
383
|
for (const key of keys) {
|
|
280
|
-
|
|
384
|
+
expireKey(key);
|
|
385
|
+
if (deleteKey(key))
|
|
281
386
|
count++;
|
|
282
|
-
hashStore.delete(key);
|
|
283
|
-
listStore.delete(key);
|
|
284
|
-
setStore.delete(key);
|
|
285
|
-
zsetStore.delete(key);
|
|
286
387
|
}
|
|
287
388
|
return ok(count);
|
|
288
389
|
},
|
|
289
390
|
async exists(...keys) {
|
|
290
391
|
let count = 0;
|
|
291
392
|
for (const key of keys) {
|
|
292
|
-
if (
|
|
393
|
+
if (keyType(key) !== "none")
|
|
293
394
|
count++;
|
|
294
395
|
}
|
|
295
396
|
return ok(count);
|
|
296
397
|
},
|
|
297
398
|
async expire(key, seconds) {
|
|
298
|
-
|
|
299
|
-
if (!entry)
|
|
399
|
+
if (keyType(key) === "none")
|
|
300
400
|
return ok(false);
|
|
301
|
-
|
|
401
|
+
setKeyExpiry(key, Date.now() + seconds * 1e3);
|
|
302
402
|
return ok(true);
|
|
303
403
|
},
|
|
304
404
|
async expireAt(key, timestamp) {
|
|
305
|
-
|
|
306
|
-
if (!entry)
|
|
405
|
+
if (keyType(key) === "none")
|
|
307
406
|
return ok(false);
|
|
308
|
-
|
|
407
|
+
setKeyExpiry(key, timestamp * 1e3);
|
|
309
408
|
return ok(true);
|
|
310
409
|
},
|
|
311
410
|
async ttl(key) {
|
|
312
|
-
|
|
313
|
-
if (!entry)
|
|
411
|
+
if (keyType(key) === "none")
|
|
314
412
|
return ok(-2);
|
|
315
|
-
|
|
413
|
+
const expiresAt = keyExpiry(key);
|
|
414
|
+
if (expiresAt === void 0)
|
|
316
415
|
return ok(-1);
|
|
317
|
-
return ok(Math.ceil((
|
|
416
|
+
return ok(Math.ceil((expiresAt - Date.now()) / 1e3));
|
|
318
417
|
},
|
|
319
418
|
async persist(key) {
|
|
320
|
-
|
|
321
|
-
if (!entry)
|
|
419
|
+
if (keyType(key) === "none")
|
|
322
420
|
return ok(false);
|
|
323
|
-
|
|
421
|
+
const entry = store.get(key);
|
|
422
|
+
if (entry)
|
|
423
|
+
delete entry.expiresAt;
|
|
424
|
+
structureExpiry.delete(key);
|
|
324
425
|
return ok(true);
|
|
325
426
|
},
|
|
326
427
|
async incr(key) {
|
|
327
428
|
return kv.incrBy(key, 1);
|
|
328
429
|
},
|
|
329
430
|
async incrBy(key, increment) {
|
|
431
|
+
const type = keyType(key);
|
|
432
|
+
if (type !== "none" && type !== "string")
|
|
433
|
+
return err(HaiCacheError.OPERATION_FAILED, cacheM("cache_valueNotNumber"));
|
|
330
434
|
const entry = getValidEntry(key);
|
|
331
435
|
const value = entry?.value ?? (entry ? Number.NaN : 0);
|
|
332
436
|
const current = typeof value === "number" || typeof value === "string" && /^-?\d+$/.test(value) ? Number(value) : Number.NaN;
|
|
@@ -352,24 +456,32 @@ function createMemoryProvider() {
|
|
|
352
456
|
}
|
|
353
457
|
return ok(results);
|
|
354
458
|
} catch (error) {
|
|
355
|
-
return err(HaiCacheError.SERIALIZATION_FAILED, cacheM("cache_operationFailed", {
|
|
459
|
+
return err(HaiCacheError.SERIALIZATION_FAILED, cacheM("cache_operationFailed", {
|
|
460
|
+
params: { error: "JSON snapshot failed" }
|
|
461
|
+
}), error);
|
|
356
462
|
}
|
|
357
463
|
},
|
|
358
464
|
async mset(entries) {
|
|
359
|
-
|
|
360
|
-
|
|
465
|
+
try {
|
|
466
|
+
const snapshots = entries.map(([key, value]) => [
|
|
467
|
+
key,
|
|
468
|
+
deserializeValue(serializeValue(value))
|
|
469
|
+
]);
|
|
470
|
+
for (const [key, value] of snapshots) {
|
|
471
|
+
deleteKey(key);
|
|
472
|
+
store.set(key, { value });
|
|
473
|
+
}
|
|
474
|
+
return ok(void 0);
|
|
475
|
+
} catch (error) {
|
|
476
|
+
return err(HaiCacheError.SERIALIZATION_FAILED, cacheM("cache_serializationFailed"), error);
|
|
361
477
|
}
|
|
362
|
-
return ok(void 0);
|
|
363
478
|
},
|
|
364
479
|
async scan(cursor, options) {
|
|
365
480
|
if (typeof cursor !== "string" || !/^\d{1,20}$/.test(cursor) || BigInt(cursor) > 18446744073709551615n || !Number.isSafeInteger(options?.count ?? 10) || (options?.count ?? 10) <= 0)
|
|
366
481
|
return err(HaiCacheError.INVALID_SCAN, cacheM("cache_invalidScan"));
|
|
367
|
-
const
|
|
368
|
-
const entry = store.get(key);
|
|
369
|
-
return entry && !isExpired(entry);
|
|
370
|
-
});
|
|
482
|
+
const keysInStore = allKeys();
|
|
371
483
|
const regex = options?.match ? globToRegex(options.match) : null;
|
|
372
|
-
const filtered = regex ?
|
|
484
|
+
const filtered = regex ? keysInStore.filter((k) => regex.test(k)) : keysInStore;
|
|
373
485
|
const count = options?.count || 10;
|
|
374
486
|
const start = Number(cursor);
|
|
375
487
|
const end = Math.min(start + count, filtered.length);
|
|
@@ -379,26 +491,10 @@ function createMemoryProvider() {
|
|
|
379
491
|
},
|
|
380
492
|
async keys(pattern) {
|
|
381
493
|
const regex = globToRegex(pattern);
|
|
382
|
-
|
|
383
|
-
for (const [key] of store.entries()) {
|
|
384
|
-
if (regex.test(key) && getValidEntry(key)) {
|
|
385
|
-
result.push(key);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
return ok(result);
|
|
494
|
+
return ok(allKeys().filter((key) => regex.test(key)));
|
|
389
495
|
},
|
|
390
496
|
async type(key) {
|
|
391
|
-
|
|
392
|
-
return ok("string");
|
|
393
|
-
if (hashStore.has(key))
|
|
394
|
-
return ok("hash");
|
|
395
|
-
if (listStore.has(key))
|
|
396
|
-
return ok("list");
|
|
397
|
-
if (setStore.has(key))
|
|
398
|
-
return ok("set");
|
|
399
|
-
if (zsetStore.has(key))
|
|
400
|
-
return ok("zset");
|
|
401
|
-
return ok("none");
|
|
497
|
+
return ok(keyType(key));
|
|
402
498
|
}
|
|
403
499
|
};
|
|
404
500
|
const hash = {
|
|
@@ -453,7 +549,10 @@ function createMemoryProvider() {
|
|
|
453
549
|
const map = hashStore.get(key);
|
|
454
550
|
if (!map)
|
|
455
551
|
return ok({});
|
|
456
|
-
return ok(Object.fromEntries(Array.from(map, ([field, value]) => [
|
|
552
|
+
return ok(Object.fromEntries(Array.from(map, ([field, value]) => [
|
|
553
|
+
field,
|
|
554
|
+
deserializeValue(value)
|
|
555
|
+
])));
|
|
457
556
|
},
|
|
458
557
|
async hkeys(key) {
|
|
459
558
|
const map = hashStore.get(key);
|
|
@@ -575,7 +674,10 @@ function createMemoryProvider() {
|
|
|
575
674
|
const arr = listStore.get(key);
|
|
576
675
|
if (arr && arr.length > 0) {
|
|
577
676
|
const val = arr.shift();
|
|
578
|
-
return ok([
|
|
677
|
+
return ok([
|
|
678
|
+
key,
|
|
679
|
+
deserializeValue(val)
|
|
680
|
+
]);
|
|
579
681
|
}
|
|
580
682
|
}
|
|
581
683
|
return ok(null);
|
|
@@ -588,7 +690,10 @@ function createMemoryProvider() {
|
|
|
588
690
|
const arr = listStore.get(key);
|
|
589
691
|
if (arr && arr.length > 0) {
|
|
590
692
|
const val = arr.pop();
|
|
591
|
-
return ok([
|
|
693
|
+
return ok([
|
|
694
|
+
key,
|
|
695
|
+
deserializeValue(val)
|
|
696
|
+
]);
|
|
592
697
|
}
|
|
593
698
|
}
|
|
594
699
|
return ok(null);
|
|
@@ -918,14 +1023,15 @@ function createMemoryProvider() {
|
|
|
918
1023
|
listStore.clear();
|
|
919
1024
|
setStore.clear();
|
|
920
1025
|
zsetStore.clear();
|
|
1026
|
+
structureExpiry.clear();
|
|
921
1027
|
connected = false;
|
|
922
1028
|
},
|
|
923
1029
|
isConnected: () => connected,
|
|
924
1030
|
kv,
|
|
925
|
-
hash,
|
|
926
|
-
list,
|
|
927
|
-
set_,
|
|
928
|
-
zset,
|
|
1031
|
+
hash: guardStructure(hash, "hash"),
|
|
1032
|
+
list: guardStructure(list, "list"),
|
|
1033
|
+
set_: guardStructure(set_, "set"),
|
|
1034
|
+
zset: guardStructure(zset, "zset"),
|
|
929
1035
|
lock,
|
|
930
1036
|
async ping() {
|
|
931
1037
|
return ok("PONG");
|
|
@@ -1014,6 +1120,14 @@ async function scanRedisKeys(client, cursor, options = {}) {
|
|
|
1014
1120
|
|
|
1015
1121
|
// src/providers/cache-provider-redis.ts
|
|
1016
1122
|
var logger = core.logger.child({ module: "cache", scope: "redis" });
|
|
1123
|
+
function redisErrorDiagnostics(error) {
|
|
1124
|
+
const cause = error && typeof error === "object" && "lastNodeError" in error ? error.lastNodeError ?? error : error;
|
|
1125
|
+
const code = cause && typeof cause === "object" && "code" in cause ? cause.code : void 0;
|
|
1126
|
+
return {
|
|
1127
|
+
causeName: cause instanceof Error ? cause.name : void 0,
|
|
1128
|
+
causeCode: typeof code === "string" && /^[A-Z0-9_]{1,64}$/.test(code) ? code : cause instanceof Error ? /^(NOAUTH|WRONGPASS|NOPERM|LOADING|CLUSTERDOWN)\b/.exec(cause.message)?.[1] : void 0
|
|
1129
|
+
};
|
|
1130
|
+
}
|
|
1017
1131
|
var COMPARE_AND_SWAP_SCRIPT = `
|
|
1018
1132
|
local current = redis.call('get', KEYS[1])
|
|
1019
1133
|
if ARGV[1] == 'missing' then
|
|
@@ -1061,7 +1175,7 @@ function createRedisProvider() {
|
|
|
1061
1175
|
if (graceful)
|
|
1062
1176
|
await owned.quit();
|
|
1063
1177
|
} catch (error) {
|
|
1064
|
-
logger.warn("Redis graceful shutdown failed; disconnecting client",
|
|
1178
|
+
logger.warn("Redis graceful shutdown failed; disconnecting client", redisErrorDiagnostics(error));
|
|
1065
1179
|
}
|
|
1066
1180
|
owned.disconnect();
|
|
1067
1181
|
client = null;
|
|
@@ -1100,7 +1214,11 @@ function createRedisProvider() {
|
|
|
1100
1214
|
return err(HaiCacheError.CROSS_SLOT, cacheM("cache_crossSlot"), error);
|
|
1101
1215
|
return err(
|
|
1102
1216
|
HaiCacheError.OPERATION_FAILED,
|
|
1103
|
-
cacheM("cache_operationFailed", {
|
|
1217
|
+
cacheM("cache_operationFailed", {
|
|
1218
|
+
params: {
|
|
1219
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1220
|
+
}
|
|
1221
|
+
}),
|
|
1104
1222
|
error
|
|
1105
1223
|
);
|
|
1106
1224
|
}
|
|
@@ -1134,7 +1252,10 @@ function createRedisProvider() {
|
|
|
1134
1252
|
return validated;
|
|
1135
1253
|
return wrapOperation(async () => {
|
|
1136
1254
|
const policy = validated.data;
|
|
1137
|
-
const args = [
|
|
1255
|
+
const args = [
|
|
1256
|
+
key,
|
|
1257
|
+
serialize(value)
|
|
1258
|
+
];
|
|
1138
1259
|
if (policy.expiry)
|
|
1139
1260
|
args.push(...policy.expiry);
|
|
1140
1261
|
if (policy.nx)
|
|
@@ -1255,7 +1376,10 @@ function createRedisProvider() {
|
|
|
1255
1376
|
return client.hset(key, fieldOrData, serialize(value));
|
|
1256
1377
|
}
|
|
1257
1378
|
if (typeof fieldOrData === "object") {
|
|
1258
|
-
const serialized = Object.fromEntries(Object.entries(fieldOrData).map(([key2, value2]) => [
|
|
1379
|
+
const serialized = Object.fromEntries(Object.entries(fieldOrData).map(([key2, value2]) => [
|
|
1380
|
+
key2,
|
|
1381
|
+
serialize(value2)
|
|
1382
|
+
]));
|
|
1259
1383
|
return client.hset(key, serialized);
|
|
1260
1384
|
}
|
|
1261
1385
|
return 0;
|
|
@@ -1273,7 +1397,10 @@ function createRedisProvider() {
|
|
|
1273
1397
|
async hgetall(key) {
|
|
1274
1398
|
return wrapOperation(async () => {
|
|
1275
1399
|
const data = await client.hgetall(key);
|
|
1276
|
-
return Object.fromEntries(Object.entries(data).map(([key2, value]) => [
|
|
1400
|
+
return Object.fromEntries(Object.entries(data).map(([key2, value]) => [
|
|
1401
|
+
key2,
|
|
1402
|
+
deserialize(value)
|
|
1403
|
+
]));
|
|
1277
1404
|
});
|
|
1278
1405
|
},
|
|
1279
1406
|
async hkeys(key) {
|
|
@@ -1347,7 +1474,10 @@ function createRedisProvider() {
|
|
|
1347
1474
|
const result = await client.blpop(...keys, timeout);
|
|
1348
1475
|
if (!result)
|
|
1349
1476
|
return null;
|
|
1350
|
-
return [
|
|
1477
|
+
return [
|
|
1478
|
+
result[0],
|
|
1479
|
+
deserialize(result[1])
|
|
1480
|
+
];
|
|
1351
1481
|
});
|
|
1352
1482
|
},
|
|
1353
1483
|
async brpop(timeout, ...keys) {
|
|
@@ -1355,7 +1485,10 @@ function createRedisProvider() {
|
|
|
1355
1485
|
const result = await client.brpop(...keys, timeout);
|
|
1356
1486
|
if (!result)
|
|
1357
1487
|
return null;
|
|
1358
|
-
return [
|
|
1488
|
+
return [
|
|
1489
|
+
result[0],
|
|
1490
|
+
deserialize(result[1])
|
|
1491
|
+
];
|
|
1359
1492
|
});
|
|
1360
1493
|
}
|
|
1361
1494
|
};
|
|
@@ -1451,7 +1584,10 @@ function createRedisProvider() {
|
|
|
1451
1584
|
const result = await client.zrange(key, String(start), String(stop), "WITHSCORES");
|
|
1452
1585
|
const members = [];
|
|
1453
1586
|
for (let i = 0; i < result.length; i += 2) {
|
|
1454
|
-
members.push({
|
|
1587
|
+
members.push({
|
|
1588
|
+
member: result[i],
|
|
1589
|
+
score: Number.parseFloat(result[i + 1])
|
|
1590
|
+
});
|
|
1455
1591
|
}
|
|
1456
1592
|
return members;
|
|
1457
1593
|
}
|
|
@@ -1464,7 +1600,10 @@ function createRedisProvider() {
|
|
|
1464
1600
|
const result = await client.zrevrange(key, start, stop, "WITHSCORES");
|
|
1465
1601
|
const members = [];
|
|
1466
1602
|
for (let i = 0; i < result.length; i += 2) {
|
|
1467
|
-
members.push({
|
|
1603
|
+
members.push({
|
|
1604
|
+
member: result[i],
|
|
1605
|
+
score: Number.parseFloat(result[i + 1])
|
|
1606
|
+
});
|
|
1468
1607
|
}
|
|
1469
1608
|
return members;
|
|
1470
1609
|
}
|
|
@@ -1473,7 +1612,11 @@ function createRedisProvider() {
|
|
|
1473
1612
|
},
|
|
1474
1613
|
async zrangeByScore(key, min, max, options) {
|
|
1475
1614
|
return wrapOperation(async () => {
|
|
1476
|
-
const args = [
|
|
1615
|
+
const args = [
|
|
1616
|
+
key,
|
|
1617
|
+
String(min),
|
|
1618
|
+
String(max)
|
|
1619
|
+
];
|
|
1477
1620
|
if (options?.withScores)
|
|
1478
1621
|
args.push("WITHSCORES");
|
|
1479
1622
|
if (options?.offset !== void 0 && options?.count !== void 0) {
|
|
@@ -1483,7 +1626,10 @@ function createRedisProvider() {
|
|
|
1483
1626
|
if (options?.withScores) {
|
|
1484
1627
|
const members = [];
|
|
1485
1628
|
for (let i = 0; i < result.length; i += 2) {
|
|
1486
|
-
members.push({
|
|
1629
|
+
members.push({
|
|
1630
|
+
member: result[i],
|
|
1631
|
+
score: Number.parseFloat(result[i + 1])
|
|
1632
|
+
});
|
|
1487
1633
|
}
|
|
1488
1634
|
return members;
|
|
1489
1635
|
}
|
|
@@ -1564,9 +1710,12 @@ function createRedisProvider() {
|
|
|
1564
1710
|
if (config.type !== "redis") {
|
|
1565
1711
|
return err(
|
|
1566
1712
|
HaiCacheError.UNSUPPORTED_TYPE,
|
|
1567
|
-
cacheM("cache_unsupportedType", {
|
|
1713
|
+
cacheM("cache_unsupportedType", {
|
|
1714
|
+
params: { type: config.type }
|
|
1715
|
+
})
|
|
1568
1716
|
);
|
|
1569
1717
|
}
|
|
1718
|
+
let connectionError;
|
|
1570
1719
|
try {
|
|
1571
1720
|
await closeClient();
|
|
1572
1721
|
const redisOptions = {
|
|
@@ -1615,14 +1764,33 @@ function createRedisProvider() {
|
|
|
1615
1764
|
db: config.db
|
|
1616
1765
|
});
|
|
1617
1766
|
}
|
|
1618
|
-
|
|
1767
|
+
const connectedClient = client;
|
|
1768
|
+
connectedClient.on("error", (error) => {
|
|
1769
|
+
if (client !== connectedClient)
|
|
1770
|
+
return;
|
|
1771
|
+
if (ready) {
|
|
1772
|
+
logger.warn("Redis client connection error", {
|
|
1773
|
+
status: connectedClient.status,
|
|
1774
|
+
...redisErrorDiagnostics(error)
|
|
1775
|
+
});
|
|
1776
|
+
} else {
|
|
1777
|
+
connectionError = error;
|
|
1778
|
+
}
|
|
1779
|
+
});
|
|
1619
1780
|
await client.connect();
|
|
1620
1781
|
await client.ping();
|
|
1782
|
+
connectionError = void 0;
|
|
1621
1783
|
ready = true;
|
|
1622
1784
|
const address = config.url ? core.sanitize.sanitizeUrl(config.url) : `${config.host}:${config.port}`;
|
|
1623
1785
|
logger.info("Redis connected", { address });
|
|
1624
1786
|
return ok(void 0);
|
|
1625
1787
|
} catch (error) {
|
|
1788
|
+
const diagnostics = redisErrorDiagnostics(error);
|
|
1789
|
+
logger.error("Redis connection failed", {
|
|
1790
|
+
errorCode: HaiCacheError.CONNECTION_FAILED.code,
|
|
1791
|
+
status: client?.status,
|
|
1792
|
+
...diagnostics.causeCode || connectionError === void 0 ? diagnostics : redisErrorDiagnostics(connectionError)
|
|
1793
|
+
});
|
|
1626
1794
|
ready = false;
|
|
1627
1795
|
let cleanupError;
|
|
1628
1796
|
try {
|
|
@@ -1633,11 +1801,19 @@ function createRedisProvider() {
|
|
|
1633
1801
|
}
|
|
1634
1802
|
const result = err(
|
|
1635
1803
|
HaiCacheError.CONNECTION_FAILED,
|
|
1636
|
-
cacheM("cache_redisConnectionFailed", {
|
|
1804
|
+
cacheM("cache_redisConnectionFailed", {
|
|
1805
|
+
params: {
|
|
1806
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1807
|
+
}
|
|
1808
|
+
}),
|
|
1637
1809
|
error
|
|
1638
1810
|
);
|
|
1639
|
-
if (cleanupError !== void 0 && !result.success)
|
|
1640
|
-
return err({
|
|
1811
|
+
if (cleanupError !== void 0 && !result.success) {
|
|
1812
|
+
return err({
|
|
1813
|
+
...result.error,
|
|
1814
|
+
ext: { cleanupPending: true, cleanupError }
|
|
1815
|
+
});
|
|
1816
|
+
}
|
|
1641
1817
|
return result;
|
|
1642
1818
|
}
|
|
1643
1819
|
},
|
|
@@ -1677,9 +1853,16 @@ async function closeOwnedProvider() {
|
|
|
1677
1853
|
return ok(void 0);
|
|
1678
1854
|
} catch (error) {
|
|
1679
1855
|
logger2.error("Provider cleanup failed; retaining resource for retry", { error });
|
|
1680
|
-
return err({
|
|
1681
|
-
|
|
1682
|
-
|
|
1856
|
+
return err({
|
|
1857
|
+
...HaiCacheError.CONNECTION_FAILED,
|
|
1858
|
+
message: cacheM("cache_operationFailed", {
|
|
1859
|
+
params: {
|
|
1860
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1861
|
+
}
|
|
1862
|
+
}),
|
|
1863
|
+
cause: error,
|
|
1864
|
+
ext: { cleanupPending: true }
|
|
1865
|
+
});
|
|
1683
1866
|
}
|
|
1684
1867
|
}
|
|
1685
1868
|
async function failInitialization(result) {
|
|
@@ -1690,9 +1873,15 @@ async function failInitialization(result) {
|
|
|
1690
1873
|
const ext = { ...result.error.ext };
|
|
1691
1874
|
delete ext.cleanupPending;
|
|
1692
1875
|
delete ext.cleanupError;
|
|
1693
|
-
return err({
|
|
1876
|
+
return err({
|
|
1877
|
+
...result.error,
|
|
1878
|
+
ext: Object.keys(ext).length > 0 ? ext : void 0
|
|
1879
|
+
});
|
|
1694
1880
|
}
|
|
1695
|
-
return err({
|
|
1881
|
+
return err({
|
|
1882
|
+
...result.error,
|
|
1883
|
+
ext: { ...result.error.ext, cleanupPending: true, cleanupError: cleanup.error }
|
|
1884
|
+
});
|
|
1696
1885
|
}
|
|
1697
1886
|
function createProvider(config) {
|
|
1698
1887
|
switch (config.type) {
|
|
@@ -1703,7 +1892,9 @@ function createProvider(config) {
|
|
|
1703
1892
|
default:
|
|
1704
1893
|
return err(
|
|
1705
1894
|
HaiCacheError.UNSUPPORTED_TYPE,
|
|
1706
|
-
cacheM("cache_unsupportedType", {
|
|
1895
|
+
cacheM("cache_unsupportedType", {
|
|
1896
|
+
params: { type: config.type }
|
|
1897
|
+
})
|
|
1707
1898
|
);
|
|
1708
1899
|
}
|
|
1709
1900
|
}
|
|
@@ -1740,7 +1931,9 @@ var cache = {
|
|
|
1740
1931
|
logger2.warn("Cache init already in progress, skipping concurrent call");
|
|
1741
1932
|
return err(
|
|
1742
1933
|
HaiCacheError.OPERATION_FAILED,
|
|
1743
|
-
cacheM("cache_operationFailed", {
|
|
1934
|
+
cacheM("cache_operationFailed", {
|
|
1935
|
+
params: { error: "Concurrent initialization detected" }
|
|
1936
|
+
})
|
|
1744
1937
|
);
|
|
1745
1938
|
}
|
|
1746
1939
|
initInProgress = true;
|
|
@@ -1758,7 +1951,9 @@ var cache = {
|
|
|
1758
1951
|
logger2.error("Cache config validation failed", { error: parseResult.error.message });
|
|
1759
1952
|
return err(
|
|
1760
1953
|
HaiCacheError.CONFIG_ERROR,
|
|
1761
|
-
cacheM("cache_configError", {
|
|
1954
|
+
cacheM("cache_configError", {
|
|
1955
|
+
params: { error: parseResult.error.message }
|
|
1956
|
+
}),
|
|
1762
1957
|
parseResult.error
|
|
1763
1958
|
);
|
|
1764
1959
|
}
|
|
@@ -1773,10 +1968,8 @@ var cache = {
|
|
|
1773
1968
|
pendingProvider = provider;
|
|
1774
1969
|
const connectResult = await provider.connect(parsed);
|
|
1775
1970
|
if (!connectResult.success) {
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
message: connectResult.error.message
|
|
1779
|
-
});
|
|
1971
|
+
if (parsed.type !== "redis")
|
|
1972
|
+
logger2.error("Cache module initialization failed", { errorCode: connectResult.error.code });
|
|
1780
1973
|
return await failInitialization(connectResult);
|
|
1781
1974
|
}
|
|
1782
1975
|
pendingProvider = null;
|
|
@@ -1789,7 +1982,9 @@ var cache = {
|
|
|
1789
1982
|
return await failInitialization(err(
|
|
1790
1983
|
HaiCacheError.CONNECTION_FAILED,
|
|
1791
1984
|
cacheM("cache_initFailed", {
|
|
1792
|
-
params: {
|
|
1985
|
+
params: {
|
|
1986
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1987
|
+
}
|
|
1793
1988
|
}),
|
|
1794
1989
|
error
|
|
1795
1990
|
));
|