K
- the key type determining the type of the lookup values in the cacheV
- the value type determining the type of values stored in the cachepublic interface Cache<K,V>
In contrast to a simple Map, the cache has a maximum size. If the cache is full, old (least recently used) entries are evicted to make room for newer ones. Also this cache will be automatically evicted from time to time so that only values which are really used remain in the cache. Therefore system resources (most essentially heap storage) is released if no longer required.
A new Cache is created by invoking CacheManager.createCache(String)
. The maximal size as well as the
time to live value for each entry is set via the cache.[cacheName] extension. Additionally
CacheManager.createCache(String, ValueComputer, ValueVerifier)
can be used to supply a
ValueComputer as well as a ValueVerifier. Those classes are responsible for creating non-
existent cache values or to verify that cached values are still up to date, before they are returned to a
user of the cache.
CacheManager
,
ValueComputer
,
ValueVerifier
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the complete cache
|
boolean |
contains(K key)
Checks if there is a cached entry for the given key
|
V |
get(K key)
Returns the value associated with the given key.
|
V |
get(K key,
ValueComputer<K,V> computer)
Returns the value associated with the given key.
|
List<CacheEntry<K,V>> |
getContents()
Provides access to the contents of this cache
|
Long |
getHitRate()
Returns the cache hit-rate (in percent)
|
List<Long> |
getHitRateHistory()
Returns the statistical values of "hit rate" for the last some eviction
intervals.
|
Date |
getLastEvictionRun()
Returns the date of the last eviction run.
|
int |
getMaxSize()
Returns the max size of the cache
|
String |
getName()
Returns the name of the cache, which is also used to load the configuration.
|
int |
getSize()
Returns the number of entries in the cache
|
List<Long> |
getUseHistory()
Returns the statistical values of "uses" for the last some eviction
intervals.
|
long |
getUses()
Returns the number of reads since the last eviction
|
Iterator<K> |
keySet()
Provides access to the keys stored in this cache
|
Cache<K,V> |
onRemove(Callback<Tuple<K,V>> onRemoveCallback)
Sets the remove callback which is invoked once a value is removed from the cache.
|
void |
put(K key,
V value)
Stores the given key value mapping in the cache
|
void |
remove(K key)
Removes the given item from the cache
|
void |
removeIf(Predicate<CacheEntry<K,V>> predicate)
Removes all cached values for which the predicate returns true.
|
String getName()
int getMaxSize()
int getSize()
long getUses()
List<Long> getUseHistory()
Long getHitRate()
List<Long> getHitRateHistory()
Date getLastEvictionRun()
void clear()
@Nullable V get(@Nonnull K key)
key
- the key used to retrieve the value in the cache@Nullable V get(@Nonnull K key, @Nullable ValueComputer<K,V> computer)
ValueComputer
is invoked.key
- the key used to retrieve the value in the cachecomputer
- the computer used to generate a value if absent in the cachevoid put(@Nonnull K key, @Nullable V value)
key
- the key used to store this entryvalue
- the value to be stored in the entryvoid remove(@Nonnull K key)
key
- the key which should be removed from the cachevoid removeIf(@Nonnull Predicate<CacheEntry<K,V>> predicate)
predicate
- the predicate used to determine if a value should be removed from the cache.boolean contains(@Nonnull K key)
key
- contains the key to check in the cacheIterator<K> keySet()
List<CacheEntry<K,V>> getContents()
Cache<K,V> onRemove(Callback<Tuple<K,V>> onRemoveCallback)
Only one handler can be set at a time.
onRemoveCallback
- the callback to call when an element is removed from the cache. Can be null
null to remove the last handler.Copyright © 2018. All rights reserved.