main
dart 35 lines 816 Bytes
Raw
1 import 'models/model_info.dart';
2
3 class ModelCache {
4 final _cache = <String, _CacheEntry>{};
5 static const Duration ttl = Duration(minutes: 5);
6
7 List<ModelInfo>? get(String serverId) {
8 final entry = _cache[serverId];
9 if (entry == null) return null;
10 if (DateTime.now().difference(entry.timestamp) > ttl) {
11 _cache.remove(serverId);
12 return null;
13 }
14 return entry.models;
15 }
16
17 void put(String serverId, List<ModelInfo> models) {
18 _cache[serverId] = _CacheEntry(models: models, timestamp: DateTime.now());
19 }
20
21 void invalidate(String serverId) {
22 _cache.remove(serverId);
23 }
24
25 void invalidateAll() {
26 _cache.clear();
27 }
28 }
29
30 class _CacheEntry {
31 final List<ModelInfo> models;
32 final DateTime timestamp;
33
34 _CacheEntry({required this.models, required this.timestamp});
35 }