Skip to main content

CacheOptions

@webda/cache


Interface: CacheOptions

Defined in: packages/cache/src/cache.ts:19

Options controlling cache key generation and storage behavior.

Properties​

cacheMap?​

optional cacheMap?: (options) => CacheMap

Defined in: packages/cache/src/cache.ts:82

Custom CacheMap implementation constructor.

Parameters​

options​

CacheOptions

Returns​

CacheMap


cacheStorage?​

optional cacheStorage?: (options?) => CacheStorage

Defined in: packages/cache/src/cache.ts:86

Custom CacheStorage implementation constructor.

Parameters​

options?​

CacheOptions

Returns​

CacheStorage


classKeyGenerator?​

optional classKeyGenerator?: (instance) => any

Defined in: packages/cache/src/cache.ts:32

Generator for the instance-level cache key (e.g., per this).

Parameters​

instance​

object

Returns​

any


enableStats?​

optional enableStats?: boolean

Defined in: packages/cache/src/cache.ts:50

Enable cache statistics tracking (hits, misses, evictions).


gcInterval?​

optional gcInterval?: number

Defined in: packages/cache/src/cache.ts:46

Automatic garbage collection interval (in milliseconds). When set, expired entries are automatically cleaned up.


hashStrategy?​

optional hashStrategy?: "sha256" | "simple"

Defined in: packages/cache/src/cache.ts:78

Algorithm to use for default argument key generation.

  • 'sha256' (default): Hashes the serialized arguments with SHA-256. Collision-resistant and produces fixed-length keys, but involves a crypto operation on every cache miss.
  • 'simple': Uses a typed, length-prefixed string encoding without hashing. Faster for hot paths but produces longer keys proportional to argument size.

Has no effect when a custom methodKeyGenerator is provided.


maxSize?​

optional maxSize?: number

Defined in: packages/cache/src/cache.ts:41

Maximum number of entries per instance cache. When exceeded, least recently used entries are evicted.


methodKeyGenerator?​

optional methodKeyGenerator?: (property, args) => string | undefined

Defined in: packages/cache/src/cache.ts:28

Define how to cache the method call. If undefined is returned then no caching is done.

Parameters​

property​

string

args​

any[]

Returns​

string | undefined


shouldCache?​

optional shouldCache?: (result) => boolean

Defined in: packages/cache/src/cache.ts:65

Predicate to decide whether a result should be cached. Return false to skip caching for a specific result value.

Called with the resolved value for async methods.

Parameters​

result​

any

Returns​

boolean

Examples​

// Only cache non-null results
shouldCache: (result) => result !== null
// Only cache successful HTTP responses
shouldCache: (result) => result?.status === 200

ttl?​

optional ttl?: number

Defined in: packages/cache/src/cache.ts:36

Define how long the cache should be kept (in milliseconds).