LruCache<K, V> class

A fixed-capacity least-recently-used cache.

A HashMap of nodes threaded on a doubly linked list, eldest first. Read, insert, bump, and evict are O(1). A LinkedHashMap with remove/reinsert is not: each removal at the front leaves a deleted slot that keys.first walks past until the map compacts, which made a full cache with many misses several times slower than no cache.

Constructors

LruCache({required int capacity, void onEvict(V value)?})

Properties

capacity → int
final
entries → Iterable<MapEntry<K, V>>
no setter
hashCode → int
The hash code for this object.
no setterinherited
keysUnsafeForIteration → Iterable<K>
All keys, eldest first. The cache must not change during iteration.
no setter
onEvict → void Function(V value)?
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
values → Iterable<V>
All values, eldest first.
no setter

Methods

clear() → void
containsKey(K key) → bool
copy() → LruCache<K, V>
Shallow clone; values themselves are not copied.
getValue(K key) → V?
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
putIfAbsent(K key, V ifAbsent()) → V
Look up the value of key, or add a new entry if it isn't there.
putValue(K key, V value) → void
remove(K key) → void
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited
operator [](K key) → V?
Lookup that also bumps the entry to MRU.
operator []=(K key, V value) → void
Insert/update; evicts LRU if the cache is full.