

void evictOldestEntries(Map<String, dynamic> map, int maxSize) {
  if (map.length <= maxSize) return;
  final entries = map.entries.toList()
    ..sort((a, b) {
      final aTime =
          DateTime.tryParse(
            (a.value as Map<String, dynamic>)['updatedAt'] as String? ?? '',
          ) ??
          DateTime(2000);
      final bTime =
          DateTime.tryParse(
            (b.value as Map<String, dynamic>)['updatedAt'] as String? ?? '',
          ) ??
          DateTime(2000);
      return aTime.compareTo(bTime);
    });
  final removeCount = map.length - maxSize;
  for (var i = 0; i < removeCount; i++) {
    map.remove(entries[i].key);
  }
}
