manual matching survives ttl now

This commit is contained in:
2026-01-02 17:54:55 +01:00
parent 59fdadd288
commit 5c4f4cf3b6
4 changed files with 176 additions and 38 deletions

View File

@@ -363,20 +363,42 @@ async function fetchBookMetadata(id: string): Promise<Book | null> {
}
async function searchChaptersInExtension(ext: Extension, name: string, lookupId: string, cacheId: string, search: boolean, origin: string, disableCache = false): Promise<ChapterWithProvider[]> {
const cacheKey = `chapters:${name}:${origin}:${search ? "search" : "id"}:${cacheId}`;
const cacheKey = `chapters:${name}:${origin}:id:${cacheId}`;
if (!disableCache) {
const cached = await getCache(cacheKey);
if (cached) {
const isExpired = Date.now() - cached.created_at > CACHE_TTL_MS;
if (!isExpired) {
console.log(`[${name}] Chapters cache hit for: ${lookupId}`);
try {
return JSON.parse(cached.result) as ChapterWithProvider[];
} catch (e) {
console.error(`[${name}] Error parsing cached chapters:`, e);
try {
const parsed = JSON.parse(cached.result) as {
mediaId?: string;
chapters: ChapterWithProvider[];
};
if (!isExpired) {
console.log(`[${name}] Chapters cache hit for: ${lookupId}`);
return parsed.chapters;
}
if (parsed.mediaId) {
const chaps = await ext.findChapters!(parsed.mediaId);
const result = chaps.map(ch => ({
id: ch.id,
number: parseFloat(ch.number.toString()),
title: ch.title,
date: ch.releaseDate,
provider: name,
index: ch.index,
language: ch.language ?? null,
}));
await setCache(cacheKey, { mediaId: parsed.mediaId, chapters: result }, CACHE_TTL_MS);
return result;
}
} catch (e) {
console.error(`[${name}] Error parsing cached chapters:`, e);
}
}
}
@@ -436,7 +458,10 @@ async function searchChaptersInExtension(ext: Extension, name: string, lookupId:
language: ch.language ?? null,
}));
await setCache(cacheKey, result, CACHE_TTL_MS);
await setCache(cacheKey, {
mediaId,
chapters: result
}, CACHE_TTL_MS);
return result;
} catch (e) {