Skip to content

Commit d9594b4

Browse files
committed
Create new optional request option for customizing local cache behavior
1 parent eba4ac9 commit d9594b4

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/HttpClient/middlewares/cache.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,14 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
107107
})
108108

109109

110+
const { localCacheOptions } = ctx.config
111+
110112
const cacheReadSpan = createCacheSpan(cacheType, 'read', tracer, span)
111113
let cached: void | Cached
112114
try {
113-
const cacheHasWithSegment = await storage.has(keyWithSegment)
114-
cached = cacheHasWithSegment ? await storage.get(keyWithSegment) : await storage.get(key)
115+
const cacheHasWithSegment = await storage.has(keyWithSegment, localCacheOptions)
116+
const keyToUse = cacheHasWithSegment ? keyWithSegment : key
117+
cached = await storage.get(keyToUse, undefined, localCacheOptions)
115118
} catch (error) {
116119
ErrorReport.create({ originalError: error }).injectOnSpan(cacheReadSpan)
117120
logger?.warn({ message: 'Error reading from the HttpClient cache', error })
@@ -223,13 +226,18 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
223226

224227
const cacheWriteSpan = createCacheSpan(cacheType, 'write', tracer, span)
225228
try {
226-
await storage.set(setKey, {
227-
etag,
228-
expiration,
229-
response: {data: cacheableData, headers, status},
230-
responseEncoding,
231-
responseType,
232-
})
229+
await storage.set(
230+
setKey,
231+
{
232+
etag,
233+
expiration,
234+
response: {data: cacheableData, headers, status},
235+
responseEncoding,
236+
responseType,
237+
},
238+
undefined,
239+
localCacheOptions
240+
)
233241

234242
span?.log({
235243
event: HttpLogEvents.LOCAL_CACHE_SAVED,

src/HttpClient/typings.ts

+3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ export interface RequestConfig extends AxiosRequestConfig, RequestTracingConfig
5151
responseEncoding?: BufferEncoding
5252
nullIfNotFound?: boolean
5353
ignoreRecorder?: boolean
54+
localCacheOptions?: LocalCacheOptions
5455
}
5556

57+
export type LocalCacheOptions = Record<string, unknown>
58+
5659
export interface CacheHit {
5760
disk?: 0 | 1
5861
memory?: 0 | 1

src/caches/CacheLayer.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { LocalCacheOptions } from '../HttpClient'
12
import { FetchResult } from './typings'
23

34
export interface CacheLayer<K, V> {
4-
get (key: K, fetcher?: () => Promise<FetchResult<V>>): Promise<V | void> | V | void,
5-
has (key: K): Promise<boolean> | boolean,
6-
set (key: K, value: V, maxAge?: number | void): Promise<boolean> | boolean,
5+
get (key: K, fetcher?: () => Promise<FetchResult<V>>, options?: LocalCacheOptions): Promise<V | void> | V | void,
6+
has (key: K, options?: LocalCacheOptions): Promise<boolean> | boolean,
7+
set (key: K, value: V, maxAge?: number | void, options?: LocalCacheOptions): Promise<boolean> | boolean,
78
getStats? (name?: string): any,
89
}

0 commit comments

Comments
 (0)