From c92156ad12c1df1e764d491402ce1a7b85009fa2 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Sun, 16 Feb 2025 22:06:15 +0100 Subject: [PATCH] feat: cache review and home loadings --- src/lib/services/home.service.ts | 74 +++++++++++++----------- src/lib/services/image.service.ts | 3 + src/lib/services/server-cache.service.ts | 22 +++---- 3 files changed, 56 insertions(+), 43 deletions(-) diff --git a/src/lib/services/home.service.ts b/src/lib/services/home.service.ts index ef1c514..722994b 100644 --- a/src/lib/services/home.service.ts +++ b/src/lib/services/home.service.ts @@ -14,43 +14,51 @@ export class HomeService extends BaseApiService { const config = await this.getKomgaConfig(); const headers = this.getAuthHeaders(config); - return this.fetchWithCache( - "home", - async () => { - // Construction des URLs - const ongoingUrl = this.buildUrl(config, "series", { - read_status: "IN_PROGRESS", - sort: "readDate,desc", - page: "0", - size: "20", - media_status: "READY", - }); + // Construction des URLs avec des paramètres optimisés + const ongoingUrl = this.buildUrl(config, "series", { + read_status: "IN_PROGRESS", + sort: "readDate,desc", + page: "0", + size: "10", + media_status: "READY", + }); - const recentlyReadUrl = this.buildUrl(config, "books/latest", { - page: "0", - size: "20", - }); + const recentlyReadUrl = this.buildUrl(config, "books/latest", { + page: "0", + size: "10", + media_status: "READY", + }); - const onDeckUrl = this.buildUrl(config, "books/ondeck", { - page: "0", - size: "20", - }); + const onDeckUrl = this.buildUrl(config, "books/ondeck", { + page: "0", + size: "10", + media_status: "READY", + }); - // Appels API parallèles avec fetchFromApi - const [ongoing, recentlyRead, onDeck] = await Promise.all([ - this.fetchFromApi>(ongoingUrl, headers), - this.fetchFromApi>(recentlyReadUrl, headers), - this.fetchFromApi>(onDeckUrl, headers), - ]); + // Appels API parallèles avec cache individuel + const [ongoing, recentlyRead, onDeck] = await Promise.all([ + this.fetchWithCache>( + "home-ongoing", + async () => this.fetchFromApi>(ongoingUrl, headers), + "HOME" + ), + this.fetchWithCache>( + "home-recently-read", + async () => this.fetchFromApi>(recentlyReadUrl, headers), + "HOME" + ), + this.fetchWithCache>( + "home-on-deck", + async () => this.fetchFromApi>(onDeckUrl, headers), + "HOME" + ), + ]); - return { - ongoing: ongoing.content || [], - recentlyRead: recentlyRead.content || [], - onDeck: onDeck.content || [], - }; - }, - "HOME" - ); + return { + ongoing: ongoing.content || [], + recentlyRead: recentlyRead.content || [], + onDeck: onDeck.content || [], + }; } catch (error) { return this.handleError(error, "Impossible de récupérer les données de la page d'accueil"); } diff --git a/src/lib/services/image.service.ts b/src/lib/services/image.service.ts index 7e4b467..db8ac4d 100644 --- a/src/lib/services/image.service.ts +++ b/src/lib/services/image.service.ts @@ -18,6 +18,9 @@ export class ImageService extends BaseApiService { async () => { const response = await fetch(url, { headers }); + // Log du résultat de la requête + console.log(`📡 [Komga API] ${response.status} ${response.statusText} - ${url}`); + if (!response.ok) { throw new Error(`Erreur HTTP: ${response.status} ${response.statusText}`); } diff --git a/src/lib/services/server-cache.service.ts b/src/lib/services/server-cache.service.ts index a6866c5..65c341c 100644 --- a/src/lib/services/server-cache.service.ts +++ b/src/lib/services/server-cache.service.ts @@ -8,17 +8,18 @@ class ServerCacheService { private static instance: ServerCacheService; private cache: Map = new Map(); - private static readonly fiveMinutes = 5 * 60; - private static readonly tenMinutes = 10 * 60; - private static readonly twentyFourHours = 24 * 60 * 60; - private static readonly oneMinute = 1 * 60; - private static readonly oneWeek = 7 * 24 * 60 * 60; + // Configuration des temps de cache en millisecondes + private static readonly fiveMinutes = 5 * 60 * 1000; + private static readonly tenMinutes = 10 * 60 * 1000; + private static readonly twentyFourHours = 24 * 60 * 60 * 1000; + private static readonly oneMinute = 1 * 60 * 1000; + private static readonly oneWeek = 7 * 24 * 60 * 60 * 1000; private static readonly noCache = 0; - // Configuration des temps de cache en secondes + // Configuration des temps de cache private static readonly DEFAULT_TTL = { DEFAULT: ServerCacheService.fiveMinutes, - HOME: ServerCacheService.fiveMinutes, + HOME: ServerCacheService.tenMinutes, LIBRARIES: ServerCacheService.twentyFourHours, SERIES: ServerCacheService.fiveMinutes, BOOKS: ServerCacheService.fiveMinutes, @@ -50,7 +51,7 @@ class ServerCacheService { set(key: string, data: any, type: keyof typeof ServerCacheService.DEFAULT_TTL = "DEFAULT"): void { this.cache.set(key, { data, - expiry: Date.now() + this.getTTL(type) * 1000, + expiry: Date.now() + this.getTTL(type), }); } @@ -97,15 +98,16 @@ class ServerCacheService { const cached = this.cache.get(key); if (cached && cached.expiry > now) { - console.log("Cache hit for key:", key); + console.log("✅ Cache hit for key:", key); return cached.data as T; } + console.log("❌ Cache not hit for key:", key); try { const data = await fetcher(); this.cache.set(key, { data, - expiry: now + this.getTTL(type) * 1000, + expiry: now + this.getTTL(type), }); return data; } catch (error) {