import "server-only"; import { cached, invalidateMemory } from "@/lib/cache"; import { redis } from "@/lib/redis"; const DEFAULT_CACHE_TTL = 60; /** * Redis-backed cached query with an in-process fast path (see `cached()`). * Use for read-heavy lookups that are safe to serve slightly stale * (login user, article bodies, …). Redis stays the shared source of truth. */ export async function cachedQuery( cacheKey: string, queryFn: () => Promise, ttl: number = DEFAULT_CACHE_TTL, ): Promise { return cached(cacheKey, ttl * 1000, queryFn); } /** Invalidate a single cache key immediately (memory + Redis). */ export async function invalidateKey(key: string): Promise { invalidateMemory(key); if (redis && redis.status !== "end") { try { return await redis.del(key); } catch { return 0; } } return 0; }