feat: better cache some stuff

This commit is contained in:
2023-08-09 23:51:40 +02:00
parent 6587ee689b
commit 3232f14bf7
10 changed files with 153 additions and 177 deletions

View File

@ -2,14 +2,6 @@ import { HandlerContext, Handlers } from "$fresh/server.ts";
import { searchMovie, searchTVShow } from "@lib/tmdb.ts";
import * as cache from "@lib/cache/cache.ts";
import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
import { json } from "@lib/helpers.ts";
type CachedMovieQuery = {
lastUpdated: number;
data: unknown;
};
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
const GET = async (
req: Request,
@ -30,27 +22,10 @@ const GET = async (
const type = u.searchParams.get("type") || "movie";
const cacheId = `/${type}/query/${query}`;
const cachedResponse = await cache.get<CachedMovieQuery>(cacheId);
if (
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
) {
return json(cachedResponse.data);
}
const res = type === "movie"
? await searchMovie(query)
: await searchTVShow(query);
cache.set(
cacheId,
JSON.stringify({
lastUpdated: Date.now(),
data: res,
}),
);
return new Response(JSON.stringify(res.results));
};