diff --git a/src/HttpClient/HttpClient.ts b/src/HttpClient/HttpClient.ts index 5b3e4ab2..fd439982 100644 --- a/src/HttpClient/HttpClient.ts +++ b/src/HttpClient/HttpClient.ts @@ -51,6 +51,7 @@ export class HttpClient { memoryCache, diskCache, memoizable = true, + asyncSetCache, locale, name, metrics, @@ -114,8 +115,8 @@ export class HttpClient { cancellationToken(cancellation), singleFlightMiddleware, acceptNotFoundMiddleware, - ...memoryCache ? [cacheMiddleware({ type: CacheType.Memory, storage: memoryCache })] : [], - ...diskCache ? [cacheMiddleware({ type: CacheType.Disk, storage: diskCache })] : [], + ...memoryCache ? [cacheMiddleware({ type: CacheType.Memory, storage: memoryCache, asyncSet: asyncSetCache })] : [], + ...diskCache ? [cacheMiddleware({ type: CacheType.Disk, storage: diskCache, asyncSet: asyncSetCache })] : [], notFoundFallbackMiddleware, routerCacheMiddleware, requestMiddleware(limit), diff --git a/src/HttpClient/middlewares/cache.ts b/src/HttpClient/middlewares/cache.ts index b76c0407..835c7b5a 100644 --- a/src/HttpClient/middlewares/cache.ts +++ b/src/HttpClient/middlewares/cache.ts @@ -82,9 +82,10 @@ const CacheTypeNames = { interface CacheOptions { type: CacheType storage: CacheLayer + asyncSet?: Boolean } -export const cacheMiddleware = ({ type, storage }: CacheOptions) => { +export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => { const CACHE_RESULT_TAG = type === CacheType.Disk ? CustomHttpTags.HTTP_DISK_CACHE_RESULT : CustomHttpTags.HTTP_MEMORY_CACHE_RESULT const cacheType = CacheTypeNames[type] @@ -223,29 +224,34 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => { const cacheWriteSpan = createCacheSpan(cacheType, 'write', tracer, span) try { - await storage.set(setKey, { - etag, - expiration, - response: {data: cacheableData, headers, status}, - responseEncoding, - responseType, - }) - - span?.log({ - event: HttpLogEvents.LOCAL_CACHE_SAVED, - [HttpCacheLogFields.CACHE_TYPE]: cacheType, - [HttpCacheLogFields.KEY_SET]: setKey, - [HttpCacheLogFields.AGE]: currentAge, - [HttpCacheLogFields.ETAG]: etag, - [HttpCacheLogFields.EXPIRATION_TIME]: (expiration - Date.now())/1000, - [HttpCacheLogFields.RESPONSE_ENCONDING]: responseEncoding, - [HttpCacheLogFields.RESPONSE_TYPE]: responseType, - }) + const storageSet = () => + storage.set(setKey, { + etag, + expiration, + response: {data: cacheableData, headers, status}, + responseEncoding, + responseType, + }) + if (asyncSet) { + storageSet() + } else { + await storageSet() + span?.log({ + event: HttpLogEvents.LOCAL_CACHE_SAVED, + [HttpCacheLogFields.CACHE_TYPE]: cacheType, + [HttpCacheLogFields.KEY_SET]: setKey, + [HttpCacheLogFields.AGE]: currentAge, + [HttpCacheLogFields.ETAG]: etag, + [HttpCacheLogFields.EXPIRATION_TIME]: (expiration - Date.now())/1000, + [HttpCacheLogFields.RESPONSE_ENCONDING]: responseEncoding, + [HttpCacheLogFields.RESPONSE_TYPE]: responseType, + }) + } } catch (error) { - ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan) - logger?.warn({ message: 'Error writing to the HttpClient cache', error }) + ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan) + logger?.warn({ message: 'Error writing to the HttpClient cache', error }) } finally { - cacheWriteSpan?.finish() + cacheWriteSpan?.finish() } return diff --git a/src/HttpClient/typings.ts b/src/HttpClient/typings.ts index 6d19620c..54dfb73c 100644 --- a/src/HttpClient/typings.ts +++ b/src/HttpClient/typings.ts @@ -83,6 +83,7 @@ export interface InstanceOptions { timeout?: number memoryCache?: CacheLayer diskCache?: CacheLayer + asyncSetCache?: Boolean /** * Enables memoization, ephemeral within each request, for all requests of this client.