cleanCache method
int
cleanCache(
- int maxMemory
)
Implementation
int cleanCache(int maxMemory) {
if (maxMemory <= 0) return -1;
if (hasTimeout) {
cleanCacheTimedOut();
}
var usedMemory = calculateCacheUsedMemory();
if (usedMemory <= maxMemory) return 0;
var entries =
List<MapEntry<_CacheRequest, HttpResponse>>.from(_cache.entries);
entries.sort((e1, e2) {
var m1 = e1.key.memorySize() + e1.value.memorySize();
var m2 = e2.key.memorySize() + e2.value.memorySize();
return m1 < m2 ? 1 : (m1 == m2 ? 0 : -1);
});
var removeNeeded = usedMemory - maxMemory;
var removed = 0;
for (var entry in entries) {
var memory = entry.key.memorySize() + entry.value.memorySize();
_cache.remove(entry.key);
removed += memory;
_log('Removed cached entry> memory: $memory / $removeNeeded');
if ((usedMemory - removed) <= maxMemory) break;
}
_log('Total removed memory: $removed / $usedMemory / $maxMemory');
return removed;
}