@@ -135,8 +135,9 @@ type CacheEntry = {
135135type CheckCacheOptions = {
136136 renewalKey : string ;
137137 renewalThreshold : number ;
138+ requestId : string ;
138139 expiration : number ;
139- options : CacheQueryResultOptions ;
140+ useInMemory : boolean ;
140141 spanId : string ;
141142 cacheKey : CacheKey ;
142143 primaryQuery : boolean ;
@@ -872,25 +873,28 @@ export class QueryCache {
872873 redisKey : string ,
873874 opts : CheckCacheOptions
874875 ) : Promise < any > {
875- // First check in-memory cache if enabled
876- if ( opts . options . useInMemory ) {
876+ if ( opts . useInMemory ) {
877877 const inMemoryResult = this . checkInMemoryCache ( redisKey , opts ) ;
878-
879878 if ( inMemoryResult ) {
880879 return inMemoryResult ;
881880 }
882881 }
883882
884- // If not found in memory, check cache driver
885- return this . cacheDriver . get ( redisKey ) ;
883+ const cachedResult = await this . cacheDriver . get ( redisKey ) ;
884+
885+ if ( opts . useInMemory ) {
886+ this . memoryCache . set ( redisKey , cachedResult , {
887+ ttl : opts . renewalThreshold
888+ } ) ;
889+ }
890+
891+ return cachedResult ;
886892 }
887893
888894 protected checkInMemoryCache (
889895 redisKey : string ,
890896 opts : CheckCacheOptions
891897 ) : any {
892- const inMemoryCacheDisablePeriod = 5 * 60 * 1000 ;
893-
894898 const inMemoryValue = this . memoryCache . get ( redisKey ) ;
895899 if ( ! inMemoryValue ) {
896900 return null ;
@@ -901,12 +905,9 @@ export class QueryCache {
901905 if (
902906 opts . renewalKey && (
903907 ! opts . renewalThreshold ||
904- ! inMemoryValue . time ||
905- // Do not cache in memory in last 5 minutes of expiry.
906- // Most likely it'll cause race condition of refreshing data with different refreshKey values.
907- renewedAgo + inMemoryCacheDisablePeriod > opts . renewalThreshold * 1000 ||
908+ renewedAgo > opts . renewalThreshold * 1000 ||
908909 inMemoryValue . renewalKey !== opts . renewalKey
909- ) || renewedAgo > opts . expiration * 1000 || renewedAgo > inMemoryCacheDisablePeriod
910+ ) || renewedAgo > opts . expiration * 1000
910911 ) {
911912 this . memoryCache . delete ( redisKey ) ;
912913 return null ;
@@ -919,7 +920,7 @@ export class QueryCache {
919920 renewalKey : inMemoryValue . renewalKey ,
920921 newRenewalKey : opts . renewalKey ,
921922 renewalThreshold : opts . renewalThreshold ,
922- requestId : opts . options . requestId ,
923+ requestId : opts . requestId ,
923924 spanId : opts . spanId ,
924925 primaryQuery : opts . primaryQuery ,
925926 renewCycle : opts . renewCycle
@@ -1002,17 +1003,17 @@ export class QueryCache {
10021003 const cachedResult = await this . checkInCache (
10031004 redisKey ,
10041005 {
1006+ requestId : options . requestId ,
1007+ useInMemory : options . useInMemory ,
10051008 renewalKey,
10061009 renewalThreshold,
10071010 expiration,
1008- options,
10091011 spanId,
10101012 cacheKey,
10111013 primaryQuery,
10121014 renewCycle
10131015 }
10141016 ) ;
1015-
10161017 if ( cachedResult ) {
10171018 const renewedAgo = ( new Date ( ) ) . getTime ( ) - cachedResult . time ;
10181019 this . logger ( 'Found cache entry' , {
@@ -1049,12 +1050,6 @@ export class QueryCache {
10491050 }
10501051
10511052 this . logger ( 'Using cache for' , { cacheKey, requestId : options . requestId , spanId, primaryQuery, renewCycle } ) ;
1052-
1053- const inMemoryCacheDisablePeriod = 5 * 60 * 1000 ;
1054- if ( options . useInMemory && renewedAgo + inMemoryCacheDisablePeriod <= renewalThreshold * 1000 ) {
1055- this . memoryCache . set ( redisKey , cachedResult ) ;
1056- }
1057-
10581053 return cachedResult . result ;
10591054 } else {
10601055 this . logger ( 'Missing cache for' , { cacheKey, requestId : options . requestId , spanId, primaryQuery, renewCycle } ) ;
0 commit comments