public interface EntryProcessor<K,V,R>
Cache.invoke(K, org.cache2k.processor.EntryProcessor<K, V, R>)
or
Cache.invokeAll(java.lang.Iterable<? extends K>, org.cache2k.processor.EntryProcessor<K, V, R>)
.
With the entry processor it is possible to realize arbitrary operation
semantics for an entry. For example, the method Cache.replaceIfEquals(K, V, V)
can be expressed with the entry processor as follows:
public boolean replaceIfEquals(final K key, final V oldValue, final V newValue) {
EntryProcessor<K, V, Boolean> p = new EntryProcessor<K, V, Boolean>() {
public Boolean process(MutableCacheEntry<K, V> entry) {
if (!entry.exists()) {
return false;
}
if (oldValue == null) {
if (null != entry.getValue()) {
return false;
}
} else {
if (!oldValue.equals(entry.getValue())) {
return false;
}
}
entry.setValue(newValue);
return true;
}
};
return cache.invoke(key, p);
}
For effects on the loader and writer and further details consult the documentation
on the MutableCacheEntry
Modifier and Type | Method and Description |
---|---|
R |
process(MutableCacheEntry<K,V> entry)
Examines or mutates an entry.
|
R process(MutableCacheEntry<K,V> entry) throws Exception
Important: The method must not have any side effects except on the processed entry.
For one call to Cache.invoke(K, org.cache2k.processor.EntryProcessor<K, V, R>)
the method might be called several times.
Some methods of MutableCacheEntry
throw exceptions that are consumed by the
cache to do asynchronous processing.
Caveat about modification time and expiry: The point in time of the last
modification, in case the entry is modified, is after the method completes.
It is possible to set an alternative time with MutableCacheEntry.setRefreshedTime(long)
The cache is only modified, if the method completes without exception.
entry
- the entry to examine or mutate. The reference is only valid within a method callException
- an arbitrary exception that will be wrapped into a
EntryProcessingException
.
If an exception happens no modifications the cache content will not be altered.cache2k API documentation. Copyright © 2000–2020 headissue GmbH, Munich.