From c3b11a90901374b901b7f7540bf53e05037b0f79 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Mon, 24 Feb 2025 13:54:55 +0100 Subject: [PATCH] fix: cache by userid --- src/lib/services/home.service.ts | 7 +++-- src/lib/services/library.service.ts | 2 +- src/lib/services/series.service.ts | 4 +-- src/lib/services/server-cache.service.ts | 36 ++++++++++++++++++------ 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/lib/services/home.service.ts b/src/lib/services/home.service.ts index 066a75e..caa9d7c 100644 --- a/src/lib/services/home.service.ts +++ b/src/lib/services/home.service.ts @@ -84,8 +84,9 @@ export class HomeService extends BaseApiService { static async invalidateHomeCache(): Promise { const cacheService = await getServerCacheService(); - cacheService.delete("home-ongoing"); - cacheService.delete("home-recently-read"); - cacheService.delete("home-on-deck"); + await cacheService.delete("home-ongoing"); + await cacheService.delete("home-recently-read"); + await cacheService.delete("home-on-deck"); + await cacheService.delete("home-latest-series"); } } diff --git a/src/lib/services/library.service.ts b/src/lib/services/library.service.ts index a2ffdf2..e053436 100644 --- a/src/lib/services/library.service.ts +++ b/src/lib/services/library.service.ts @@ -136,6 +136,6 @@ export class LibraryService extends BaseApiService { static async invalidateLibrarySeriesCache(libraryId: string): Promise { const cacheService = await getServerCacheService(); - cacheService.delete(`library-${libraryId}-all-series`); + await cacheService.delete(`library-${libraryId}-all-series`); } } diff --git a/src/lib/services/series.service.ts b/src/lib/services/series.service.ts index 3f774f1..b2de200 100644 --- a/src/lib/services/series.service.ts +++ b/src/lib/services/series.service.ts @@ -21,7 +21,7 @@ export class SeriesService extends BaseApiService { static async invalidateSeriesCache(seriesId: string): Promise { const cacheService = await getServerCacheService(); - cacheService.delete(`series-${seriesId}`); + await cacheService.delete(`series-${seriesId}`); } static async getAllSeriesBooks(seriesId: string): Promise { @@ -128,7 +128,7 @@ export class SeriesService extends BaseApiService { static async invalidateSeriesBooksCache(seriesId: string): Promise { const cacheService = await getServerCacheService(); - cacheService.delete(`series-${seriesId}-all-books`); + await cacheService.delete(`series-${seriesId}-all-books`); } static async getFirstBook(seriesId: string): Promise { diff --git a/src/lib/services/server-cache.service.ts b/src/lib/services/server-cache.service.ts index a764621..f997df4 100644 --- a/src/lib/services/server-cache.service.ts +++ b/src/lib/services/server-cache.service.ts @@ -2,6 +2,7 @@ import fs from "fs"; import path from "path"; import { PreferencesService } from "./preferences.service"; import { DebugService } from "./debug.service"; +import { AuthServerService } from "./auth-server.service"; type CacheMode = "file" | "memory"; @@ -286,11 +287,17 @@ class ServerCacheService { /** * Supprime une entrée du cache */ - delete(key: string): void { + async delete(key: string): Promise { + const user = await AuthServerService.getCurrentUser(); + if (!user) { + throw new Error("Utilisateur non authentifié"); + } + const cacheKey = `${user.id}-${key}`; + if (this.config.mode === "memory") { - this.memoryCache.delete(key); + this.memoryCache.delete(cacheKey); } else { - const filePath = this.getCacheFilePath(key); + const filePath = this.getCacheFilePath(cacheKey); if (fs.existsSync(filePath)) { fs.unlinkSync(filePath); } @@ -300,15 +307,21 @@ class ServerCacheService { /** * Supprime toutes les entrées du cache qui commencent par un préfixe */ - deleteAll(prefix: string): void { + async deleteAll(prefix: string): Promise { + const user = await AuthServerService.getCurrentUser(); + if (!user) { + throw new Error("Utilisateur non authentifié"); + } + const prefixKey = `${user.id}-${prefix}`; + if (this.config.mode === "memory") { this.memoryCache.forEach((value, key) => { - if (key.startsWith(prefix)) { + if (key.startsWith(prefixKey)) { this.memoryCache.delete(key); } }); } else { - const cacheDir = path.join(this.cacheDir, prefix); + const cacheDir = path.join(this.cacheDir, prefixKey); if (fs.existsSync(cacheDir)) { fs.rmdirSync(cacheDir, { recursive: true }); } @@ -372,14 +385,19 @@ class ServerCacheService { type: keyof typeof ServerCacheService.DEFAULT_TTL = "DEFAULT" ): Promise { const startTime = performance.now(); + const user = await AuthServerService.getCurrentUser(); + if (!user) { + throw new Error("Utilisateur non authentifié"); + } - const cached = this.get(key); + const cacheKey = `${user.id}-${key}`; + const cached = this.get(cacheKey); if (cached !== null) { const endTime = performance.now(); // Log la requête avec l'indication du cache await DebugService.logRequest({ - url: key, + url: cacheKey, startTime, endTime, fromCache: true, @@ -391,7 +409,7 @@ class ServerCacheService { try { const data = await fetcher(); - this.set(key, data, type); + this.set(cacheKey, data, type); return data; } catch (error) { throw error;