Package org.messaginghub.pooled.jms.util
Class LRUCache<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
java.util.LinkedHashMap<K,V>
org.messaginghub.pooled.jms.util.LRUCache<K,V>
- Type Parameters:
K- The type of the map key.V- The type of the map value.
- All Implemented Interfaces:
Serializable,Cloneable,Map<K,V>
A Simple LRU Cache based on a LinkedHashMap. Not thread-safe.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionLRUCache()Default constructor for an LRU Cache The default capacity is 10000LRUCache(int maximumCacheSize) Constructs a LRUCache with a maximum capacityLRUCache(int initialCapacity, int maximumCacheSize, float loadFactor, boolean accessOrder) Constructs an emptyLRUCacheinstance with the specified initial capacity, maximumCacheSize,load factor and ordering mode. -
Method Summary
Modifier and TypeMethodDescriptionintprotected voidonCacheEviction(Map.Entry<K, V> eldest) Event point used by subclasses to perform some cleanup action when an element is evicted from the cache.protected booleanremoveEldestEntry(Map.Entry<K, V> eldest) voidsetMaxCacheSize(int maxCacheSize) Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, replaceAll, valuesMethods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, sizeMethods inherited from class java.util.AbstractMap
equals, hashCode, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
-
Field Details
-
maxCacheSize
protected int maxCacheSize
-
-
Constructor Details
-
LRUCache
public LRUCache()Default constructor for an LRU Cache The default capacity is 10000 -
LRUCache
public LRUCache(int maximumCacheSize) Constructs a LRUCache with a maximum capacity- Parameters:
maximumCacheSize- The maximum number of elements to keep in the Cache before eviction starts.
-
LRUCache
public LRUCache(int initialCapacity, int maximumCacheSize, float loadFactor, boolean accessOrder) Constructs an emptyLRUCacheinstance with the specified initial capacity, maximumCacheSize,load factor and ordering mode.- Parameters:
initialCapacity- The initial capacity.maximumCacheSize- The maximum number of elements to keep in the Cache before eviction starts.loadFactor- The load factor to configure on the underlying map.accessOrder- the ordering modetruefor access-order,falsefor insertion-order.- Throws:
IllegalArgumentException- if the initial capacity is negative or the load factor is non-positive.
-
-
Method Details
-
getMaxCacheSize
public int getMaxCacheSize()- Returns:
- Returns the maxCacheSize.
-
setMaxCacheSize
public void setMaxCacheSize(int maxCacheSize) - Parameters:
maxCacheSize- The maxCacheSize to set.
-
removeEldestEntry
- Overrides:
removeEldestEntryin classLinkedHashMap<K,V>
-
onCacheEviction
Event point used by subclasses to perform some cleanup action when an element is evicted from the cache.- Parameters:
eldest- the item being evicted from the LRUCache.
-