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>
public class SoftCache<K,V> extends java.lang.Object implements 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
Fields Modifier and Type Field Description protected intcapacityThe cache capacity.protected static floatLOAD_FACTORThe default cache load factor.protected java.lang.ref.SoftReference<java.util.Map<K,V>>referenceThe soft reference to the cache map.
-
Constructor Summary
Constructors Constructor Description SoftCache(int theSize)Creates a new instance of a soft cache.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcapacity()Returns the cache capacity, the maximum number of elements it can contain.voidclear()Clears the cache.protected <KT,VT>
java.util.Map<KT,VT>createMap(int cacheSize)Creates a cache store.static <K,V>
java.util.Map<K,V>createSynchronizedLinkedHashMap(int capacity)Creates a synchronized LinkedHashMap.java.util.Collection<java.util.Map.Entry<K,V>>entries()Produces the cache entry set.Vget(K key)Gets a value from cache.Vput(K key, V script)Puts a value in cache.intsize()Returns the cache size, the actual number of elements it contains.
-
-
-
Field Detail
-
LOAD_FACTOR
protected static final float LOAD_FACTOR
The default cache load factor.- See Also:
- Constant Field Values
-
capacity
protected final int capacity
The cache capacity.
-
-
Method Detail
-
createSynchronizedLinkedHashMap
public static <K,V> java.util.Map<K,V> createSynchronizedLinkedHashMap(int capacity)
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.
-
createMap
protected <KT,VT> java.util.Map<KT,VT> createMap(int cacheSize)
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
public java.util.Collection<java.util.Map.Entry<K,V>> entries()
Produces the cache entry set.For implementations testing only
-
-