feat: cache review and home loadings

This commit is contained in:
Julien Froidefond
2025-02-16 22:06:15 +01:00
parent 068c623e37
commit c92156ad12
3 changed files with 56 additions and 43 deletions

View File

@@ -14,43 +14,51 @@ export class HomeService extends BaseApiService {
const config = await this.getKomgaConfig(); const config = await this.getKomgaConfig();
const headers = this.getAuthHeaders(config); const headers = this.getAuthHeaders(config);
return this.fetchWithCache<HomeData>( // Construction des URLs avec des paramètres optimisés
"home", const ongoingUrl = this.buildUrl(config, "series", {
async () => { read_status: "IN_PROGRESS",
// Construction des URLs sort: "readDate,desc",
const ongoingUrl = this.buildUrl(config, "series", { page: "0",
read_status: "IN_PROGRESS", size: "10",
sort: "readDate,desc", media_status: "READY",
page: "0", });
size: "20",
media_status: "READY",
});
const recentlyReadUrl = this.buildUrl(config, "books/latest", { const recentlyReadUrl = this.buildUrl(config, "books/latest", {
page: "0", page: "0",
size: "20", size: "10",
}); media_status: "READY",
});
const onDeckUrl = this.buildUrl(config, "books/ondeck", { const onDeckUrl = this.buildUrl(config, "books/ondeck", {
page: "0", page: "0",
size: "20", size: "10",
}); media_status: "READY",
});
// Appels API parallèles avec fetchFromApi // Appels API parallèles avec cache individuel
const [ongoing, recentlyRead, onDeck] = await Promise.all([ const [ongoing, recentlyRead, onDeck] = await Promise.all([
this.fetchFromApi<LibraryResponse<KomgaSeries>>(ongoingUrl, headers), this.fetchWithCache<LibraryResponse<KomgaSeries>>(
this.fetchFromApi<LibraryResponse<KomgaBook>>(recentlyReadUrl, headers), "home-ongoing",
this.fetchFromApi<LibraryResponse<KomgaBook>>(onDeckUrl, headers), async () => this.fetchFromApi<LibraryResponse<KomgaSeries>>(ongoingUrl, headers),
]); "HOME"
),
this.fetchWithCache<LibraryResponse<KomgaBook>>(
"home-recently-read",
async () => this.fetchFromApi<LibraryResponse<KomgaBook>>(recentlyReadUrl, headers),
"HOME"
),
this.fetchWithCache<LibraryResponse<KomgaBook>>(
"home-on-deck",
async () => this.fetchFromApi<LibraryResponse<KomgaBook>>(onDeckUrl, headers),
"HOME"
),
]);
return { return {
ongoing: ongoing.content || [], ongoing: ongoing.content || [],
recentlyRead: recentlyRead.content || [], recentlyRead: recentlyRead.content || [],
onDeck: onDeck.content || [], onDeck: onDeck.content || [],
}; };
},
"HOME"
);
} catch (error) { } catch (error) {
return this.handleError(error, "Impossible de récupérer les données de la page d'accueil"); return this.handleError(error, "Impossible de récupérer les données de la page d'accueil");
} }

View File

@@ -18,6 +18,9 @@ export class ImageService extends BaseApiService {
async () => { async () => {
const response = await fetch(url, { headers }); 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) { if (!response.ok) {
throw new Error(`Erreur HTTP: ${response.status} ${response.statusText}`); throw new Error(`Erreur HTTP: ${response.status} ${response.statusText}`);
} }

View File

@@ -8,17 +8,18 @@ class ServerCacheService {
private static instance: ServerCacheService; private static instance: ServerCacheService;
private cache: Map<string, { data: unknown; expiry: number }> = new Map(); private cache: Map<string, { data: unknown; expiry: number }> = new Map();
private static readonly fiveMinutes = 5 * 60; // Configuration des temps de cache en millisecondes
private static readonly tenMinutes = 10 * 60; private static readonly fiveMinutes = 5 * 60 * 1000;
private static readonly twentyFourHours = 24 * 60 * 60; private static readonly tenMinutes = 10 * 60 * 1000;
private static readonly oneMinute = 1 * 60; private static readonly twentyFourHours = 24 * 60 * 60 * 1000;
private static readonly oneWeek = 7 * 24 * 60 * 60; private static readonly oneMinute = 1 * 60 * 1000;
private static readonly oneWeek = 7 * 24 * 60 * 60 * 1000;
private static readonly noCache = 0; private static readonly noCache = 0;
// Configuration des temps de cache en secondes // Configuration des temps de cache
private static readonly DEFAULT_TTL = { private static readonly DEFAULT_TTL = {
DEFAULT: ServerCacheService.fiveMinutes, DEFAULT: ServerCacheService.fiveMinutes,
HOME: ServerCacheService.fiveMinutes, HOME: ServerCacheService.tenMinutes,
LIBRARIES: ServerCacheService.twentyFourHours, LIBRARIES: ServerCacheService.twentyFourHours,
SERIES: ServerCacheService.fiveMinutes, SERIES: ServerCacheService.fiveMinutes,
BOOKS: ServerCacheService.fiveMinutes, BOOKS: ServerCacheService.fiveMinutes,
@@ -50,7 +51,7 @@ class ServerCacheService {
set(key: string, data: any, type: keyof typeof ServerCacheService.DEFAULT_TTL = "DEFAULT"): void { set(key: string, data: any, type: keyof typeof ServerCacheService.DEFAULT_TTL = "DEFAULT"): void {
this.cache.set(key, { this.cache.set(key, {
data, 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); const cached = this.cache.get(key);
if (cached && cached.expiry > now) { if (cached && cached.expiry > now) {
console.log("Cache hit for key:", key); console.log("Cache hit for key:", key);
return cached.data as T; return cached.data as T;
} }
console.log("❌ Cache not hit for key:", key);
try { try {
const data = await fetcher(); const data = await fetcher();
this.cache.set(key, { this.cache.set(key, {
data, data,
expiry: now + this.getTTL(type) * 1000, expiry: now + this.getTTL(type),
}); });
return data; return data;
} catch (error) { } catch (error) {