Class SoftCache<K,V>

java.lang.Object
org.apache.commons.jexl3.internal.SoftCache<K,V>
Type Parameters:
K - the cache key entry type
V - the cache key value type
All Implemented Interfaces:
JexlCache<K,V>

public class SoftCache<K,V> extends 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.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected 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
    Constructor
    Description
    SoftCache(int theSize)
    Creates a new instance of a soft cache.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the cache capacity, the maximum number of elements it can contain.
    void
    Clears the cache.
    protected <KT, VT> Map<KT,VT>
    createMap(int cacheSize)
    Creates a cache store.
    static <K, V> Map<K,V>
    Creates a synchronized LinkedHashMap.
    Produces the cache entry set.
    get(K key)
    Gets a value from cache.
    put(K key, V script)
    Puts a value in cache.
    int
    Returns the cache size, the actual number of elements it contains.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LOAD_FACTOR

      protected static final float LOAD_FACTOR
      The default cache load factor.
      See Also:
    • capacity

      protected final int capacity
      The cache capacity.
    • reference

      protected volatile SoftReference<Map<K,V>> 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

      public static <K, V> Map<K,V> createSynchronizedLinkedHashMap(int capacity)
      Creates a synchronized LinkedHashMap.
      Type Parameters:
      K - key type
      V - 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.
      Specified by:
      capacity in interface JexlCache<K,V>
      Returns:
      the cache capacity
    • clear

      public void clear()
      Clears the cache.
      Specified by:
      clear in interface JexlCache<K,V>
    • createMap

      protected <KT, VT> Map<KT,VT> createMap(int cacheSize)
      Creates a cache store.
      Type Parameters:
      KT - the key type
      VT - 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 Collection<Map.Entry<K,V>> entries()
      Produces the cache entry set.

      For implementations testing only

      Specified by:
      entries in interface JexlCache<K,V>
      Returns:
      the cache entry list
    • get

      public V get(K key)
      Gets a value from cache.
      Specified by:
      get in interface JexlCache<K,V>
      Parameters:
      key - the cache entry key
      Returns:
      the cache entry value
    • put

      public V put(K key, V script)
      Puts a value in cache.
      Specified by:
      put in interface JexlCache<K,V>
      Parameters:
      key - the cache entry key
      script - the cache entry value
      Returns:
      the previously associated value if any
    • size

      public int size()
      Returns the cache size, the actual number of elements it contains.
      Specified by:
      size in interface JexlCache<K,V>
      Returns:
      the cache size