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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final int
The cache capacity.protected static final float
The default cache load factor.protected SoftReference
<Map<K, V>> The soft reference to the cache map. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
capacity()
Returns the cache capacity, the maximum number of elements it can contain.void
clear()
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 cache.Puts a value in cache.int
size()
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 cache. -
put
Puts a value in cache. -
size
public int size()Returns the cache size, the actual number of elements it contains.
-