Files
portalweb-api/src/shared/cache.util.ts
2025-03-29 15:06:21 -03:00

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;
}