Skip to main content

cacheWithRedis

@abimongo/core v1.1.4


@abimongo/core / cacheWithRedis

Function: cacheWithRedis()

cacheWithRedis<T>(client, key, fetcher, options): Promise<T>

Defined in: core/src/utils/cacheWithRedis.ts:27

Caches a value in Redis with optional tenant and namespace scoping. If the value is not found in cache, it runs the provided fetcher function to get the value, caches it, and then returns it.

Type Parameters

T

T

Parameters

client

any

key

string

The cache key to use.

fetcher

() => Promise<T>

A function that fetches the value if not cached.

options

CacheOptions = {}

Options for caching behavior.

Returns

Promise<T>

  • The cached or fetched value.

Example

const value = await cacheWithRedis(redisClient, 'myKey', async () => {
// Fetch from database or external API
return await fetchDataFromSource();