mirror of
https://github.com/ld3z/new-manga.git
synced 2026-11-09 20:13:44 -07:00
introduce caching
This commit is contained in:
parent
3c76e395ab
commit
07d245e94d
2 changed files with 35 additions and 2 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import type { Comic, Genre, ComicType } from './types';
|
||||
import { getRedisClient } from './db';
|
||||
|
||||
const languageMap = {
|
||||
"en": "English",
|
||||
|
|
@ -256,9 +257,27 @@ export async function getChaptersForSlugs(
|
|||
): Promise<ChapterDetail[]> {
|
||||
console.log(`Fetching chapters for slugs:`, slugs, `in language: ${lang}`);
|
||||
const chapters: ChapterDetail[] = [];
|
||||
const redis = await getRedisClient();
|
||||
|
||||
// Cache results for 1 hour (3600 seconds)
|
||||
const CACHE_TTL = 60 * 60;
|
||||
|
||||
for (const slug of slugs) {
|
||||
console.log(`Processing slug: ${slug}`);
|
||||
const cacheKey = `chapters:${slug}:${lang}`;
|
||||
|
||||
try {
|
||||
// Try to get cached chapters
|
||||
const cachedChapters = await redis.get(cacheKey);
|
||||
if (cachedChapters) {
|
||||
console.log(`Using cached chapters for ${slug}`);
|
||||
chapters.push(...JSON.parse(cachedChapters));
|
||||
continue;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Redis cache read error:', error);
|
||||
}
|
||||
|
||||
const comicDetail = await getComicBySlug(slug);
|
||||
|
||||
if (!comicDetail) {
|
||||
|
|
@ -284,6 +303,19 @@ export async function getChaptersForSlugs(
|
|||
|
||||
if (comicChapters.length > 0) {
|
||||
chapters.push(...comicChapters);
|
||||
|
||||
// Cache the results
|
||||
try {
|
||||
await redis.set(
|
||||
cacheKey,
|
||||
JSON.stringify(comicChapters),
|
||||
'EX',
|
||||
CACHE_TTL
|
||||
);
|
||||
console.log(`Cached chapters for ${slug}`);
|
||||
} catch (error) {
|
||||
console.error('Redis cache write error:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue