mirror of
https://github.com/ld3z/new-manga.git
synced 2026-11-09 21:13:46 -07:00
Compare commits
3 commits
af61e4f32c
...
b8499d90c6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8499d90c6 | ||
|
|
07d245e94d | ||
|
|
3c76e395ab |
2 changed files with 40 additions and 5 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import type { Comic, Genre, ComicType } from './types';
|
import type { Comic, Genre, ComicType } from './types';
|
||||||
|
import { getRedisClient } from './db';
|
||||||
|
|
||||||
const languageMap = {
|
const languageMap = {
|
||||||
"en": "English",
|
"en": "English",
|
||||||
|
|
@ -177,11 +178,11 @@ export async function getComicBySlug(slug: string): Promise<ComicDetail | null>
|
||||||
export async function getChaptersByHid(
|
export async function getChaptersByHid(
|
||||||
hid: string,
|
hid: string,
|
||||||
slug: string,
|
slug: string,
|
||||||
params: ChapterParams = { limit: 15, lang: 'en' }
|
params: ChapterParams = { limit: 5, lang: 'en' }
|
||||||
): Promise<ChapterDetail[]> {
|
): Promise<ChapterDetail[]> {
|
||||||
try {
|
try {
|
||||||
const queryParams = new URLSearchParams({
|
const queryParams = new URLSearchParams({
|
||||||
limit: params.limit?.toString() || '15',
|
limit: params.limit?.toString() || '5',
|
||||||
lang: params.lang || 'en',
|
lang: params.lang || 'en',
|
||||||
ordering: '-created_at'
|
ordering: '-created_at'
|
||||||
});
|
});
|
||||||
|
|
@ -256,9 +257,27 @@ export async function getChaptersForSlugs(
|
||||||
): Promise<ChapterDetail[]> {
|
): Promise<ChapterDetail[]> {
|
||||||
console.log(`Fetching chapters for slugs:`, slugs, `in language: ${lang}`);
|
console.log(`Fetching chapters for slugs:`, slugs, `in language: ${lang}`);
|
||||||
const chapters: ChapterDetail[] = [];
|
const chapters: ChapterDetail[] = [];
|
||||||
|
const redis = await getRedisClient();
|
||||||
|
|
||||||
|
// Cache results for 1 hour (3600 seconds)
|
||||||
|
const CACHE_TTL = 60 * 60;
|
||||||
|
|
||||||
for (const slug of slugs) {
|
for (const slug of slugs) {
|
||||||
console.log(`Processing slug: ${slug}`);
|
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);
|
const comicDetail = await getComicBySlug(slug);
|
||||||
|
|
||||||
if (!comicDetail) {
|
if (!comicDetail) {
|
||||||
|
|
@ -276,7 +295,7 @@ export async function getChaptersForSlugs(
|
||||||
comicDetail.comic.hid,
|
comicDetail.comic.hid,
|
||||||
slug,
|
slug,
|
||||||
{
|
{
|
||||||
limit: 15,
|
limit: 5,
|
||||||
lang: lang
|
lang: lang
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
@ -284,6 +303,19 @@ export async function getChaptersForSlugs(
|
||||||
|
|
||||||
if (comicChapters.length > 0) {
|
if (comicChapters.length > 0) {
|
||||||
chapters.push(...comicChapters);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ const REDIS_URL = import.meta.env.REDIS_URL || process.env.REDIS_URL;
|
||||||
let redis: Redis | null = null;
|
let redis: Redis | null = null;
|
||||||
let redisRetryCount = 0;
|
let redisRetryCount = 0;
|
||||||
const MAX_RETRIES = 3;
|
const MAX_RETRIES = 3;
|
||||||
|
const DEFAULT_CACHE_TTL = 60 * 60 * 24 * 30; // 30 days
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('Environment check:', {
|
console.log('Environment check:', {
|
||||||
|
|
@ -60,7 +61,7 @@ try {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getRedisClient(): Promise<Redis> {
|
export async function getRedisClient(): Promise<Redis> {
|
||||||
if (redis?.status === 'ready') return redis;
|
if (redis?.status === 'ready') return redis;
|
||||||
|
|
||||||
if (redisRetryCount >= MAX_RETRIES) {
|
if (redisRetryCount >= MAX_RETRIES) {
|
||||||
|
|
@ -109,7 +110,7 @@ export async function storeFeedMapping(feedId: string, slugs: string[], lang: st
|
||||||
feedId,
|
feedId,
|
||||||
JSON.stringify(mapping),
|
JSON.stringify(mapping),
|
||||||
'EX',
|
'EX',
|
||||||
60 * 60 * 24 * 30 // 30 days in seconds
|
DEFAULT_CACHE_TTL
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error storing feed mapping:', error);
|
console.error('Error storing feed mapping:', error);
|
||||||
|
|
@ -127,6 +128,8 @@ export async function getFeedMapping(feedId: string): Promise<FeedMapping | null
|
||||||
const mapping = await redis.get(feedId);
|
const mapping = await redis.get(feedId);
|
||||||
if (!mapping) return null;
|
if (!mapping) return null;
|
||||||
|
|
||||||
|
await redis.expire(feedId, DEFAULT_CACHE_TTL);
|
||||||
|
|
||||||
const data = JSON.parse(mapping);
|
const data = JSON.parse(mapping);
|
||||||
return {
|
return {
|
||||||
slugs: data.slugs,
|
slugs: data.slugs,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue