fix: cache by userid

This commit is contained in:
Julien Froidefond
2025-02-24 13:54:55 +01:00
parent 206418aa57
commit c3b11a9090
4 changed files with 34 additions and 15 deletions

View File

@@ -84,8 +84,9 @@ export class HomeService extends BaseApiService {
static async invalidateHomeCache(): Promise<void> {
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");
}
}

View File

@@ -136,6 +136,6 @@ export class LibraryService extends BaseApiService {
static async invalidateLibrarySeriesCache(libraryId: string): Promise<void> {
const cacheService = await getServerCacheService();
cacheService.delete(`library-${libraryId}-all-series`);
await cacheService.delete(`library-${libraryId}-all-series`);
}
}

View File

@@ -21,7 +21,7 @@ export class SeriesService extends BaseApiService {
static async invalidateSeriesCache(seriesId: string): Promise<void> {
const cacheService = await getServerCacheService();
cacheService.delete(`series-${seriesId}`);
await cacheService.delete(`series-${seriesId}`);
}
static async getAllSeriesBooks(seriesId: string): Promise<KomgaBook[]> {
@@ -128,7 +128,7 @@ export class SeriesService extends BaseApiService {
static async invalidateSeriesBooksCache(seriesId: string): Promise<void> {
const cacheService = await getServerCacheService();
cacheService.delete(`series-${seriesId}-all-books`);
await cacheService.delete(`series-${seriesId}-all-books`);
}
static async getFirstBook(seriesId: string): Promise<string> {

View File

@@ -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<void> {
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<void> {
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<T> {
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;