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
This commit is contained in:
kyuuseiryuu 2026-02-26 20:42:18 +09:00
parent b7d2109f2e
commit 1fae8206f2
2 changed files with 18 additions and 6 deletions

View File

@ -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}`;

View File

@ -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<IEventInfo[]> {
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);
}