@h-ai/cache 1.0.0-beta.2 → 1.0.0-beta.21
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 +258 -89
- 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");
|
|
@@ -1100,7 +1206,11 @@ function createRedisProvider() {
|
|
|
1100
1206
|
return err(HaiCacheError.CROSS_SLOT, cacheM("cache_crossSlot"), error);
|
|
1101
1207
|
return err(
|
|
1102
1208
|
HaiCacheError.OPERATION_FAILED,
|
|
1103
|
-
cacheM("cache_operationFailed", {
|
|
1209
|
+
cacheM("cache_operationFailed", {
|
|
1210
|
+
params: {
|
|
1211
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1212
|
+
}
|
|
1213
|
+
}),
|
|
1104
1214
|
error
|
|
1105
1215
|
);
|
|
1106
1216
|
}
|
|
@@ -1134,7 +1244,10 @@ function createRedisProvider() {
|
|
|
1134
1244
|
return validated;
|
|
1135
1245
|
return wrapOperation(async () => {
|
|
1136
1246
|
const policy = validated.data;
|
|
1137
|
-
const args = [
|
|
1247
|
+
const args = [
|
|
1248
|
+
key,
|
|
1249
|
+
serialize(value)
|
|
1250
|
+
];
|
|
1138
1251
|
if (policy.expiry)
|
|
1139
1252
|
args.push(...policy.expiry);
|
|
1140
1253
|
if (policy.nx)
|
|
@@ -1255,7 +1368,10 @@ function createRedisProvider() {
|
|
|
1255
1368
|
return client.hset(key, fieldOrData, serialize(value));
|
|
1256
1369
|
}
|
|
1257
1370
|
if (typeof fieldOrData === "object") {
|
|
1258
|
-
const serialized = Object.fromEntries(Object.entries(fieldOrData).map(([key2, value2]) => [
|
|
1371
|
+
const serialized = Object.fromEntries(Object.entries(fieldOrData).map(([key2, value2]) => [
|
|
1372
|
+
key2,
|
|
1373
|
+
serialize(value2)
|
|
1374
|
+
]));
|
|
1259
1375
|
return client.hset(key, serialized);
|
|
1260
1376
|
}
|
|
1261
1377
|
return 0;
|
|
@@ -1273,7 +1389,10 @@ function createRedisProvider() {
|
|
|
1273
1389
|
async hgetall(key) {
|
|
1274
1390
|
return wrapOperation(async () => {
|
|
1275
1391
|
const data = await client.hgetall(key);
|
|
1276
|
-
return Object.fromEntries(Object.entries(data).map(([key2, value]) => [
|
|
1392
|
+
return Object.fromEntries(Object.entries(data).map(([key2, value]) => [
|
|
1393
|
+
key2,
|
|
1394
|
+
deserialize(value)
|
|
1395
|
+
]));
|
|
1277
1396
|
});
|
|
1278
1397
|
},
|
|
1279
1398
|
async hkeys(key) {
|
|
@@ -1347,7 +1466,10 @@ function createRedisProvider() {
|
|
|
1347
1466
|
const result = await client.blpop(...keys, timeout);
|
|
1348
1467
|
if (!result)
|
|
1349
1468
|
return null;
|
|
1350
|
-
return [
|
|
1469
|
+
return [
|
|
1470
|
+
result[0],
|
|
1471
|
+
deserialize(result[1])
|
|
1472
|
+
];
|
|
1351
1473
|
});
|
|
1352
1474
|
},
|
|
1353
1475
|
async brpop(timeout, ...keys) {
|
|
@@ -1355,7 +1477,10 @@ function createRedisProvider() {
|
|
|
1355
1477
|
const result = await client.brpop(...keys, timeout);
|
|
1356
1478
|
if (!result)
|
|
1357
1479
|
return null;
|
|
1358
|
-
return [
|
|
1480
|
+
return [
|
|
1481
|
+
result[0],
|
|
1482
|
+
deserialize(result[1])
|
|
1483
|
+
];
|
|
1359
1484
|
});
|
|
1360
1485
|
}
|
|
1361
1486
|
};
|
|
@@ -1451,7 +1576,10 @@ function createRedisProvider() {
|
|
|
1451
1576
|
const result = await client.zrange(key, String(start), String(stop), "WITHSCORES");
|
|
1452
1577
|
const members = [];
|
|
1453
1578
|
for (let i = 0; i < result.length; i += 2) {
|
|
1454
|
-
members.push({
|
|
1579
|
+
members.push({
|
|
1580
|
+
member: result[i],
|
|
1581
|
+
score: Number.parseFloat(result[i + 1])
|
|
1582
|
+
});
|
|
1455
1583
|
}
|
|
1456
1584
|
return members;
|
|
1457
1585
|
}
|
|
@@ -1464,7 +1592,10 @@ function createRedisProvider() {
|
|
|
1464
1592
|
const result = await client.zrevrange(key, start, stop, "WITHSCORES");
|
|
1465
1593
|
const members = [];
|
|
1466
1594
|
for (let i = 0; i < result.length; i += 2) {
|
|
1467
|
-
members.push({
|
|
1595
|
+
members.push({
|
|
1596
|
+
member: result[i],
|
|
1597
|
+
score: Number.parseFloat(result[i + 1])
|
|
1598
|
+
});
|
|
1468
1599
|
}
|
|
1469
1600
|
return members;
|
|
1470
1601
|
}
|
|
@@ -1473,7 +1604,11 @@ function createRedisProvider() {
|
|
|
1473
1604
|
},
|
|
1474
1605
|
async zrangeByScore(key, min, max, options) {
|
|
1475
1606
|
return wrapOperation(async () => {
|
|
1476
|
-
const args = [
|
|
1607
|
+
const args = [
|
|
1608
|
+
key,
|
|
1609
|
+
String(min),
|
|
1610
|
+
String(max)
|
|
1611
|
+
];
|
|
1477
1612
|
if (options?.withScores)
|
|
1478
1613
|
args.push("WITHSCORES");
|
|
1479
1614
|
if (options?.offset !== void 0 && options?.count !== void 0) {
|
|
@@ -1483,7 +1618,10 @@ function createRedisProvider() {
|
|
|
1483
1618
|
if (options?.withScores) {
|
|
1484
1619
|
const members = [];
|
|
1485
1620
|
for (let i = 0; i < result.length; i += 2) {
|
|
1486
|
-
members.push({
|
|
1621
|
+
members.push({
|
|
1622
|
+
member: result[i],
|
|
1623
|
+
score: Number.parseFloat(result[i + 1])
|
|
1624
|
+
});
|
|
1487
1625
|
}
|
|
1488
1626
|
return members;
|
|
1489
1627
|
}
|
|
@@ -1564,7 +1702,9 @@ function createRedisProvider() {
|
|
|
1564
1702
|
if (config.type !== "redis") {
|
|
1565
1703
|
return err(
|
|
1566
1704
|
HaiCacheError.UNSUPPORTED_TYPE,
|
|
1567
|
-
cacheM("cache_unsupportedType", {
|
|
1705
|
+
cacheM("cache_unsupportedType", {
|
|
1706
|
+
params: { type: config.type }
|
|
1707
|
+
})
|
|
1568
1708
|
);
|
|
1569
1709
|
}
|
|
1570
1710
|
try {
|
|
@@ -1633,11 +1773,19 @@ function createRedisProvider() {
|
|
|
1633
1773
|
}
|
|
1634
1774
|
const result = err(
|
|
1635
1775
|
HaiCacheError.CONNECTION_FAILED,
|
|
1636
|
-
cacheM("cache_redisConnectionFailed", {
|
|
1776
|
+
cacheM("cache_redisConnectionFailed", {
|
|
1777
|
+
params: {
|
|
1778
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1779
|
+
}
|
|
1780
|
+
}),
|
|
1637
1781
|
error
|
|
1638
1782
|
);
|
|
1639
|
-
if (cleanupError !== void 0 && !result.success)
|
|
1640
|
-
return err({
|
|
1783
|
+
if (cleanupError !== void 0 && !result.success) {
|
|
1784
|
+
return err({
|
|
1785
|
+
...result.error,
|
|
1786
|
+
ext: { cleanupPending: true, cleanupError }
|
|
1787
|
+
});
|
|
1788
|
+
}
|
|
1641
1789
|
return result;
|
|
1642
1790
|
}
|
|
1643
1791
|
},
|
|
@@ -1677,9 +1825,16 @@ async function closeOwnedProvider() {
|
|
|
1677
1825
|
return ok(void 0);
|
|
1678
1826
|
} catch (error) {
|
|
1679
1827
|
logger2.error("Provider cleanup failed; retaining resource for retry", { error });
|
|
1680
|
-
return err({
|
|
1681
|
-
|
|
1682
|
-
|
|
1828
|
+
return err({
|
|
1829
|
+
...HaiCacheError.CONNECTION_FAILED,
|
|
1830
|
+
message: cacheM("cache_operationFailed", {
|
|
1831
|
+
params: {
|
|
1832
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1833
|
+
}
|
|
1834
|
+
}),
|
|
1835
|
+
cause: error,
|
|
1836
|
+
ext: { cleanupPending: true }
|
|
1837
|
+
});
|
|
1683
1838
|
}
|
|
1684
1839
|
}
|
|
1685
1840
|
async function failInitialization(result) {
|
|
@@ -1690,9 +1845,15 @@ async function failInitialization(result) {
|
|
|
1690
1845
|
const ext = { ...result.error.ext };
|
|
1691
1846
|
delete ext.cleanupPending;
|
|
1692
1847
|
delete ext.cleanupError;
|
|
1693
|
-
return err({
|
|
1848
|
+
return err({
|
|
1849
|
+
...result.error,
|
|
1850
|
+
ext: Object.keys(ext).length > 0 ? ext : void 0
|
|
1851
|
+
});
|
|
1694
1852
|
}
|
|
1695
|
-
return err({
|
|
1853
|
+
return err({
|
|
1854
|
+
...result.error,
|
|
1855
|
+
ext: { ...result.error.ext, cleanupPending: true, cleanupError: cleanup.error }
|
|
1856
|
+
});
|
|
1696
1857
|
}
|
|
1697
1858
|
function createProvider(config) {
|
|
1698
1859
|
switch (config.type) {
|
|
@@ -1703,7 +1864,9 @@ function createProvider(config) {
|
|
|
1703
1864
|
default:
|
|
1704
1865
|
return err(
|
|
1705
1866
|
HaiCacheError.UNSUPPORTED_TYPE,
|
|
1706
|
-
cacheM("cache_unsupportedType", {
|
|
1867
|
+
cacheM("cache_unsupportedType", {
|
|
1868
|
+
params: { type: config.type }
|
|
1869
|
+
})
|
|
1707
1870
|
);
|
|
1708
1871
|
}
|
|
1709
1872
|
}
|
|
@@ -1740,7 +1903,9 @@ var cache = {
|
|
|
1740
1903
|
logger2.warn("Cache init already in progress, skipping concurrent call");
|
|
1741
1904
|
return err(
|
|
1742
1905
|
HaiCacheError.OPERATION_FAILED,
|
|
1743
|
-
cacheM("cache_operationFailed", {
|
|
1906
|
+
cacheM("cache_operationFailed", {
|
|
1907
|
+
params: { error: "Concurrent initialization detected" }
|
|
1908
|
+
})
|
|
1744
1909
|
);
|
|
1745
1910
|
}
|
|
1746
1911
|
initInProgress = true;
|
|
@@ -1758,7 +1923,9 @@ var cache = {
|
|
|
1758
1923
|
logger2.error("Cache config validation failed", { error: parseResult.error.message });
|
|
1759
1924
|
return err(
|
|
1760
1925
|
HaiCacheError.CONFIG_ERROR,
|
|
1761
|
-
cacheM("cache_configError", {
|
|
1926
|
+
cacheM("cache_configError", {
|
|
1927
|
+
params: { error: parseResult.error.message }
|
|
1928
|
+
}),
|
|
1762
1929
|
parseResult.error
|
|
1763
1930
|
);
|
|
1764
1931
|
}
|
|
@@ -1789,7 +1956,9 @@ var cache = {
|
|
|
1789
1956
|
return await failInitialization(err(
|
|
1790
1957
|
HaiCacheError.CONNECTION_FAILED,
|
|
1791
1958
|
cacheM("cache_initFailed", {
|
|
1792
|
-
params: {
|
|
1959
|
+
params: {
|
|
1960
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1961
|
+
}
|
|
1793
1962
|
}),
|
|
1794
1963
|
error
|
|
1795
1964
|
));
|