Skip to content

perf: add Redis cache for vector counting #4432

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/service/common/redis/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addLog } from '../system/log';
import Redis from 'ioredis';

const REDIS_URL = process.env.REDIS_URL ?? 'redis://localhost:6379';
Expand Down Expand Up @@ -25,3 +26,37 @@ export function newWorkerRedisConnection() {
});
return redis;
}

export function getGlobalRedisCacheConnection() {
if (global.redisCache) return global.redisCache;
const redis = new Redis(REDIS_URL, { keyPrefix: 'fastgpt:cache:' });
redis.on('connect', () => {
console.log('Redis connected');
});
redis.on('error', (error) => {
console.error('Redis connection error', error);
});
global.redisCache = redis;
return redis;
}

export async function checkAndIncr(redis: Redis, key: string, retry = 0) {
// start a transaction
await redis.watch(key);

const exists = await redis.exists(key);
if (exists) {
const result = await redis.multi().incr(key).exec();
const err = result?.[0][0];
if (err) {
addLog.error('redis opt checkAndIncr', err);
// retry
if (retry < 10) {
await checkAndIncr(redis, key, retry + 1);
} else {
// try to delete the dirty cache
await redis.del(key);
}
}
}
}
5 changes: 5 additions & 0 deletions packages/service/common/redis/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Redis from 'ioredis';

declare global {
var redisCache: Redis | null;
}
29 changes: 26 additions & 3 deletions packages/service/common/vectorStore/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import { PgVectorCtrl } from './pg/class';
import { ObVectorCtrl } from './oceanbase/class';
import { getVectorsByText } from '../../core/ai/embedding';
import { InsertVectorProps } from './controller.d';
import { DelDatasetVectorCtrlProps, InsertVectorProps } from './controller.d';
import { EmbeddingModelItemType } from '@fastgpt/global/core/ai/model.d';
import { MILVUS_ADDRESS, PG_ADDRESS, OCEANBASE_ADDRESS } from './constants';
import { MilvusCtrl } from './milvus/class';
import { getGlobalRedisCacheConnection, checkAndIncr } from '../redis';

const getVectorObj = () => {
if (PG_ADDRESS) return new PgVectorCtrl();
Expand All @@ -17,14 +18,28 @@ const getVectorObj = () => {

const Vector = getVectorObj();

const redis = getGlobalRedisCacheConnection();

export const initVectorStore = Vector.init;
export const deleteDatasetDataVector = Vector.delete;
export const recallFromVectorStore = Vector.embRecall;
export const getVectorDataByTime = Vector.getVectorDataByTime;
export const getVectorCountByTeamId = Vector.getVectorCountByTeamId;

export const getVectorCountByTeamId = async (teamId: string) => {
const countStr = await redis.getex(`countByTeamId:${teamId}`, 'EX', 1800);
if (!countStr) {
return await initCache(teamId);
}
return parseInt(countStr);
};
export const getVectorCountByDatasetId = Vector.getVectorCountByDatasetId;
export const getVectorCountByCollectionId = Vector.getVectorCountByCollectionId;

async function initCache(teamId: string) {
const count = await Vector.getVectorCountByTeamId(teamId);
await redis.setex(`countByTeamId:${teamId}`, 1800, count);
return count;
}

export const insertDatasetDataVector = async ({
model,
query,
Expand All @@ -43,8 +58,16 @@ export const insertDatasetDataVector = async ({
vector: vectors[0]
});

await checkAndIncr(redis, `countByTeamId:${props.teamId}`);

return {
tokens,
insertId
};
};

export const deleteDatasetDataVector = async (props: DelDatasetVectorCtrlProps) => {
const result = await Vector.delete(props);
await initCache(props.teamId);
return result;
};
Loading