diff --git a/config.example.json b/config.example.json index 38ea13b..dd7aff1 100644 --- a/config.example.json +++ b/config.example.json @@ -1,3 +1,4 @@ { + "cache_time": 7200, "api_site": {} } diff --git a/src/app/api/detail/route.ts b/src/app/api/detail/route.ts index 1b892a0..6ce5090 100644 --- a/src/app/api/detail/route.ts +++ b/src/app/api/detail/route.ts @@ -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 }, diff --git a/src/app/api/douban/route.ts b/src/app/api/douban/route.ts index 39b40b1..75b45a6 100644 --- a/src/app/api/douban/route.ts +++ b/src/app/api/douban/route.ts @@ -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); diff --git a/src/app/api/search/route.ts b/src/app/api/search/route.ts index 7b8fbd2..081f35a 100644 --- a/src/app/api/search/route.ts +++ b/src/app/api/search/route.ts @@ -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 }); } diff --git a/src/lib/config.ts b/src/lib/config.ts index 905525a..417faa8 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -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]) => ({