From 4d73ce5f2b41696e057eda136c2f198650221682 Mon Sep 17 00:00:00 2001 From: kyuuseiryuu Date: Tue, 3 Mar 2026 18:36:32 +0900 Subject: [PATCH] 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. --- src/utils/server.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/utils/server.ts b/src/utils/server.ts index 7b22191..3bc7f45 100644 --- a/src/utils/server.ts +++ b/src/utils/server.ts @@ -14,8 +14,9 @@ if (!REQUIRED_ENVS.every(v => !!v)) { console.error('Missing required environment variables. Please check your .env'); 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 ?? ''); @@ -43,10 +44,10 @@ const htmlRequestHeaders = { */ export async function listEvent(tagid: string): Promise { const key = `my-kaiqiuwang:evnet:${tagid}`; - let html = await redis.get(key); + let html = await redis.get(key).catch(() => ''); if (!html) { 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); } @@ -117,10 +118,10 @@ export function parseEventInfo(html: string) { export async function getMatchInfo(matchId: string) { const key = `my-kaiqiuwang:matchinfo:${matchId}`; - let html = await redis.get(key); + let html = await redis.get(key).catch(() => ''); if (!html) { 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); }