Fix and enhance cache handling and logging in server utils

- Corrected the default value assignment for REDIS_CACHE_HOUR using logical OR instead of nullish coalescing.
- Added a debug log to output the cache hour value.
- Added error handling for redis.get calls to ensure they return a string or an empty string in case of failure.
This commit is contained in:
kyuuseiryuu 2026-03-03 18:36:32 +09:00
parent 0ac1d2e31d
commit 4d73ce5f2b

View File

@ -14,8 +14,9 @@ if (!REQUIRED_ENVS.every(v => !!v)) {
console.error('Missing required environment variables. Please check your .env'); console.error('Missing required environment variables. Please check your .env');
process.exit(1); process.exit(1);
} }
export const REDIS_CACHE_HOUR = Number(process.env.REDIS_CACHE_HOUR) || 8;
export const REDIS_CACHE_HOUR = Number(process.env.REDIS_CACHE_HOUR) ?? 8; console.debug('Cache hour: %s', REDIS_CACHE_HOUR);
export const xcxApi = new XCXAPI(process.env.KAIQIUCC_TOKEN ?? ''); export const xcxApi = new XCXAPI(process.env.KAIQIUCC_TOKEN ?? '');
@ -43,10 +44,10 @@ const htmlRequestHeaders = {
*/ */
export async function listEvent(tagid: string): Promise<IEventInfo[]> { export async function listEvent(tagid: string): Promise<IEventInfo[]> {
const key = `my-kaiqiuwang:evnet:${tagid}`; const key = `my-kaiqiuwang:evnet:${tagid}`;
let html = await redis.get(key); let html = await redis.get(key).catch(() => '');
if (!html) { if (!html) {
html = await fetchEventListHTML(tagid); html = await fetchEventListHTML(tagid);
await redis.setex(key, 60 * 60 * REDIS_CACHE_HOUR, html); redis.setex(key, 60 * 60 * REDIS_CACHE_HOUR, html);
} }
return parseEventList(html); return parseEventList(html);
} }
@ -117,10 +118,10 @@ export function parseEventInfo(html: string) {
export async function getMatchInfo(matchId: string) { export async function getMatchInfo(matchId: string) {
const key = `my-kaiqiuwang:matchinfo:${matchId}`; const key = `my-kaiqiuwang:matchinfo:${matchId}`;
let html = await redis.get(key); let html = await redis.get(key).catch(() => '');
if (!html) { if (!html) {
html = await fetchEventContentHTML(matchId); html = await fetchEventContentHTML(matchId);
await redis.setex(key, 60 * 60 * REDIS_CACHE_HOUR, html); redis.setex(key, 60 * 60 * REDIS_CACHE_HOUR, html);
} }
return parseEventInfo(html); return parseEventInfo(html);
} }