feat: completely remove redis
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user