Feat: Plan cache (#5052)

* add cache for team member,app,dataset (#5007)

* add cache for team member,app,dataset

* update for hook

* update redis

* update

* perf: cache code

* fix: i18n

---------

Co-authored-by: gggaaallleee <91131304+gggaaallleee@users.noreply.github.com>
This commit is contained in:
Archer
2025-06-17 18:14:38 +08:00
committed by GitHub
parent af3221fa47
commit 41f73e6f7b
6 changed files with 104 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import { getTeamPlanStatus, getTeamStandPlan } from '../../support/wallet/sub/utils';
import { getTeamPlanStatus, getTeamStandPlan, getTeamPoints } from '../../support/wallet/sub/utils';
import { MongoApp } from '../../core/app/schema';
import { MongoDataset } from '../../core/dataset/schema';
import { DatasetTypeEnum } from '@fastgpt/global/core/dataset/constants';
@@ -10,11 +10,9 @@ import { TeamMemberStatusEnum } from '@fastgpt/global/support/user/team/constant
import { getVectorCountByTeamId } from '../../common/vectorDB/controller';
export const checkTeamAIPoints = async (teamId: string) => {
const { standardConstants, totalPoints, usedPoints } = await getTeamPlanStatus({
teamId
});
if (!global.subPlans?.standard) return;
if (!standardConstants) return;
const { totalPoints, usedPoints } = await getTeamPoints({ teamId });
if (usedPoints >= totalPoints) {
return Promise.reject(TeamErrEnum.aiPointsNotEnough);

View File

@@ -13,6 +13,14 @@ import dayjs from 'dayjs';
import { type ClientSession } from '../../../common/mongo';
import { addMonths } from 'date-fns';
import { readFromSecondary } from '../../../common/mongo/utils';
import {
setRedisCache,
getRedisCache,
delRedisCache,
CacheKeyEnum,
CacheKeyEnumTime,
incrValueToCache
} from '../../../common/redis/cache';
export const getStandardPlansConfig = () => {
return global?.subPlans?.standard;
@@ -168,6 +176,8 @@ export const getTeamPlanStatus = async ({
? standardPlans[standardPlan.currentSubLevel]
: undefined;
updateTeamPointsCache({ teamId, totalPoints, surplusPoints });
return {
[SubTypeEnum.standard]: standardPlan,
standardConstants: standardConstants
@@ -185,3 +195,59 @@ export const getTeamPlanStatus = async ({
datasetMaxSize: totalDatasetSize
};
};
export const clearTeamPointsCache = async (teamId: string) => {
const surplusCacheKey = `${CacheKeyEnum.team_point_surplus}:${teamId}`;
const totalCacheKey = `${CacheKeyEnum.team_point_total}:${teamId}`;
await Promise.all([delRedisCache(surplusCacheKey), delRedisCache(totalCacheKey)]);
};
export const incrTeamPointsCache = async ({ teamId, value }: { teamId: string; value: number }) => {
const surplusCacheKey = `${CacheKeyEnum.team_point_surplus}:${teamId}`;
await incrValueToCache(surplusCacheKey, value);
};
export const updateTeamPointsCache = async ({
teamId,
totalPoints,
surplusPoints
}: {
teamId: string;
totalPoints: number;
surplusPoints: number;
}) => {
const surplusCacheKey = `${CacheKeyEnum.team_point_surplus}:${teamId}`;
const totalCacheKey = `${CacheKeyEnum.team_point_total}:${teamId}`;
await Promise.all([
setRedisCache(surplusCacheKey, surplusPoints, CacheKeyEnumTime.team_point_surplus),
setRedisCache(totalCacheKey, totalPoints, CacheKeyEnumTime.team_point_total)
]);
};
export const getTeamPoints = async ({ teamId }: { teamId: string }) => {
const surplusCacheKey = `${CacheKeyEnum.team_point_surplus}:${teamId}`;
const totalCacheKey = `${CacheKeyEnum.team_point_total}:${teamId}`;
const [surplusCacheStr, totalCacheStr] = await Promise.all([
getRedisCache(surplusCacheKey),
getRedisCache(totalCacheKey)
]);
if (surplusCacheStr && totalCacheStr) {
const totalPoints = Number(totalCacheStr);
const surplusPoints = Number(surplusCacheStr);
return {
totalPoints,
surplusPoints,
usedPoints: totalPoints - surplusPoints
};
}
const planStatus = await getTeamPlanStatus({ teamId });
return {
totalPoints: planStatus.totalPoints,
surplusPoints: planStatus.totalPoints - planStatus.usedPoints,
usedPoints: planStatus.usedPoints
};
};