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> e)
Examines or mutates an entry.
|
R process(MutableCacheEntry<K,V> e) throws Exception
From inside this method it is illegal to call methods on the same cache. This may cause a deadlock.
The method may not have any side effects except on the processed entry. The cache may call the method multiple times for each invocation via the cache, when needed.
The cache is only modified, if the method complete successfully.
e
- the entry to examine or mutate. The reference is only valid within a method call,
don't pass or store itException
- 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–2019 headissue GmbH, Munich.