feat: better cache some stuff
This commit is contained in:
24
lib/cache/cache.ts
vendored
24
lib/cache/cache.ts
vendored
@ -104,3 +104,27 @@ export async function set<T extends RedisValue>(
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
export const cacheFunction = async <T extends (() => Promise<unknown>)>(
|
||||
{
|
||||
fn,
|
||||
id,
|
||||
options = {},
|
||||
}: {
|
||||
fn: T;
|
||||
id: string;
|
||||
options?: RedisOptions;
|
||||
},
|
||||
): Promise<Awaited<ReturnType<T>>> => {
|
||||
const cacheResult = await get(id) as string;
|
||||
|
||||
if (cacheResult) {
|
||||
return JSON.parse(cacheResult) as Awaited<ReturnType<typeof fn>>;
|
||||
}
|
||||
|
||||
const result = await fn();
|
||||
|
||||
set(id, JSON.stringify(result), options);
|
||||
|
||||
return result as Awaited<ReturnType<typeof fn>>;
|
||||
};
|
||||
|
@ -19,10 +19,10 @@ export type Movie = {
|
||||
};
|
||||
};
|
||||
|
||||
function renderMovie(movie: Movie) {
|
||||
export function renderMovie(movie: Movie) {
|
||||
const meta = movie.meta;
|
||||
if ("date" in meta) {
|
||||
meta.date = formatDate(meta.date);
|
||||
if ("date" in meta && typeof meta.date !== "string") {
|
||||
meta.date = formatDate(meta.date) as unknown as Date;
|
||||
}
|
||||
|
||||
return fixRenderedMarkdown(`${
|
||||
|
76
lib/tmdb.ts
76
lib/tmdb.ts
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user