select method Null safety

  1. @override
CacheStat? select(
  1. Iterable<CacheStat?> entries,
  2. CacheStat justAdded

Finds the best eviction candidate based on the sampled entries. What distinguishes this approach from the classic data structures approach is that an CacheStat contains metadata (e.g. usage statistics) which can be used for making policy decisions, while generic data structures do not. It is expected that implementations will take advantage of that metadata.

  • entries: A list of non nullable sample entries to consider
  • justAdded: The entry added, provided so that it can be ignored if selected.

Returns the CacheStat of the entry that should be evicted or null if the entry list is empty


CacheStat? select(Iterable<CacheStat?> entries, CacheStat justAdded) {
  CacheStat? selectedEntry;
  for (var entry in entries) {
    if (entry != null &&
        (selectedEntry == null || entry.hitCount < selectedEntry.hitCount) &&
        justAdded.key != entry.key) {
      selectedEntry = entry;

  return selectedEntry;