Skip to content

Commit

Permalink
Create new optional request option for customizing local cache behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mairatma committed Nov 8, 2023
1 parent 64cce6c commit 4624c52
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
26 changes: 17 additions & 9 deletions src/HttpClient/middlewares/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
})


const { localCacheOptions } = ctx.config

const cacheReadSpan = createCacheSpan(cacheType, 'read', tracer, span)
let cached: void | Cached
try {
const cacheHasWithSegment = await storage.has(keyWithSegment)
cached = cacheHasWithSegment ? await storage.get(keyWithSegment) : await storage.get(key)
const cacheHasWithSegment = await storage.has(keyWithSegment, localCacheOptions)
const keyToUse = cacheHasWithSegment ? keyWithSegment : key
cached = await storage.get(keyToUse, undefined, localCacheOptions)
} catch (error) {
ErrorReport.create({ originalError: error }).injectOnSpan(cacheReadSpan)
logger?.warn({ message: 'Error reading from the HttpClient cache', error })
Expand Down Expand Up @@ -223,13 +226,18 @@ 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,
})
await storage.set(
setKey,
{
etag,
expiration,
response: {data: cacheableData, headers, status},
responseEncoding,
responseType,
},
undefined,
localCacheOptions
)

span?.log({
event: HttpLogEvents.LOCAL_CACHE_SAVED,
Expand Down
3 changes: 3 additions & 0 deletions src/HttpClient/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ export interface RequestConfig extends AxiosRequestConfig, RequestTracingConfig
responseEncoding?: BufferEncoding
nullIfNotFound?: boolean
ignoreRecorder?: boolean
localCacheOptions?: LocalCacheOptions
}

export type LocalCacheOptions = Record<string, unknown>

export interface CacheHit {
disk?: 0 | 1
memory?: 0 | 1
Expand Down
7 changes: 4 additions & 3 deletions src/caches/CacheLayer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { LocalCacheOptions } from '../HttpClient'
import { FetchResult } from './typings'

export interface CacheLayer<K, V> {
get (key: K, fetcher?: () => Promise<FetchResult<V>>): Promise<V | void> | V | void,
has (key: K): Promise<boolean> | boolean,
set (key: K, value: V, maxAge?: number | void): Promise<boolean> | boolean,
get (key: K, fetcher?: () => Promise<FetchResult<V>>, options?: LocalCacheOptions): Promise<V | void> | V | void,
has (key: K, options?: LocalCacheOptions): Promise<boolean> | boolean,
set (key: K, value: V, maxAge?: number | void, options?: LocalCacheOptions): Promise<boolean> | boolean,
getStats? (name?: string): any,
}

0 comments on commit 4624c52

Please sign in to comment.