@@ -190,7 +190,7 @@ export const getTeamPlanStatus = async ({
190190 ? standardPlans [ standardPlan . currentSubLevel ]
191191 : undefined ;
192192
193- updateTeamPointsCache ( { teamId, totalPoints, surplusPoints } ) ;
193+ teamPoint . updateTeamPointsCache ( { teamId, totalPoints, surplusPoints } ) ;
194194
195195 return {
196196 [ SubTypeEnum . standard ] : standardPlan ,
@@ -223,58 +223,99 @@ export const getTeamPlanStatus = async ({
223223 } ;
224224} ;
225225
226- export const clearTeamPointsCache = async ( teamId : string ) => {
227- const surplusCacheKey = `${ CacheKeyEnum . team_point_surplus } :${ teamId } ` ;
228- const totalCacheKey = `${ CacheKeyEnum . team_point_total } :${ teamId } ` ;
229-
230- await Promise . all ( [ delRedisCache ( surplusCacheKey ) , delRedisCache ( totalCacheKey ) ] ) ;
231- } ;
232-
233- export const incrTeamPointsCache = async ( { teamId, value } : { teamId : string ; value : number } ) => {
234- const surplusCacheKey = `${ CacheKeyEnum . team_point_surplus } :${ teamId } ` ;
235- await incrValueToCache ( surplusCacheKey , value ) ;
236- } ;
237- export const updateTeamPointsCache = async ( {
238- teamId,
239- totalPoints,
240- surplusPoints
241- } : {
242- teamId : string ;
243- totalPoints : number ;
244- surplusPoints : number ;
245- } ) => {
246- const surplusCacheKey = `${ CacheKeyEnum . team_point_surplus } :${ teamId } ` ;
247- const totalCacheKey = `${ CacheKeyEnum . team_point_total } :${ teamId } ` ;
226+ export const teamPoint = {
227+ getTeamPoints : async ( { teamId } : { teamId : string } ) => {
228+ const surplusCacheKey = `${ CacheKeyEnum . team_point_surplus } :${ teamId } ` ;
229+ const totalCacheKey = `${ CacheKeyEnum . team_point_total } :${ teamId } ` ;
230+
231+ const [ surplusCacheStr , totalCacheStr ] = await Promise . all ( [
232+ getRedisCache ( surplusCacheKey ) ,
233+ getRedisCache ( totalCacheKey )
234+ ] ) ;
235+
236+ if ( surplusCacheStr && totalCacheStr ) {
237+ const totalPoints = Number ( totalCacheStr ) ;
238+ const surplusPoints = Number ( surplusCacheStr ) ;
239+ return {
240+ totalPoints,
241+ surplusPoints,
242+ usedPoints : totalPoints - surplusPoints
243+ } ;
244+ }
248245
249- await Promise . all ( [
250- setRedisCache ( surplusCacheKey , surplusPoints , CacheKeyEnumTime . team_point_surplus ) ,
251- setRedisCache ( totalCacheKey , totalPoints , CacheKeyEnumTime . team_point_total )
252- ] ) ;
246+ const planStatus = await getTeamPlanStatus ( { teamId } ) ;
247+ return {
248+ totalPoints : planStatus . totalPoints ,
249+ surplusPoints : planStatus . totalPoints - planStatus . usedPoints ,
250+ usedPoints : planStatus . usedPoints
251+ } ;
252+ } ,
253+ incrTeamPointsCache : async ( { teamId, value } : { teamId : string ; value : number } ) => {
254+ const surplusCacheKey = `${ CacheKeyEnum . team_point_surplus } :${ teamId } ` ;
255+ await incrValueToCache ( surplusCacheKey , value ) ;
256+ } ,
257+ updateTeamPointsCache : async ( {
258+ teamId,
259+ totalPoints,
260+ surplusPoints
261+ } : {
262+ teamId : string ;
263+ totalPoints : number ;
264+ surplusPoints : number ;
265+ } ) => {
266+ const surplusCacheKey = `${ CacheKeyEnum . team_point_surplus } :${ teamId } ` ;
267+ const totalCacheKey = `${ CacheKeyEnum . team_point_total } :${ teamId } ` ;
268+
269+ await Promise . all ( [
270+ setRedisCache ( surplusCacheKey , surplusPoints , CacheKeyEnumTime . team_point_surplus ) ,
271+ setRedisCache ( totalCacheKey , totalPoints , CacheKeyEnumTime . team_point_total )
272+ ] ) ;
273+ } ,
274+ clearTeamPointsCache : async ( teamId : string ) => {
275+ const surplusCacheKey = `${ CacheKeyEnum . team_point_surplus } :${ teamId } ` ;
276+ const totalCacheKey = `${ CacheKeyEnum . team_point_total } :${ teamId } ` ;
277+
278+ await Promise . all ( [ delRedisCache ( surplusCacheKey ) , delRedisCache ( totalCacheKey ) ] ) ;
279+ }
253280} ;
281+ export const teamQPM = {
282+ getTeamQPMLimit : async ( teamId : string ) : Promise < number | null > => {
283+ // 1. 尝试从缓存中获取
284+ const cacheKey = `${ CacheKeyEnum . team_qpm_limit } :${ teamId } ` ;
285+ const cached = await getRedisCache ( cacheKey ) ;
286+
287+ if ( cached ) {
288+ return Number ( cached ) ;
289+ }
254290
255- export const getTeamPoints = async ( { teamId } : { teamId : string } ) => {
256- const surplusCacheKey = `${ CacheKeyEnum . team_point_surplus } :${ teamId } ` ;
257- const totalCacheKey = `${ CacheKeyEnum . team_point_total } :${ teamId } ` ;
291+ // 2. Computed
292+ const teamPlanStatus = await getTeamPlanStatus ( { teamId } ) ;
293+ const limit =
294+ teamPlanStatus [ SubTypeEnum . standard ] ?. requestsPerMinute ??
295+ teamPlanStatus . standardConstants ?. requestsPerMinute ;
258296
259- const [ surplusCacheStr , totalCacheStr ] = await Promise . all ( [
260- getRedisCache ( surplusCacheKey ) ,
261- getRedisCache ( totalCacheKey )
262- ] ) ;
297+ if ( ! limit ) {
298+ if ( process . env . CHAT_MAX_QPM ) return Number ( process . env . CHAT_MAX_QPM ) ;
299+ return null ;
300+ }
263301
264- if ( surplusCacheStr && totalCacheStr ) {
265- const totalPoints = Number ( totalCacheStr ) ;
266- const surplusPoints = Number ( surplusCacheStr ) ;
267- return {
268- totalPoints,
269- surplusPoints,
270- usedPoints : totalPoints - surplusPoints
271- } ;
302+ // 3. Set cache
303+ await teamQPM . setCachedTeamQPMLimit ( teamId , limit ) ;
304+
305+ return limit ;
306+ } ,
307+ setCachedTeamQPMLimit : async ( teamId : string , limit : number ) : Promise < void > => {
308+ const cacheKey = `${ CacheKeyEnum . team_qpm_limit } :${ teamId } ` ;
309+ await setRedisCache ( cacheKey , limit . toString ( ) , CacheKeyEnumTime . team_qpm_limit ) ;
310+ } ,
311+ clearTeamQPMLimitCache : async ( teamId : string ) : Promise < void > => {
312+ const cacheKey = `${ CacheKeyEnum . team_qpm_limit } :${ teamId } ` ;
313+ await delRedisCache ( cacheKey ) ;
272314 }
315+ } ;
273316
274- const planStatus = await getTeamPlanStatus ( { teamId } ) ;
275- return {
276- totalPoints : planStatus . totalPoints ,
277- surplusPoints : planStatus . totalPoints - planStatus . usedPoints ,
278- usedPoints : planStatus . usedPoints
279- } ;
317+ // controler
318+ export const clearTeamPlanCache = async ( teamId : string ) => {
319+ await teamPoint . clearTeamPointsCache ( teamId ) ;
320+ await teamQPM . clearTeamQPMLimitCache ( teamId ) ;
280321} ;
0 commit comments