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>
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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface EvictionStrategy
EvictionStrategy.EvictionState<K> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled when an existing key is accessed (e.g., via get() or containsKey()).voidCalled when a new or updated key is inserted.voidCalled when a key is removed from the map.voidReplace strategy-specific eviction metadata from a snapshot.@Nullable KSelects a key to evict based on the eviction strategy.intsize()Returns the number of distinct keys currently tracked.Capture strategy-specific eviction metadata in deterministic victim order.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface EvictionStrategy
isEmpty
-
Constructor Details
-
LfuEvictionStrategy
public LfuEvictionStrategy()
-
-
Method Details
-
onAccess
Description copied from interface:EvictionStrategyCalled when an existing key is accessed (e.g., via get() or containsKey()). Updates tracking information to reflect the access.- Specified by:
onAccessin interfaceEvictionStrategy<K>
-
onInsert
Description copied from interface:EvictionStrategyCalled when a new or updated key is inserted. Initializes or resets tracking information for that key.- Specified by:
onInsertin interfaceEvictionStrategy<K>
-
onRemove
Description copied from interface:EvictionStrategyCalled when a key is removed from the map. Cleans up any tracking information associated with the key.- Specified by:
onRemovein interfaceEvictionStrategy<K>
-
selectVictim
Description copied from interface:EvictionStrategySelects a key to evict based on the eviction strategy. Returns null if no suitable victim can be found (e.g., empty map).- Specified by:
selectVictimin interfaceEvictionStrategy<K>
-
size
public int size()Description copied from interface:EvictionStrategyReturns the number of distinct keys currently tracked. Used for statistics and diagnostics.- Specified by:
sizein interfaceEvictionStrategy<K>
-
snapshotState
Description copied from interface:EvictionStrategyCapture strategy-specific eviction metadata in deterministic victim order.- Specified by:
snapshotStatein interfaceEvictionStrategy<K>
-
restoreState
Description copied from interface:EvictionStrategyReplace strategy-specific eviction metadata from a snapshot.- Specified by:
restoreStatein interfaceEvictionStrategy<K>
-