tryGetValue<T> method

  1. @override
bool tryGetValue<T>(
  1. Object key,
  2. void setValue(
    1. T? value
    )
)
override

Gets the value associated with key if it exists.

Returns the cached value, or null if the key is not found. Sets value to the cached value if found.

Returns true if the key was found.

Implementation

@override
bool tryGetValue<T>(Object key, void Function(T? value) setValue) {
  final entry = _entries[key];

  if (entry == null) {
    if (_options.trackStatistics) {
      _missCount++;
    }
    setValue(null);
    return false;
  }

  // Check if expired
  if (entry.isExpired) {
    removeEntry(key, EvictionReason.expired);
    if (_options.trackStatistics) {
      _missCount++;
    }
    setValue(null);
    return false;
  }

  // Update last accessed for sliding expiration
  entry.updateLastAccessed();

  if (_options.trackStatistics) {
    _hitCount++;
  }

  setValue(entry.value as T?);
  return true;
}