refactor: amélioration de la configuration des temps de cache avec constantes nommées
This commit is contained in:
@@ -1,7 +1,32 @@
|
||||
class CacheService {
|
||||
private static instance: CacheService;
|
||||
private cacheName = "komga-cache-v1";
|
||||
private defaultTTL = 5 * 60; // 5 minutes en secondes
|
||||
|
||||
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 noCache = 0;
|
||||
|
||||
// Configuration des temps de cache en secondes
|
||||
private static readonly TTL = {
|
||||
DEFAULT: CacheService.fiveMinutes, // 5 minutes
|
||||
HOME: CacheService.fiveMinutes, // 5 minutes
|
||||
LIBRARIES: CacheService.tenMinutes, // 10 minutes
|
||||
SERIES: CacheService.fiveMinutes, // 5 minutes
|
||||
BOOKS: CacheService.fiveMinutes, // 5 minutes
|
||||
IMAGES: CacheService.twentyFourHours, // 24 heures
|
||||
READ_PROGRESS: CacheService.oneMinute, // 1 minute
|
||||
};
|
||||
// private static readonly TTL = {
|
||||
// DEFAULT: CacheService.noCache, // 5 minutes
|
||||
// HOME: CacheService.noCache, // 5 minutes
|
||||
// LIBRARIES: CacheService.noCache, // 10 minutes
|
||||
// SERIES: CacheService.noCache, // 5 minutes
|
||||
// BOOKS: CacheService.noCache, // 5 minutes
|
||||
// IMAGES: CacheService.noCache, // 24 heures
|
||||
// READ_PROGRESS: CacheService.noCache, // 1 minute
|
||||
// };
|
||||
|
||||
private constructor() {}
|
||||
|
||||
@@ -12,10 +37,21 @@ class CacheService {
|
||||
return CacheService.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne le TTL pour un type de données spécifique
|
||||
*/
|
||||
public getTTL(type: keyof typeof CacheService.TTL): number {
|
||||
return CacheService.TTL[type];
|
||||
}
|
||||
|
||||
/**
|
||||
* Met en cache une réponse avec une durée de vie
|
||||
*/
|
||||
async set(key: string, response: Response, ttl: number = this.defaultTTL): Promise<void> {
|
||||
async set(
|
||||
key: string,
|
||||
response: Response,
|
||||
ttl: number = CacheService.TTL.DEFAULT
|
||||
): Promise<void> {
|
||||
if (typeof window === "undefined") return;
|
||||
|
||||
try {
|
||||
@@ -98,7 +134,7 @@ class CacheService {
|
||||
async getOrFetch(
|
||||
key: string,
|
||||
fetcher: () => Promise<Response>,
|
||||
ttl: number = this.defaultTTL
|
||||
type: keyof typeof CacheService.TTL = "DEFAULT"
|
||||
): Promise<Response> {
|
||||
const cachedResponse = await this.get(key);
|
||||
if (cachedResponse) {
|
||||
@@ -107,7 +143,7 @@ class CacheService {
|
||||
|
||||
const response = await fetcher();
|
||||
const clonedResponse = response.clone();
|
||||
await this.set(key, clonedResponse, ttl);
|
||||
await this.set(key, clonedResponse, CacheService.TTL[type]);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,17 @@ type CacheEntry = {
|
||||
class ServerCacheService {
|
||||
private static instance: ServerCacheService;
|
||||
private cache: Map<string, { data: unknown; expiry: number }> = new Map();
|
||||
private defaultTTL = 5 * 60; // 5 minutes en secondes
|
||||
|
||||
// Configuration des temps de cache en secondes (identique à CacheService)
|
||||
private static readonly TTL = {
|
||||
DEFAULT: 5 * 60, // 5 minutes
|
||||
HOME: 5 * 60, // 5 minutes
|
||||
LIBRARIES: 10 * 60, // 10 minutes
|
||||
SERIES: 5 * 60, // 5 minutes
|
||||
BOOKS: 5 * 60, // 5 minutes
|
||||
IMAGES: 24 * 60 * 60, // 24 heures
|
||||
READ_PROGRESS: 1 * 60, // 1 minute
|
||||
};
|
||||
|
||||
private constructor() {
|
||||
// Private constructor to prevent external instantiation
|
||||
@@ -20,13 +30,20 @@ class ServerCacheService {
|
||||
return ServerCacheService.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne le TTL pour un type de données spécifique
|
||||
*/
|
||||
public getTTL(type: keyof typeof ServerCacheService.TTL): number {
|
||||
return ServerCacheService.TTL[type];
|
||||
}
|
||||
|
||||
/**
|
||||
* Met en cache des données avec une durée de vie
|
||||
*/
|
||||
set(key: string, data: any, ttl: number = this.defaultTTL): void {
|
||||
set(key: string, data: any, type: keyof typeof ServerCacheService.TTL = "DEFAULT"): void {
|
||||
this.cache.set(key, {
|
||||
data,
|
||||
expiry: Date.now() + ttl * 1000,
|
||||
expiry: Date.now() + ServerCacheService.TTL[type] * 1000,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -63,7 +80,11 @@ class ServerCacheService {
|
||||
/**
|
||||
* Récupère des données du cache ou exécute la fonction si nécessaire
|
||||
*/
|
||||
async getOrSet<T>(key: string, fetcher: () => Promise<T>, ttl: number): Promise<T> {
|
||||
async getOrSet<T>(
|
||||
key: string,
|
||||
fetcher: () => Promise<T>,
|
||||
type: keyof typeof ServerCacheService.TTL = "DEFAULT"
|
||||
): Promise<T> {
|
||||
const now = Date.now();
|
||||
const cached = this.cache.get(key);
|
||||
|
||||
@@ -75,7 +96,7 @@ class ServerCacheService {
|
||||
const data = await fetcher();
|
||||
this.cache.set(key, {
|
||||
data,
|
||||
expiry: now + ttl * 1000,
|
||||
expiry: now + ServerCacheService.TTL[type] * 1000,
|
||||
});
|
||||
return data;
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user