Class CaffeineCache
- All Implemented Interfaces:
CacheMap
,ConfigurableCacheMap
,ObservableMap
,ConcurrentMap
,Map
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:
-
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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
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()
compute
(Object oKey, BiFunction remappingFunction) computeIfAbsent
(Object oKey, Function mappingFunction) computeIfPresent
(Object oKey, BiFunction remappingFunction) boolean
containsKey
(Object oKey) boolean
containsValue
(Object oValue) entrySet()
boolean
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 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.getAll
(Collection colKeys) Get all the specified keys, if they are in the cache.getCacheEntry
(Object oKey) Locate a cache Entry in the cache based on its key.Returns the statistics for this cache.Obtain the registered EvictionApprover.Obtain the current EvictionPolicy used by the cache.int
Determine the default "time to live" for each individual cache entry.int
Determine the limit of the cache size in units.int
Determine the point to which the cache will shrink when it prunes.long
Determine the next expiry time for the cache entries.Obtain the current UnitCalculator used by the cache.int
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()
keySet()
merge
(Object oKey, Object oValue, BiFunction remappingFunction) Associates the specified value with the specified key in this cache.Associates the specified value with the specified key in this cache.void
putIfAbsent
(Object oKey, Object oValue) boolean
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.boolean
void
replaceAll
(BiFunction function) void
Set the EvictionApprover for this ConfigurableCacheMap.void
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.toString()
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
-
Constructor Details
-
CaffeineCache
public CaffeineCache()CreateCaffeineCache
instance.
-
-
Method Details
-
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
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
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
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
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
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
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
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
Description copied from interface:ConfigurableCacheMap
Obtain the registered EvictionApprover.- Specified by:
getEvictionApprover
in interfaceConfigurableCacheMap
- Returns:
- the EvictionApprover (could be null)
-
setEvictionApprover
Description copied from interface:ConfigurableCacheMap
Set the EvictionApprover for this ConfigurableCacheMap.- Specified by:
setEvictionApprover
in interfaceConfigurableCacheMap
- Parameters:
approver
- the EvictionApprover
-
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
Description copied from interface:ConfigurableCacheMap
Set the EvictionPolicy for the cache to use.- Specified by:
setEvictionPolicy
in interfaceConfigurableCacheMap
- Parameters:
policy
- an EvictionPolicy
-
addMapListener
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
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
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
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
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
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
-
isEmpty
public boolean isEmpty() -
size
public int size() -
clear
public void clear() -
containsKey
- Specified by:
containsKey
in interfaceMap
-
containsValue
- Specified by:
containsValue
in interfaceMap
-
get
-
put
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
-
putAll
-
putIfAbsent
- Specified by:
putIfAbsent
in interfaceConcurrentMap
- Specified by:
putIfAbsent
in interfaceMap
-
replace
- Specified by:
replace
in interfaceConcurrentMap
- Specified by:
replace
in interfaceMap
-
replace
- Specified by:
replace
in interfaceConcurrentMap
- Specified by:
replace
in interfaceMap
-
replaceAll
- Specified by:
replaceAll
in interfaceConcurrentMap
- Specified by:
replaceAll
in interfaceMap
-
remove
-
remove
- Specified by:
remove
in interfaceConcurrentMap
- Specified by:
remove
in interfaceMap
-
computeIfAbsent
- Specified by:
computeIfAbsent
in interfaceConcurrentMap
- Specified by:
computeIfAbsent
in interfaceMap
-
computeIfPresent
- Specified by:
computeIfPresent
in interfaceConcurrentMap
- Specified by:
computeIfPresent
in interfaceMap
-
compute
- Specified by:
compute
in interfaceConcurrentMap
- Specified by:
compute
in interfaceMap
-
merge
- Specified by:
merge
in interfaceConcurrentMap
- Specified by:
merge
in interfaceMap
-
keySet
-
values
-
entrySet
-
equals
-
hashCode
public int hashCode() -
toString
-