Add caching into the code to make it faster.

This commit is contained in:
yuanhau 2025-05-20 20:14:29 +08:00
parent 2d8628d0da
commit bc9a63f6ab
3 changed files with 101 additions and 15 deletions

View file

@ -1,5 +1,17 @@
// Check /about/scraping_line_today_home.md for more info or https://news.yuanhau.com/datainfo/linetodayjsondata.json
interface CacheItem {
data: string[];
timestamp: number;
}
const cache: Record<string, CacheItem> = {};
const CACHE_DURATION = 1000 * 60 * 60; // 1 Hour
async function getLineTodayData(type: string) {
if (cache[type] && Date.now() - cache[type].timestamp < CACHE_DURATION) {
console.log("Serving from cache for type:", type);
return cache[type].data;
}
try {
const buildUrl = `https://today.line.me/_next/data/v1/tw/v3/tab/${type}.json?tabs=${type}`;
const req = await fetch(buildUrl, {
@ -21,17 +33,42 @@ async function getLineTodayData(type: string) {
req3.push(listing.id);
}
});
} else if (listings && listings.id) {
req3.push(listings.id);
}
});
cache[type] = {
data: req3,
timestamp: Date.now(),
};
return req3;
} catch (e) {
console.log(e);
if (cache[type]) {
console.log("Serving expired cache due to error");
return cache[type].data;
}
return [];
}
}
function filterUUIDs(ids: string[]): string[] {
const uuidPattern =
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
return ids.filter((id) => uuidPattern.test(id));
}
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, "slug");
return await getLineTodayData(slug);
const query = getQuery(event);
if (!query.query) {
return {
error: "NOT_A_QUERY",
};
}
const data = await getLineTodayData(String(query.query));
const validUUIDs = filterUUIDs(data || []);
return {
data: validUUIDs,
cached: !!cache[String(query.query)],
};
});