All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.commons.api.AsyncCache Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.commons.api;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.infinispan.commons.util.concurrent.NotifyingFuture;

/**
 * AsyncCache. This interface is implemented by caches which support asynchronous variants of the various
 * put/get/remove/clear/replace/putAll methods
 *
 * Note that these methods only really make sense if you are using a clustered cache.  I.e., when used in LOCAL mode,
 * these "async" operations offer no benefit whatsoever.  These methods, such as {@link #putAsync(Object, Object)}
 * offer the best of both worlds between a fully synchronous and a fully asynchronous cache in that a
 * {@link NotifyingFuture} is returned.  The NotifyingFuture can then be ignored or thrown away for typical
 * asynchronous behaviour, or queried for synchronous behaviour, which would block until any remote calls complete.
 * Note that all remote calls are, as far as the transport is concerned, synchronous.  This allows you the guarantees
 * that remote calls succeed, while not blocking your application thread unnecessarily.  For example, usage such as
 * the following could benefit from the async operations:
 * 
 *   NotifyingFuture f1 = cache.putAsync("key1", "value1");
 *   NotifyingFuture f2 = cache.putAsync("key2", "value2");
 *   NotifyingFuture f3 = cache.putAsync("key3", "value3");
 *   f1.get();
 *   f2.get();
 *   f3.get();
 * 
* The net result is behavior similar to synchronous RPC calls in that at the end, you have guarantees that all calls * completed successfully, but you have the added benefit that the three calls could happen in parallel. This is * especially advantageous if the cache uses distribution and the three keys map to different cache instances in the * cluster. *

* Also, the use of async operations when within a transaction return your local value only, as expected. A * NotifyingFuture is still returned though for API consistency. *

* * @author Mircea Markus * @author Manik Surtani * @author Galder Zamarreño * @author Tristan Tarrant * @since 6.0 */ public interface AsyncCache { /** * Asynchronous version of {@link #put(Object, Object)}. This method does not block on remote calls, even if your * cache mode is synchronous. Has no benefit over {@link #put(Object, Object)} if used in LOCAL mode. *

* * @param key key to use * @param value value to store * @return a future containing the old value replaced. */ NotifyingFuture putAsync(K key, V value); /** * Asynchronous version of {@link #put(Object, Object, long, TimeUnit)} . This method does not block on remote * calls, even if your cache mode is synchronous. Has no benefit over {@link #put(Object, Object, long, TimeUnit)} * if used in LOCAL mode. * * @param key key to use * @param value value to store * @param lifespan lifespan of entry * @param unit time unit for lifespan * @return a future containing the old value replaced */ NotifyingFuture putAsync(K key, V value, long lifespan, TimeUnit unit); /** * Asynchronous version of {@link #put(Object, Object, long, TimeUnit, long, TimeUnit)}. This method does not block * on remote calls, even if your cache mode is synchronous. Has no benefit over {@link #put(Object, Object, long, * TimeUnit, long, TimeUnit)} if used in LOCAL mode. * * @param key key to use * @param value value to store * @param lifespan lifespan of entry * @param lifespanUnit time unit for lifespan * @param maxIdle the maximum amount of time this key is allowed to be idle for before it is considered as * expired * @param maxIdleUnit time unit for max idle time * @return a future containing the old value replaced */ NotifyingFuture putAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit); /** * Asynchronous version of {@link #putAll(Map)}. This method does not block on remote calls, even if your cache mode * is synchronous. Has no benefit over {@link #putAll(Map)} if used in LOCAL mode. * * @param data to store * @return a future containing a void return type */ NotifyingFuture putAllAsync(Map data); /** * Asynchronous version of {@link #putAll(Map, long, TimeUnit)}. This method does not block on remote calls, even if * your cache mode is synchronous. Has no benefit over {@link #putAll(Map, long, TimeUnit)} if used in LOCAL mode. * * @param data to store * @param lifespan lifespan of entry * @param unit time unit for lifespan * @return a future containing a void return type */ NotifyingFuture putAllAsync(Map data, long lifespan, TimeUnit unit); /** * Asynchronous version of {@link #putAll(Map, long, TimeUnit, long, TimeUnit)}. This method does not block on * remote calls, even if your cache mode is synchronous. Has no benefit over {@link #putAll(Map, long, TimeUnit, * long, TimeUnit)} if used in LOCAL mode. * * @param data to store * @param lifespan lifespan of entry * @param lifespanUnit time unit for lifespan * @param maxIdle the maximum amount of time this key is allowed to be idle for before it is considered as * expired * @param maxIdleUnit time unit for max idle time * @return a future containing a void return type */ NotifyingFuture putAllAsync(Map data, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit); /** * Asynchronous version of {@link #clear()}. This method does not block on remote calls, even if your cache mode is * synchronous. Has no benefit over {@link #clear()} if used in LOCAL mode. * * @return a future containing a void return type */ NotifyingFuture clearAsync(); /** * Asynchronous version of {@link #putIfAbsent(Object, Object)}. This method does not block on remote calls, even if * your cache mode is synchronous. Has no benefit over {@link #putIfAbsent(Object, Object)} if used in LOCAL mode. *

* * @param key key to use * @param value value to store * @return a future containing the old value replaced. */ NotifyingFuture putIfAbsentAsync(K key, V value); /** * Asynchronous version of {@link #putIfAbsent(Object, Object, long, TimeUnit)} . This method does not block on * remote calls, even if your cache mode is synchronous. Has no benefit over {@link #putIfAbsent(Object, Object, * long, TimeUnit)} if used in LOCAL mode. * * @param key key to use * @param value value to store * @param lifespan lifespan of entry * @param unit time unit for lifespan * @return a future containing the old value replaced */ NotifyingFuture putIfAbsentAsync(K key, V value, long lifespan, TimeUnit unit); /** * Asynchronous version of {@link #putIfAbsent(Object, Object, long, TimeUnit, long, TimeUnit)}. This method does * not block on remote calls, even if your cache mode is synchronous. Has no benefit over {@link * #putIfAbsent(Object, Object, long, TimeUnit, long, TimeUnit)} if used in LOCAL mode. * * @param key key to use * @param value value to store * @param lifespan lifespan of entry * @param lifespanUnit time unit for lifespan * @param maxIdle the maximum amount of time this key is allowed to be idle for before it is considered as * expired * @param maxIdleUnit time unit for max idle time * @return a future containing the old value replaced */ NotifyingFuture putIfAbsentAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit); /** * Asynchronous version of {@link #remove(Object)}. This method does not block on remote calls, even if your cache * mode is synchronous. Has no benefit over {@link #remove(Object)} if used in LOCAL mode. * * @param key key to remove * @return a future containing the value removed */ NotifyingFuture removeAsync(Object key); /** * Asynchronous version of {@link #remove(Object, Object)}. This method does not block on remote calls, even if your * cache mode is synchronous. Has no benefit over {@link #remove(Object, Object)} if used in LOCAL mode. * * @param key key to remove * @param value value to match on * @return a future containing a boolean, indicating whether the entry was removed or not */ NotifyingFuture removeAsync(Object key, Object value); /** * Asynchronous version of {@link #replace(Object, Object)}. This method does not block on remote calls, even if * your cache mode is synchronous. Has no benefit over {@link #replace(Object, Object)} if used in LOCAL mode. * * @param key key to remove * @param value value to store * @return a future containing the previous value overwritten */ NotifyingFuture replaceAsync(K key, V value); /** * Asynchronous version of {@link #replace(Object, Object, long, TimeUnit)}. This method does not block on remote * calls, even if your cache mode is synchronous. Has no benefit over {@link #replace(Object, Object, long, * TimeUnit)} if used in LOCAL mode. * * @param key key to remove * @param value value to store * @param lifespan lifespan of entry * @param unit time unit for lifespan * @return a future containing the previous value overwritten */ NotifyingFuture replaceAsync(K key, V value, long lifespan, TimeUnit unit); /** * Asynchronous version of {@link #replace(Object, Object, long, TimeUnit, long, TimeUnit)}. This method does not * block on remote calls, even if your cache mode is synchronous. Has no benefit over {@link #replace(Object, * Object, long, TimeUnit, long, TimeUnit)} if used in LOCAL mode. * * @param key key to remove * @param value value to store * @param lifespan lifespan of entry * @param lifespanUnit time unit for lifespan * @param maxIdle the maximum amount of time this key is allowed to be idle for before it is considered as * expired * @param maxIdleUnit time unit for max idle time * @return a future containing the previous value overwritten */ NotifyingFuture replaceAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit); /** * Asynchronous version of {@link #replace(Object, Object, Object)}. This method does not block on remote calls, * even if your cache mode is synchronous. Has no benefit over {@link #replace(Object, Object, Object)} if used in * LOCAL mode. * * @param key key to remove * @param oldValue value to overwrite * @param newValue value to store * @return a future containing a boolean, indicating whether the entry was replaced or not */ NotifyingFuture replaceAsync(K key, V oldValue, V newValue); /** * Asynchronous version of {@link #replace(Object, Object, Object, long, TimeUnit)}. This method does not block on * remote calls, even if your cache mode is synchronous. Has no benefit over {@link #replace(Object, Object, Object, * long, TimeUnit)} if used in LOCAL mode. * * @param key key to remove * @param oldValue value to overwrite * @param newValue value to store * @param lifespan lifespan of entry * @param unit time unit for lifespan * @return a future containing a boolean, indicating whether the entry was replaced or not */ NotifyingFuture replaceAsync(K key, V oldValue, V newValue, long lifespan, TimeUnit unit); /** * Asynchronous version of {@link #replace(Object, Object, Object, long, TimeUnit, long, TimeUnit)}. This method * does not block on remote calls, even if your cache mode is synchronous. Has no benefit over {@link * #replace(Object, Object, Object, long, TimeUnit, long, TimeUnit)} if used in LOCAL mode. * * @param key key to remove * @param oldValue value to overwrite * @param newValue value to store * @param lifespan lifespan of entry * @param lifespanUnit time unit for lifespan * @param maxIdle the maximum amount of time this key is allowed to be idle for before it is considered as * expired * @param maxIdleUnit time unit for max idle time * @return a future containing a boolean, indicating whether the entry was replaced or not */ NotifyingFuture replaceAsync(K key, V oldValue, V newValue, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit); /** * Asynchronous version of {@link #get(Object)} that allows user code to * retrieve the value associated with a key at a later stage, hence allowing * multiple parallel get requests to be sent. Normally, when this method * detects that the value is likely to be retrieved from from a remote * entity, it will span a different thread in order to allow the * asynchronous get call to return immediately. If the call will definitely * resolve locally, for example when the cache is configured with LOCAL mode * and no stores are configured, the get asynchronous call will act * sequentially and will have no different to {@link #get(Object)}. * * @param key key to retrieve * @return a future that can be used to retrieve value associated with the * key when this is available. The actual value returned by the future * follows the same rules as {@link #get(Object)} */ NotifyingFuture getAsync(K key); }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy