mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-05-21 13:47:31 +08:00
feat: add cache control
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
{
|
{
|
||||||
|
"cache_time": 7200,
|
||||||
"api_site": {}
|
"api_site": {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { NextResponse } from 'next/server';
|
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;
|
const M3U8_PATTERN = /(https?:\/\/[^"'\s]+?\.m3u8)/g;
|
||||||
|
|
||||||
@@ -218,7 +218,13 @@ export async function GET(request: Request) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await getVideoDetail(id, sourceCode);
|
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) {
|
} catch (error) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: (error as Error).message },
|
{ error: (error as Error).message },
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
import { getCacheTime } from '@/lib/config';
|
||||||
|
|
||||||
interface DoubanItem {
|
interface DoubanItem {
|
||||||
title: string;
|
title: string;
|
||||||
poster: string;
|
poster: string;
|
||||||
@@ -110,7 +112,12 @@ export async function GET(request: Request) {
|
|||||||
list: list,
|
list: list,
|
||||||
};
|
};
|
||||||
|
|
||||||
return NextResponse.json(response);
|
const cacheTime = getCacheTime();
|
||||||
|
return NextResponse.json(response, {
|
||||||
|
headers: {
|
||||||
|
'Cache-Control': `public, max-age=${cacheTime}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: '获取豆瓣数据失败', details: (error as Error).message },
|
{ error: '获取豆瓣数据失败', details: (error as Error).message },
|
||||||
@@ -173,7 +180,12 @@ function handleTop250(pageStart: number) {
|
|||||||
list: movies,
|
list: movies,
|
||||||
};
|
};
|
||||||
|
|
||||||
return NextResponse.json(apiResponse);
|
const cacheTime = getCacheTime();
|
||||||
|
return NextResponse.json(apiResponse, {
|
||||||
|
headers: {
|
||||||
|
'Cache-Control': `public, max-age=${cacheTime}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { NextResponse } from 'next/server';
|
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';
|
import { getVideoDetail } from '../detail/route';
|
||||||
|
|
||||||
@@ -160,7 +160,15 @@ export async function GET(request: Request) {
|
|||||||
const query = searchParams.get('q');
|
const query = searchParams.get('q');
|
||||||
|
|
||||||
if (!query) {
|
if (!query) {
|
||||||
return NextResponse.json({ results: [] });
|
const cacheTime = getCacheTime();
|
||||||
|
return NextResponse.json(
|
||||||
|
{ results: [] },
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Cache-Control': `public, max-age=${cacheTime}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiSites = getApiSites();
|
const apiSites = getApiSites();
|
||||||
@@ -169,8 +177,16 @@ export async function GET(request: Request) {
|
|||||||
try {
|
try {
|
||||||
const results = await Promise.all(searchPromises);
|
const results = await Promise.all(searchPromises);
|
||||||
const flattenedResults = results.flat();
|
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) {
|
} catch (error) {
|
||||||
return NextResponse.json({ error: '搜索失败' }, { status: 500 });
|
return NextResponse.json({ error: '搜索失败' }, { status: 500 });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ export interface ApiSite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Config {
|
export interface Config {
|
||||||
|
cache_time?: number;
|
||||||
api_site: {
|
api_site: {
|
||||||
[key: string]: ApiSite;
|
[key: string]: ApiSite;
|
||||||
};
|
};
|
||||||
@@ -49,6 +50,11 @@ export function getConfig(): Config {
|
|||||||
return parsedConfig;
|
return parsedConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getCacheTime(): number {
|
||||||
|
const config = getConfig();
|
||||||
|
return config.cache_time || 300; // 默认5分钟缓存
|
||||||
|
}
|
||||||
|
|
||||||
export function getApiSites(): ApiSite[] {
|
export function getApiSites(): ApiSite[] {
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
return Object.entries(config.api_site).map(([key, site]) => ({
|
return Object.entries(config.api_site).map(([key, site]) => ({
|
||||||
|
|||||||
Reference in New Issue
Block a user