diff --git a/CHANGELOG.md b/CHANGELOG.md index 951a9780..0567aefb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] -## Fixed +## [6.47.0] - 2024-06-11 +### Added +- Add option to save asynchronous cache + +### Fixed - Add handler name for runtime http handlers ## [6.46.1] - 2024-01-31 diff --git a/package.json b/package.json index 1911d8df..2d2c5647 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vtex/api", - "version": "6.46.1", + "version": "6.47.0", "description": "VTEX I/O API client", "main": "lib/index.js", "typings": "lib/index.d.ts", 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..93771649 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,24 +224,29 @@ 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 }) diff --git a/src/HttpClient/typings.ts b/src/HttpClient/typings.ts index 6d19620c..fb42fdb4 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.