Files
unionflow-server-impl-quarkus/unionflow-mobile-apps/lib/features/authentication/data/datasources/dashboard_cache_manager.dart
2025-11-17 16:02:04 +00:00

72 lines
2.1 KiB
Dart

/// Gestionnaire de cache pour le dashboard
/// Cache intelligent basé sur les rôles utilisateurs
library dashboard_cache_manager;
import 'dart:async';
import 'package:flutter/foundation.dart';
import '../models/user_role.dart';
/// Gestionnaire de cache pour optimiser les performances du dashboard
class DashboardCacheManager {
static final Map<String, dynamic> _cache = {};
static final Map<String, DateTime> _cacheTimestamps = {};
static const Duration _cacheExpiry = Duration(minutes: 15);
/// Invalide le cache pour un rôle spécifique
static Future<void> invalidateForRole(UserRole role) async {
final keysToRemove = _cache.keys
.where((key) => key.startsWith('dashboard_${role.name}'))
.toList();
for (final key in keysToRemove) {
_cache.remove(key);
_cacheTimestamps.remove(key);
}
debugPrint('🗑️ Cache invalidé pour le rôle: ${role.displayName}');
}
/// Vide complètement le cache
static Future<void> clear() async {
_cache.clear();
_cacheTimestamps.clear();
debugPrint('🧹 Cache dashboard complètement vidé');
}
/// Obtient une valeur du cache
static T? get<T>(String key) {
final timestamp = _cacheTimestamps[key];
if (timestamp == null) return null;
// Vérifier l'expiration
if (DateTime.now().difference(timestamp) > _cacheExpiry) {
_cache.remove(key);
_cacheTimestamps.remove(key);
return null;
}
return _cache[key] as T?;
}
/// Met une valeur en cache
static void set<T>(String key, T value) {
_cache[key] = value;
_cacheTimestamps[key] = DateTime.now();
}
/// Obtient les statistiques du cache
static Map<String, dynamic> getStats() {
final now = DateTime.now();
final validEntries = _cacheTimestamps.entries
.where((entry) => now.difference(entry.value) <= _cacheExpiry)
.length;
return {
'totalEntries': _cache.length,
'validEntries': validEntries,
'expiredEntries': _cache.length - validEntries,
'cacheHitRate': '${(validEntries / _cache.length * 100).toStringAsFixed(1)}%',
};
}
}