Class 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 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.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int capacity
      The cache capacity.
      protected static float LOAD_FACTOR
      The default cache load factor.
      protected java.lang.ref.SoftReference<java.util.Map<K,​V>> reference
      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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int capacity()
      Returns the cache capacity, the maximum number of elements it can contain.
      void clear()
      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.
      V get​(K key)
      Gets a value from cache.
      V put​(K key, V script)
      Puts a value in cache.
      int size()
      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 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.
      • reference

        protected volatile java.lang.ref.SoftReference<java.util.Map<K,​V>> reference
        The soft reference to the cache map.
    • Constructor Detail

      • SoftCache

        public SoftCache​(int theSize)
        Creates a new instance of a soft cache.
        Parameters:
        theSize - the cache size
    • Method Detail

      • createSynchronizedLinkedHashMap

        public static <K,​V> java.util.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> java.util.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 java.util.Collection<java.util.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