K
- the type of keyV
- the type of valuepublic interface Cache<K,V> extends Iterable<Cache.Entry<K,V>>, Closeable
Cache
is a Map-like data structure that provides temporary storage
of application data.
Like Map
s, Cache
s
Cache.Entry
Iterable
Map
s, Cache
s
null
will result in a NullPointerException
CacheLoader
(read-through-caching)
when a value being requested is not in a cacheCacheWriter
(write-through-caching)
when a value being created/updated/removed from a cache
String cacheName = "sampleCache";
CachingProvider provider = Caching.getCachingProvider();
CacheManager manager = provider.getCacheManager();
Cache<Integer, Date> cache = manager.getCache(cacheName, Integer.class,
Date.class);
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).
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the contents of the cache, without notifying listeners or
CacheWriter s. |
void |
close()
Closing a
Cache signals to the CacheManager that produced or
owns the said Cache that it should no longer be managed. |
boolean |
containsKey(K key)
Determines if the
Cache contains an entry for the specified key. |
void |
deregisterCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
Deregisters a listener, using as its unique identifier the
CacheEntryListenerConfiguration which was used to register it. |
V |
get(K key)
Gets an entry from the cache.
|
Map<K,V> |
getAll(Set<? extends K> 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 some
value.
|
V |
getAndReplace(K key,
V value)
Atomically replaces the value for a given key if and only if there is a
value currently mapped by the key.
|
CacheManager |
getCacheManager()
Gets the
CacheManager that owns and manages the Cache . |
Configuration<K,V> |
getConfiguration()
Obtains an immutable representation of the
Configuration that
was used to configure the Cache . |
String |
getName()
Return the name of the cache.
|
<T> T |
invoke(K key,
EntryProcessor<K,V,T> entryProcessor,
Object... arguments)
Invokes an
EntryProcessor against the Cache.Entry specified by
the provided key. |
<T> Map<K,T> |
invokeAll(Set<? extends K> keys,
EntryProcessor<K,V,T> entryProcessor,
Object... arguments)
Invokes an
EntryProcessor against the set of Cache.Entry s
specified by the set of keys. |
boolean |
isClosed()
Determines whether this Cache instance has been closed.
|
Iterator<Cache.Entry<K,V>> |
iterator()
The ordering of iteration over entries is undefined.
|
void |
loadAll(Set<? extends K> keys,
boolean replaceExistingValues,
CompletionListener completionListener)
Asynchronously loads the specified entries into the cache using the
configured
CacheLoader for the given keys. |
void |
put(K key,
V value)
Associates the specified value with the specified key in the cache.
|
void |
putAll(Map<? extends K,? extends V> map)
Copies all of the entries from the specified map to the
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.
|
void |
registerCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
Registers a
CacheEntryListener . |
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.
|
<T> T |
unwrap(Class<T> clazz)
Provides a standard way to access the underlying concrete caching
implementation to provide access to further, proprietary features.
|
V get(K key)
CacheLoader
is
called
which will attempt to load the entry.key
- the key whose associated value is to be returnedIllegalStateException
- if the cache is isClosed()
NullPointerException
- if the key is nullCacheException
- if there is a problem fetching the valueClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key type is incompatible with that
which has been configured for the Cache
Map<K,V> getAll(Set<? extends K> keys)
Cache
, returning them as
Map
of the values associated with the set of keys requested.
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 set 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 isClosed()
CacheException
- if there is a problem fetching the valuesClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and any key type is incompatible with that
which has been configured for the
Cache
boolean containsKey(K key)
Cache
contains an entry for the specified key.
More formally, returns true if and only if this cache contains a
mapping for a key k such that key.equals(k).
(There can be at most one such mapping.)key
- key whose presence in this cache is to be tested.NullPointerException
- if key is nullIllegalStateException
- if the cache is isClosed()
CacheException
- it there is a problem checking the mappingClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key type is incompatible with that
which has been configured for the
Cache
Map.containsKey(Object)
void loadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener)
CacheLoader
for the given keys.
If an entry for a key already exists in the Cache, a value will be loaded
if and only if replaceExistingValues
is true. If no loader
is configured for the cache, no objects will be loaded. If a problem is
encountered during the retrieving or loading of the objects,
an exception is provided to the CompletionListener
. Once the
operation has completed, the specified CompletionListener is notified.
Implementations may choose to load multiple keys from the provided
Set
in parallel. Iteration however must not occur in parallel,
thus allow for non-thread-safe Set
s to be used.
The thread on which the completion listener is called is implementation
dependent. An implementation may also choose to serialize calls to
different CompletionListeners rather than use a thread per
CompletionListener.keys
- the keys to loadreplaceExistingValues
- when true existing values in the Cache will
be replaced by those loaded from a CacheLoadercompletionListener
- the CompletionListener (may be null)NullPointerException
- if keys is null or if keys contains a null.IllegalStateException
- if the cache is isClosed()
CacheException
- thrown if there is a problem performing the
load. This may also be thrown on calling if
there are insufficient threads available to
perform the load.ClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and any key type is incompatible with that
which has been configured for the
Cache
void put(K key, V value)
Cache
previously contained a mapping for the key, the old
value is replaced by the specified value. (A cache c is said to
contain a mapping for a key k if and only if c.containsKey(k)
would return true.)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 isClosed()
CacheException
- if there is a problem doing the putClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key or value types are incompatible
with those which have been configured for the
Cache
Map.put(Object, Object)
,
getAndPut(Object, Object)
,
getAndReplace(Object, Object)
,
CacheWriter.write(javax.cache.Cache.Entry<? extends K, ? extends V>)
V getAndPut(K key, V value)
c.containsKey(k)
would return
true.)
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 isClosed()
CacheException
- if there is a problem doing the putClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key or value types are incompatible
with those which have been configured for the
Cache
put(Object, Object)
,
getAndReplace(Object, Object)
,
CacheWriter.write(javax.cache.Cache.Entry<? extends K, ? extends V>)
void putAll(Map<? extends K,? extends V> map)
Cache
.
The effect of this call is equivalent to that of calling
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 occur is undefined.
The behavior of this operation is undefined if entries in the cache
corresponding to entries in the map are modified or removed while this
operation is in progress. or if map is modified while the operation is in
progress.
In Default Consistency mode, individual puts occur atomically but not
the entire putAll. Listeners may observe individual updates.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 isClosed()
CacheException
- if there is a problem doing the put.ClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and any of the key or value types are
incompatible with those that have been
configured for the Cache
CacheWriter.writeAll(java.util.Collection<javax.cache.Cache.Entry<? extends K, ? extends V>>)
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.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 isClosed()
CacheException
- if there is a problem doing the putClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key or value types are incompatible
with those that have been configured for the
Cache
CacheWriter.write(javax.cache.Cache.Entry<? extends K, ? extends V>)
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 isClosed()
CacheException
- if there is a problem doing the removeClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key type is incompatible with that
which has been configured for the
Cache
CacheWriter.delete(java.lang.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 isClosed()
CacheException
- if there is a problem doing the removeClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key or value types are incompatible
with those that have been configured for the
Cache
CacheWriter.delete(java.lang.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 isClosed()
CacheException
- if there is a problem during the removeClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key type is incompatible with that
which has been configured for the Cache
CacheWriter.delete(java.lang.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 isClosed()
CacheException
- if there is a problem during the replaceClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key or value types are incompatible
with those which have been configured for the
Cache
CacheWriter.write(javax.cache.Cache.Entry<? extends K, ? extends V>)
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.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 isClosed()
CacheException
- if there is a problem during the replaceClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key or value types are incompatible
with those that have been configured for the
Cache
getAndReplace(Object, Object)
,
CacheWriter.write(javax.cache.Cache.Entry<? extends K, ? extends V>)
V getAndReplace(K key, V value)
if (cache.containsKey(key)) {
V oldValue = cache.get(key);
cache.put(key, value);
return oldValue;
} 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 isClosed()
CacheException
- if there is a problem during the replaceClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key or value types are incompatible
with those that have been configured for the
Cache
ConcurrentMap.replace(Object, Object)
,
CacheWriter.write(javax.cache.Cache.Entry<? extends K, ? extends V>)
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 isClosed()
CacheException
- if there is a problem during the removeClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and any key type is incompatible with that
which has been configured for the Cache
CacheWriter.deleteAll(java.util.Collection<?>)
void removeAll()
clear()
to avoid this.IllegalStateException
- if the cache is isClosed()
CacheException
- if there is a problem during the removeclear()
,
CacheWriter.deleteAll(java.util.Collection<?>)
void clear()
CacheWriter
s.IllegalStateException
- if the cache is isClosed()
CacheException
- if there is a problem during the removeConfiguration<K,V> getConfiguration()
Configuration
that
was used to configure the Cache
.Configuration
<T> T invoke(K key, EntryProcessor<K,V,T> entryProcessor, Object... arguments) throws EntryProcessorException
EntryProcessor
against the Cache.Entry
specified by
the provided key. If an Cache.Entry
does not exist for the specified key,
an attempt is made to load it (if a loader is configured) or a surrogate
Cache.Entry
, consisting of the key with a null value is used instead.
key
- the key to the entryentryProcessor
- the EntryProcessor
to invokearguments
- additional arguments to pass to the
EntryProcessor
EntryProcessor
implementationNullPointerException
- if key or EntryProcessor
is nullIllegalStateException
- if the cache is isClosed()
ClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key or value types are incompatible
with those that have been configured for the
Cache
EntryProcessorException
- if an exception is thrown by the EntryProcessor
, a Caching Implementation
must wrap any Exception
thrown
wrapped
in an EntryProcessorException
.EntryProcessor
<T> Map<K,T> invokeAll(Set<? extends K> keys, EntryProcessor<K,V,T> entryProcessor, Object... arguments)
EntryProcessor
against the set of Cache.Entry
s
specified by the set of keys. If an Cache.Entry
does not exist for the
specified key, an attempt is made to load it (if a loader is configured) or a
surrogate Cache.Entry
, consisting of the key with a null value is used
instead.
The order in which the entries for the keys are processed is undefined.
Implementations may choose to process the entries in any order, including
concurrently. Furthermore there is no guarantee implementations will
use the same EntryProcessor
instance to process each entry, as
the case may be in a non-local cache topology.keys
- the set of keys for entries to processentryProcessor
- the EntryProcessor
to invokearguments
- additional arguments to pass to the
EntryProcessor
EntryProcessor
implementation. No mappings will be
returned for EntryProcessor
s that return a null
value for a keyNullPointerException
- if keys or EntryProcessor
are nullIllegalStateException
- if the cache is isClosed()
CacheException
- if an exception occurred while executing
the EntryProcessor
(the causing
exception will be wrapped by the
CacheException)ClassCastException
- if the implementation supports and is
configured to perform runtime-type-checking,
and the key or value types are incompatible
with those that have been configured for the
Cache
EntryProcessorException
- if an exception is thrown by the EntryProcessor
, a Caching Implementation
must wrap any Exception
thrown wrapped
in an EntryProcessorException
.EntryProcessor
String getName()
CacheManager getCacheManager()
CacheManager
that owns and manages the Cache
.null
if the Cache
is not
managedvoid close()
Cache
signals to the CacheManager
that produced or
owns the said Cache
that it should no longer be managed. At this
point in time the CacheManager
:
CacheManager
. This includes calling the close
method on configured CacheLoader
,
CacheWriter
, registered CacheEntryListener
s and
ExpiryPolicy
instances that implement the java.io.Closeable
interface.
CacheEntryListener
s
registered on the Cache
IllegalStateException
.close
in interface AutoCloseable
close
in interface Closeable
boolean isClosed()
close()
method has been calledgetCacheManager()
has been closed, orgetCacheManager()
<T> T unwrap(Class<T> clazz)
IllegalArgumentException
is thrown.clazz
- the proprietary class or interface of the underlying concrete
cache. It is this type which is returned.IllegalArgumentException
- if the caching provider doesn't support
the specified class.void registerCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
CacheEntryListener
. The supplied
CacheEntryListenerConfiguration
is used to instantiate a listener
and apply it to those events specified in the configuration.cacheEntryListenerConfiguration
- a factory and related configuration
for creating the listenerIllegalArgumentException
- is the same CacheEntryListenerConfiguration
is used more than onceCacheEntryListener
void deregisterCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
CacheEntryListenerConfiguration
which was used to register it.
Both listeners registered at configuration time,
and those created at runtime with registerCacheEntryListener(javax.cache.configuration.CacheEntryListenerConfiguration<K, V>)
can
be deregistered.cacheEntryListenerConfiguration
- the factory and related configuration
that was used to create the
listenerIterator<Cache.Entry<K,V>> iterator()
Iterator.next()
may return null if the entry is no
longer present, has expired or has been evicted.iterator
in interface Iterable<Cache.Entry<K,V>>
Copyright © 2013. All Rights Reserved.