onCapacity method

  1. @override
void onCapacity(
  1. K key,
  2. V element
)

called if the length of the map reaches the capacity and we want to insert another item into the map

Implementation

@override
void onCapacity(K key, V element) {
//    storage.entries.sort((a, b) => (a as LruCacheEntry).lastUse - (b as LruCacheEntry).lastUse);
  // Iterate on all keys, so the eviction is O(n) to allow an insertion at O(1)
  LruCacheEntry<K, V> min = storage.entries
      .map((e) => e as LruCacheEntry<K, V>)
      .reduce((element1, element2) =>
          element1.lastUse < element2.lastUse ? element1 : element2);

  storage.remove(min.key);
}