16 lines
398 B
TypeScript
16 lines
398 B
TypeScript
import { IRedisClient } from '../core/configs/cache/IRedisClient';
|
|
|
|
export async function getOrSetCache<T>(
|
|
redisClient: IRedisClient,
|
|
key: string,
|
|
ttlSeconds: number,
|
|
fallback: () => Promise<T>
|
|
): Promise<T> {
|
|
const cached = await redisClient.get<T>(key);
|
|
if (cached) return cached;
|
|
|
|
const data = await fallback();
|
|
await redisClient.set<T>(key, data, ttlSeconds);
|
|
return data;
|
|
}
|