Class FifoEvictionStrategy<K>

java.lang.Object
com.loomcache.server.datastructures.FifoEvictionStrategy<K>
Type Parameters:
K - the type of keys being tracked
All Implemented Interfaces:
EvictionStrategy<K>

public final class FifoEvictionStrategy<K> extends Object implements EvictionStrategy<K>
FIFO (First In, First Out) eviction strategy.

Evicts entries in insertion order — the oldest-inserted entry is always the first victim, regardless of access patterns. Unlike LRU, accessing a key does not change its position in the eviction queue.

Uses an ArrayDeque for O(1) insertion and eviction, with a companion HashSet for O(1) membership checks. Both are protected by a ReentrantLock to ensure atomic updates between the two data structures, preventing phantom entries.

  • Constructor Details

    • FifoEvictionStrategy

      public FifoEvictionStrategy()
  • Method Details

    • onAccess

      public void onAccess(K key)
      Description copied from interface: EvictionStrategy
      Called when an existing key is accessed (e.g., via get() or containsKey()). Updates tracking information to reflect the access.
      Specified by:
      onAccess in interface EvictionStrategy<K>
    • onInsert

      public void onInsert(K key)
      Description copied from interface: EvictionStrategy
      Called when a new or updated key is inserted. Initializes or resets tracking information for that key.
      Specified by:
      onInsert in interface EvictionStrategy<K>
    • onRemove

      public void onRemove(K key)
      Description copied from interface: EvictionStrategy
      Called when a key is removed from the map. Cleans up any tracking information associated with the key.
      Specified by:
      onRemove in interface EvictionStrategy<K>
    • selectVictim

      public @Nullable K selectVictim()
      Description copied from interface: EvictionStrategy
      Selects a key to evict based on the eviction strategy. Returns null if no suitable victim can be found (e.g., empty map).
      Specified by:
      selectVictim in interface EvictionStrategy<K>
    • size

      public int size()
      Description copied from interface: EvictionStrategy
      Returns the number of distinct keys currently tracked. Used for statistics and diagnostics.
      Specified by:
      size in interface EvictionStrategy<K>
    • snapshotState

      public EvictionStrategy.EvictionState<K> snapshotState()
      Description copied from interface: EvictionStrategy
      Capture strategy-specific eviction metadata in deterministic victim order.
      Specified by:
      snapshotState in interface EvictionStrategy<K>
    • restoreState

      public void restoreState(EvictionStrategy.EvictionState<K> state)
      Description copied from interface: EvictionStrategy
      Replace strategy-specific eviction metadata from a snapshot.
      Specified by:
      restoreState in interface EvictionStrategy<K>