feat: ajout du système de notifications toast et amélioration du cache avec TTL configurable

This commit is contained in:
Julien Froidefond
2025-02-12 13:55:29 +01:00
parent 0c8109b366
commit 16f0714e9b
9 changed files with 459 additions and 22 deletions

View File

@@ -3,14 +3,7 @@ import { AuthConfig } from "@/types/auth";
import { serverCacheService } from "./server-cache.service";
// Types de cache disponibles
export type CacheType =
| "DEFAULT"
| "HOME"
| "LIBRARIES"
| "SERIES"
| "BOOKS"
| "IMAGES"
| "READ_PROGRESS";
export type CacheType = "DEFAULT" | "HOME" | "LIBRARIES" | "SERIES" | "BOOKS" | "IMAGES";
export abstract class BaseApiService {
protected static async getKomgaConfig(): Promise<AuthConfig> {

View File

@@ -15,14 +15,13 @@ class ServerCacheService {
private static readonly noCache = 0;
// Configuration des temps de cache en secondes
private static readonly TTL = {
DEFAULT: ServerCacheService.fiveMinutes, // 5 minutes
HOME: ServerCacheService.oneMinute, // 1 minute
LIBRARIES: ServerCacheService.tenMinutes, // 10 minutes
SERIES: ServerCacheService.fiveMinutes, // 5 minutes
BOOKS: ServerCacheService.fiveMinutes, // 5 minutes
IMAGES: ServerCacheService.twentyFourHours, // 24 heures
READ_PROGRESS: ServerCacheService.oneMinute, // 1 minute
private static readonly DEFAULT_TTL = {
DEFAULT: ServerCacheService.fiveMinutes,
HOME: ServerCacheService.fiveMinutes,
LIBRARIES: ServerCacheService.twentyFourHours,
SERIES: ServerCacheService.fiveMinutes,
BOOKS: ServerCacheService.fiveMinutes,
IMAGES: ServerCacheService.twentyFourHours,
};
private constructor() {
@@ -39,17 +38,33 @@ class ServerCacheService {
/**
* Retourne le TTL pour un type de données spécifique
*/
public getTTL(type: keyof typeof ServerCacheService.TTL): number {
return ServerCacheService.TTL[type];
public getTTL(type: keyof typeof ServerCacheService.DEFAULT_TTL): number {
// Essayer de récupérer la configuration utilisateur
try {
const ttlConfig = localStorage.getItem("ttlConfig");
if (ttlConfig) {
const config = JSON.parse(ttlConfig);
const key = `${type.toLowerCase()}TTL` as keyof typeof config;
if (config[key]) {
// Convertir les minutes en secondes
return config[key] * 60;
}
}
} catch (error) {
console.error("Erreur lors de la lecture de la configuration TTL:", error);
}
// Utiliser la valeur par défaut si pas de configuration utilisateur
return ServerCacheService.DEFAULT_TTL[type];
}
/**
* Met en cache des données avec une durée de vie
*/
set(key: string, data: any, type: keyof typeof ServerCacheService.TTL = "DEFAULT"): void {
set(key: string, data: any, type: keyof typeof ServerCacheService.DEFAULT_TTL = "DEFAULT"): void {
this.cache.set(key, {
data,
expiry: Date.now() + ServerCacheService.TTL[type] * 1000,
expiry: Date.now() + this.getTTL(type) * 1000,
});
}
@@ -89,7 +104,7 @@ class ServerCacheService {
async getOrSet<T>(
key: string,
fetcher: () => Promise<T>,
type: keyof typeof ServerCacheService.TTL = "DEFAULT"
type: keyof typeof ServerCacheService.DEFAULT_TTL = "DEFAULT"
): Promise<T> {
const now = Date.now();
const cached = this.cache.get(key);
@@ -103,7 +118,7 @@ class ServerCacheService {
const data = await fetcher();
this.cache.set(key, {
data,
expiry: now + ServerCacheService.TTL[type] * 1000,
expiry: now + this.getTTL(type) * 1000,
});
return data;
} catch (error) {

View File

@@ -2,6 +2,16 @@ import { AuthConfig } from "@/types/auth";
const CREDENTIALS_KEY = "komgaCredentials";
const USER_KEY = "komgaUser";
const TTL_CONFIG_KEY = "ttlConfig";
interface TTLConfig {
defaultTTL: number;
homeTTL: number;
librariesTTL: number;
seriesTTL: number;
booksTTL: number;
imagesTTL: number;
}
class StorageService {
private static instance: StorageService;
@@ -100,6 +110,27 @@ class StorageService {
}
}
/**
* Stocke la configuration des TTL
*/
setTTLConfig(config: TTLConfig): void {
localStorage.setItem(TTL_CONFIG_KEY, JSON.stringify(config));
}
/**
* Récupère la configuration des TTL
*/
getTTLConfig(): TTLConfig | null {
const stored = localStorage.getItem(TTL_CONFIG_KEY);
if (!stored) return null;
try {
return JSON.parse(stored);
} catch {
return null;
}
}
/**
* Efface toutes les données stockées
*/