Class CaffeineCache

java.lang.Object
com.oracle.coherence.caffeine.CaffeineCache
All Implemented Interfaces:
CacheMap, ConfigurableCacheMap, ObservableMap, ConcurrentMap, Map

public class CaffeineCache extends Object implements ConfigurableCacheMap, ConcurrentMap
A ConfigurableCacheMap backed by Caffeine. This implementation provides high read and write concurrency, a near optimal hit rate, and amortized O(1) expiration.

This implementation does not support providing an ConfigurableCacheMap.EvictionPolicy or ConfigurableCacheMap.EvictionApprover, and always uses the TinyLFU policy. The maximum size is set by setHighUnits(int) and the low watermark, setLowUnits(int), has no effect. Cache entries do not support touch(), getTouchCount(), getLastTouchMillis(), or setUnits(c). By default, the cache is unbounded and will not be limited by size or expiration until set.

Like ConcurrentHashMap but unlike HashMap and LocalCache, this cache does not support null keys or values.

Since:
22.06
Author:
Ben Manes 2022.04.01
See Also:
  • Constructor Details

    • CaffeineCache

      public CaffeineCache()
      Create CaffeineCache instance.
  • Method Details

    • getCacheStatistics

      public CacheStatistics getCacheStatistics()
      Returns the statistics for this cache.
      API Note:
      This method is called via reflection by CacheModel, in order to populate CacheMBean attributes.
    • setOptimizeGetTime

      public void setOptimizeGetTime(boolean fOptimize)
      Specify whether this cache is used in the environment, where the Base.getSafeTimeMillis() is used very frequently and as a result, the Base.getLastSafeTimeMillis() could be used without sacrificing the clock precision. By default, the optimization is off.
      Parameters:
      fOptimize - pass true to turn the "last safe time" optimization on
    • getAll

      public Map getAll(Collection colKeys)
      Description copied from interface: CacheMap
      Get all the specified keys, if they are in the cache. For each key that is in the cache, that key and its corresponding value will be placed in the map that is returned by this method. The absence of a key in the returned map indicates that it was not in the cache, which may imply (for caches that can load behind the scenes) that the requested data could not be loaded.

      The result of this method is defined to be semantically the same as the following implementation, without regards to threading issues:

       Map map = new AnyMap(); // could be a HashMap (but does not have to)
       for (Iterator iter = colKeys.iterator(); iter.hasNext(); )
           {
           Object oKey = iter.next();
           Object oVal = get(oKey);
           if (oVal != null || containsKey(oKey))
               {
               map.put(oKey, oVal);
               }
           }
       return map;
       
      Specified by:
      getAll in interface CacheMap
      Parameters:
      colKeys - a collection of keys that may be in the named cache
      Returns:
      a Map of keys to values for the specified keys passed in colKeys
    • put

      public Object put(Object oKey, Object oValue, long cMillis)
      Description copied from interface: CacheMap
      Associates the specified value with the specified key in this cache. If the cache previously contained a mapping for this key, the old value is replaced. This variation of the CacheMap.put(Object oKey, Object oValue) method allows the caller to specify an expiry (or "time to live") for the cache entry.
      Specified by:
      put in interface CacheMap
      Parameters:
      oKey - key with which the specified value is to be associated
      oValue - value to be associated with the specified key
      cMillis - the number of milliseconds until the cache entry will expire, also referred to as the entry's "time to live"; pass CacheMap.EXPIRY_DEFAULT to use the cache's default time-to-live setting; pass CacheMap.EXPIRY_NEVER to indicate that the cache entry should never expire; this milliseconds value is not a date/time value, such as is returned from System.currentTimeMillis()
      Returns:
      previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values
    • getUnits

      public int getUnits()
      Description copied from interface: ConfigurableCacheMap
      Determine the number of units that the cache currently stores.

      Note: It is expected that the return type will be widened to a long in Coherence 4.

      Specified by:
      getUnits in interface ConfigurableCacheMap
      Returns:
      the current size of the cache in units
    • getHighUnits

      public int getHighUnits()
      Description copied from interface: ConfigurableCacheMap
      Determine the limit of the cache size in units. The cache will prune itself automatically once it reaches its maximum unit level. This is often referred to as the "high water mark" of the cache.

      Note: It is expected that the return type will be widened to a long in Coherence 4.

      Specified by:
      getHighUnits in interface ConfigurableCacheMap
      Returns:
      the limit of the cache size in units
    • setHighUnits

      public void setHighUnits(int units)
      Description copied from interface: ConfigurableCacheMap
      Update the maximum size of the cache in units. This is often referred to as the "high water mark" of the cache.

      Note: It is expected that the parameter will be widened to a long in Coherence 4.

      Specified by:
      setHighUnits in interface ConfigurableCacheMap
      Parameters:
      units - the new maximum size of the cache, in units
    • getLowUnits

      public int getLowUnits()
      Description copied from interface: ConfigurableCacheMap
      Determine the point to which the cache will shrink when it prunes. This is often referred to as a "low water mark" of the cache. If the cache incrementally prunes, then this setting will have no effect.

      Note: It is expected that the parameter will be widened to a long in Coherence 4.

      Specified by:
      getLowUnits in interface ConfigurableCacheMap
      Returns:
      the number of units that the cache prunes to
    • setLowUnits

      public void setLowUnits(int units)
      Description copied from interface: ConfigurableCacheMap
      Specify the point to which the cache will shrink when it prunes. This is often referred to as a "low water mark" of the cache.

      Note: It is expected that the parameter will be widened to a long in Coherence 4.

      Specified by:
      setLowUnits in interface ConfigurableCacheMap
      Parameters:
      units - the number of units that the cache prunes to
    • getUnitFactor

      public int getUnitFactor()
      Description copied from interface: ConfigurableCacheMap
      Determine the factor by which the Units, LowUnits and HighUnits properties are adjusted. Using a binary unit calculator, for example, the factor 1048576 could be used to count megabytes instead of bytes.

      Note: This property exists only to avoid changing the type of the units, low units and high units properties from 32-bit values to 64-bit values. It is expected that the parameter will be dropped in Coherence 4.

      Specified by:
      getUnitFactor in interface ConfigurableCacheMap
      Returns:
      the units factor; the default is 1
    • setUnitFactor

      public void setUnitFactor(int nUnitFactor)
      Description copied from interface: ConfigurableCacheMap
      Determine the factor by which the Units, LowUnits and HighUnits properties are adjusted. Using a binary unit calculator, for example, the factor 1048576 could be used to count megabytes instead of bytes.

      Note: This property exists only to avoid changing the type of the units, low units and high units properties from 32-bit values to 64-bit values. It is expected that the parameter will be dropped in Coherence 4.

      Specified by:
      setUnitFactor in interface ConfigurableCacheMap
      Parameters:
      nUnitFactor - the units factor; the default is 1
    • getUnitCalculator

      public ConfigurableCacheMap.UnitCalculator getUnitCalculator()
      Description copied from interface: ConfigurableCacheMap
      Obtain the current UnitCalculator used by the cache.
      Specified by:
      getUnitCalculator in interface ConfigurableCacheMap
      Returns:
      the UnitCalculator used by the cache
    • setUnitCalculator

      public void setUnitCalculator(ConfigurableCacheMap.UnitCalculator calculator)
      Description copied from interface: ConfigurableCacheMap
      Set the UnitCalculator for the cache to use.
      Specified by:
      setUnitCalculator in interface ConfigurableCacheMap
      Parameters:
      calculator - a UnitCalculator
    • toInternalUnits

      protected static long toInternalUnits(int cUnits, int nFactor)
      Convert from an external 32-bit unit value to an internal 64-bit unit value using the configured units factor.
      Parameters:
      cUnits - an external 32-bit units value
      nFactor - the unit factor
      Returns:
      an internal 64-bit units value
    • toExternalUnits

      protected static int toExternalUnits(long cUnits, int nFactor)
      Convert from an internal 64-bit unit value to an external 32-bit unit value using the configured units factor.
      Parameters:
      cUnits - an internal 64-bit units value
      nFactor - the unit factor
      Returns:
      an external 32-bit units value
    • evict

      public void evict(Object oKey)
      Description copied from interface: ConfigurableCacheMap
      Evict a specified key from the cache, as if it had expired from the cache. If the key is not in the cache, then the method has no effect.
      Specified by:
      evict in interface ConfigurableCacheMap
      Parameters:
      oKey - the key to evict from the cache
    • evictAll

      public void evictAll(Collection colKeys)
      Description copied from interface: ConfigurableCacheMap
      Evict the specified keys from the cache, as if they had each expired from the cache.

      The result of this method is defined to be semantically the same as the following implementation:

      
       for (Iterator iter = colKeys.iterator(); iter.hasNext(); )
           {
           Object oKey = iter.next();
           evict(oKey);
           }
       
      Specified by:
      evictAll in interface ConfigurableCacheMap
      Parameters:
      colKeys - a collection of keys to evict from the cache
    • evict

      public void evict()
      Description copied from interface: ConfigurableCacheMap
      Evict all entries from the cache that are no longer valid, and potentially prune the cache size if the cache is size-limited and its size is above the caching low water mark.
      Specified by:
      evict in interface ConfigurableCacheMap
    • getExpiryDelay

      public int getExpiryDelay()
      Description copied from interface: ConfigurableCacheMap
      Determine the default "time to live" for each individual cache entry.
      Specified by:
      getExpiryDelay in interface ConfigurableCacheMap
      Returns:
      the number of milliseconds that a cache entry value will live, or zero if cache entries are never automatically expired
    • setExpiryDelay

      public void setExpiryDelay(int cMillis)
      Description copied from interface: ConfigurableCacheMap
      Specify the default "time to live" for cache entries. This does not affect the already-scheduled expiry of existing entries.
      Specified by:
      setExpiryDelay in interface ConfigurableCacheMap
      Parameters:
      cMillis - the number of milliseconds that cache entries will live, or zero to disable automatic expiry
    • getNextExpiryTime

      public long getNextExpiryTime()
      Description copied from interface: ConfigurableCacheMap
      Determine the next expiry time for the cache entries. This value is supposed to be used only by the "active" expiry algorithms, so for implementations that choose to return the value of zero the entries will be evicted as with pre-existing "passive" expiry approach.
      Specified by:
      getNextExpiryTime in interface ConfigurableCacheMap
      Returns:
      the earliest time (using the SafeClock) that one or more cache entries will expire or zero if the cache is empty, its entries never expire or the implementation chooses to avoid the pro-active eviction
    • getCacheEntry

      public ConfigurableCacheMap.Entry getCacheEntry(Object oKey)
      Description copied from interface: ConfigurableCacheMap
      Locate a cache Entry in the cache based on its key.
      Specified by:
      getCacheEntry in interface ConfigurableCacheMap
      Parameters:
      oKey - the key object to search for
      Returns:
      the Entry or null
    • getEvictionApprover

      public ConfigurableCacheMap.EvictionApprover getEvictionApprover()
      Description copied from interface: ConfigurableCacheMap
      Obtain the registered EvictionApprover.
      Specified by:
      getEvictionApprover in interface ConfigurableCacheMap
      Returns:
      the EvictionApprover (could be null)
    • setEvictionApprover

      public void setEvictionApprover(ConfigurableCacheMap.EvictionApprover approver)
      Description copied from interface: ConfigurableCacheMap
      Set the EvictionApprover for this ConfigurableCacheMap.
      Specified by:
      setEvictionApprover in interface ConfigurableCacheMap
      Parameters:
      approver - the EvictionApprover
    • getEvictionPolicy

      public ConfigurableCacheMap.EvictionPolicy getEvictionPolicy()
      Description copied from interface: ConfigurableCacheMap
      Obtain the current EvictionPolicy used by the cache.
      Specified by:
      getEvictionPolicy in interface ConfigurableCacheMap
      Returns:
      the EvictionPolicy used by the cache
    • setEvictionPolicy

      public void setEvictionPolicy(ConfigurableCacheMap.EvictionPolicy policy)
      Description copied from interface: ConfigurableCacheMap
      Set the EvictionPolicy for the cache to use.
      Specified by:
      setEvictionPolicy in interface ConfigurableCacheMap
      Parameters:
      policy - an EvictionPolicy
    • addMapListener

      public void addMapListener(MapListener listener)
      Description copied from interface: ObservableMap
      Add a standard map listener that will receive all events (inserts, updates, deletes) that occur against the map, with the key, old-value and new-value included. This has the same result as the following call:
         addMapListener(listener, (Filter) null, false);
       
      Specified by:
      addMapListener in interface ObservableMap
      Parameters:
      listener - the MapEvent listener to add
    • removeMapListener

      public void removeMapListener(MapListener listener)
      Description copied from interface: ObservableMap
      Remove a standard map listener that previously signed up for all events. This has the same result as the following call:
         removeMapListener(listener, (Filter) null);
       
      Specified by:
      removeMapListener in interface ObservableMap
      Parameters:
      listener - the listener to remove
    • addMapListener

      public void addMapListener(MapListener listener, Filter filter, boolean fLite)
      Description copied from interface: ObservableMap
      Add a map listener that receives events based on a filter evaluation.

      The listeners will receive MapEvent objects, but if fLite is passed as true, they might not contain the OldValue and NewValue properties.

      To unregister the MapListener, use the ObservableMap.removeMapListener(MapListener, Filter) method.

      Specified by:
      addMapListener in interface ObservableMap
      Parameters:
      listener - the MapEvent listener to add
      filter - a filter that will be passed MapEvent objects to select from; a MapEvent will be delivered to the listener only if the filter evaluates to true for that MapEvent (see MapEventFilter); null is equivalent to a filter that alway returns true
      fLite - true to indicate that the MapEvent objects do not have to include the OldValue and NewValue property values in order to allow optimizations
    • removeMapListener

      public void removeMapListener(MapListener listener, Filter filter)
      Description copied from interface: ObservableMap
      Remove a map listener that previously signed up for events based on a filter evaluation.
      Specified by:
      removeMapListener in interface ObservableMap
      Parameters:
      listener - the listener to remove
      filter - the filter that was passed into the corresponding addMapListener() call
    • addMapListener

      public void addMapListener(MapListener listener, Object oKey, boolean fLite)
      Description copied from interface: ObservableMap
      Add a map listener for a specific key.

      The listeners will receive MapEvent objects, but if fLite is passed as true, they might not contain the OldValue and NewValue properties.

      To unregister the MapListener, use the ObservableMap.removeMapListener(MapListener, Object) method.

      Specified by:
      addMapListener in interface ObservableMap
      Parameters:
      listener - the MapEvent listener to add
      oKey - the key that identifies the entry for which to raise events
      fLite - true to indicate that the MapEvent objects do not have to include the OldValue and NewValue property values in order to allow optimizations
    • removeMapListener

      public void removeMapListener(MapListener listener, Object oKey)
      Description copied from interface: ObservableMap
      Remove a map listener that previously signed up for events about a specific key.
      Specified by:
      removeMapListener in interface ObservableMap
      Parameters:
      listener - the listener to remove
      oKey - the key that identifies the entry for which to raise events
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map
    • size

      public int size()
      Specified by:
      size in interface Map
    • clear

      public void clear()
      Specified by:
      clear in interface Map
    • containsKey

      public boolean containsKey(Object oKey)
      Specified by:
      containsKey in interface Map
    • containsValue

      public boolean containsValue(Object oValue)
      Specified by:
      containsValue in interface Map
    • get

      public Object get(Object oKey)
      Specified by:
      get in interface Map
    • put

      public Object put(Object oKey, Object oValue)
      Description copied from interface: CacheMap
      Associates the specified value with the specified key in this cache. If the cache previously contained a mapping for this key, the old value is replaced.

      Invoking this method is equivalent to the following call:

           put(oKey, oValue, EXPIRY_DEFAULT);
       
      Specified by:
      put in interface CacheMap
      Specified by:
      put in interface Map
      Parameters:
      oKey - key with which the specified value is to be associated
      oValue - value to be associated with the specified key
      Returns:
      previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values
    • putAll

      public void putAll(Map map)
      Specified by:
      putAll in interface Map
    • putIfAbsent

      public Object putIfAbsent(Object oKey, Object oValue)
      Specified by:
      putIfAbsent in interface ConcurrentMap
      Specified by:
      putIfAbsent in interface Map
    • replace

      public Object replace(Object oKey, Object oValue)
      Specified by:
      replace in interface ConcurrentMap
      Specified by:
      replace in interface Map
    • replace

      public boolean replace(Object oKey, Object oValueOld, Object oValueNew)
      Specified by:
      replace in interface ConcurrentMap
      Specified by:
      replace in interface Map
    • replaceAll

      public void replaceAll(BiFunction function)
      Specified by:
      replaceAll in interface ConcurrentMap
      Specified by:
      replaceAll in interface Map
    • remove

      public Object remove(Object oKey)
      Specified by:
      remove in interface Map
    • remove

      public boolean remove(Object oKey, Object oValue)
      Specified by:
      remove in interface ConcurrentMap
      Specified by:
      remove in interface Map
    • computeIfAbsent

      public Object computeIfAbsent(Object oKey, Function mappingFunction)
      Specified by:
      computeIfAbsent in interface ConcurrentMap
      Specified by:
      computeIfAbsent in interface Map
    • computeIfPresent

      public Object computeIfPresent(Object oKey, BiFunction remappingFunction)
      Specified by:
      computeIfPresent in interface ConcurrentMap
      Specified by:
      computeIfPresent in interface Map
    • compute

      public Object compute(Object oKey, BiFunction remappingFunction)
      Specified by:
      compute in interface ConcurrentMap
      Specified by:
      compute in interface Map
    • merge

      public Object merge(Object oKey, Object oValue, BiFunction remappingFunction)
      Specified by:
      merge in interface ConcurrentMap
      Specified by:
      merge in interface Map
    • keySet

      public Set keySet()
      Specified by:
      keySet in interface Map
    • values

      public Collection values()
      Specified by:
      values in interface Map
    • entrySet

      public Set<Map.Entry> entrySet()
      Specified by:
      entrySet in interface Map
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Map
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object