Interface Cache<K,V>
- Type Parameters:
K- the key type for the cacheV- the value type for the cache
- All Superinterfaces:
Iterable<Cache.Entry<K,V>>
- All Known Subinterfaces:
PersistentUserManagedCache<K,,V> UserManagedCache<K,V>
In order to function, cache keys must respect the hash code and
equals contracts. This contract is what will be used to lookup values based on key.
A Cache is not a map, mostly because it has the following two concepts linked to mappings:
- Eviction: A
Cachehas a capacity constraint and in order to honor it, aCachecan evict (remove) a mapping at any point in time. Note that eviction may occur before maximum capacity is reached. - Expiry: Data in a
Cachecan be configured to expire after some time. There is no way for aCacheuser to differentiate from the API between a mapping being absent or expired.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all mappings currently present in theCache.booleancontainsKey(K key) Checks whether a mapping for the given key is present, without retrieving the associated value.Retrieves the value currently mapped to the provided key.Retrieves all values associated with the given key set.Exposes theCacheRuntimeConfigurationassociated with this Cache instance.iterator()Returns an iterator over the cache entries.voidAssociates the given value to the given key in thisCache.voidAssociates all the provided key:value pairs.putIfAbsent(K key, V value) Maps the specified key to the specified value in this cache, unless a non-expired mapping already exists.voidRemoves the value, if any, associated with the provided key.booleanRemoves the entry for a key only if currently mapped to the given value and the entry is not expired.voidRemoves any associated value for the given key set.Replaces the entry for a key only if currently mapped to some value and the entry is not expired.booleanReplaces the entry for a key only if currently mapped to the given value and the entry is not expired.Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
get
Retrieves the value currently mapped to the provided key.- Parameters:
key- the key, may not benull- Returns:
- the value mapped to the key,
nullif none - Throws:
NullPointerException- if the provided key isnullCacheLoadingException- if theCacheLoaderWriterassociated with this cache was invoked and threw anException
-
put
Associates the given value to the given key in thisCache.- Parameters:
key- the key, may not benullvalue- the value, may not benull- Throws:
NullPointerException- if either key or value isnullCacheWritingException- if theCacheLoaderWriterassociated with this cache threw anExceptionwhile writing the value for the given key to the underlying system of record.
-
containsKey
Checks whether a mapping for the given key is present, without retrieving the associated value.- Parameters:
key- the key, may not benull- Returns:
trueif a mapping is present,falseotherwise- Throws:
NullPointerException- if the provided key isnull
-
remove
Removes the value, if any, associated with the provided key.- Parameters:
key- the key to remove the value for, may not benull- Throws:
NullPointerException- if the provided key isnullCacheWritingException- if theCacheLoaderWriterassociated with this cache threw anExceptionwhile removing the value for the given key from the underlying system of record.
-
getAll
Retrieves all values associated with the given key set.- Parameters:
keys- keys to query for, may not containnull- Returns:
- a map from keys to values or
nullif the key was not mapped - Throws:
NullPointerException- if theSetor any of the contained keys arenull.BulkCacheLoadingException- if loading some or all values failed
-
putAll
Associates all the provided key:value pairs.- Parameters:
entries- key:value pairs to associate, keys or values may not benull- Throws:
NullPointerException- if theMapor any of the contained keys or values arenull.BulkCacheWritingException- if theCacheLoaderWriterassociated with this cache threw anExceptionwhile writing given key:value pairs to the underlying system of record.
-
removeAll
Removes any associated value for the given key set.- Parameters:
keys- keys to remove values for, may not benull- Throws:
NullPointerException- if theSetor any of the contained keys arenull.BulkCacheWritingException- if theCacheLoaderWriterassociated with this cache threw anExceptionwhile removing mappings for given keys from the underlying system of record.
-
clear
void clear()Removes all mappings currently present in theCache.It does so without invoking the
CacheLoaderWriteror any registeredCacheEventListenerinstances. This is not an atomic operation and can potentially be very expensive. -
putIfAbsent
Maps the specified key to the specified value in this cache, unless a non-expired mapping already exists.This is equivalent to
if (!cache.containsKey(key)) cache.put(key, value); return null; else return cache.get(key);except that the action is performed atomically.The value can be retrieved by calling the
getmethod with a key that is equal to the original key.Neither the key nor the value can be
null.- Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key- Returns:
- the value to which the specified key was previously mapped,
or
nullif no such mapping existed or the mapping was expired - Throws:
NullPointerException- if any of the arguments isnullCacheWritingException- if theCacheLoaderWriterassociated with this cache threw anExceptionwhile writing the value for the given key to the underlying system of record.CacheLoadingException- if theCacheLoaderWriterassociated with this cache was invoked and threw anException
-
remove
Removes the entry for a key only if currently mapped to the given value and the entry is not expired.This is equivalent to
if (cache.containsKey(key) && cache.get(key).equals(value)) { cache.remove(key); return true; } else return false;except that the action is performed atomically.The key cannot be
null.- Parameters:
key- key with which the specified value is associatedvalue- value expected to be removed- Returns:
- true if the value was successfully removed
- Throws:
NullPointerException- if any of the arguments isnullCacheWritingException- if theCacheLoaderWriterassociated with this cache threw anExceptionwhile removing the value for the given key from the underlying system of record.
-
replace
Replaces the entry for a key only if currently mapped to some value and the entry is not expired.This is equivalent to
V oldValue = cache.get(key); if (oldValue != null) { cache.put(key, value); } return oldValue;except that the action is performed atomically.Neither the key nor the value can be
null.- Parameters:
key- of the value to be replacedvalue- the new value- Returns:
- the existing value that was associated with the key, or
nullif no such mapping existed or the mapping was expired - Throws:
NullPointerException- if any of the arguments isnullCacheWritingException- if theCacheLoaderWriterassociated with this cache threw anExceptionwhile writing the value for the given key to the underlying system of record.CacheLoadingException- if theCacheLoaderWriterassociated with this cache was invoked and threw anException
-
replace
Replaces the entry for a key only if currently mapped to the given value and the entry is not expired.This is equivalent to
if (cache.containsKey(key) && cache.get(key).equals(oldValue)) { cache.put(key, newValue); return true; } else return false;except that the action is performed atomically.Neither the key nor the value can be
null.- Parameters:
key- key with which the specified value is associatedoldValue- value expected to be associated with the specified keynewValue- value to be associated with the specified key- Returns:
- true if the oldValue was successfully replaced by the newValue
- Throws:
NullPointerException- if any of the arguments isnullCacheWritingException- if theCacheLoaderWriterassociated with this cache threw anExceptionwhile writing the value for the given key to the underlying system of record.CacheLoadingException- if theCacheLoaderWriterassociated with this cache was invoked and threw anException
-
getRuntimeConfiguration
CacheRuntimeConfiguration<K,V> getRuntimeConfiguration()Exposes theCacheRuntimeConfigurationassociated with this Cache instance.- Returns:
- the configuration currently in use
-
iterator
Iterator<Cache.Entry<K,V>> iterator()Returns an iterator over the cache entries.Due to the interactions of the cache and iterator contracts it is possible for iteration to return expired entries.
-