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,29 +2,63 @@ import * as cache from "@lib/cache/cache.ts";
import { MovieDb } from "https://esm.sh/moviedb-promise@3.4.1";
const moviedb = new MovieDb(Deno.env.get("TMDB_API_KEY") || "");
export function searchMovie(query: string) {
return moviedb.searchMovie({ query });
}
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
export function searchTVShow(query: string) {
return moviedb.searchTv({ query });
}
export const searchMovie = (query: string) =>
cache.cacheFunction({
fn: () => moviedb.searchMovie({ query }),
id: `query:moviesearch:${query}`,
options: {
expires: CACHE_INTERVAL,
},
});
export function getMovie(id: number) {
return moviedb.movieInfo({ id });
}
export const searchTVShow = (query: string) =>
cache.cacheFunction(
{
fn: () => moviedb.searchTv({ query }),
id: `query:tvshowsearch:${query}`,
options: {
expires: CACHE_INTERVAL,
},
},
);
export function getSeries(id: number) {
return moviedb.tvInfo({ id });
}
export const getMovie = (id: number) =>
cache.cacheFunction({
fn: () => moviedb.movieInfo({ id }),
id: `query:movie:${id}`,
options: {
expires: CACHE_INTERVAL,
},
});
export function getMovieCredits(id: number) {
return moviedb.movieCredits(id);
}
export const getSeries = (id: number) =>
cache.cacheFunction({
fn: () => moviedb.tvInfo({ id }),
id: `query:tvshow:${id}`,
options: {
expires: CACHE_INTERVAL,
},
});
export function getSeriesCredits(id: number) {
return moviedb.tvCredits(id);
}
export const getMovieCredits = (id: number) =>
cache.cacheFunction({
fn: () => moviedb.movieCredits(id),
id: `query:moviecredits:${id}`,
options: {
expires: CACHE_INTERVAL,
},
});
export const getSeriesCredits = (id: number) =>
cache.cacheFunction({
fn: () => moviedb.tvCredits(id),
id: `query:tvshowcredits:${id}`,
options: {
expires: CACHE_INTERVAL,
},
});
export async function getMovieGenre(id: number) {
const genres = await cache.get("/genres/movies");
@@ -36,14 +70,8 @@ export async function getSeriesGenre(id: number) {
}
export async function getMoviePoster(id: string): Promise<ArrayBuffer> {
const cachedPoster = await cache.get("posters:" + id);
if (cachedPoster) return cachedPoster as ArrayBuffer;
const posterUrl = `https://image.tmdb.org/t/p/original/${id}`;
const response = await fetch(posterUrl);
const poster = await response.arrayBuffer();
cache.set(`posters:${id}`, new Uint8Array());
return poster;
}