From 0d040fb5b0b64e39bc0ad25eb7a84dabafab4e68 Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 21 Dec 2024 01:22:48 +0800 Subject: [PATCH] fix: use cache --- lib/routes/163/music/djradio.ts | 41 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/routes/163/music/djradio.ts b/lib/routes/163/music/djradio.ts index 844f8de1da9ed5..665cbd3fddab11 100644 --- a/lib/routes/163/music/djradio.ts +++ b/lib/routes/163/music/djradio.ts @@ -6,6 +6,8 @@ import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import { art } from '@/utils/render'; import path from 'node:path'; +import cache from '@/utils/cache'; +import { config } from '@/config'; export const route: Route = { path: '/music/djradio/:id/:info?', @@ -25,36 +27,39 @@ export const route: Route = { handler, }; +const ProcessFeed = (id, limit, offset) => + cache.tryGet( + `163:music:djradio:${id}:${limit}:${offset}`, + async () => + await got.post('https://music.163.com/api/dj/program/byradio', { + headers: { + Referer: 'https://music.163.com/', + }, + form: { + radioId: id, + limit, + offset, + }, + }), + config.cache.routeExpire, + false + ); + async function handler(ctx) { const id = ctx.req.param('id'); const info = !ctx.req.param('info'); - const ProcessFeed = (limit, offset) => - got.post('https://music.163.com/api/dj/program/byradio', { - headers: { - Referer: 'https://music.163.com/', - }, - form: { - radioId: id, - limit, - offset, - }, - }); - - const response = await ProcessFeed(1, 0); + const response = await ProcessFeed(id, 1, 0); const programs = response.data.programs || []; const { radio, dj } = programs[0] || { radio: {}, dj: {} }; const count = response.data.count || 0; - const countPage = []; - for (let i = 0; i < Math.ceil(count / 500); i++) { - countPage.push(i); - } + const countPage = Array.from({ length: Math.ceil(count / 500) }, (_, i) => i); const items = await Promise.all( countPage.map(async (item) => { - const response = await ProcessFeed(500, item * 500); + const response = await ProcessFeed(id, 500, item * 500); const programs = response.data.programs || []; const list = programs.map((pg) => { const description = (pg.description || '').split('\n').map((p) => p);