Add per-request CSP nonces (drop script unsafe-inline), post-deploy /api/health gate, bump next-auth to beta.32, and remove unused motion/cache/permission helpers. Co-authored-by: Cursor <[email protected]>
42 lines
793 B
TypeScript
42 lines
793 B
TypeScript
import "server-only";
|
|
|
|
import { redis } from "@/lib/redis";
|
|
|
|
/**
|
|
* Cache the result of a fetch function in Redis.
|
|
* Falls back to the fresh fetch if Redis is unavailable.
|
|
*/
|
|
export async function redisCache<T>(
|
|
key: string,
|
|
ttlSeconds: number,
|
|
fetch: () => Promise<T>,
|
|
): Promise<T> {
|
|
if (!redis) return fetch();
|
|
|
|
try {
|
|
const cached = await redis.get(key);
|
|
if (cached !== null) {
|
|
return JSON.parse(cached) as T;
|
|
}
|
|
} catch {
|
|
// cache miss or error — fall through to fresh fetch
|
|
}
|
|
|
|
const fresh = await fetch();
|
|
|
|
try {
|
|
await redis.setex(key, ttlSeconds, JSON.stringify(fresh));
|
|
} catch {
|
|
// ignore write errors
|
|
}
|
|
|
|
return fresh;
|
|
}
|
|
|
|
/**
|
|
* Build a namespaced cache key for API routes.
|
|
*/
|
|
export function apiCacheKey(path: string): string {
|
|
return `api:${path}`;
|
|
}
|