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:
parent
b7d2109f2e
commit
1fae8206f2
@ -1,7 +1,4 @@
|
|||||||
import { RedisClient } from 'bun';
|
import { redis, xcxApi } from '../utils/server';
|
||||||
import { xcxApi } from '../utils/server';
|
|
||||||
|
|
||||||
const redis = new RedisClient('redis://default:redis_8YnmBw@192.168.50.126:6379');
|
|
||||||
|
|
||||||
const getKey = (uid: string) => `my-kaiqiuwang:uid-score:${uid}`;
|
const getKey = (uid: string) => `my-kaiqiuwang:uid-score:${uid}`;
|
||||||
|
|
||||||
|
|||||||
@ -2,9 +2,12 @@ import type { IEventInfo, Player } from "../types";
|
|||||||
import * as cheerio from "cheerio";
|
import * as cheerio from "cheerio";
|
||||||
import { XCXAPI } from "../services/xcxApi";
|
import { XCXAPI } from "../services/xcxApi";
|
||||||
import { BASE_URL } from "./common";
|
import { BASE_URL } from "./common";
|
||||||
|
import { RedisClient } from "bun";
|
||||||
|
|
||||||
export const xcxApi = new XCXAPI(process.env.KAIQIUCC_TOKEN ?? '');
|
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 = {
|
const htmlRequestHeaders = {
|
||||||
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
|
"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",
|
"accept-language": "zh-CN,zh;q=0.8",
|
||||||
@ -26,7 +29,13 @@ const htmlRequestHeaders = {
|
|||||||
* @param tagid 俱乐部 ID
|
* @param tagid 俱乐部 ID
|
||||||
*/
|
*/
|
||||||
export async function listEvent(tagid: string): Promise<IEventInfo[]> {
|
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) {
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user