compact method
Performs compaction on the cache by removing percentage of entries.
percentage should be a value between 0.0 and 1.0.
Entries are removed based on their priority, with lower priority
entries being removed first.
Implementation
@override
void compact(double percentage) {
if (percentage < 0.0 || percentage > 1.0) {
throw ArgumentError.value(
percentage,
'percentage',
'Percentage must be between 0.0 and 1.0',
);
}
final targetRemovalCount = (_entries.length * percentage).ceil();
if (targetRemovalCount == 0) {
return;
}
// Sort entries by priority (low to high) and then by last accessed
final sortedEntries = _entries.entries.toList()
..sort((a, b) {
// First by priority
final priorityCompare = _priorityValue(a.value.priority)
.compareTo(_priorityValue(b.value.priority));
if (priorityCompare != 0) {
return priorityCompare;
}
// Then by last accessed (oldest first)
return a.value.lastAccessed.compareTo(b.value.lastAccessed);
});
var removed = 0;
for (final entry in sortedEntries) {
if (removed >= targetRemovalCount) {
break;
}
// Don't remove NeverRemove items
if (entry.value.priority == CacheItemPriority.neverRemove) {
continue;
}
removeEntry(entry.key, EvictionReason.capacity);
removed++;
}
}