@@ -82,9 +82,10 @@ const CacheTypeNames = {
82
82
interface CacheOptions {
83
83
type : CacheType
84
84
storage : CacheLayer < string , Cached >
85
+ asyncSet ?: Boolean
85
86
}
86
87
87
- export const cacheMiddleware = ( { type, storage } : CacheOptions ) => {
88
+ export const cacheMiddleware = ( { type, storage, asyncSet } : CacheOptions ) => {
88
89
const CACHE_RESULT_TAG = type === CacheType . Disk ? CustomHttpTags . HTTP_DISK_CACHE_RESULT : CustomHttpTags . HTTP_MEMORY_CACHE_RESULT
89
90
const cacheType = CacheTypeNames [ type ]
90
91
@@ -223,29 +224,34 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
223
224
224
225
const cacheWriteSpan = createCacheSpan ( cacheType , 'write' , tracer , span )
225
226
try {
226
- await storage . set ( setKey , {
227
- etag,
228
- expiration,
229
- response : { data : cacheableData , headers, status} ,
230
- responseEncoding,
231
- responseType,
232
- } )
233
-
234
- span ?. log ( {
235
- event : HttpLogEvents . LOCAL_CACHE_SAVED ,
236
- [ HttpCacheLogFields . CACHE_TYPE ] : cacheType ,
237
- [ HttpCacheLogFields . KEY_SET ] : setKey ,
238
- [ HttpCacheLogFields . AGE ] : currentAge ,
239
- [ HttpCacheLogFields . ETAG ] : etag ,
240
- [ HttpCacheLogFields . EXPIRATION_TIME ] : ( expiration - Date . now ( ) ) / 1000 ,
241
- [ HttpCacheLogFields . RESPONSE_ENCONDING ] : responseEncoding ,
242
- [ HttpCacheLogFields . RESPONSE_TYPE ] : responseType ,
243
- } )
227
+ const storageSet = ( ) =>
228
+ storage . set ( setKey , {
229
+ etag,
230
+ expiration,
231
+ response : { data : cacheableData , headers, status} ,
232
+ responseEncoding,
233
+ responseType,
234
+ } )
235
+ if ( asyncSet ) {
236
+ storageSet ( )
237
+ } else {
238
+ await storageSet ( )
239
+ span ?. log ( {
240
+ event : HttpLogEvents . LOCAL_CACHE_SAVED ,
241
+ [ HttpCacheLogFields . CACHE_TYPE ] : cacheType ,
242
+ [ HttpCacheLogFields . KEY_SET ] : setKey ,
243
+ [ HttpCacheLogFields . AGE ] : currentAge ,
244
+ [ HttpCacheLogFields . ETAG ] : etag ,
245
+ [ HttpCacheLogFields . EXPIRATION_TIME ] : ( expiration - Date . now ( ) ) / 1000 ,
246
+ [ HttpCacheLogFields . RESPONSE_ENCONDING ] : responseEncoding ,
247
+ [ HttpCacheLogFields . RESPONSE_TYPE ] : responseType ,
248
+ } )
249
+ }
244
250
} catch ( error ) {
245
- ErrorReport . create ( { originalError : error } ) . injectOnSpan ( cacheWriteSpan )
246
- logger ?. warn ( { message : 'Error writing to the HttpClient cache' , error } )
251
+ ErrorReport . create ( { originalError : error } ) . injectOnSpan ( cacheWriteSpan )
252
+ logger ?. warn ( { message : 'Error writing to the HttpClient cache' , error } )
247
253
} finally {
248
- cacheWriteSpan ?. finish ( )
254
+ cacheWriteSpan ?. finish ( )
249
255
}
250
256
251
257
return
0 commit comments