fix: cache file KO if reload
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { AuthConfig } from "@/types/auth";
|
||||
import { serverCacheService } from "./server-cache.service";
|
||||
import { getServerCacheService } from "./server-cache.service";
|
||||
import { ConfigDBService } from "./config-db.service";
|
||||
|
||||
// Types de cache disponibles
|
||||
@@ -51,7 +51,8 @@ export abstract class BaseApiService {
|
||||
fetcher: () => Promise<T>,
|
||||
type: CacheType = "DEFAULT"
|
||||
): Promise<T> {
|
||||
return serverCacheService.getOrSet(key, fetcher, type);
|
||||
const cacheService = await getServerCacheService();
|
||||
return cacheService.getOrSet(key, fetcher, type);
|
||||
}
|
||||
|
||||
protected static handleError(error: unknown, defaultMessage: string): never {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { BaseApiService } from "./base-api.service";
|
||||
import { KomgaBook, KomgaSeries } from "@/types/komga";
|
||||
import { LibraryResponse } from "@/types/library";
|
||||
import { serverCacheService } from "./server-cache.service";
|
||||
import { getServerCacheService } from "./server-cache.service";
|
||||
|
||||
interface HomeData {
|
||||
ongoing: KomgaSeries[];
|
||||
@@ -67,9 +67,10 @@ export class HomeService extends BaseApiService {
|
||||
}
|
||||
}
|
||||
|
||||
static async clearHomeCache() {
|
||||
serverCacheService.delete("home-ongoing");
|
||||
serverCacheService.delete("home-recently-read");
|
||||
serverCacheService.delete("home-on-deck");
|
||||
static async invalidateHomeCache(): Promise<void> {
|
||||
const cacheService = await getServerCacheService();
|
||||
cacheService.delete("home-ongoing");
|
||||
cacheService.delete("home-recently-read");
|
||||
cacheService.delete("home-on-deck");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { BaseApiService } from "./base-api.service";
|
||||
import { Library, LibraryResponse } from "@/types/library";
|
||||
import { Series } from "@/types/series";
|
||||
import { serverCacheService } from "./server-cache.service";
|
||||
import { getServerCacheService } from "./server-cache.service";
|
||||
|
||||
export class LibraryService extends BaseApiService {
|
||||
static async getLibraries(): Promise<Library[]> {
|
||||
@@ -134,7 +134,8 @@ export class LibraryService extends BaseApiService {
|
||||
}
|
||||
}
|
||||
|
||||
static async clearLibrarySeriesCache(libraryId: string) {
|
||||
serverCacheService.delete(`library-${libraryId}-all-series`);
|
||||
static async invalidateLibrarySeriesCache(libraryId: string): Promise<void> {
|
||||
const cacheService = await getServerCacheService();
|
||||
cacheService.delete(`library-${libraryId}-all-series`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { KomgaBook, KomgaSeries } from "@/types/komga";
|
||||
import { BookService } from "./book.service";
|
||||
import { ImageService } from "./image.service";
|
||||
import { PreferencesService } from "./preferences.service";
|
||||
import { serverCacheService } from "./server-cache.service";
|
||||
import { getServerCacheService } from "./server-cache.service";
|
||||
|
||||
export class SeriesService extends BaseApiService {
|
||||
static async getSeries(seriesId: string): Promise<KomgaSeries> {
|
||||
@@ -19,8 +19,9 @@ export class SeriesService extends BaseApiService {
|
||||
}
|
||||
}
|
||||
|
||||
static async clearSeriesCache(seriesId: string) {
|
||||
serverCacheService.delete(`series-${seriesId}`);
|
||||
static async invalidateSeriesCache(seriesId: string): Promise<void> {
|
||||
const cacheService = await getServerCacheService();
|
||||
cacheService.delete(`series-${seriesId}`);
|
||||
}
|
||||
|
||||
static async getAllSeriesBooks(seriesId: string): Promise<KomgaBook[]> {
|
||||
@@ -125,8 +126,9 @@ export class SeriesService extends BaseApiService {
|
||||
}
|
||||
}
|
||||
|
||||
static async clearSeriesBooksCache(seriesId: string) {
|
||||
serverCacheService.delete(`series-${seriesId}-all-books`);
|
||||
static async invalidateSeriesBooksCache(seriesId: string): Promise<void> {
|
||||
const cacheService = await getServerCacheService();
|
||||
cacheService.delete(`series-${seriesId}-all-books`);
|
||||
}
|
||||
|
||||
static async getFirstBook(seriesId: string): Promise<string> {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { PreferencesService } from "./preferences.service";
|
||||
|
||||
type CacheMode = "file" | "memory";
|
||||
|
||||
@@ -37,6 +38,17 @@ class ServerCacheService {
|
||||
this.cacheDir = path.join(process.cwd(), ".cache");
|
||||
this.ensureCacheDirectory();
|
||||
this.cleanExpiredCache();
|
||||
this.initializeCacheMode();
|
||||
}
|
||||
|
||||
private async initializeCacheMode(): Promise<void> {
|
||||
try {
|
||||
const preferences = await PreferencesService.getPreferences();
|
||||
this.setCacheMode(preferences.cacheMode);
|
||||
} catch (error) {
|
||||
console.error("Error initializing cache mode from preferences:", error);
|
||||
// Keep default memory mode if preferences can't be loaded
|
||||
}
|
||||
}
|
||||
|
||||
private ensureCacheDirectory(): void {
|
||||
@@ -117,9 +129,10 @@ class ServerCacheService {
|
||||
cleanDirectory(this.cacheDir);
|
||||
}
|
||||
|
||||
public static getInstance(): ServerCacheService {
|
||||
public static async getInstance(): Promise<ServerCacheService> {
|
||||
if (!ServerCacheService.instance) {
|
||||
ServerCacheService.instance = new ServerCacheService();
|
||||
await ServerCacheService.instance.initializeCacheMode();
|
||||
}
|
||||
return ServerCacheService.instance;
|
||||
}
|
||||
@@ -376,4 +389,15 @@ class ServerCacheService {
|
||||
}
|
||||
}
|
||||
|
||||
export const serverCacheService = ServerCacheService.getInstance();
|
||||
// Créer une instance initialisée du service
|
||||
let initializedInstance: Promise<ServerCacheService>;
|
||||
|
||||
export const getServerCacheService = async (): Promise<ServerCacheService> => {
|
||||
if (!initializedInstance) {
|
||||
initializedInstance = ServerCacheService.getInstance();
|
||||
}
|
||||
return initializedInstance;
|
||||
};
|
||||
|
||||
// Exporter aussi la classe pour les tests
|
||||
export { ServerCacheService };
|
||||
|
||||
Reference in New Issue
Block a user