Class LruEvictionStrategy<K>
java.lang.Object
com.loomcache.server.datastructures.LruEvictionStrategy<K>
- Type Parameters:
K- the type of keys being tracked
- All Implemented Interfaces:
EvictionStrategy<K>
LRU (Least Recently Used) eviction strategy using LinkedHashMap.
LinkedHashMap with accessOrder=true maintains insertion/access order automatically. This is the most efficient way to track recency for LRU eviction, avoiding the overhead of a separate tracking deque.
Thread-safe with ReentrantLock for virtual thread compatibility (avoids carrier thread pinning that synchronized blocks can cause on pre-JEP 491 runtimes).
-
Nested Class Summary
Nested classes/interfaces inherited from interface EvictionStrategy
EvictionStrategy.EvictionState<K> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanisEmpty()Returns true if the eviction strategy is tracking no keys.voidCalled 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.
-
Constructor Details
-
LruEvictionStrategy
public LruEvictionStrategy()
-
-
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>
-
isEmpty
public boolean isEmpty()Returns true if the eviction strategy is tracking no keys.- Specified by:
isEmptyin 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>
-