Skip to content

Commit

Permalink
feat: add async set cache
Browse files Browse the repository at this point in the history
  • Loading branch information
vsseixaso committed Jun 3, 2024
1 parent 94f3dee commit 3dcf1c3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
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
50 changes: 28 additions & 22 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,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
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 3dcf1c3

Please sign in to comment.