Class LfuEvictionStrategy<K>

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

public final class LfuEvictionStrategy<K> extends Object implements EvictionStrategy<K>
LFU (Least Frequently Used) eviction strategy using frequency buckets.

Maintains a mapping from frequency level to an insertion-ordered set of keys at that level. A separate minFrequency field tracks the current lowest frequency bucket, enabling O(1) victim selection. On tie (multiple keys with same frequency), evicts the oldest entry in the bucket (insertion order preserved by LinkedHashSet).

Thread safety is provided by a ReentrantReadWriteLock: mutations (onAccess, onInsert, onRemove) acquire the write lock; selectVictim acquires the read lock.

  • Constructor Details

    • LfuEvictionStrategy

      public LfuEvictionStrategy()
  • 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>