Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new optional request option for customizing local cache behavior #553

Open
wants to merge 2 commits into
base: feat/vbaseReqConfig
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]

### Added
- Create new optional request option for customizing local cache behavior
- Allow passing request config to VBase's getJSON method

## [6.46.0] - 2023-10-25
Expand Down
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,
Comment on lines +5 to +7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows all cache classes to receive this new optional param.

getStats? (name?: string): any,
}
Loading