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
AConfigurableCacheMapbacked by Caffeine. This implementation provides high read and write concurrency, a near optimal hit rate, and amortizedO(1)expiration.This implementation does not support providing an
ConfigurableCacheMap.EvictionPolicyorConfigurableCacheMap.EvictionApprover, and always uses the TinyLFU policy. The maximum size is set bysetHighUnits(int)and the low watermark,setLowUnits(int), has no effect. Cache entries do not supporttouch(),getTouchCount(),getLastTouchMillis(), orsetUnits(c). By default, the cache is unbounded and will not be limited by size or expiration until set.Like
ConcurrentHashMapbut unlikeHashMapandOldCache, this cache does not supportnullkeys or values.- Since:
- 22.06
- Author:
- Ben Manes 2022.04.01
- See Also:
- Caffeine Project, Design of a Modern Cache, TinyLFU: A Highly Efficient Cache Admission Policy
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.tangosol.net.cache.ConfigurableCacheMap
ConfigurableCacheMap.Entry, ConfigurableCacheMap.EvictionApprover, ConfigurableCacheMap.EvictionPolicy, ConfigurableCacheMap.UnitCalculator
-
-
Field Summary
-
Fields inherited from interface com.tangosol.net.cache.CacheMap
EXPIRY_DEFAULT, EXPIRY_NEVER
-
-
Constructor Summary
Constructors Constructor Description CaffeineCache()CreateCaffeineCacheinstance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddMapListener(MapListener listener)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.voidaddMapListener(MapListener listener, Filter filter, boolean fLite)Add a map listener that receives events based on a filter evaluation.voidaddMapListener(MapListener listener, Object oKey, boolean fLite)Add a map listener for a specific key.voidclear()Objectcompute(Object oKey, BiFunction remappingFunction)ObjectcomputeIfAbsent(Object oKey, Function mappingFunction)ObjectcomputeIfPresent(Object oKey, BiFunction remappingFunction)booleancontainsKey(Object oKey)booleancontainsValue(Object oValue)Set<Map.Entry>entrySet()booleanequals(Object o)voidevict()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.voidevict(Object oKey)Evict a specified key from the cache, as if it had expired from the cache.voidevictAll(Collection colKeys)Evict the specified keys from the cache, as if they had each expired from the cache.Objectget(Object oKey)MapgetAll(Collection colKeys)Get all the specified keys, if they are in the cache.ConfigurableCacheMap.EntrygetCacheEntry(Object oKey)Locate a cache Entry in the cache based on its key.CacheStatisticsgetCacheStatistics()Returns the statistics for this cache.ConfigurableCacheMap.EvictionApprovergetEvictionApprover()Obtain the registered EvictionApprover.ConfigurableCacheMap.EvictionPolicygetEvictionPolicy()Obtain the current EvictionPolicy used by the cache.intgetExpiryDelay()Determine the default "time to live" for each individual cache entry.intgetHighUnits()Determine the limit of the cache size in units.intgetLowUnits()Determine the point to which the cache will shrink when it prunes.longgetNextExpiryTime()Determine the next expiry time for the cache entries.ConfigurableCacheMap.UnitCalculatorgetUnitCalculator()Obtain the current UnitCalculator used by the cache.intgetUnitFactor()Determine the factor by which the Units, LowUnits and HighUnits properties are adjusted.intgetUnits()Determine the number of units that the cache currently stores.inthashCode()booleanisEmpty()SetkeySet()Objectmerge(Object oKey, Object oValue, BiFunction remappingFunction)Objectput(Object oKey, Object oValue)Associates the specified value with the specified key in this cache.Objectput(Object oKey, Object oValue, long cMillis)Associates the specified value with the specified key in this cache.voidputAll(Map map)ObjectputIfAbsent(Object oKey, Object oValue)Objectremove(Object oKey)booleanremove(Object oKey, Object oValue)voidremoveMapListener(MapListener listener)Remove a standard map listener that previously signed up for all events.voidremoveMapListener(MapListener listener, Filter filter)Remove a map listener that previously signed up for events based on a filter evaluation.voidremoveMapListener(MapListener listener, Object oKey)Remove a map listener that previously signed up for events about a specific key.Objectreplace(Object oKey, Object oValue)booleanreplace(Object oKey, Object oValueOld, Object oValueNew)voidreplaceAll(BiFunction function)voidsetEvictionApprover(ConfigurableCacheMap.EvictionApprover approver)Set the EvictionApprover for this ConfigurableCacheMap.voidsetEvictionPolicy(ConfigurableCacheMap.EvictionPolicy policy)Set the EvictionPolicy for the cache to use.voidsetExpiryDelay(int cMillis)Specify the default "time to live" for cache entries.voidsetHighUnits(int units)Update the maximum size of the cache in units.voidsetLowUnits(int units)Specify the point to which the cache will shrink when it prunes.voidsetOptimizeGetTime(boolean fOptimize)Specify whether this cache is used in the environment, where theBase.getSafeTimeMillis()is used very frequently and as a result, theBase.getLastSafeTimeMillis()could be used without sacrificing the clock precision.voidsetUnitCalculator(ConfigurableCacheMap.UnitCalculator calculator)Set the UnitCalculator for the cache to use.voidsetUnitFactor(int nUnitFactor)Determine the factor by which the Units, LowUnits and HighUnits properties are adjusted.intsize()protected static inttoExternalUnits(long cUnits, int nFactor)Convert from an internal 64-bit unit value to an external 32-bit unit value using the configured units factor.protected static longtoInternalUnits(int cUnits, int nFactor)Convert from an external 32-bit unit value to an internal 64-bit unit value using the configured units factor.StringtoString()Collectionvalues()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.concurrent.ConcurrentMap
forEach, getOrDefault
-
-
-
-
Method Detail
-
getCacheStatistics
public CacheStatistics getCacheStatistics()
Returns the statistics for this cache.- API Note:
- This method is called via reflection by
CacheModel, in order to populateCacheMBeanattributes.
-
setOptimizeGetTime
public void setOptimizeGetTime(boolean fOptimize)
Specify whether this cache is used in the environment, where theBase.getSafeTimeMillis()is used very frequently and as a result, theBase.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:CacheMapGet 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;
-
put
public Object put(Object oKey, Object oValue, long cMillis)
Description copied from interface:CacheMapAssociates 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 theCacheMap.put(Object oKey, Object oValue)method allows the caller to specify an expiry (or "time to live") for the cache entry.- Specified by:
putin interfaceCacheMap- Parameters:
oKey- key with which the specified value is to be associatedoValue- value to be associated with the specified keycMillis- the number of milliseconds until the cache entry will expire, also referred to as the entry's "time to live"; passCacheMap.EXPIRY_DEFAULTto use the cache's default time-to-live setting; passCacheMap.EXPIRY_NEVERto 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:ConfigurableCacheMapDetermine 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:
getUnitsin interfaceConfigurableCacheMap- Returns:
- the current size of the cache in units
-
getHighUnits
public int getHighUnits()
Description copied from interface:ConfigurableCacheMapDetermine 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:
getHighUnitsin interfaceConfigurableCacheMap- Returns:
- the limit of the cache size in units
-
setHighUnits
public void setHighUnits(int units)
Description copied from interface:ConfigurableCacheMapUpdate 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:
setHighUnitsin interfaceConfigurableCacheMap- Parameters:
units- the new maximum size of the cache, in units
-
getLowUnits
public int getLowUnits()
Description copied from interface:ConfigurableCacheMapDetermine 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:
getLowUnitsin interfaceConfigurableCacheMap- Returns:
- the number of units that the cache prunes to
-
setLowUnits
public void setLowUnits(int units)
Description copied from interface:ConfigurableCacheMapSpecify 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:
setLowUnitsin interfaceConfigurableCacheMap- Parameters:
units- the number of units that the cache prunes to
-
getUnitFactor
public int getUnitFactor()
Description copied from interface:ConfigurableCacheMapDetermine 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:
getUnitFactorin interfaceConfigurableCacheMap- Returns:
- the units factor; the default is 1
-
setUnitFactor
public void setUnitFactor(int nUnitFactor)
Description copied from interface:ConfigurableCacheMapDetermine 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:
setUnitFactorin interfaceConfigurableCacheMap- Parameters:
nUnitFactor- the units factor; the default is 1
-
getUnitCalculator
public ConfigurableCacheMap.UnitCalculator getUnitCalculator()
Description copied from interface:ConfigurableCacheMapObtain the current UnitCalculator used by the cache.- Specified by:
getUnitCalculatorin interfaceConfigurableCacheMap- Returns:
- the UnitCalculator used by the cache
-
setUnitCalculator
public void setUnitCalculator(ConfigurableCacheMap.UnitCalculator calculator)
Description copied from interface:ConfigurableCacheMapSet the UnitCalculator for the cache to use.- Specified by:
setUnitCalculatorin interfaceConfigurableCacheMap- 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 valuenFactor- 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 valuenFactor- the unit factor- Returns:
- an external 32-bit units value
-
evict
public void evict(Object oKey)
Description copied from interface:ConfigurableCacheMapEvict 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:
evictin interfaceConfigurableCacheMap- Parameters:
oKey- the key to evict from the cache
-
evictAll
public void evictAll(Collection colKeys)
Description copied from interface:ConfigurableCacheMapEvict 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:
evictAllin interfaceConfigurableCacheMap- Parameters:
colKeys- a collection of keys to evict from the cache
-
evict
public void evict()
Description copied from interface:ConfigurableCacheMapEvict 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:
evictin interfaceConfigurableCacheMap
-
getExpiryDelay
public int getExpiryDelay()
Description copied from interface:ConfigurableCacheMapDetermine the default "time to live" for each individual cache entry.- Specified by:
getExpiryDelayin interfaceConfigurableCacheMap- 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:ConfigurableCacheMapSpecify the default "time to live" for cache entries. This does not affect the already-scheduled expiry of existing entries.- Specified by:
setExpiryDelayin interfaceConfigurableCacheMap- 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:ConfigurableCacheMapDetermine 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:
getNextExpiryTimein interfaceConfigurableCacheMap- 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:ConfigurableCacheMapLocate a cache Entry in the cache based on its key.- Specified by:
getCacheEntryin interfaceConfigurableCacheMap- Parameters:
oKey- the key object to search for- Returns:
- the Entry or null
-
getEvictionApprover
public ConfigurableCacheMap.EvictionApprover getEvictionApprover()
Description copied from interface:ConfigurableCacheMapObtain the registered EvictionApprover.- Specified by:
getEvictionApproverin interfaceConfigurableCacheMap- Returns:
- the EvictionApprover (could be null)
-
setEvictionApprover
public void setEvictionApprover(ConfigurableCacheMap.EvictionApprover approver)
Description copied from interface:ConfigurableCacheMapSet the EvictionApprover for this ConfigurableCacheMap.- Specified by:
setEvictionApproverin interfaceConfigurableCacheMap- Parameters:
approver- the EvictionApprover
-
getEvictionPolicy
public ConfigurableCacheMap.EvictionPolicy getEvictionPolicy()
Description copied from interface:ConfigurableCacheMapObtain the current EvictionPolicy used by the cache.- Specified by:
getEvictionPolicyin interfaceConfigurableCacheMap- Returns:
- the EvictionPolicy used by the cache
-
setEvictionPolicy
public void setEvictionPolicy(ConfigurableCacheMap.EvictionPolicy policy)
Description copied from interface:ConfigurableCacheMapSet the EvictionPolicy for the cache to use.- Specified by:
setEvictionPolicyin interfaceConfigurableCacheMap- Parameters:
policy- an EvictionPolicy
-
addMapListener
public void addMapListener(MapListener listener)
Description copied from interface:ObservableMapAdd 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:
addMapListenerin interfaceObservableMap- Parameters:
listener- theMapEventlistener to add
-
removeMapListener
public void removeMapListener(MapListener listener)
Description copied from interface:ObservableMapRemove 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:
removeMapListenerin interfaceObservableMap- Parameters:
listener- the listener to remove
-
addMapListener
public void addMapListener(MapListener listener, Filter filter, boolean fLite)
Description copied from interface:ObservableMapAdd 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:
addMapListenerin interfaceObservableMap- Parameters:
listener- theMapEventlistener to addfilter- 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 (seeMapEventFilter); null is equivalent to a filter that alway returns truefLite- true to indicate that theMapEventobjects 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:ObservableMapRemove a map listener that previously signed up for events based on a filter evaluation.- Specified by:
removeMapListenerin interfaceObservableMap- Parameters:
listener- the listener to removefilter- the filter that was passed into the corresponding addMapListener() call
-
addMapListener
public void addMapListener(MapListener listener, Object oKey, boolean fLite)
Description copied from interface:ObservableMapAdd 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:
addMapListenerin interfaceObservableMap- Parameters:
listener- theMapEventlistener to addoKey- the key that identifies the entry for which to raise eventsfLite- true to indicate that theMapEventobjects 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:ObservableMapRemove a map listener that previously signed up for events about a specific key.- Specified by:
removeMapListenerin interfaceObservableMap- Parameters:
listener- the listener to removeoKey- the key that identifies the entry for which to raise events
-
containsKey
public boolean containsKey(Object oKey)
- Specified by:
containsKeyin interfaceMap
-
containsValue
public boolean containsValue(Object oValue)
- Specified by:
containsValuein interfaceMap
-
put
public Object put(Object oKey, Object oValue)
Description copied from interface:CacheMapAssociates 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:
putin interfaceCacheMap- Specified by:
putin interfaceMap- Parameters:
oKey- key with which the specified value is to be associatedoValue- 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
-
putIfAbsent
public Object putIfAbsent(Object oKey, Object oValue)
- Specified by:
putIfAbsentin interfaceConcurrentMap- Specified by:
putIfAbsentin interfaceMap
-
replace
public Object replace(Object oKey, Object oValue)
- Specified by:
replacein interfaceConcurrentMap- Specified by:
replacein interfaceMap
-
replace
public boolean replace(Object oKey, Object oValueOld, Object oValueNew)
- Specified by:
replacein interfaceConcurrentMap- Specified by:
replacein interfaceMap
-
replaceAll
public void replaceAll(BiFunction function)
- Specified by:
replaceAllin interfaceConcurrentMap- Specified by:
replaceAllin interfaceMap
-
remove
public boolean remove(Object oKey, Object oValue)
- Specified by:
removein interfaceConcurrentMap- Specified by:
removein interfaceMap
-
computeIfAbsent
public Object computeIfAbsent(Object oKey, Function mappingFunction)
- Specified by:
computeIfAbsentin interfaceConcurrentMap- Specified by:
computeIfAbsentin interfaceMap
-
computeIfPresent
public Object computeIfPresent(Object oKey, BiFunction remappingFunction)
- Specified by:
computeIfPresentin interfaceConcurrentMap- Specified by:
computeIfPresentin interfaceMap
-
compute
public Object compute(Object oKey, BiFunction remappingFunction)
- Specified by:
computein interfaceConcurrentMap- Specified by:
computein interfaceMap
-
merge
public Object merge(Object oKey, Object oValue, BiFunction remappingFunction)
- Specified by:
mergein interfaceConcurrentMap- Specified by:
mergein interfaceMap
-
values
public Collection values()
-
equals
public boolean equals(Object o)
-
hashCode
public int hashCode()
-
-