Presearch google activities.
This commit is contained in:
62
lib/services/activity_cache_service.dart
Normal file
62
lib/services/activity_cache_service.dart
Normal file
@@ -0,0 +1,62 @@
|
||||
import '../models/activity.dart';
|
||||
|
||||
class ActivityCacheService {
|
||||
static final ActivityCacheService _instance = ActivityCacheService._internal();
|
||||
|
||||
factory ActivityCacheService() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
ActivityCacheService._internal();
|
||||
|
||||
// Cache : tripId -> liste d'activités
|
||||
final Map<String, List<Activity>> _cache = {};
|
||||
|
||||
// Timestamps pour invalider le cache après 30 minutes
|
||||
final Map<String, DateTime> _cacheTimestamps = {};
|
||||
|
||||
static const Duration _cacheValidityDuration = Duration(minutes: 30);
|
||||
|
||||
/// Stocker les activités Google pour un voyage
|
||||
void setCachedActivities(String tripId, List<Activity> activities) {
|
||||
_cache[tripId] = activities;
|
||||
_cacheTimestamps[tripId] = DateTime.now();
|
||||
}
|
||||
|
||||
/// Récupérer les activités en cache si elles sont toujours valides
|
||||
List<Activity>? getCachedActivities(String tripId) {
|
||||
if (!_cache.containsKey(tripId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final timestamp = _cacheTimestamps[tripId];
|
||||
if (timestamp != null) {
|
||||
final age = DateTime.now().difference(timestamp);
|
||||
if (age > _cacheValidityDuration) {
|
||||
// Cache expiré, nettoyer
|
||||
_cache.remove(tripId);
|
||||
_cacheTimestamps.remove(tripId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return _cache[tripId];
|
||||
}
|
||||
|
||||
/// Vérifier si des activités sont en cache pour ce voyage
|
||||
bool hasCachedActivities(String tripId) {
|
||||
return getCachedActivities(tripId) != null;
|
||||
}
|
||||
|
||||
/// Nettoyer le cache pour un voyage spécifique
|
||||
void clearCache(String tripId) {
|
||||
_cache.remove(tripId);
|
||||
_cacheTimestamps.remove(tripId);
|
||||
}
|
||||
|
||||
/// Nettoyer tout le cache
|
||||
void clearAllCache() {
|
||||
_cache.clear();
|
||||
_cacheTimestamps.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user