add pipeline delete keys

This commit is contained in:
archer 2025-12-18 10:48:33 +08:00
parent 4b15494107
commit c87940251e
No known key found for this signature in database
GPG Key ID: 4446499B846D4A9E
3 changed files with 12 additions and 3 deletions

View File

@ -23,7 +23,11 @@ export const refreshVersionKey = async (key: `${SystemCacheKeyEnum}`, id?: strin
const pattern = `${cachePrefix}${key}`;
const keys = await getAllKeysByPrefix(pattern);
if (keys.length > 0) {
await redis.del(keys);
const pipeline = redis.pipeline();
for (const k of keys) {
pipeline.del(k);
}
await pipeline.exec();
}
} else {
await redis.set(versionKey, val);

View File

@ -59,7 +59,7 @@ export const getAllKeysByPrefix = async (key: string) => {
cursor = nextCursor;
for (const k of keys) {
results.push(key.replace(FASTGPT_REDIS_PREFIX, ''));
results.push(k.replace(FASTGPT_REDIS_PREFIX, ''));
}
} while (cursor !== '0');

View File

@ -17,7 +17,12 @@ const createMockRedisClient = () => ({
del: vi.fn().mockResolvedValue(1),
exists: vi.fn().mockResolvedValue(0),
keys: vi.fn().mockResolvedValue([]),
scan: vi.fn().mockResolvedValue(['0', []]),
scan: vi.fn().mockImplementation((cursor) => {
// 模拟多次迭代的场景
if (cursor === '0') return ['100', ['key1', 'key2']];
if (cursor === '100') return ['0', ['key3']];
return ['0', []];
}),
// Hash operations
hget: vi.fn().mockResolvedValue(null),