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

public class SoftCache<K,V> extends Object
A soft referenced cache.

The actual cache is held through a soft reference, allowing it to be GCed under memory pressure.

  • Field Details

    • LOAD_FACTOR

      private static final float LOAD_FACTOR
      The default cache load factor.
      See Also:
    • size

      private final int size
      The cache size.
    • ref

      private SoftReference<Map<K,V>> ref
      The soft reference to the cache map.
    • lock

      private final ReadWriteLock lock
      The cache r/w lock.
  • Constructor Details

    • SoftCache

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

    • size

      public int size()
      Returns the cache size.
      Returns:
      the cache size
    • clear

      public void clear()
      Clears the cache.
    • get

      public V get(K key)
      Gets a value from cache.
      Parameters:
      key - the cache entry key
      Returns:
      the cache entry value
    • put

      public void put(K key, V script)
      Puts a value in cache.
      Parameters:
      key - the cache entry key
      script - the cache entry value
    • entries

      public List<Map.Entry<K,V>> entries()
      Produces the cache entry set.

      For testing only, perform deep copy of cache entries

      Returns:
      the cache entry list
    • createCache

      public <K, V> Map<K,V> createCache(int cacheSize)
      Creates the cache store.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      cacheSize - the cache size, must be > 0
      Returns:
      a Map usable as a cache bounded to the given size