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
AConfigurableCacheMap
backed 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.EvictionPolicy
orConfigurableCacheMap.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
ConcurrentHashMap
but unlikeHashMap
andOldCache
, this cache does not supportnull
keys 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()
CreateCaffeineCache
instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addMapListener(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.void
addMapListener(MapListener listener, Filter filter, boolean fLite)
Add a map listener that receives events based on a filter evaluation.void
addMapListener(MapListener listener, Object oKey, boolean fLite)
Add a map listener for a specific key.void
clear()
Object
compute(Object oKey, BiFunction remappingFunction)
Object
computeIfAbsent(Object oKey, Function mappingFunction)
Object
computeIfPresent(Object oKey, BiFunction remappingFunction)
boolean
containsKey(Object oKey)
boolean
containsValue(Object oValue)
Set<Map.Entry>
entrySet()
boolean
equals(Object o)
void
evict()
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.void
evict(Object oKey)
Evict a specified key from the cache, as if it had expired from the cache.void
evictAll(Collection colKeys)
Evict the specified keys from the cache, as if they had each expired from the cache.Object
get(Object oKey)
Map
getAll(Collection colKeys)
Get all the specified keys, if they are in the cache.ConfigurableCacheMap.Entry
getCacheEntry(Object oKey)
Locate a cache Entry in the cache based on its key.CacheStatistics
getCacheStatistics()
Returns the statistics for this cache.ConfigurableCacheMap.EvictionApprover
getEvictionApprover()
Obtain the registered EvictionApprover.ConfigurableCacheMap.EvictionPolicy
getEvictionPolicy()
Obtain the current EvictionPolicy used by the cache.int
getExpiryDelay()
Determine the default "time to live" for each individual cache entry.int
getHighUnits()
Determine the limit of the cache size in units.int
getLowUnits()
Determine the point to which the cache will shrink when it prunes.long
getNextExpiryTime()
Determine the next expiry time for the cache entries.ConfigurableCacheMap.UnitCalculator
getUnitCalculator()
Obtain the current UnitCalculator used by the cache.int
getUnitFactor()
Determine the factor by which the Units, LowUnits and HighUnits properties are adjusted.int
getUnits()
Determine the number of units that the cache currently stores.int
hashCode()
boolean
isEmpty()
Set
keySet()
Object
merge(Object oKey, Object oValue, BiFunction remappingFunction)
Object
put(Object oKey, Object oValue)
Associates the specified value with the specified key in this cache.Object
put(Object oKey, Object oValue, long cMillis)
Associates the specified value with the specified key in this cache.void
putAll(Map map)
Object
putIfAbsent(Object oKey, Object oValue)
Object
remove(Object oKey)
boolean
remove(Object oKey, Object oValue)
void
removeMapListener(MapListener listener)
Remove a standard map listener that previously signed up for all events.void
removeMapListener(MapListener listener, Filter filter)
Remove a map listener that previously signed up for events based on a filter evaluation.void
removeMapListener(MapListener listener, Object oKey)
Remove a map listener that previously signed up for events about a specific key.Object
replace(Object oKey, Object oValue)
boolean
replace(Object oKey, Object oValueOld, Object oValueNew)
void
replaceAll(BiFunction function)
void
setEvictionApprover(ConfigurableCacheMap.EvictionApprover approver)
Set the EvictionApprover for this ConfigurableCacheMap.void
setEvictionPolicy(ConfigurableCacheMap.EvictionPolicy policy)
Set the EvictionPolicy for the cache to use.void
setExpiryDelay(int cMillis)
Specify the default "time to live" for cache entries.void
setHighUnits(int units)
Update the maximum size of the cache in units.void
setLowUnits(int units)
Specify the point to which the cache will shrink when it prunes.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.void
setUnitCalculator(ConfigurableCacheMap.UnitCalculator calculator)
Set the UnitCalculator for the cache to use.void
setUnitFactor(int nUnitFactor)
Determine the factor by which the Units, LowUnits and HighUnits properties are adjusted.int
size()
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.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.String
toString()
Collection
values()
-
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 populateCacheMBean
attributes.
-
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: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;
-
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 theCacheMap.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 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_DEFAULT
to use the cache's default time-to-live setting; passCacheMap.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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 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: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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
-
getExpiryDelay
public int getExpiryDelay()
Description copied from interface:ConfigurableCacheMap
Determine the default "time to live" for each individual cache entry.- Specified by:
getExpiryDelay
in 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: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 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: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 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:ConfigurableCacheMap
Locate a cache Entry in the cache based on its key.- Specified by:
getCacheEntry
in interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceConfigurableCacheMap
- 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 interfaceObservableMap
- Parameters:
listener
- theMapEvent
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 interfaceObservableMap
- 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 interfaceObservableMap
- Parameters:
listener
- theMapEvent
listener 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 theMapEvent
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 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: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 interfaceObservableMap
- Parameters:
listener
- theMapEvent
listener to addoKey
- the key that identifies the entry for which to raise eventsfLite
- true to indicate that theMapEvent
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 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:
containsKey
in interfaceMap
-
containsValue
public boolean containsValue(Object oValue)
- Specified by:
containsValue
in interfaceMap
-
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 interfaceCacheMap
- Specified by:
put
in 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:
putIfAbsent
in interfaceConcurrentMap
- Specified by:
putIfAbsent
in interfaceMap
-
replace
public Object replace(Object oKey, Object oValue)
- Specified by:
replace
in interfaceConcurrentMap
- Specified by:
replace
in interfaceMap
-
replace
public boolean replace(Object oKey, Object oValueOld, Object oValueNew)
- Specified by:
replace
in interfaceConcurrentMap
- Specified by:
replace
in interfaceMap
-
replaceAll
public void replaceAll(BiFunction function)
- Specified by:
replaceAll
in interfaceConcurrentMap
- Specified by:
replaceAll
in interfaceMap
-
remove
public boolean remove(Object oKey, Object oValue)
- Specified by:
remove
in interfaceConcurrentMap
- Specified by:
remove
in interfaceMap
-
computeIfAbsent
public Object computeIfAbsent(Object oKey, Function mappingFunction)
- Specified by:
computeIfAbsent
in interfaceConcurrentMap
- Specified by:
computeIfAbsent
in interfaceMap
-
computeIfPresent
public Object computeIfPresent(Object oKey, BiFunction remappingFunction)
- Specified by:
computeIfPresent
in interfaceConcurrentMap
- Specified by:
computeIfPresent
in interfaceMap
-
compute
public Object compute(Object oKey, BiFunction remappingFunction)
- Specified by:
compute
in interfaceConcurrentMap
- Specified by:
compute
in interfaceMap
-
merge
public Object merge(Object oKey, Object oValue, BiFunction remappingFunction)
- Specified by:
merge
in interfaceConcurrentMap
- Specified by:
merge
in interfaceMap
-
values
public Collection values()
-
equals
public boolean equals(Object o)
-
hashCode
public int hashCode()
-
-