From b8cb1f29bd62358b830e4f0906e0b50465aac5f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mai=CC=81ra=20Bello?= Date: Mon, 30 Oct 2023 14:56:46 -0300 Subject: [PATCH] Create new optional request option for customizing local cache behavior --- src/HttpClient/middlewares/cache.ts | 26 +++++++++++++++++--------- src/HttpClient/typings.ts | 3 +++ src/caches/CacheLayer.ts | 7 ++++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/HttpClient/middlewares/cache.ts b/src/HttpClient/middlewares/cache.ts index b76c0407a..b671cc790 100644 --- a/src/HttpClient/middlewares/cache.ts +++ b/src/HttpClient/middlewares/cache.ts @@ -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 }) @@ -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, diff --git a/src/HttpClient/typings.ts b/src/HttpClient/typings.ts index 6d19620c0..d8417839f 100644 --- a/src/HttpClient/typings.ts +++ b/src/HttpClient/typings.ts @@ -51,8 +51,11 @@ export interface RequestConfig extends AxiosRequestConfig, RequestTracingConfig responseEncoding?: BufferEncoding nullIfNotFound?: boolean ignoreRecorder?: boolean + localCacheOptions?: LocalCacheOptions } +export type LocalCacheOptions = Record + export interface CacheHit { disk?: 0 | 1 memory?: 0 | 1 diff --git a/src/caches/CacheLayer.ts b/src/caches/CacheLayer.ts index 01db4d445..cbad4b49b 100644 --- a/src/caches/CacheLayer.ts +++ b/src/caches/CacheLayer.ts @@ -1,8 +1,9 @@ +import { LocalCacheOptions } from '../HttpClient'; import { FetchResult } from './typings' export interface CacheLayer { - get (key: K, fetcher?: () => Promise>): Promise | V | void, - has (key: K): Promise | boolean, - set (key: K, value: V, maxAge?: number | void): Promise | boolean, + get (key: K, fetcher?: () => Promise>, options?: LocalCacheOptions): Promise | V | void, + has (key: K, options?: LocalCacheOptions): Promise | boolean, + set (key: K, value: V, maxAge?: number | void, options?: LocalCacheOptions): Promise | boolean, getStats? (name?: string): any, }