From b82a7eae98dd8f38a667bb5dd233f8a2a09edd7d Mon Sep 17 00:00:00 2001 From: naskya Date: Thu, 20 Jul 2023 07:35:27 +0000 Subject: [PATCH] perf: fetch patrons from local file --- README.md | 2 ++ .../src/server/api/endpoints/patrons.ts | 31 ++----------------- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 3fa382b82..ef562022d 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,8 @@ ## 細かい変更点 +- 支援者リストをファイルから読み込む + - 外部のサーバーが落ちるとユーザーページが開けなくなることを防ぐため - 閲覧注意の注釈と画像の代替テキストもアンテナで調べる対象にする - ~~「そぎぎ」でアンテナを作れる~~ - インデックス拒否に `noindex` に加えて `nofollow,noarchive,nocache,noimageindex` も指定 diff --git a/packages/backend/src/server/api/endpoints/patrons.ts b/packages/backend/src/server/api/endpoints/patrons.ts index b5feea20e..c9dc43fb2 100644 --- a/packages/backend/src/server/api/endpoints/patrons.ts +++ b/packages/backend/src/server/api/endpoints/patrons.ts @@ -1,5 +1,4 @@ import define from "../define.js"; -import { redisClient } from "@/db/redis.js"; import * as fs from "node:fs"; import { fileURLToPath } from "node:url"; import { dirname } from "node:path"; @@ -24,33 +23,9 @@ export const paramDef = { } as const; export default define(meta, paramDef, async (ps) => { - let patrons; - const cachedPatrons = await redisClient.get("patrons"); - if (!ps.forceUpdate && cachedPatrons) { - patrons = JSON.parse(cachedPatrons); - } else { - AbortSignal.timeout ??= function timeout(ms) { - const ctrl = new AbortController(); - setTimeout(() => ctrl.abort(), ms); - return ctrl.signal; - }; - - patrons = await fetch( - "https://gitlab.prometheus.systems/firefish/firefish/-/raw/develop/patrons.json", - { signal: AbortSignal.timeout(2000) }, - ) - .then((response) => response.json()) - .catch(() => { - const staticPatrons = JSON.parse( - fs.readFileSync( - `${_dirname}/../../../../../../patrons.json`, - "utf-8", - ), - ); - patrons = cachedPatrons ? JSON.parse(cachedPatrons) : staticPatrons; - }); - await redisClient.set("patrons", JSON.stringify(patrons), "EX", 3600); - } + const patrons = JSON.parse( + fs.readFileSync(`${_dirname}/../../../../../../patrons.json`, "utf-8"), + ); return { patrons: patrons["patrons"], sponsors: patrons["sponsors"],