feat: add cache control

This commit is contained in:
shinya
2025-06-19 12:45:46 +08:00
parent da46031ae6
commit 660b51d9bd
5 changed files with 48 additions and 7 deletions

View File

@@ -1,3 +1,4 @@
{
"cache_time": 7200,
"api_site": {}
}

View File

@@ -1,6 +1,6 @@
import { NextResponse } from 'next/server';
import { API_CONFIG, ApiSite, getApiSites } from '@/lib/config';
import { API_CONFIG, ApiSite, getApiSites, getCacheTime } from '@/lib/config';
const M3U8_PATTERN = /(https?:\/\/[^"'\s]+?\.m3u8)/g;
@@ -218,7 +218,13 @@ export async function GET(request: Request) {
try {
const result = await getVideoDetail(id, sourceCode);
return NextResponse.json(result);
const cacheTime = getCacheTime();
return NextResponse.json(result, {
headers: {
'Cache-Control': `public, max-age=${cacheTime}`,
},
});
} catch (error) {
return NextResponse.json(
{ error: (error as Error).message },

View File

@@ -1,5 +1,7 @@
import { NextResponse } from 'next/server';
import { getCacheTime } from '@/lib/config';
interface DoubanItem {
title: string;
poster: string;
@@ -110,7 +112,12 @@ export async function GET(request: Request) {
list: list,
};
return NextResponse.json(response);
const cacheTime = getCacheTime();
return NextResponse.json(response, {
headers: {
'Cache-Control': `public, max-age=${cacheTime}`,
},
});
} catch (error) {
return NextResponse.json(
{ error: '获取豆瓣数据失败', details: (error as Error).message },
@@ -173,7 +180,12 @@ function handleTop250(pageStart: number) {
list: movies,
};
return NextResponse.json(apiResponse);
const cacheTime = getCacheTime();
return NextResponse.json(apiResponse, {
headers: {
'Cache-Control': `public, max-age=${cacheTime}`,
},
});
})
.catch((error) => {
clearTimeout(timeoutId);

View File

@@ -1,6 +1,6 @@
import { NextResponse } from 'next/server';
import { API_CONFIG, ApiSite, getApiSites } from '@/lib/config';
import { API_CONFIG, ApiSite, getApiSites, getCacheTime } from '@/lib/config';
import { getVideoDetail } from '../detail/route';
@@ -160,7 +160,15 @@ export async function GET(request: Request) {
const query = searchParams.get('q');
if (!query) {
return NextResponse.json({ results: [] });
const cacheTime = getCacheTime();
return NextResponse.json(
{ results: [] },
{
headers: {
'Cache-Control': `public, max-age=${cacheTime}`,
},
}
);
}
const apiSites = getApiSites();
@@ -169,8 +177,16 @@ export async function GET(request: Request) {
try {
const results = await Promise.all(searchPromises);
const flattenedResults = results.flat();
const cacheTime = getCacheTime();
return NextResponse.json({ results: flattenedResults });
return NextResponse.json(
{ results: flattenedResults },
{
headers: {
'Cache-Control': `public, max-age=${cacheTime}`,
},
}
);
} catch (error) {
return NextResponse.json({ error: '搜索失败' }, { status: 500 });
}

View File

@@ -9,6 +9,7 @@ export interface ApiSite {
}
export interface Config {
cache_time?: number;
api_site: {
[key: string]: ApiSite;
};
@@ -49,6 +50,11 @@ export function getConfig(): Config {
return parsedConfig;
}
export function getCacheTime(): number {
const config = getConfig();
return config.cache_time || 300; // 默认5分钟缓存
}
export function getApiSites(): ApiSite[] {
const config = getConfig();
return Object.entries(config.api_site).map(([key, site]) => ({