feat: completely remove redis

This commit is contained in:
2025-01-06 16:14:29 +01:00
parent d3009ac315
commit 53c4d5b129
24 changed files with 629 additions and 311 deletions

View File

@@ -1,8 +1,8 @@
import * as cache from "@lib/cache/cache.ts";
import * as openai from "@lib/openai.ts";
import * as tmdb from "@lib/tmdb.ts";
import { GenericResource } from "@lib/types.ts";
import { parseRating } from "@lib/helpers.ts";
import { createCache } from "@lib/cache.ts";
type RecommendationResource = {
id: string;
@@ -15,12 +15,14 @@ type RecommendationResource = {
year?: number;
};
const cache = createCache<RecommendationResource>();
export async function createRecommendationResource(
res: GenericResource,
description?: string,
) {
const cacheId = `recommendations:${res.type}:${res.id.replaceAll(":", "")}`;
const resource: RecommendationResource = await cache.get(cacheId) || {
const cacheId = `${res.type}:${res.id.replaceAll(":", "")}`;
const resource = cache.get(cacheId) || {
id: res.id,
type: res.type,
rating: -1,
@@ -58,20 +60,15 @@ export async function createRecommendationResource(
cache.set(cacheId, JSON.stringify(resource));
}
export async function getRecommendation(
export function getRecommendation(
id: string,
type: string,
): Promise<RecommendationResource | null> {
const res = await cache.get(`recommendations:${type}:${id}`) as string;
try {
return JSON.parse(res);
} catch (_) {
return null;
}
): RecommendationResource | undefined {
return cache.get(`recommendations:${type}:${id}`);
}
export async function getSimilarMovies(id: string) {
const recs = await getRecommendation(id, "movie");
const recs = getRecommendation(id, "movie");
if (!recs?.keywords?.length) return;
const recommendations = await openai.getMovieRecommendations(
@@ -91,8 +88,7 @@ export async function getSimilarMovies(id: string) {
export async function getAllRecommendations(): Promise<
RecommendationResource[]
> {
const keys = await cache.keys("recommendations:movie:*");
return Promise.all(keys.map((k) => cache.get(k))).then((res) =>
res.map((r) => JSON.parse(r))
);
const keys = cache.keys("recommendations:movie:*");
const res = await Promise.all(keys.map((k) => cache.get(k)));
return res.map((r) => JSON.parse(r));
}