From 8c28f983cb16344db4935edcb6415a01f249b924 Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Thu, 18 Dec 2025 10:37:59 +0800 Subject: [PATCH] perf: get redis keys function --- packages/service/common/cache/index.ts | 52 +++++--------------------- packages/service/common/redis/index.ts | 14 +++---- 2 files changed, 14 insertions(+), 52 deletions(-) diff --git a/packages/service/common/cache/index.ts b/packages/service/common/cache/index.ts index 562b7d947..dc4f5f3ca 100644 --- a/packages/service/common/cache/index.ts +++ b/packages/service/common/cache/index.ts @@ -1,5 +1,5 @@ import './init'; -import { getGlobalRedisConnection } from '../../common/redis'; +import { getAllKeysByPrefix, getGlobalRedisConnection } from '../../common/redis'; import type { SystemCacheKeyEnum } from './type'; import { randomUUID } from 'node:crypto'; import { initCache } from './init'; @@ -12,10 +12,7 @@ const cachePrefix = `VERSION_KEY:`; * @param key SystemCacheKeyEnum * @param id string (teamId, tmbId, etc), if '*' is used, all keys will be refreshed */ -export const refreshVersionKey = async ( - key: `${SystemCacheKeyEnum}`, - id?: string | '*' -) => { +export const refreshVersionKey = async (key: `${SystemCacheKeyEnum}`, id?: string | '*') => { const redis = getGlobalRedisConnection(); if (!global.systemCache) initCache(); @@ -23,45 +20,14 @@ export const refreshVersionKey = async ( const versionKey = id ? `${cachePrefix}${key}:${id}` : `${cachePrefix}${key}`; if (id === '*') { - const pattern = `${cachePrefix}${key}:*`; - - let cursor = '0'; - const batchSize = 1000; // SCAN 每次取多少 - const delChunk = 500; // 每次 pipeline 删除多少(可按需调) - - let buffer: string[] = []; - - const flush = async () => { - if (buffer.length === 0) return; - const pipeline = redis.pipeline(); - for (const k of buffer) pipeline.del(k); - await pipeline.exec(); - buffer = []; - }; - - do { - const [nextCursor, keys] = await redis.scan( - cursor, - 'MATCH', - pattern, - 'COUNT', - batchSize - ); - cursor = nextCursor; - - for (const k of keys) { - buffer.push(k); - if (buffer.length >= delChunk) { - await flush(); - } - } - } while (cursor !== '0'); - - await flush(); - return; + const pattern = `${cachePrefix}${key}`; + const keys = await getAllKeysByPrefix(pattern); + if (keys.length > 0) { + await redis.del(keys); + } + } else { + await redis.set(versionKey, val); } - - await redis.set(versionKey, val); }; export const getVersionKey = async (key: `${SystemCacheKeyEnum}`, id?: string) => { diff --git a/packages/service/common/redis/index.ts b/packages/service/common/redis/index.ts index efbcbeb24..74a7eee89 100644 --- a/packages/service/common/redis/index.ts +++ b/packages/service/common/redis/index.ts @@ -44,22 +44,18 @@ export const getGlobalRedisConnection = () => { }; export const getAllKeysByPrefix = async (key: string) => { + if (!key) return []; + const redis = getGlobalRedisConnection(); const prefix = FASTGPT_REDIS_PREFIX; const pattern = `${prefix}${key}:*`; let cursor = '0'; - const batchSize = 1000; // SCAN 每次取多少 + const batchSize = 1000; // SCAN 每次取多少 const results: string[] = []; do { - const [nextCursor, keys] = await redis.scan( - cursor, - 'MATCH', - pattern, - 'COUNT', - batchSize - ); + const [nextCursor, keys] = await redis.scan(cursor, 'MATCH', pattern, 'COUNT', batchSize); cursor = nextCursor; for (const k of keys) { @@ -68,4 +64,4 @@ export const getAllKeysByPrefix = async (key: string) => { } while (cursor !== '0'); return results; -}; \ No newline at end of file +};