K
- Cache key type.V
- Cache value type.public class CaffeineCache<K extends Serializable,V extends Serializable> extends Object implements Cache<K,V>
Cache
. This implementation intends to make Caffeine caches compatible
with utility functionality that is based on Cache
.
Caffeine is a highly configurable implementation. To be able to achieve
compatibility with Cache
, a specific configuration is required:
secretKey, secretValue
Modifier and Type | Method and Description |
---|---|
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 standard
Map.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,
or
null 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.
|
int |
getCacheSize()
Returns the size of the cache contents in bytes limited to integer size.
|
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 standard
Map.keySet() implementation, the set returned from
this method cannot be modified. |
static <K extends Serializable,V extends Serializable> |
of(com.github.benmanes.caffeine.cache.Cache<K,V> cache,
String name)
Wraps a Caffeine cache in an implementation that's Compatible with
Cache . |
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 |
setMaxCacheSize(int maxSize)
Sets the maximum size of the cache in bytes limited to integer size.
|
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 cache
|
int |
size()
Returns the number of key-value mappings in this map.
|
Collection<V> |
values()
IMPORTANT: Unlike the standard
Map.values() implementation, the collection returned from
this method cannot be modified. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getLock, isKeySecret, isValueSecret, setSecretKey, setSecretValue
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
public static <K extends Serializable,V extends Serializable> CaffeineCache of(com.github.benmanes.caffeine.cache.Cache<K,V> cache, String name)
Cache
.K
- Cache key type.V
- Cache value type.cache
- The cache to be wrapped (cannot be null).name
- A human-readable name for the cache that's wrapped.IllegalArgumentException
- when the cache configuration does not conform to the contract defined in the javadoc of this class.public String getName()
getName
in interface Cache<K extends Serializable,V extends Serializable>
public void setName(String name)
setName
in interface Cache<K extends Serializable,V extends Serializable>
name
- the name of the cachepublic long getMaxCacheSize()
getMaxCacheSize
in interface Cache<K extends Serializable,V extends Serializable>
public void setMaxCacheSize(int maxSize)
Note: If using the Hazelcast clustering plugin, this will not take effect until the next time the cache is created
setMaxCacheSize
in interface Cache<K extends Serializable,V extends Serializable>
maxSize
- the maximum size of the cache in bytes.public void setMaxCacheSize(long maxSize)
Note: If using the Hazelcast clustering plugin, this will not take effect until the next time the cache is created
setMaxCacheSize
in interface Cache<K extends Serializable,V extends Serializable>
maxSize
- the maximum size of the cache in bytes.public long getMaxLifetime()
getMaxLifetime
in interface Cache<K extends Serializable,V extends Serializable>
public void setMaxLifetime(long maxLifetime)
Note: If using the Hazelcast clustering plugin, this will not take effect until the next time the cache is created
setMaxLifetime
in interface Cache<K extends Serializable,V extends Serializable>
maxLifetime
- the maximum number of milliseconds before objects are expired.public int getCacheSize()
getCacheSize
in interface Cache<K extends Serializable,V extends Serializable>
public long getLongCacheSize()
getLongCacheSize
in interface Cache<K extends Serializable,V extends Serializable>
public long getCacheHits()
Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
getCacheHits
in interface Cache<K extends Serializable,V extends Serializable>
public long getCacheMisses()
Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
getCacheMisses
in interface Cache<K extends Serializable,V extends Serializable>
public Collection<V> values()
Map.values()
implementation, the collection returned from
this method cannot be modified.values
in interface Map<K extends Serializable,V extends Serializable>
values
in interface Cache<K extends Serializable,V extends Serializable>
public Set<Map.Entry<K,V>> entrySet()
Map.entrySet()
implementation, the set returned from
this method cannot be modified.entrySet
in interface Map<K extends Serializable,V extends Serializable>
entrySet
in interface Cache<K extends Serializable,V extends Serializable>
public int size()
size
in interface Map<K extends Serializable,V extends Serializable>
public boolean isEmpty()
isEmpty
in interface Map<K extends Serializable,V extends Serializable>
public boolean containsKey(Object key)
containsKey
in interface Map<K extends Serializable,V extends Serializable>
key
- key whose presence in this map is to be testedClassCastException
- 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)public boolean containsValue(Object value)
containsValue
in interface Map<K extends Serializable,V extends Serializable>
value
- value whose presence in this map is to be testedClassCastException
- 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)public V get(Object key)
null
if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key
k
to a value v
such that (key==null ? k==null :
key.equals(k))
, then this method returns v
; otherwise
it returns null
. (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 to null
. The containsKey
operation may be used to distinguish these two cases.
get
in interface Map<K extends Serializable,V extends Serializable>
key
- the key whose associated value is to be returnednull
if this map contains no mapping for the keyClassCastException
- 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)public V put(K key, V value)
m.containsKey(k)
would return
true.)put
in interface Map<K extends Serializable,V extends Serializable>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyUnsupportedOperationException
- 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 mappublic V remove(Object key)
(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.
remove
in interface Map<K extends Serializable,V extends Serializable>
key
- key whose mapping is to be removed from the mapUnsupportedOperationException
- 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)public void putAll(Map<? extends K,? extends V> m)
put(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.putAll
in interface Map<K extends Serializable,V extends Serializable>
m
- mappings to be stored in this mapUnsupportedOperationException
- 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 mappublic void clear()
clear
in interface Map<K extends Serializable,V extends Serializable>
UnsupportedOperationException
- if the clear operation
is not supported by this mappublic Set<K> keySet()
Map.keySet()
implementation, the set returned from
this method cannot be modified.keySet
in interface Map<K extends Serializable,V extends Serializable>
keySet
in interface Cache<K extends Serializable,V extends Serializable>
Copyright © 2003–2020 Ignite Realtime. All rights reserved.