K - the key typeV - the value type@PublicApi
public interface Cache<K,V>
Note: null keys and values are NOT supported. Although using null values may work for some
implementations, it won't work for all implementations and the behaviour may change over time.
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(CacheEntryListener<K,V> listener,
boolean includeValues)
Deprecated.
since 2.9 may not be supported in all implementations and will be removed after June 2016
|
boolean |
containsKey(K key)
Returns whether an entry exists in the cache under the specified key.
|
V |
get(K key)
Retrieve an object from this cache.
|
V |
get(K key,
Supplier<? extends V> valueSupplier)
Retrieve an object from this cache.
|
default Map<K,V> |
getBulk(Set<K> keys,
Function<Set<K>,Map<K,V>> valuesSupplier)
Retrieve multiple entries from the cache.
|
Collection<K> |
getKeys()
Gets the keys of all objects currently stored in the cache.
|
String |
getName()
The name of the cache, uniquely identifies this cache.
|
void |
put(K key,
V value)
Put an object into the cache.
|
V |
putIfAbsent(K key,
V value)
Atomically associates the specified key with the given value if it is not already associated with a value.
|
void |
remove(K key)
Remove the object identified by the key from the cache.
|
boolean |
remove(K key,
V value)
Atomically removes the entry for a key only if currently mapped to a given value.
|
void |
removeAll()
Remove all of the objects from this cache.
|
void |
removeListener(CacheEntryListener<K,V> listener)
Deprecated.
since 2.9 may not be supported in all implementations and will be removed after June 2016
|
boolean |
replace(K key,
V oldValue,
V newValue)
Atomically replaces the entry for a key only if currently mapped to a given value.
|
@Nonnull String getName()
boolean containsKey(@Nonnull K key)
Note that:
CacheLoader,
it will not be called. Obviously, any call to get(Object)
will call the corresponding CacheLoader (if required).CacheSettings.getReplicateViaCopy()
set to false and CacheSettings.getLocal() set to false,
then only the local copy of the cache is checked. A local cache on
another node may contain an entry under the specified key.key - the key for the entry to check for containment@Nonnull Collection<K> getKeys()
Objects keys@Nullable V get(@Nonnull K key)
key - the key uniquely identifying the object to be retrievednull if the object is not found@Nonnull V get(@Nonnull K key, @Nonnull Supplier<? extends V> valueSupplier)
If no value is present in the cache, the valueSupplier will be used to populate the entry and be counted
as a cache miss.
key - the key uniquely identifying the object to be retrievedvalueSupplier - the supplier to call if no value is stored in the cache. the value supplied by the supplier
cannot be null@Nonnull default Map<K,V> getBulk(@Nonnull Set<K> keys, @Nonnull Function<Set<K>,Map<K,V>> valuesSupplier)
get(Object) and put(Object, Object).
This should be fine for the majority of cache implementations, and will still benefit from the bulk value loader.
Individual cache implementations, however, may wish to override this method and provide an optimised
implementation.keys - the set of keys for the entries to be fetched from the cachevaluesSupplier - a function that takes the set of keys that were not present in the cache, and returns a map of keys to new valuesvoid put(@Nonnull K key, @Nonnull V value)
NOTE: This method should not be used on hybrid
caches that maintain consistency only by invalidating the key on other cluster nodes. Instead, such hybrid
caches should use CacheLoader semantics and supply a CacheLoader when getting the Cache.
key - the key uniquely identifying the object to be added into the cachevalue - the non-null value to be cached@Nullable V putIfAbsent(@Nonnull K key, @Nonnull V value)
NOTE: This method should not be used on hybrid
caches that maintain consistency only by invalidating the key on other cluster nodes. Instead, such hybrid
caches should use CacheLoader semantics and supply a CacheLoader when getting the Cache.
key - the key with which the specified value is associatedvalue - the non-null value to be cachednull if there was no mapping for the keyvoid remove(@Nonnull K key)
key - the key that uniquely identifies the object to be removedboolean remove(@Nonnull K key, @Nonnull V value)
NOTE: Some cache backends (e.g. Ehcache) require this operation to be performed by CAS and will throw an exception when the cache is replicated
key - the key with which the specified value is associatedvalue - the value expected to be associated with the specified keytrue if the value was removed, false otherwisevoid removeAll()
boolean replace(@Nonnull K key, @Nonnull V oldValue, @Nonnull V newValue)
NOTE: Some cache backends (e.g. Ehcache) require this operation to be performed by CAS and will throw an exception when the cache is replicated
key - the key with which the specified value is associatedoldValue - the value expected to be associated with the specified keynewValue - the value to be associated with the specified keytrue if the value was replaced, false otherwise@Deprecated void addListener(@Nonnull CacheEntryListener<K,V> listener, boolean includeValues)
CacheEntryListenerlistener - the listenerincludeValues - if the events sent to this listener will include old/new value. This is can be used
in cases when the cost of finding these values is big (network sync) but the listener is not interested in
the concrete values for events its getting. The support for this parameter is optional and implementation dependantUnsupportedOperationException - if not supported@Deprecated void removeListener(@Nonnull CacheEntryListener<K,V> listener)
CacheEntryListenerlistener - the listenerUnsupportedOperationException - if not supportedCopyright © 2023 Atlassian. All rights reserved.