Class CaffeineCache<K extends Serializable,V extends Serializable>
- java.lang.Object
-
- org.jivesoftware.util.cache.CaffeineCache<K,V>
-
- Type Parameters:
K
- Cache key type.V
- Cache value type.
public class CaffeineCache<K extends Serializable,V extends Serializable> extends Object implements Cache<K,V>
Wraps an instance of Ben Manes' Caffeine cache in a class that inherits fromCache
. This implementation intends to make Caffeine caches compatible with utility functionality that is based onCache
. Caffeine is a highly configurable implementation. To be able to achieve compatibility withCache
, a specific configuration is required:- The cache must have an eviction policy, which must be based on 'weight'. Caffeine does not define a unit for weight. This class assumes that the weight is based on byte size of the cache entry.
- The cache must be configured to expire entries after write.
- See Also:
- Caffeine
-
-
Field Summary
-
Fields inherited from interface org.jivesoftware.util.cache.Cache
secretKey, secretValue
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
addClusteredCacheEntryListener(ClusteredCacheEntryListener<K,V> listener, boolean includeValues, boolean includeEventsFromLocalNode)
Registers a listener to receive entry events for this cache.void
clear()
Removes all of the mappings from this map (optional operation).boolean
containsKey(Object key)
Returns true if this map contains a mapping for the specified key.boolean
containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.Set<Map.Entry<K,V>>
entrySet()
IMPORTANT: Unlike the standardMap.entrySet()
implementation, the set returned from this method cannot be modified.V
get(Object key)
Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.long
getCacheHits()
Returns the number of cache hits.long
getCacheMisses()
Returns the number of cache misses.Cache.CapacityUnit
getCapacityUnit()
Defines the unit used to calculate the capacity of the cache, which for all instances of this class is byte-size based.long
getLongCacheSize()
Returns the size of the cache contents in bytes.long
getMaxCacheSize()
Returns the maximum size of the cache in bytes.long
getMaxLifetime()
Returns the maximum number of milliseconds that any object can live in cache.String
getName()
Returns the name of the cache.boolean
isEmpty()
Returns true if this map contains no key-value mappings.Set<K>
keySet()
IMPORTANT: Unlike the standardMap.keySet()
implementation, the set returned from this method cannot be modified.static <K extends Serializable,V extends Serializable>
CaffeineCacheof(com.github.benmanes.caffeine.cache.Cache<K,V> cache, String name)
Wraps a Caffeine cache in an implementation that's Compatible withCache
.V
put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).void
putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map (optional operation).V
remove(Object key)
Removes the mapping for a key from this map if it is present (optional operation).void
removeClusteredCacheEntryListener(String listenerId)
Removes a previously registered event listener.void
setMaxCacheSize(long maxSize)
Sets the maximum size of the cache in bytes.void
setMaxLifetime(long maxLifetime)
Sets the maximum number of milliseconds that any object can live in cache.void
setName(String name)
Sets the name of the cacheint
size()
Returns the number of key-value mappings in this map.Collection<V>
values()
IMPORTANT: Unlike the standardMap.values()
implementation, the collection returned from this method cannot be modified.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.jivesoftware.util.cache.Cache
getCacheSizeRemark, getLock, getMaxCacheSizeRemark, isKeySecret, isValueSecret, setSecretKey, setSecretValue
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Method Detail
-
of
public static <K extends Serializable,V extends Serializable> CaffeineCache of(com.github.benmanes.caffeine.cache.Cache<K,V> cache, String name)
Wraps a Caffeine cache in an implementation that's Compatible withCache
.- Type Parameters:
K
- Cache key type.V
- Cache value type.- Parameters:
cache
- The cache to be wrapped (cannot be null).name
- A human-readable name for the cache that's wrapped.- Returns:
- A wrapped cache (never null).
- Throws:
IllegalArgumentException
- when the cache configuration does not conform to the contract defined in the javadoc of this class.
-
getCapacityUnit
public Cache.CapacityUnit getCapacityUnit()
Defines the unit used to calculate the capacity of the cache, which for all instances of this class is byte-size based.- Specified by:
getCapacityUnit
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the unit to be used to calculate the capacity of this cache, which will be
Cache.CapacityUnit.BYTES
.
-
getName
public String getName()
Returns the name of the cache.- Specified by:
getName
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the name of the cache.
-
setName
public void setName(String name)
Sets the name of the cache- Specified by:
setName
in interfaceCache<K extends Serializable,V extends Serializable>
- Parameters:
name
- the name of the cache
-
getMaxCacheSize
public long getMaxCacheSize()
Returns the maximum size of the cache in bytes. If the cache grows larger than the max size, the least frequently used items will be removed. If the max cache size is set to -1, there is no size limit.- Specified by:
getMaxCacheSize
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the maximum size of the cache in bytes.
-
setMaxCacheSize
public void setMaxCacheSize(long maxSize)
Sets the maximum size of the cache in bytes. If the cache grows larger than the max size, the least frequently used items will be removed. If the max cache size is set to -1, there is no size limit.Note: If using the Hazelcast clustering plugin, this will not take effect until the next time the cache is created
- Specified by:
setMaxCacheSize
in interfaceCache<K extends Serializable,V extends Serializable>
- Parameters:
maxSize
- the maximum size of the cache in bytes.
-
getMaxLifetime
public long getMaxLifetime()
Returns the maximum number of milliseconds that any object can live in cache. Once the specified number of milliseconds passes, the object will be automatically expired from cache. If the max lifetime is set to -1, then objects never expire.- Specified by:
getMaxLifetime
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the maximum number of milliseconds before objects are expired.
-
setMaxLifetime
public void setMaxLifetime(long maxLifetime)
Sets the maximum number of milliseconds that any object can live in cache. Once the specified number of milliseconds passes, the object will be automatically expired from cache. If the max lifetime is set to -1, then objects never expire.Note: If using the Hazelcast clustering plugin, this will not take effect until the next time the cache is created
- Specified by:
setMaxLifetime
in interfaceCache<K extends Serializable,V extends Serializable>
- Parameters:
maxLifetime
- the maximum number of milliseconds before objects are expired.
-
getLongCacheSize
public long getLongCacheSize()
Returns the size of the cache contents in bytes. This value is only a rough approximation, so cache users should expect that actual VM memory used by the cache could be significantly higher than the value reported by this method.- Specified by:
getLongCacheSize
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the size of the cache contents in bytes.
-
getCacheHits
public long getCacheHits()
Returns the number of cache hits. A cache hit occurs every time the get method is called and the cache contains the requested object.Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
- Specified by:
getCacheHits
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the number of cache hits.
-
getCacheMisses
public long getCacheMisses()
Returns the number of cache misses. A cache miss occurs every time the get method is called and the cache does not contain the requested object.Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
- Specified by:
getCacheMisses
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the number of cache hits.
-
values
public Collection<V> values()
IMPORTANT: Unlike the standardMap.values()
implementation, the collection returned from this method cannot be modified.- Specified by:
values
in interfaceCache<K extends Serializable,V extends Serializable>
- Specified by:
values
in interfaceMap<K extends Serializable,V extends Serializable>
- Returns:
- an unmodifiable collection view of the values contained in this map
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
IMPORTANT: Unlike the standardMap.entrySet()
implementation, the set returned from this method cannot be modified.- Specified by:
entrySet
in interfaceCache<K extends Serializable,V extends Serializable>
- Specified by:
entrySet
in interfaceMap<K extends Serializable,V extends Serializable>
- Returns:
- an unmodifiable set view of the mappings contained in this map
-
size
public int size()
Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.- Specified by:
size
in interfaceMap<K extends Serializable,V extends Serializable>
- Returns:
- the number of key-value mappings in this map
-
isEmpty
public boolean isEmpty()
Returns true if this map contains no key-value mappings.- Specified by:
isEmpty
in interfaceMap<K extends Serializable,V extends Serializable>
- Returns:
- true if this map contains no key-value mappings
-
containsKey
public boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)- Specified by:
containsKey
in interfaceMap<K extends Serializable,V extends Serializable>
- Parameters:
key
- key whose presence in this map is to be tested- Returns:
- true if this map contains a mapping for the specified key
- Throws:
ClassCastException
- if the key is of an inappropriate type for this map (optional)NullPointerException
- if the specified key is null and this map does not permit null keys (optional)
-
containsValue
public boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.- Specified by:
containsValue
in interfaceMap<K extends Serializable,V extends Serializable>
- Parameters:
value
- value whose presence in this map is to be tested- Returns:
- true if this map maps one or more keys to the specified value
- Throws:
ClassCastException
- if the value is of an inappropriate type for this map (optional)NullPointerException
- if the specified value is null and this map does not permit null values (optional)
-
get
public V get(Object key)
Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.More formally, if this map contains a mapping from a key
k
to a valuev
such that(key==null ? k==null : key.equals(k))
, then this method returnsv
; otherwise it returnsnull
. (There can be at most one such mapping.)If this map permits null values, then a return value of
null
does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key tonull
. ThecontainsKey
operation may be used to distinguish these two cases.- Specified by:
get
in interfaceMap<K extends Serializable,V extends Serializable>
- Parameters:
key
- the key whose associated value is to be returned- Returns:
- the value to which the specified key is mapped, or
null
if this map contains no mapping for the key - Throws:
ClassCastException
- if the key is of an inappropriate type for this map (optional)NullPointerException
- if the specified key is null and this map does not permit null keys (optional)
-
put
public V put(K key, V value)
Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only ifm.containsKey(k)
would return true.)- Specified by:
put
in interfaceMap<K extends Serializable,V extends Serializable>
- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)
- Throws:
UnsupportedOperationException
- if the put operation is not supported by this mapClassCastException
- if the class of the specified key or value prevents it from being stored in this mapNullPointerException
- if the specified key or value is null and this map does not permit null keys or valuesIllegalArgumentException
- if some property of the specified key or value prevents it from being stored in this map
-
remove
public V remove(Object key)
Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that(key==null ? k==null : key.equals(k))
, that mapping is removed. (The map can contain at most one such mapping.)Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.
If this map permits null values, then a return value of null does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key to null.
The map will not contain a mapping for the specified key once the call returns.
- Specified by:
remove
in interfaceMap<K extends Serializable,V extends Serializable>
- Parameters:
key
- key whose mapping is to be removed from the map- Returns:
- the previous value associated with key, or null if there was no mapping for key.
- Throws:
UnsupportedOperationException
- if the remove operation is not supported by this mapClassCastException
- if the key is of an inappropriate type for this map (optional)NullPointerException
- if the specified key is null and this map does not permit null keys (optional)
-
putAll
public void putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of callingput(k, v)
on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.- Specified by:
putAll
in interfaceMap<K extends Serializable,V extends Serializable>
- Parameters:
m
- mappings to be stored in this map- Throws:
UnsupportedOperationException
- if the putAll operation is not supported by this mapClassCastException
- if the class of a key or value in the specified map prevents it from being stored in this mapNullPointerException
- if the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys or valuesIllegalArgumentException
- if some property of a key or value in the specified map prevents it from being stored in this map
-
clear
public void clear()
Removes all of the mappings from this map (optional operation). The map will be empty after this call returns.- Specified by:
clear
in interfaceMap<K extends Serializable,V extends Serializable>
- Throws:
UnsupportedOperationException
- if the clear operation is not supported by this map
-
keySet
public Set<K> keySet()
IMPORTANT: Unlike the standardMap.keySet()
implementation, the set returned from this method cannot be modified.- Specified by:
keySet
in interfaceCache<K extends Serializable,V extends Serializable>
- Specified by:
keySet
in interfaceMap<K extends Serializable,V extends Serializable>
- Returns:
- an unmodifiable set view of the keys contained in this map
-
addClusteredCacheEntryListener
public String addClusteredCacheEntryListener(@Nonnull ClusteredCacheEntryListener<K,V> listener, boolean includeValues, boolean includeEventsFromLocalNode)
Description copied from interface:Cache
Registers a listener to receive entry events for this cache. To optimize the amount of data that is being processed, this method allows the listener to be registered in a way that suppresses values from being sent to it. In that case, only keys will be included in the event listener invocation.- Specified by:
addClusteredCacheEntryListener
in interfaceCache<K extends Serializable,V extends Serializable>
- Parameters:
listener
- the listener to be registered.includeValues
- defines if the listener should receive the values associated with the events.includeEventsFromLocalNode
- defines if the listener should be invoked for events that originate on the local node.- Returns:
- a unique identifier for the listener which is used as a key to remove the listener
-
removeClusteredCacheEntryListener
public void removeClusteredCacheEntryListener(@Nonnull String listenerId)
Description copied from interface:Cache
Removes a previously registered event listener.- Specified by:
removeClusteredCacheEntryListener
in interfaceCache<K extends Serializable,V extends Serializable>
- Parameters:
listenerId
- the identifier of the listener to be removed.
-
-