onCapacity method
called if we want to
insert another item denoted by key
and element
into the map. This
method checks if the capacity is reached and eventually evicts old items.
Implementation
@override
void onCapacity(K key, V element) {
if (length < capacity) return;
int toRemove =
DateTime.now().millisecondsSinceEpoch - expiration.inMilliseconds;
if (lastCleanup > toRemove) return;
Iterable<CacheEntry<K, V>> itemsToRemove = storage.entries.where(
(element) => (element as ExpirationCacheEntry).insertTime < toRemove);
itemsToRemove.forEach((element) {
// do not call onCapacity because if the entry is expired we do not want to keep it anyway
storage.removeInternal(element.key);
});
lastCleanup = DateTime.now().millisecondsSinceEpoch;
}