From 5f92a9859625f75bce4aa28c3cae211a5d18bcad Mon Sep 17 00:00:00 2001 From: ThatOneCalculator Date: Thu, 8 Jun 2023 13:19:46 -0700 Subject: [PATCH] perf: set patrons in redis --- .../src/server/api/endpoints/patrons.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/patrons.ts b/packages/backend/src/server/api/endpoints/patrons.ts index aa9d25cf0..a0a759812 100644 --- a/packages/backend/src/server/api/endpoints/patrons.ts +++ b/packages/backend/src/server/api/endpoints/patrons.ts @@ -1,5 +1,5 @@ import define from "../define.js"; -import Logger from "@/services/logger.js"; +import { redisClient } from "@/db/redis.js"; export const meta = { tags: ["meta"], @@ -17,16 +17,16 @@ export const paramDef = { export default define(meta, paramDef, async () => { let patrons; - await fetch( - "https://codeberg.org/calckey/calckey/raw/branch/develop/patrons.json", - ) - .then((response) => response.json()) - .then((data) => { - patrons = data["patrons"]; - }) - .catch((error) => { - console.error("Error fetching patrons:", error); - }); + const cachedPatrons = await redisClient.get("patrons"); + if (cachedPatrons) { + patrons = JSON.parse(cachedPatrons); + } + else { + patrons = await fetch( + "https://codeberg.org/calckey/calckey/raw/branch/develop/patrons.json", + ).then((response) => response.json()); + await redisClient.set("patrons", JSON.stringify(patrons), "EX", 3600); + } - return patrons; + return patrons["patrons"]; });