import { HandlerContext } from "$fresh/server.ts"; import { getMovieCredits } from "@lib/tmdb.ts"; import * as cache from "@lib/cache/cache.ts"; import { json } from "@lib/helpers.ts"; import { createLogger } from "@lib/log.ts"; type CachedMovieCredits = { lastUpdated: number; data: unknown; }; const CACHE_INTERVAL = 1000 * 60 * 24 * 30; const log = createLogger("api/tmdb"); export const handler = async ( _req: Request, _ctx: HandlerContext, ) => { const id = _ctx.params.id; if (!id) { return new Response("Bad Request", { status: 400, }); } log.debug("getting movie credits"); const cacheId = `/movie/credits/${id}`; const cachedResponse = await cache.get(cacheId); if ( cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL) ) { return json(cachedResponse.data); } const res = await getMovieCredits(+id); cache.set( cacheId, JSON.stringify({ lastUpdated: Date.now(), data: res, }), ); return json(res); };