Package org.cache2k.processor
Interface EntryProcessor<K,V,R>
-
- All Superinterfaces:
DataAware<K,V>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface EntryProcessor<K,V,@Nullable R> extends DataAware<K,V>
An invokable function to perform an atomic operation on a cache entry. The entry processor is called viaCache.invoke(K, org.cache2k.processor.EntryProcessor<K, V, R>)
orCache.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
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description R
process(MutableCacheEntry<K,V> entry)
Examines or mutates an entry.
-
-
-
Method Detail
-
process
@Nullable R process(MutableCacheEntry<K,V> entry) throws Exception
Examines or mutates an entry.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 ofMutableCacheEntry
throw exceptions that are consumed by the cache to do asynchronous processing.The cache is only modified, if the method completes without exception.
- Parameters:
entry
- the entry to examine or mutate. The reference is only valid within a method call- Returns:
- a user defined result, that will be passed through and returned
- Throws:
Exception
- an arbitrary exception that will be wrapped into aEntryProcessingException
. If an exception happens, the cache content will not be altered.
-
-