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); }