Skip to content

Commit

Permalink
Merge pull request #559 from vtex/feat/add-async-set-cache
Browse files Browse the repository at this point in the history
feat: add async set cache
  • Loading branch information
GusGuerra authored Jun 11, 2024
2 parents 94f3dee + be819d3 commit bfe0cae
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/HttpClient/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class HttpClient {
memoryCache,
diskCache,
memoizable = true,
asyncSetCache,
locale,
name,
metrics,
Expand Down Expand Up @@ -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),
Expand Down
44 changes: 25 additions & 19 deletions src/HttpClient/middlewares/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ const CacheTypeNames = {
interface CacheOptions {
type: CacheType
storage: CacheLayer<string, Cached>
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]

Expand Down Expand Up @@ -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 })
Expand Down
1 change: 1 addition & 0 deletions src/HttpClient/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export interface InstanceOptions {
timeout?: number
memoryCache?: CacheLayer<string, Cached>
diskCache?: CacheLayer<string, Cached>
asyncSetCache?: boolean

/**
* Enables memoization, ephemeral within each request, for all requests of this client.
Expand Down

0 comments on commit bfe0cae

Please sign in to comment.