import { redis } from '../../config/redis.js'; export const cached = async (key, ttlSeconds, fetchFn) => { if (!redis) return fetchFn(); try { const hit = await redis.get(key); if (hit) return JSON.parse(hit); } catch { return fetchFn(); } const value = await fetchFn(); try { await redis.setEx(key, ttlSeconds, JSON.stringify(value)); } catch { // cache write failure is non-critical } return value; }; export const invalidateCache = async (...keys) => { if (!redis || !keys.length) return; try { await redis.del(keys); } catch { // non-critical } };