短剧推荐增加缓存
This commit is contained in:
@@ -9,6 +9,12 @@ import { cleanHtmlTags } from '@/lib/utils';
|
|||||||
|
|
||||||
export const runtime = 'nodejs';
|
export const runtime = 'nodejs';
|
||||||
|
|
||||||
|
// 服务端内存缓存
|
||||||
|
let cachedRecommends: {
|
||||||
|
timestamp: number;
|
||||||
|
data: SearchResult[];
|
||||||
|
} | null = null;
|
||||||
|
|
||||||
interface ApiSearchItem {
|
interface ApiSearchItem {
|
||||||
vod_id: string;
|
vod_id: string;
|
||||||
vod_name: string;
|
vod_name: string;
|
||||||
@@ -34,6 +40,27 @@ interface CmsClassResponse {
|
|||||||
*/
|
*/
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
|
// 检查内存缓存
|
||||||
|
const now = Date.now();
|
||||||
|
const CACHE_DURATION = 60 * 60 * 1000; // 1小时
|
||||||
|
|
||||||
|
if (cachedRecommends && now - cachedRecommends.timestamp < CACHE_DURATION) {
|
||||||
|
console.log('使用缓存的短剧推荐数据');
|
||||||
|
const cacheTime = await getCacheTime();
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
code: 200,
|
||||||
|
message: '获取成功',
|
||||||
|
data: cachedRecommends.data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Cache-Control': `public, max-age=${cacheTime}, s-maxage=${cacheTime}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// 获取短剧视频源列表
|
// 获取短剧视频源列表
|
||||||
const sources = await getDuanjuSources();
|
const sources = await getDuanjuSources();
|
||||||
|
|
||||||
@@ -167,6 +194,12 @@ export async function GET() {
|
|||||||
|
|
||||||
console.log(`返回 ${filteredVideos.length} 个短剧视频`);
|
console.log(`返回 ${filteredVideos.length} 个短剧视频`);
|
||||||
|
|
||||||
|
// 保存到内存缓存
|
||||||
|
cachedRecommends = {
|
||||||
|
timestamp: Date.now(),
|
||||||
|
data: filteredVideos,
|
||||||
|
};
|
||||||
|
|
||||||
const cacheTime = await getCacheTime();
|
const cacheTime = await getCacheTime();
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user