From 1fae8206f2cdb44426aed1a283014bbca32c6115 Mon Sep 17 00:00:00 2001 From: kyuuseiryuu Date: Thu, 26 Feb 2026 20:42:18 +0900 Subject: [PATCH] refactor(redis): centralize Redis client and add HTML cache for event endpoints - export shared redis client from utils/server - reuse shared redis in uidScoreStore - cache listEvent HTML for 30 minutes - cache getMatchInfo HTML for 3 minutes --- src/services/uidScoreStore.ts | 5 +---- src/utils/server.ts | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/services/uidScoreStore.ts b/src/services/uidScoreStore.ts index 95590f0..d33e03d 100644 --- a/src/services/uidScoreStore.ts +++ b/src/services/uidScoreStore.ts @@ -1,7 +1,4 @@ -import { RedisClient } from 'bun'; -import { xcxApi } from '../utils/server'; - -const redis = new RedisClient('redis://default:redis_8YnmBw@192.168.50.126:6379'); +import { redis, xcxApi } from '../utils/server'; const getKey = (uid: string) => `my-kaiqiuwang:uid-score:${uid}`; diff --git a/src/utils/server.ts b/src/utils/server.ts index d87471c..43eb747 100644 --- a/src/utils/server.ts +++ b/src/utils/server.ts @@ -2,9 +2,12 @@ import type { IEventInfo, Player } from "../types"; import * as cheerio from "cheerio"; import { XCXAPI } from "../services/xcxApi"; import { BASE_URL } from "./common"; +import { RedisClient } from "bun"; export const xcxApi = new XCXAPI(process.env.KAIQIUCC_TOKEN ?? ''); +export const redis = new RedisClient('redis://default:redis_8YnmBw@192.168.50.126:6379'); + const htmlRequestHeaders = { "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", "accept-language": "zh-CN,zh;q=0.8", @@ -26,7 +29,13 @@ const htmlRequestHeaders = { * @param tagid 俱乐部 ID */ export async function listEvent(tagid: string): Promise { - return parseEventList(await fetchEventListHTML(tagid)); + const key = `my-kaiqiuwang:evnet:${tagid}`; + let html = await redis.get(key); + if (!html) { + html = await fetchEventListHTML(tagid); + await redis.setex(key, 60 * 30, html); + } + return parseEventList(html); } /** @@ -94,5 +103,11 @@ export function parseEventInfo(html: string) { } export async function getMatchInfo(matchId: string) { - return parseEventInfo(await fetchEventContentHTML(matchId)); + const key = `my-kaiqiuwang:matchinfo:${matchId}`; + let html = await redis.get(key); + if (!html) { + html = await fetchEventContentHTML(matchId); + await redis.setex(key, 60 * 3, html); + } + return parseEventInfo(html); }