public class AbstractCache<K,V> extends Object implements Cache<K,V>
UnsupportedOperationException
.Constructor and Description |
---|
AbstractCache() |
Modifier and Type | Method and Description |
---|---|
ConcurrentMap<K,V> |
asMap()
Returns a map interface for operating with this cache.
|
void |
clear()
Clear the cache in a fast way, causing minimal disruption.
|
void |
clearAndClose()
This is currently identical to
Cache.close() . |
void |
close()
Free all resources and remove the cache from the CacheManager.
|
V |
computeIfAbsent(K key,
Callable<V> callable)
If the specified key is not already associated with a value (or exception),
call the provided task and associate it with the returned value.
|
boolean |
containsAndRemove(K key)
Removes the mapping for a key from the cache and returns
true if it
one was present. |
boolean |
containsKey(K key)
Returns
true , if there is a mapping for the specified key. |
Iterable<CacheEntry<K,V>> |
entries()
Iterate all entries in the cache.
|
void |
expireAt(K key,
long millis)
Updates an existing not expired mapping to expire at the given point in time.
|
V |
get(K key)
Returns a value associated with the given key.
|
Map<K,V> |
getAll(Iterable<? extends K> keys)
Retrieve values from the cache associated with the provided keys.
|
CacheManager |
getCacheManager()
Return the cache manager for this cache instance.
|
CacheEntry<K,V> |
getEntry(K key)
Returns an entry that contains the cache value associated with the given key.
|
String |
getName()
A configured or generated name of this cache instance.
|
<R> R |
invoke(K key,
EntryProcessor<K,V,R> entryProcessor)
Invoke a user defined function on a cache entry.
|
<R> Map<K,EntryProcessingResult<R>> |
invokeAll(Iterable<? extends K> keys,
EntryProcessor<K,V,R> entryProcessor)
Invoke a user defined function on multiple cache entries specified by the
keys parameter. |
boolean |
isClosed()
Returns
true if cache was closed or closing is in progress. |
Iterable<K> |
keys()
Iterate all keys in the cache.
|
void |
loadAll(Iterable<? extends K> keys,
CacheOperationCompletionListener listener)
Asynchronously loads the given set of keys into the cache.
|
V |
peek(K key)
Returns the value associated to the given key.
|
Map<K,V> |
peekAll(Iterable<? extends K> keys)
Bulk version for
Cache.peek(Object) |
V |
peekAndPut(K key,
V value)
Updates an existing cache entry for the specified key, so it associates
the given value, or, insert a new cache entry for this key and value.
|
V |
peekAndRemove(K key)
Removes the mapping for a key from the cache if it is present.
|
V |
peekAndReplace(K key,
V value)
Replaces the entry for a key only if currently mapped to some value.
|
CacheEntry<K,V> |
peekEntry(K key)
Returns an entry that contains the cache value associated with the given key.
|
void |
prefetch(K key)
Notifies the cache about the intention to retrieve the value for this key in the
near future.
|
void |
prefetchAll(Iterable<? extends K> keys,
CacheOperationCompletionListener listener)
Notifies the cache about the intention to retrieve the value for this key in the
near future.
|
void |
put(K key,
V value)
Inserts a new value associated with the given key or updates an
existing association of the same key with the new value.
|
void |
putAll(Map<? extends K,? extends V> valueMap)
Insert all elements of the map into the cache.
|
boolean |
putIfAbsent(K key,
V value)
If the specified key is not already associated
with a value, associate it with the given value.
|
void |
reloadAll(Iterable<? extends K> keys,
CacheOperationCompletionListener listener)
Asynchronously loads the given set of keys into the cache.
|
void |
remove(K key)
Removes the mapping for a key from the cache if it is present.
|
void |
removeAll()
Removes all cache contents.
|
void |
removeAll(Iterable<? extends K> keys)
Removes a set of keys.
|
boolean |
removeIfEquals(K key,
V expectedValue)
Remove the mapping if the stored value is the equal to the comparison value.
|
boolean |
replace(K key,
V value)
Replaces the entry for a key only if currently mapped to some value.
|
boolean |
replaceIfEquals(K key,
V oldValue,
V newValue)
Replaces the entry for a key only if currently mapped to a given value.
|
<X> X |
requestInterface(Class<X> _type)
Request an alternative interface for this cache instance.
|
public String getName()
Cache
getName
in interface Cache<K,V>
Cache2kBuilder.name(String)
public V get(K key)
Cache
null
is returned.
If the CacheLoader
is invoked, subsequent requests of the same key will block
until the loading is completed. Details see CacheLoader
.
As an alternative Cache.peek(K)
can be used if the loader should
not be invoked.
get
in interface Cache<K,V>
get
in interface KeyValueSource<K,V>
key
- key with which the specified value is associatednull
if there was no mapping for the key.
(If nulls are permitted a null
can also indicate that the cache
previously associated null
with the key)Cache.get(Object)
public CacheEntry<K,V> getEntry(K key)
Cache
null
is returned.
If the loader is invoked, subsequent requests of the same key will block
until the loading is completed, details see CacheLoader
In case the cache loader yields an exception, the entry object will
be returned. The exception can be retrieved via CacheEntry.getException()
.
If null
values are present the method can be used to
check for an existent mapping and retrieve the value in one API call.
The alternative method Cache.peekEntry(K)
can be used if the loader
should not be invoked.
public void prefetch(K key)
Cache
The method will return immediately and the cache will load
the value asynchronously if not yet present in the cache. Prefetching
is done via a separate thread pool if specified via
Cache2kBuilder.prefetchExecutor(Executor)
.
If no CacheLoader
is defined the method will do nothing.
This method doesn't throw an exception in case the loader produced an exception. Exceptions will be propagated when the value is accessed.
prefetch
in interface AdvancedKeyValueSource<K,V>
prefetch
in interface Cache<K,V>
key
- the key that should be loaded, not null
Cache2kBuilder.loaderThreadCount(int)
,
Cache2kBuilder.prefetchExecutor(Executor)
public void prefetchAll(Iterable<? extends K> keys, CacheOperationCompletionListener listener)
Cache
The method will return immediately and the cache will load
the value asynchronously if not yet present in the cache. Prefetching
is done via a separate thread pool if specified via
Cache2kBuilder.prefetchExecutor(Executor)
.
If no CacheLoader
is defined the method will do nothing.
Exceptions from the loader will not be propagated via the listener. Exceptions will be propagated when the respective value is accessed.
prefetchAll
in interface AdvancedKeyValueSource<K,V>
prefetchAll
in interface Cache<K,V>
keys
- the keys which should be loaded, not null
listener
- Listener interface that is invoked upon completion. May be null
if no
completion notification is needed.Cache.prefetchAll(Iterable, CacheOperationCompletionListener)
public V peek(K key)
Cache
In contrast to Cache.get(Object)
this method solely operates
on the cache content and does not invoke the cache loader.
API rationale: Consequently all methods that do not invoke the loader
but return a value or a cache entry are prefixed with peek
within this interface
to make the different semantics immediately obvious by the name.
peek
in interface Cache<K,V>
key
- key with which the specified value is associatednull
if there was no mapping for the key.
(If nulls are permitted a null
can also indicate that the cache
previously associated null
with the key)public CacheEntry<K,V> peekEntry(K key)
Cache
null
is returned.
The cache loader will not be invoked by this method.
In case an exception is present, for example from a load operation carried out
previously, the entry object will be returned. The exception can be
retrieved via CacheEntry.getException()
.
If null
values are present the method can be used to
check for an existent mapping and retrieve the value in one API call.
public boolean containsKey(K key)
Cache
true
, if there is a mapping for the specified key.
Effect on statistics: The operation does increase the usage counter if a mapping is present, but does not count as read and therefore does not influence miss or hit values.
containsKey
in interface Cache<K,V>
key
- key which association should be checkedtrue
, if this cache contains a mapping for the specified
keypublic void put(K key, V value)
Cache
If an ExpiryPolicy
is specified in the
cache configuration it is called and will determine the expiry time.
If a CacheWriter
is registered, then it is called with the
new value. If the ExpiryPolicy
or CacheWriter
yield an exception the operation will be aborted and the previous
mapping will be preserved.
put
in interface Cache<K,V>
put
in interface KeyValueStore<K,V>
key
- key with which the specified value is associatedvalue
- value to be associated with the specified keyCache.put(Object, Object)
public V computeIfAbsent(K key, Callable<V> callable)
Cache
if (!cache.containsKey(key)) {
V value = callable.call();
cache.put(key, value);
return value;
} else {
return cache.peek(key);
}
except that the action is performed atomically.
See Cache.put(Object, Object)
for the effects on the cache writer and
expiry calculation.
Statistics: If an entry exists this operation counts as a hit, if the entry is missing, a miss and put is counted.
Exceptions: If call throws an exception the cache contents will not be modified and the exception is propagated. The customized exception propagator is not used for this method.
Rationale: The Function
interface that Map.computeIfAbsent
uses is only
available in Java 8. Callable
is a useful fallback and we can use it directly
for the Spring integration. A mismatch is that Callable.call()
declares a checked exception but
the cache access method do not.
computeIfAbsent
in interface Cache<K,V>
key
- key with which the specified value is to be associatedcallable
- task that computes the valuepublic boolean putIfAbsent(K key, V value)
Cache
if (!cache.containsKey(key)) {
cache.put(key, value);
return true;
} else {
return false;
}
except that the action is performed atomically.
See Cache.put(Object, Object)
for the effects on the cache writer and
expiry calculation.
Statistics: If an entry exists this operation counts as a hit, if the entry
is missing, a miss and put is counted. This definition is identical to the JSR107
statistics semantics. This is not consistent with other operations like
Cache.containsAndRemove(Object)
and Cache.containsKey(Object)
that don't update
the hit and miss counter if solely the existence of an entry is tested and not the
value itself is requested. This counting is subject to discussion and future change.
putIfAbsent
in interface Cache<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keytrue
, if no entry was present and the value was associated with the keypublic V peekAndReplace(K key, V value)
Cache
if (cache.containsKey(key)) {
cache.put(key, value);
return cache.peek(key);
} else
return null;
except that the action is performed atomically.
As with Cache.peek(Object)
, no request to the CacheLoader
is made,
if no entry is associated to the requested key.
peekAndReplace
in interface Cache<K,V>
key
- key with which the specified value is associatedvalue
- value to be associated with the specified keynull
if there was no mapping for the key.
(A null
return can also indicate that the cache
previously associated null
with the key)public boolean replace(K key, V value)
Cache
if (cache.containsKey(key)) {
cache.put(key, value);
return true
} else
return false;
except that the action is performed atomically.
Statistics: If an entry exists this operation counts as a hit, if the entry
is missing, a miss and put is counted. This definition is identical to the JSR107
statistics semantics. This is not consistent with other operations like
Cache.containsAndRemove(Object)
and Cache.containsKey(Object)
that don't update
the hit and miss counter if solely the existence of an entry is tested and not the
value itself is requested. This counting is subject to discussion and future change.
public boolean replaceIfEquals(K key, V oldValue, V newValue)
Cache
if (cache.containsKey(key) && Objects.equals(cache.get(key), oldValue)) {
cache.put(key, newValue);
return true;
} else
return false;
except that the action is performed atomically.replaceIfEquals
in interface Cache<K,V>
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 keytrue
if the value was replacedpublic V peekAndRemove(K key)
Cache
Returns the value to which the cache previously associated the key,
or null
if the cache contained no mapping for the key.
If the cache does permit null values, then a return value of
null
does not necessarily indicate that the cache
contained no mapping for the key. It is also possible that the cache
explicitly associated the key to the value null
.
This is equivalent to
V tmp = cache.peek(key);
cache.remove(key);
return tmp;
except that the action is performed atomically.
As with Cache.peek(Object)
, no request to the CacheLoader
is made,
if no entry is associated to the requested key.
peekAndRemove
in interface Cache<K,V>
key
- key whose mapping is to be removed from the cachepublic boolean containsAndRemove(K key)
Cache
true
if it
one was present.containsAndRemove
in interface Cache<K,V>
key
- key whose mapping is to be removed from the cachetrue
if the cache contained a mapping for the specified keypublic void remove(K key)
Cache
If a writer is registered CacheWriter.delete(Object)
will get called.
These alternative versions of the remove operation exist:
Cache.containsAndRemove(Object)
, returning a success flagCache.peekAndRemove(Object)
, returning the removed valueCache.removeIfEquals(Object, Object)
, conditional removal matching on the current valueSee KeyValueStore.remove(Object)
, for an explanation why no flag or object is returned.
remove
in interface Cache<K,V>
remove
in interface KeyValueStore<K,V>
key
- key which mapping is to be removed from the cache, not nullCache.remove(K)
public boolean removeIfEquals(K key, V expectedValue)
Cache
removeIfEquals
in interface Cache<K,V>
key
- key whose mapping is to be removed from the cacheexpectedValue
- value that must match with the existing value in the cachepublic void removeAll(Iterable<? extends K> keys)
Cache
removeAll
in interface Cache<K,V>
removeAll
in interface KeyValueStore<K,V>
keys
- a set of keys to removeCache.removeAll(java.lang.Iterable<? extends K>)
public V peekAndPut(K key, V value)
Cache
Returns the value to which the cache previously associated the key,
or null
if the cache contained no mapping for the key.
If the cache does permit null values, then a return value of
null
does not necessarily indicate that the cache
contained no mapping for the key. It is also possible that the cache
explicitly associated the key to the value null
.
This is equivalent to
V tmp = cache.peek(key);
cache.put(key, value);
return tmp;
except that the action is performed atomically.
As with Cache.peek(Object)
, no request to the CacheLoader
is made,
if no entry is associated to the requested key.
See Cache.put(Object, Object)
for the effects on the cache writer and
expiry calculation.
peekAndPut
in interface Cache<K,V>
key
- key with which the specified value is associatedvalue
- value to be associated with the specified keytrue
if a mapping is present and the value was replaced.
false
if no entry is present and no action was performed.public void expireAt(K key, long millis)
Cache
ExpiryTimeValues.NOW
and
ExpiryTimeValues.REFRESH
also effect an entry that was just
refreshed.
If the expiry time is in the past, the entry will expire immediately and refresh ahead is triggered, if enabled.
Although the special time value ExpiryTimeValues.NOW
will lead
to an effective removal of the cache entry, the writer is not called, since the
method is for cache control only.
The cache must be configured with a ExpiryPolicy
or
Cache2kBuilder.expireAfterWrite(long, TimeUnit)
otherwise expiry
timing is not available and this method will throw an exception. An immediate expire
via ExpiryTimeValues.NOW
is always working.
expireAt
in interface Cache<K,V>
key
- key with which the specified value is associatedmillis
- Time in milliseconds since epoch when the entry should expire.
Also see ExpiryTimeValues
public void loadAll(Iterable<? extends K> keys, CacheOperationCompletionListener listener)
Cache
The cache uses multiple threads to load the values in parallel. If thread resources
are not sufficient, meaning the used executor is throwing RejectedExecutionException
the calling thread is used to produce back pressure.
If no loader is defined, the method will throw an immediate exception.
After the load is completed, the completion listener will be called, if provided.
public void reloadAll(Iterable<? extends K> keys, CacheOperationCompletionListener listener)
Cache
The cache uses multiple threads to load the values in parallel. If thread resources
are not sufficient, meaning the used executor is throwing
RejectedExecutionException
the calling thread is used to produce back pressure.
If no loader is defined, the method will throw an immediate exception.
After the load is completed, the completion listener will be called, if provided.
public <R> R invoke(K key, EntryProcessor<K,V,R> entryProcessor)
Cache
EntryProcessor
and MutableCacheEntry
.invoke
in interface Cache<K,V>
R
- type of the resultkey
- the key of the cache entry that should be processedentryProcessor
- processor instance to be invokedEntryProcessor
,
MutableCacheEntry
public <R> Map<K,EntryProcessingResult<R>> invokeAll(Iterable<? extends K> keys, EntryProcessor<K,V,R> entryProcessor)
Cache
keys
parameter.
The order of the invocation is unspecified. To speed up processing the cache
may invoke the entry processor in parallel.
For examples and further details consult the documentation of EntryProcessor
and MutableCacheEntry
.
invokeAll
in interface Cache<K,V>
R
- type of the resultkeys
- the keys of the cache entries that should be processedentryProcessor
- processor instance to be invokedEntryProcessor
,
MutableCacheEntry
public Map<K,V> getAll(Iterable<? extends K> keys)
Cache
Executing the request, the cache may do optimizations like utilizing multiple threads for invoking the loader or using the bulk methods on the loader. This is not yet fully exploited and will improve with further cache2k releases.
Exception handling: The method may terminate normal, even if the cache loader failed to provide values for some keys. The cache will generally do everything to delay the propagation of the exception until the key is requested, to be most specific. If the loader has permanent failures this method may throw an exception immediately.
The operation is not performed atomically. This operation may call different
loader methods either CacheLoader.loadAll(Iterable, Executor)
or
CacheLoader.load(Object)
.
Performance: A better technique is using Cache.prefetchAll(java.lang.Iterable<? extends K>, org.cache2k.CacheOperationCompletionListener)
and then Cache.get(Object)
to request the the values.
getAll
in interface AdvancedKeyValueSource<K,V>
getAll
in interface Cache<K,V>
Cache.getAll(Iterable)
public Map<K,V> peekAll(Iterable<? extends K> keys)
Cache
Cache.peek(Object)
If the cache permits null values, the map will contain entries mapped to a null value.
If the loading of an entry produced an exception, which was not
suppressed and is not yet expired. This exception will be thrown
as CacheLoaderException
when the entry is accessed
via the map interface.
The operation is not performed atomically. Mutations of the cache during this operation may or may not affect the result.
public void putAll(Map<? extends K,? extends V> valueMap)
Cache
See Cache.put(Object, Object)
for information about the
interaction with the CacheWriter
and ExpiryPolicy
public Iterable<K> keys()
Cache
Contract: The iteration is usable while concurrent operations happen on the cache.
All entry keys will be iterated when present in the cache at the moment
of the call to Iterable.iterator()
. An expiration or mutation
happening during the iteration, may or may not be reflected. Separate calls to
Iterable.iterator()
to the identical Iterable
instance start
a separate iteration. It is ensured that every key is only iterated once.
The iterator itself is not thread safe. Calls to one iterator instance from different threads are illegal or need proper synchronization.
Statistics: Iteration is neutral to the cache statistics.
Efficiency: Iterating keys is faster as iterating complete entries.
public Iterable<CacheEntry<K,V>> entries()
Cache
See Cache.keys()
for the general iterator contract.
Efficiency: Iterating entries is less efficient then just iterating keys. The cache needs to create a new entry object and employ some sort of synchronisation to supply a consistent and immutable entry.
entries
in interface Cache<K,V>
Cache.keys()
public void removeAll()
Cache
clear
but listeners will be called.public void clear()
Cache
public void clearAndClose()
Cache
Cache.close()
.
This method is to future proof the API, when a persistence feature is added. In this case the method will stop cache operations and remove all stored external data.
Rationale: The corresponding method in JSR107 is CacheManager.destroyCache()
.
Decided to put it on the cache interface, because: An application can finish its operation on
a cache with one API call; to destroy all the cached data the cache must read the configuration
and build its internal representation, which leads to a "half activated" cache; the additional
call may lead to a race conditions.
clearAndClose
in interface Cache<K,V>
public void close()
Cache
The method is designed to free resources and finish operations as gracefully and fast
as possible. Some cache operations take an unpredictable long time such as the call of
the CacheLoader
, so it may happen that the cache still has threads
in use when this method returns.
After close, subsequent cache operations will throw a IllegalStateException
.
Cache operations currently in progress, may or may not terminated with an exception. A subsequent
call to close will not throw an exception.
If all caches need to be closed it is more effective to use CacheManager.close()
The next releases of cache2k may store cache entries between restarts. If an application will
never use the cached content again, the method Cache.clearAndClose()
should be used instead.
public CacheManager getCacheManager()
Cache
getCacheManager
in interface Cache<K,V>
public boolean isClosed()
Cache
true
if cache was closed or closing is in progress.public <X> X requestInterface(Class<X> _type)
Cache
requestInterface
in interface Cache<K,V>
public ConcurrentMap<K,V> asMap()
Cache
The returned map supports null
values if enabled via
Cache2kBuilder.permitNullValues(boolean)
.
The equals
and hashCode
methods of the Map
are forwarded to the cache. A
map is considered identical when from the same cache instance. This is not compatible to the general
Map
contract.
Multiple calls to this method return a new object instance which is a wrapper of the cache instance. Calling this method is a cheap operation.
The current ConcurrentMap
implementation is minimalistic and not optimized for all
usage aspects. Calling the cache methods directly could be more effective.
cache2k API documentation. Copyright © 2000–2019 headissue GmbH, Munich.