putIfAbsent method

  1. @override
V putIfAbsent (K key, V ifAbsent())
override

Look up the value associated with key, or add a new value if it isn't there. The pair is promoted to the MRU position.

Otherwise calls ifAbsent to get a new value, associates key to that value, and then returns the new value, with the key-value pair in the MRU position. If this causes length to exceed maximumSize, then the LRU position is removed.

Implementation

@override
V putIfAbsent(K key, V ifAbsent()) {
  final entry =
      _entries.putIfAbsent(key, () => _createEntry(key, ifAbsent()));
  if (length > maximumSize) {
    _removeLru();
  }
  _promoteEntry(entry);
  return entry.value;
}