feat(api): add cache and force refresh for club events; fix event watch rate limits
- Introduce Redis caching for event lists to reduce redundant fetches and handle timeouts gracefully. Added a `force` query parameter to bypass cache when needed. - Added a check in `EventWatchSchedule` to skip processing if player list is empty, preventing errors from rate-limited or failed requests. - Updated the `listClubEvents` signature to accept the new `force` parameter and modified API endpoints to propagate this flag.
This commit is contained in:
parent
8d18796d9b
commit
04a347c981
@ -123,8 +123,10 @@ const server = Bun.serve({
|
|||||||
},
|
},
|
||||||
"/api/club/:clubid/events": {
|
"/api/club/:clubid/events": {
|
||||||
async GET(req) {
|
async GET(req) {
|
||||||
const page = Number(new URL(req.url).searchParams.get('page')) ?? 1;
|
const searchParams = new URL(req.url).searchParams;
|
||||||
const data = await KaiqiuService.listClubEvents(req.params.clubid, page);
|
const page = Number(searchParams.get('page')) ?? 1;
|
||||||
|
const force = Boolean(searchParams.get('force')) ?? false;
|
||||||
|
const data = await KaiqiuService.listClubEvents(req.params.clubid, page, force);
|
||||||
return Response.json(data);
|
return Response.json(data);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -134,11 +136,11 @@ const server = Bun.serve({
|
|||||||
const clubInfo = await KaiqiuService.getClubInfo(id);
|
const clubInfo = await KaiqiuService.getClubInfo(id);
|
||||||
let allEvents: IEventInfo[] = [];
|
let allEvents: IEventInfo[] = [];
|
||||||
let page = 1;
|
let page = 1;
|
||||||
let events = await KaiqiuService.listClubEvents(id, page);
|
let events = await KaiqiuService.listClubEvents(id, page, true);
|
||||||
allEvents = allEvents.concat(...events.data);
|
allEvents = allEvents.concat(...events.data);
|
||||||
while (events.data.every(e => !e.isFinished)) {
|
while (events.data.every(e => !e.isFinished)) {
|
||||||
page += 1;
|
page += 1;
|
||||||
events = await KaiqiuService.listClubEvents(id, page);
|
events = await KaiqiuService.listClubEvents(id, page, true);
|
||||||
allEvents = allEvents.concat(...events.data);
|
allEvents = allEvents.concat(...events.data);
|
||||||
}
|
}
|
||||||
const configs: ics.EventAttributes[] = allEvents.filter(e => !e.isFinished).map(e => ({
|
const configs: ics.EventAttributes[] = allEvents.filter(e => !e.isFinished).map(e => ({
|
||||||
|
|||||||
@ -26,6 +26,10 @@ export class EventWatchSchedule implements Schedule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async diffEvent(event: EventDetail) {
|
async diffEvent(event: EventDetail) {
|
||||||
|
if (event.players.length === 0) {
|
||||||
|
console.debug('名单为空,可能请求过于频繁,跳过处理...', event.title, event.eventId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const key = `my-kaiqiuwang:watch:${event.eventId}`;
|
const key = `my-kaiqiuwang:watch:${event.eventId}`;
|
||||||
const oldPlayers = await redis.get(key).then(s => s ? JSON.parse(s) as Player[] : []);
|
const oldPlayers = await redis.get(key).then(s => s ? JSON.parse(s) as Player[] : []);
|
||||||
await redis.set(key, JSON.stringify(event.players));
|
await redis.set(key, JSON.stringify(event.players));
|
||||||
|
|||||||
@ -61,12 +61,16 @@ export class KaiqiuService {
|
|||||||
return resp.text();
|
return resp.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async listClubEvents(clubId: string, page = 1) {
|
public static async listClubEvents(clubId: string, page = 1, force?: boolean) {
|
||||||
if (!clubId) return {
|
if (!clubId) return {
|
||||||
data: [],
|
data: [],
|
||||||
total: 0,
|
total: 0,
|
||||||
};
|
};
|
||||||
const html = await this.#fetchEventListHTML(clubId, page);
|
let html = await redis.get(`my-kaiqiuwang:events:${clubId}:${page}`);
|
||||||
|
if (!html || html.includes('连接超时') || force) {
|
||||||
|
html = await this.#fetchEventListHTML(clubId, page);
|
||||||
|
await redis.setex(`my-kaiqiuwang:events:${clubId}:${page}`, 60 * 3, html);
|
||||||
|
}
|
||||||
const data = await this.#parseEventList(html);
|
const data = await this.#parseEventList(html);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user