K
- the type of keys maintained by this cacheV
- the type of cached valuespublic interface Cache<K,V> extends Iterable<Cache.Entry<K,V>>, CacheLifecycle
ConcurrentMap
with some modifications for
fast distributed performance.
A Cache does not allow null keys or values. Attempts to store a null value or
to use a null key either in a get or put operation will result in a NullPointerException
.
Caches use generics throughout providing a level of type safety akin to the collections package.
Cache implements Iterable
for Cache.Entry
, providing support for simplified iteration.
However iteration should be used with caution. It is an O(n) operation and may be
slow on large or distributed caches.
The Cache API also provides:
String cacheName = "sampleCache"; CacheManager cacheManager = Caching.getCacheManager(); Cache<Integer, Date> cache = cacheManager.getCache(cacheName); if (cache == null) { cache = cacheManager.<Integer,Date>createCacheBuilder(cacheName).build(); } Date value1 = new Date(); Integer key = 1; cache.put(key, value1); Date value2 = cache.get(key);
Modifier and Type | Interface and Description |
---|---|
static interface |
Cache.Entry<K,V>
A cache entry (key-value pair).
|
static interface |
Cache.EntryProcessor<K,V,T>
Allows execution of code which may mutate a cache entry with exclusive
access (including reads) to that entry.
|
static interface |
Cache.MutableEntry<K,V>
An accessor and mutator to the underlying Cache
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the contents of the cache, without notifying listeners or
CacheWriter s. |
boolean |
containsKey(K key)
Returns true if this cache contains a mapping for the specified
key.
|
V |
get(K key)
Gets an entry from the cache.
|
Map<K,V> |
getAll(Set<? extends K> keys)
The getAll method will return, from the cache, a
Map of the objects
associated with the Collection of keys in argument "keys". |
V |
getAndPut(K key,
V value)
Associates the specified value with the specified key in this cache,
returning an existing value if one existed.
|
V |
getAndRemove(K key)
Atomically removes the entry for a key only if currently mapped to a given value.
|
V |
getAndReplace(K key,
V value)
Atomically replaces the entry for a key only if currently mapped to some value.
|
CacheManager |
getCacheManager()
Gets the CacheManager managing this cache.
|
Configuration<K,V> |
getConfiguration()
Returns a Configuration.
|
String |
getName()
Return the name of the cache.
|
<T> T |
invokeEntryProcessor(K key,
Cache.EntryProcessor<K,V,T> entryProcessor,
Object... arguments)
Passes the cache entry associated with the key to the entry
processor.
|
Iterator<Cache.Entry<K,V>> |
iterator()
The ordering of iteration over entries is undefined.
|
void |
loadAll(Iterable<? extends K> keys,
boolean replaceExistingValues,
CompletionListener listener)
The loadAll method provides a means to "pre-load" objects into the cache.
|
void |
put(K key,
V value)
Associates the specified value with the specified key in this cache
If the cache previously contained a mapping for
the key, the old value is replaced by the specified value.
|
void |
putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this cache.
|
boolean |
putIfAbsent(K key,
V value)
Atomically associates the specified key with the given value if it is
not already associated with a value.
|
boolean |
registerCacheEntryListener(CacheEntryListener<? super K,? super V> cacheEntryListener,
boolean requireOldValue,
CacheEntryEventFilter<? super K,? super V> cacheEntryFilter,
boolean synchronous)
Adds a listener to the notification service.
|
boolean |
remove(K key)
Removes the mapping for a key from this cache if it is present.
|
boolean |
remove(K key,
V oldValue)
Atomically removes the mapping for a key only if currently mapped to the given value.
|
void |
removeAll()
Removes all of the mappings from this cache.
|
void |
removeAll(Set<? extends K> keys)
Removes entries for the specified keys.
|
boolean |
replace(K key,
V value)
Atomically replaces the entry for a key only if currently mapped to some value.
|
boolean |
replace(K key,
V oldValue,
V newValue)
Atomically replaces the entry for a key only if currently mapped to a given value.
|
boolean |
unregisterCacheEntryListener(CacheEntryListener<?,?> cacheEntryListener)
Removes a call back listener.
|
<T> T |
unwrap(Class<T> cls)
Return an object of the specified type to allow access to the provider-specific API.
|
getStatus, start, stop
V get(K key)
CacheLoader
is called which will attempt
to load the entry.
CacheLoader
if enabled and key not present in cachekey
- the key whose associated value is to be returnedIllegalStateException
- if the cache is not Status.STARTED
NullPointerException
- if the key is nullCacheException
- if there is a problem fetching the valueMap.get(Object)
Map<K,V> getAll(Set<? extends K> keys)
Map
of the objects
associated with the Collection of keys in argument "keys".
If the cache is configured read-through, and a get would return null because an entry
is missing from the cache, the Cache's CacheLoader
is called which will attempt
to load the entry. This is done for each key in the collection for which this is the case.
If an entry cannot be loaded for a given key, the key will not be present in the returned Map.
keys
- The keys whose associated values are to be returned.NullPointerException
- if keys is null or if keys contains a nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem fetching the values.boolean containsKey(K key)
key
- key whose presence in this cache is to be tested.NullPointerException
- if key is nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- it there is a problem checking the mappingMap.containsKey(Object)
void loadAll(Iterable<? extends K> keys, boolean replaceExistingValues, CompletionListener listener)
keys
- the keys to loadreplaceExistingValues
- when true existing values in the Cache will
be replaced by those loaded from a CacheLoaderlistener
- the CompletionListener (may be null)NullPointerException
- if keys is null or if keys contains a null.IllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem doing the loadvoid put(K key, V value)
c.containsKey(k)
would return
true.)
In contrast to the corresponding Map operation, does not return
the previous value.key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyNullPointerException
- if key is null or if value is nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem doing the putMap.put(Object, Object)
,
getAndPut(Object, Object)
,
getAndReplace(Object, Object)
V getAndPut(K key, V value)
c.containsKey(k)
would return
true.)
The the previous value is returned, or null if there was no value associated
with the key previously.key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyNullPointerException
- if key is null or if value is nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem doing the putMap.put(Object, Object)
,
put(Object, Object)
,
getAndReplace(Object, Object)
void putAll(Map<? extends K,? extends V> map)
put(k, v)
on this cache once
for each mapping from key k to value v in the
specified map.
The order in which the individual puts will occur is undefined.
The behavior of this operation is undefined if the specified cache or map is modified while the
operation is in progress.map
- mappings to be stored in this cacheNullPointerException
- if map is null or if map contains null keys or values.IllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem doing the putMap.putAll(java.util.Map)
boolean putIfAbsent(K key, V value)
if (!cache.containsKey(key)) {} cache.put(key, value); return true; } else { return false; }except that the action is performed atomically. In contrast to the corresponding ConcurrentMap operation, does not return the previous value.
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyNullPointerException
- if key is null or value is nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem doing the putConcurrentMap.putIfAbsent(Object, Object)
boolean remove(K key)
(key==null ? k==null : key.equals(k))
, that mapping
is removed. (The cache can contain at most one such mapping.)
Returns true if this cache previously associated the key, or false if the cache contained no mapping for the key.
The cache will not contain a mapping for the specified key once the call returns.
key
- key whose mapping is to be removed from the cacheNullPointerException
- if key is nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem doing the putMap.remove(Object)
boolean remove(K key, V oldValue)
if (cache.containsKey(key) && cache.get(key).equals(oldValue)) { cache.remove(key); return true; } else { return false; }except that the action is performed atomically.
key
- key whose mapping is to be removed from the cacheoldValue
- value expected to be associated with the specified keyNullPointerException
- if key is nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem doing the putMap.remove(Object)
V getAndRemove(K key)
if (cache.containsKey(key)) { V oldValue = cache.get(key); cache.remove(key); return oldValue; } else { return null; }except that the action is performed atomically.
key
- key with which the specified value is associatedNullPointerException
- if the specified key or value is null.IllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem during the removeMap.remove(Object)
boolean replace(K key, V oldValue, V newValue)
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.
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 keyNullPointerException
- if key is null or if the values are nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem during the replaceConcurrentMap.replace(Object, Object, Object)
boolean replace(K key, V value)
if (cache.containsKey(key)) { cache.put(key, value); return true; } else { return false; }except that the action is performed atomically. In contrast to the corresponding ConcurrentMap operation, does not return the previous value.
key
- key with which the specified value is associatedvalue
- value to be associated with the specified keyNullPointerException
- if key is null or if value is nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem during the replacegetAndReplace(Object, Object)
,
ConcurrentMap.replace(Object, Object)
V getAndReplace(K key, V value)
if (cache.containsKey(key)) { V value = cache.get(key, value); cache.put(key, value); return value; } else { return null; }except that the action is performed atomically.
key
- key with which the specified value is associatedvalue
- value to be associated with the specified keyNullPointerException
- if key is null or if value is nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem during the replaceConcurrentMap.replace(Object, Object)
void removeAll(Set<? extends K> keys)
keys
- the keys to removeNullPointerException
- if keys is null or if it contains a null keyIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem during the removevoid removeAll()
IllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem during the removeclear()
void clear()
CacheWriter
s.IllegalStateException
- if the cache is not Status.STARTED
CacheException
- if there is a problem during the removeConfiguration<K,V> getConfiguration()
Status.STARTED
an implementation must respect the following:
If an implementation permits mutation of configuration to a running cache, those changes must be reflected
in the cache. In the case where mutation is not allowed InvalidConfigurationException
must be thrown on
an attempt to mutate the configuration.Configuration
of this cacheboolean registerCacheEntryListener(CacheEntryListener<? super K,? super V> cacheEntryListener, boolean requireOldValue, CacheEntryEventFilter<? super K,? super V> cacheEntryFilter, boolean synchronous)
cacheEntryListener
- The listener to add. Listeners fire synchronously in the execution path, and after the
causing event. if a listener throws an exception it will be wrapped in a CacheException
and propagated back to the caller. The listener may not be nullrequireOldValue
- whether the old value is supplied to CacheEntryEvent
.cacheEntryFilter
- If present the listener will only be called if the filter evaluates to true. If null the listener
is always called.synchronous
- whether the caller is blocked until the listener invocation completes.NullPointerException
- if the listener is null.boolean unregisterCacheEntryListener(CacheEntryListener<?,?> cacheEntryListener)
cacheEntryListener
- the listener to remove<T> T invokeEntryProcessor(K key, Cache.EntryProcessor<K,V,T> entryProcessor, Object... arguments)
key
- the key to the entryentryProcessor
- the processor which will process the entryarguments
- a number of arguments to the processNullPointerException
- if key or entryProcessor are nullIllegalStateException
- if the cache is not Status.STARTED
CacheException
- if an exception occurred while executing
the EntryProcessor (the causing exception
will be wrapped by the CacheException)Cache.EntryProcessor
String getName()
CacheManager getCacheManager()
<T> T unwrap(Class<T> cls)
IllegalArgumentException
is thrown.cls
- he class of the object to be returned. This is normally either the underlying implementation class or an interface that it implements.IllegalArgumentException
- if the provider doesn't support the specified class.Iterator<Cache.Entry<K,V>> iterator()
iterator
in interface Iterable<Cache.Entry<K,V>>
Copyright © 2013. All Rights Reserved.