Package org.apache.shiro.lang.util
Class SoftHashMap<K,V>
java.lang.Object
org.apache.shiro.lang.util.SoftHashMap<K,V>
- Type Parameters:
K- KV- V
- All Implemented Interfaces:
Map<K,V>
A
SoftHashMap is a memory-constrained map that stores its values in
SoftReferences. (Contrast this with the JDK's
WeakHashMap, which uses weak references for its keys, which is of little value if you
want the cache to auto-resize itself based on memory constraints).
Having the values wrapped by soft references allows the cache to automatically reduce its size based on memory
limitations and garbage collection. This ensures that the cache will not cause memory leaks by holding strong
references to all of its values.
This class is a generics-enabled Map based on initial ideas from Heinz Kabutz's and Sydney Redelinghuys's
publicly posted version (with their approval), with
continued modifications.
This implementation is thread-safe and usable in concurrent environments.- Since:
- 1.0
-
Nested Class Summary
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new SoftHashMap with a default retention size size ofDEFAULT_RETENTION_SIZE(100 entries).SoftHashMap(int retentionSize) Creates a new SoftHashMap with the specified retention size.SoftHashMap(Map<K, V> source) Creates aSoftHashMapbacked by the specifiedsource, with a default retention size ofDEFAULT_RETENTION_SIZE(100 entries).SoftHashMap(Map<K, V> source, int retentionSize) Creates aSoftHashMapbacked by the specifiedsource, with the specified retention size. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()booleanisEmpty()keySet()Creates a new entry, but wraps the value in a SoftValue instance to enable auto garbage collection.voidintsize()values()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
SoftHashMap
public SoftHashMap()Creates a new SoftHashMap with a default retention size size ofDEFAULT_RETENTION_SIZE(100 entries).- See Also:
-
SoftHashMap
public SoftHashMap(int retentionSize) Creates a new SoftHashMap with the specified retention size. The retention size (n) is the total number of most recent entries in the map that will be strongly referenced (i.e.'retained') to prevent them from being eagerly garbage collected. That is, the point of a SoftHashMap is to allow the garbage collector to remove as many entries from this map as it desires, but there will always be (n) elements retained after a GC due to the strong references. Note that in a highly concurrent environments the exact total number of strong references may differ slightly than the actualretentionSizevalue. This number is intended to be a best-effort retention low water mark.- Parameters:
retentionSize- the total number of most recent entries in the map that will be strongly referenced (retained), preventing them from being eagerly garbage collected by the JVM.
-
SoftHashMap
Creates aSoftHashMapbacked by the specifiedsource, with a default retention size ofDEFAULT_RETENTION_SIZE(100 entries).- Parameters:
source- the backing map to populate thisSoftHashMap- See Also:
-
SoftHashMap
Creates aSoftHashMapbacked by the specifiedsource, with the specified retention size. The retention size (n) is the total number of most recent entries in the map that will be strongly referenced (i.e.'retained') to prevent them from being eagerly garbage collected. That is, the point of a SoftHashMap is to allow the garbage collector to remove as many entries from this map as it desires, but there will always be (n) elements retained after a GC due to the strong references. Note that in a highly concurrent environments the exact total number of strong references may differ slightly than the actualretentionSizevalue. This number is intended to be a best-effort retention low water mark.- Parameters:
source- the backing map to populate thisSoftHashMapretentionSize- the total number of most recent entries in the map that will be strongly referenced (retained), preventing them from being eagerly garbage collected by the JVM.
-
-
Method Details
-
get
-
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V>
-
putAll
-
keySet
-
values
-
put
Creates a new entry, but wraps the value in a SoftValue instance to enable auto garbage collection. -
remove
-
clear
public void clear() -
size
public int size() -
entrySet
-