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,6 +1,31 @@
import lineToday from "~/server/scrape/line_today";
import sql from "~/server/components/postgres";
import saveDataToSql from "~/server/scrape/save_scrape_data";
interface CacheItems {
title: string;
paragraph: string[];
origin: string;
author: string;
images: string[];
cached: boolean;
articleId: string;
timestamp: number;
}
const CACHE_DURATION = 1000 * 60 * 60;
const cache: Record<string, CacheItems> = {};
function cleanupCache() {
const now = Date.now();
Object.keys(cache).forEach((key) => {
if (now - cache[key].timestamp > CACHE_DURATION) {
delete cache[key];
}
});
}
setInterval(cleanupCache, CACHE_DURATION);
function cleanUpSlug(orgslug: string) {
let slug = orgslug.trim();
@ -14,16 +39,36 @@ function cleanUpSlug(orgslug: string) {
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, "slug");
const cleanSlug = cleanUpSlug(slug);
/*const result = await sql`
select * from articles_lt
where slug = ${cleanSlug}
`;*/
if (false) {
//return result;
} else {
if (
cache[cleanSlug] &&
Date.now() - cache[cleanSlug].timestamp < CACHE_DURATION
) {
return {
...cache[cleanSlug],
cached: true,
};
}
try {
const data = await lineToday(cleanSlug);
//saveDataToSql(data, slug);
console.log(data);
return data;
cache[cleanSlug] = {
...data,
timestamp: Date.now(),
};
return {
...data,
cached: false,
};
} catch (e) {
if (cache[cleanSlug]) {
return {
...cache[cleanSlug],
cached: true,
};
}
throw createError({
statusCode: 500,
message: "SERVER_SIDE_ERROR",
});
}
});