Class SoftCache<K,V>
java.lang.Object
org.apache.commons.jexl3.internal.SoftCache<K,V>
- Type Parameters:
K- The cache key entry typeV- The cache key value type
- All Implemented Interfaces:
JexlCache<K,V>
A soft referenced cache.
The actual cache is held through a soft reference, allowing it to be GCed under memory pressure.
Note that the underlying map is a synchronized LinkedHashMap. The reason is that a get() will reorder elements (the LRU queue) and thus needs synchronization to ensure thread-safety.
When caching JEXL scripts or expressions, one should expect the execution cost of those to be several fold the cost of the cache handling; after some (synthetic) tests, measures indicate cache handling is a marginal latency factor.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.commons.jexl3.JexlCache
JexlCache.Reference -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final intThe cache capacity.protected static final floatThe default cache load factor.protected SoftReference<Map<K,V>> The soft reference to the cache map. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintcapacity()Returns the cache capacity, the maximum number of elements it can contain.voidclear()Clears the cache.protected <KT,VT> Map<KT, VT> createMap(int cacheSize) Creates a cache store.static <K,V> Map<K, V> createSynchronizedLinkedHashMap(int capacity) Creates a synchronized LinkedHashMap.entries()Produces the cache entry set.Gets a value from this cache.Puts a value in this cache.intsize()Returns the cache size, the actual number of elements it contains.
-
Field Details
-
LOAD_FACTOR
protected static final float LOAD_FACTORThe default cache load factor.- See Also:
-
capacity
protected final int capacityThe cache capacity. -
reference
The soft reference to the cache map.
-
-
Constructor Details
-
SoftCache
public SoftCache(int theSize) Creates a new instance of a soft cache.- Parameters:
theSize- The cache size
-
-
Method Details
-
createSynchronizedLinkedHashMap
Creates a synchronized LinkedHashMap.- Type Parameters:
K- key typeV- value type- Parameters:
capacity- The map capacity- Returns:
- The map instance
-
capacity
public int capacity()Returns the cache capacity, the maximum number of elements it can contain. -
clear
public void clear()Clears the cache. -
createMap
Creates a cache store.- Type Parameters:
KT- the key typeVT- the value type- Parameters:
cacheSize- The cache size, must be > 0- Returns:
- A Map usable as a cache bounded to the given size
-
entries
Produces the cache entry set.For implementations testing only
-
get
Gets a value from this cache. -
put
Puts a value in this cache. -
size
public int size()Returns the cache size, the actual number of elements it contains.
-