@@ -26,12 +26,36 @@ const getVectorObj = () => {
2626 return new PgVectorCtrl ( ) ;
2727} ;
2828
29- const getChcheKey = ( teamId : string ) => `${ CacheKeyEnum . team_vector_count } :${ teamId } ` ;
30- const onDelCache = throttle ( ( teamId : string ) => delRedisCache ( getChcheKey ( teamId ) ) , 30000 , {
31- leading : true ,
32- trailing : true
33- } ) ;
34- const onIncrCache = ( teamId : string ) => incrValueToCache ( getChcheKey ( teamId ) , 1 ) ;
29+ const teamVectorCache = {
30+ getKey : function ( teamId : string ) {
31+ return `${ CacheKeyEnum . team_vector_count } :${ teamId } ` ;
32+ } ,
33+ get : async function ( teamId : string ) {
34+ const countStr = await getRedisCache ( teamVectorCache . getKey ( teamId ) ) ;
35+ if ( countStr ) {
36+ return Number ( countStr ) ;
37+ }
38+ return undefined ;
39+ } ,
40+ set : function ( { teamId, count } : { teamId : string ; count : number } ) {
41+ retryFn ( ( ) =>
42+ setRedisCache ( teamVectorCache . getKey ( teamId ) , count , CacheKeyEnumTime . team_vector_count )
43+ ) . catch ( ) ;
44+ } ,
45+ delete : throttle (
46+ function ( teamId : string ) {
47+ return retryFn ( ( ) => delRedisCache ( teamVectorCache . getKey ( teamId ) ) ) . catch ( ) ;
48+ } ,
49+ 30000 ,
50+ {
51+ leading : true ,
52+ trailing : true
53+ }
54+ ) ,
55+ incr : function ( teamId : string , count : number ) {
56+ retryFn ( ( ) => incrValueToCache ( teamVectorCache . getKey ( teamId ) , count ) ) . catch ( ) ;
57+ }
58+ } ;
3559
3660const Vector = getVectorObj ( ) ;
3761
@@ -41,16 +65,17 @@ export const recallFromVectorStore = (props: EmbeddingRecallCtrlProps) =>
4165export const getVectorDataByTime = Vector . getVectorDataByTime ;
4266
4367export const getVectorCountByTeamId = async ( teamId : string ) => {
44- const key = getChcheKey ( teamId ) ;
45-
46- const countStr = await getRedisCache ( key ) ;
47- if ( countStr ) {
48- return Number ( countStr ) ;
68+ const cacheCount = await teamVectorCache . get ( teamId ) ;
69+ if ( cacheCount !== undefined ) {
70+ return cacheCount ;
4971 }
5072
5173 const count = await Vector . getVectorCountByTeamId ( teamId ) ;
5274
53- await setRedisCache ( key , count , CacheKeyEnumTime . team_vector_count ) ;
75+ teamVectorCache . set ( {
76+ teamId,
77+ count
78+ } ) ;
5479
5580 return count ;
5681} ;
@@ -78,7 +103,7 @@ export const insertDatasetDataVector = async ({
78103 } )
79104 ) ;
80105
81- onIncrCache ( props . teamId ) ;
106+ teamVectorCache . incr ( props . teamId , insertIds . length ) ;
82107
83108 return {
84109 tokens,
@@ -88,6 +113,6 @@ export const insertDatasetDataVector = async ({
88113
89114export const deleteDatasetDataVector = async ( props : DelDatasetVectorCtrlProps ) => {
90115 const result = await retryFn ( ( ) => Vector . delete ( props ) ) ;
91- onDelCache ( props . teamId ) ;
116+ teamVectorCache . delete ( props . teamId ) ;
92117 return result ;
93118} ;
0 commit comments