Skip to content

Commit 3dcf1c3

Browse files
committed
feat: add async set cache
1 parent 94f3dee commit 3dcf1c3

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

src/HttpClient/HttpClient.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class HttpClient {
5151
memoryCache,
5252
diskCache,
5353
memoizable = true,
54+
asyncSetCache,
5455
locale,
5556
name,
5657
metrics,
@@ -114,8 +115,8 @@ export class HttpClient {
114115
cancellationToken(cancellation),
115116
singleFlightMiddleware,
116117
acceptNotFoundMiddleware,
117-
...memoryCache ? [cacheMiddleware({ type: CacheType.Memory, storage: memoryCache })] : [],
118-
...diskCache ? [cacheMiddleware({ type: CacheType.Disk, storage: diskCache })] : [],
118+
...memoryCache ? [cacheMiddleware({ type: CacheType.Memory, storage: memoryCache, asyncSet: asyncSetCache })] : [],
119+
...diskCache ? [cacheMiddleware({ type: CacheType.Disk, storage: diskCache, asyncSet: asyncSetCache })] : [],
119120
notFoundFallbackMiddleware,
120121
routerCacheMiddleware,
121122
requestMiddleware(limit),

src/HttpClient/middlewares/cache.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ const CacheTypeNames = {
8282
interface CacheOptions {
8383
type: CacheType
8484
storage: CacheLayer<string, Cached>
85+
asyncSet?: Boolean
8586
}
8687

87-
export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
88+
export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => {
8889
const CACHE_RESULT_TAG = type === CacheType.Disk ? CustomHttpTags.HTTP_DISK_CACHE_RESULT : CustomHttpTags.HTTP_MEMORY_CACHE_RESULT
8990
const cacheType = CacheTypeNames[type]
9091

@@ -223,29 +224,34 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
223224

224225
const cacheWriteSpan = createCacheSpan(cacheType, 'write', tracer, span)
225226
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+
}
244250
} 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 })
247253
} finally {
248-
cacheWriteSpan?.finish()
254+
cacheWriteSpan?.finish()
249255
}
250256

251257
return

src/HttpClient/typings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export interface InstanceOptions {
8383
timeout?: number
8484
memoryCache?: CacheLayer<string, Cached>
8585
diskCache?: CacheLayer<string, Cached>
86+
asyncSetCache?: Boolean
8687

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

0 commit comments

Comments
 (0)