cleanCacheTimedOut method

int cleanCacheTimedOut([
  1. Duration? timeout
])

Implementation

int cleanCacheTimedOut([Duration? timeout]) {
  timeout ??= this.timeout;
  if (timeout.inMilliseconds <= 0) return 0;

  var entries =
      List<MapEntry<_CacheRequest, HttpResponse>>.from(_cache.entries);

  entries.sort((e1, e2) {
    var t1 = max(e1.key.accessTime, e1.value.accessTime!);
    var t2 = max(e2.key.accessTime, e2.value.accessTime!);
    return t1 < t2 ? -1 : (t1 == t2 ? 0 : 1);
  });

  var now = DateTime.now().millisecondsSinceEpoch;

  var removed = 0;
  for (var entry in entries) {
    var accessTime = max(entry.key.accessTime, entry.value.accessTime!);
    var elapsedTime = now - accessTime;
    if (elapsedTime <= timeout.inMilliseconds) break;

    var memory = entry.key.memorySize() + entry.value.memorySize();
    _cache.remove(entry.key);
    removed += memory;

    _log(
        'Removed cached entry> memory: $memory ; timeout: $elapsedTime / $timeout');
  }

  _log('Total removed memory: $removed');

  return removed;
}