mirror of
https://github.com/labring/FastGPT.git
synced 2025-12-25 20:02:47 +00:00
perf: get redis keys function
This commit is contained in:
parent
342aca0b21
commit
8c28f983cb
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue