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 voidclear()Removes all of the mappings from this map (optional operation).booleancontainsKey(Object key)Returns true if this map contains a mapping for the specified key.booleancontainsValue(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.Vget(Object key)Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.longgetCacheHits()Returns the number of cache hits.longgetCacheMisses()Returns the number of cache misses.intgetCacheSize()Returns the size of the cache contents in bytes limited to integer size.longgetLongCacheSize()Returns the size of the cache contents in bytes.longgetMaxCacheSize()Returns the maximum size of the cache in bytes.longgetMaxLifetime()Returns the maximum number of milliseconds that any object can live in cache.StringgetName()Returns the name of the cache.booleanisEmpty()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.Vput(K key, V value)Associates the specified value with the specified key in this map (optional operation).voidputAll(Map<? extends K,? extends V> m)Copies all of the mappings from the specified map to this map (optional operation).Vremove(Object key)Removes the mapping for a key from this map if it is present (optional operation).voidsetMaxCacheSize(int maxSize)Sets the maximum size of the cache in bytes limited to integer size.voidsetMaxCacheSize(long maxSize)Sets the maximum size of the cache in bytes.voidsetMaxLifetime(long maxLifetime)Sets the maximum number of milliseconds that any object can live in cache.voidsetName(String name)Sets the name of the cacheintsize()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
getLock, 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.
-
getName
public String getName()
Returns the name of the cache.- Specified by:
getNamein 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:
setNamein 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:
getMaxCacheSizein interfaceCache<K extends Serializable,V extends Serializable>- Returns:
- the maximum size of the cache in bytes.
-
setMaxCacheSize
public void setMaxCacheSize(int maxSize)
Sets the maximum size of the cache in bytes limited to integer size. 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:
setMaxCacheSizein interfaceCache<K extends Serializable,V extends Serializable>- Parameters:
maxSize- 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:
setMaxCacheSizein 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:
getMaxLifetimein 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:
setMaxLifetimein interfaceCache<K extends Serializable,V extends Serializable>- Parameters:
maxLifetime- the maximum number of milliseconds before objects are expired.
-
getCacheSize
public int getCacheSize()
Returns the size of the cache contents in bytes limited to integer size. 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:
getCacheSizein interfaceCache<K extends Serializable,V extends Serializable>- Returns:
- the size of the cache contents in bytes.
-
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:
getLongCacheSizein 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:
getCacheHitsin 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:
getCacheMissesin 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:
valuesin interfaceCache<K extends Serializable,V extends Serializable>- Specified by:
valuesin 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:
entrySetin interfaceCache<K extends Serializable,V extends Serializable>- Specified by:
entrySetin 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:
sizein 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:
isEmptyin 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:
containsKeyin 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:
containsValuein 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, ornullif this map contains no mapping for the key.More formally, if this map contains a mapping from a key
kto a valuevsuch 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
nulldoes not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key tonull. ThecontainsKeyoperation may be used to distinguish these two cases.- Specified by:
getin 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
nullif 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:
putin 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:
removein 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:
putAllin 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:
clearin 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:
keySetin interfaceCache<K extends Serializable,V extends Serializable>- Specified by:
keySetin interfaceMap<K extends Serializable,V extends Serializable>- Returns:
- an unmodifiable set view of the keys contained in this map
-
-