public interface AsyncCache<K,V>
putAsync(Object, Object)
offer the best of both worlds between a fully synchronous and a fully asynchronous cache in that a
CompletableFuture
is returned. The CompletableFuture 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:
CompletableFuture f1 = cache.putAsync("key1", "value1"); CompletableFuture f2 = cache.putAsync("key2", "value2"); CompletableFuture 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
CompletableFuture
is still returned though for API consistency.
These methods can have benefit over their sync versions even in LOCAL mode.
CompletableFuture<V> putAsync(K key, V value)
BasicCache.put(Object, Object)
. This method does not block on remote calls, even if your
cache mode is synchronous.key
- key to usevalue
- value to storeCompletableFuture<V> putAsync(K key, V value, long lifespan, TimeUnit unit)
BasicCache.put(Object, Object, long, TimeUnit)
. This method does not block on remote
calls, even if your cache mode is synchronous.key
- key to usevalue
- value to storelifespan
- lifespan of entryunit
- time unit for lifespanCompletableFuture<V> putAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit)
BasicCache.put(Object, Object, long, TimeUnit, long, TimeUnit)
. This method does not block
on remote calls, even if your cache mode is synchronous.key
- key to usevalue
- value to storelifespan
- lifespan of entrylifespanUnit
- time unit for lifespanmaxIdle
- the maximum amount of time this key is allowed to be idle for before it is considered as
expiredmaxIdleUnit
- time unit for max idle timeCompletableFuture<Void> putAllAsync(Map<? extends K,? extends V> data)
Map.putAll(Map)
. This method does not block on remote calls, even if your cache mode
is synchronous.data
- to storeCompletableFuture<Void> putAllAsync(Map<? extends K,? extends V> data, long lifespan, TimeUnit unit)
BasicCache.putAll(Map, long, TimeUnit)
. This method does not block on remote calls, even if
your cache mode is synchronous.data
- to storelifespan
- lifespan of entryunit
- time unit for lifespanCompletableFuture<Void> putAllAsync(Map<? extends K,? extends V> data, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit)
BasicCache.putAll(Map, long, TimeUnit, long, TimeUnit)
. This method does not block on
remote calls, even if your cache mode is synchronous.data
- to storelifespan
- lifespan of entrylifespanUnit
- time unit for lifespanmaxIdle
- the maximum amount of time this key is allowed to be idle for before it is considered as
expiredmaxIdleUnit
- time unit for max idle timeCompletableFuture<Void> clearAsync()
Map.clear()
. This method does not block on remote calls, even if your cache mode is
synchronous.CompletableFuture<Long> sizeAsync()
Map.size()
. This method does not block on remote calls, even if your cache mode is
synchronous.CompletableFuture<V> putIfAbsentAsync(K key, V value)
ConcurrentMap.putIfAbsent(Object, Object)
. This method does not block on remote calls, even if
your cache mode is synchronous.key
- key to usevalue
- value to storeCompletableFuture<V> putIfAbsentAsync(K key, V value, long lifespan, TimeUnit unit)
BasicCache.putIfAbsent(Object, Object, long, TimeUnit)
. This method does not block on
remote calls, even if your cache mode is synchronous.key
- key to usevalue
- value to storelifespan
- lifespan of entryunit
- time unit for lifespanCompletableFuture<V> putIfAbsentAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit)
BasicCache.putIfAbsent(Object, Object, long, TimeUnit, long, TimeUnit)
. This method does
not block on remote calls, even if your cache mode is synchronous.key
- key to usevalue
- value to storelifespan
- lifespan of entrylifespanUnit
- time unit for lifespanmaxIdle
- the maximum amount of time this key is allowed to be idle for before it is considered as
expiredmaxIdleUnit
- time unit for max idle timeCompletableFuture<V> removeAsync(Object key)
BasicCache.remove(Object)
. This method does not block on remote calls, even if your cache
mode is synchronous.key
- key to removeCompletableFuture<Boolean> removeAsync(Object key, Object value)
ConcurrentMap.remove(Object, Object)
. This method does not block on remote calls, even if your
cache mode is synchronous.key
- key to removevalue
- value to match onCompletableFuture<V> replaceAsync(K key, V value)
ConcurrentMap.replace(Object, Object)
. This method does not block on remote calls, even if
your cache mode is synchronous.key
- key to removevalue
- value to storeCompletableFuture<V> replaceAsync(K key, V value, long lifespan, TimeUnit unit)
BasicCache.replace(Object, Object, long, TimeUnit)
. This method does not block on remote
calls, even if your cache mode is synchronous.key
- key to removevalue
- value to storelifespan
- lifespan of entryunit
- time unit for lifespanCompletableFuture<V> replaceAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit)
BasicCache.replace(Object, Object, long, TimeUnit, long, TimeUnit)
. This method does not
block on remote calls, even if your cache mode is synchronous.key
- key to removevalue
- value to storelifespan
- lifespan of entrylifespanUnit
- time unit for lifespanmaxIdle
- the maximum amount of time this key is allowed to be idle for before it is considered as
expiredmaxIdleUnit
- time unit for max idle timeCompletableFuture<Boolean> replaceAsync(K key, V oldValue, V newValue)
ConcurrentMap.replace(Object, Object, Object)
. This method does not block on remote calls,
even if your cache mode is synchronous.key
- key to removeoldValue
- value to overwritenewValue
- value to storeCompletableFuture<Boolean> replaceAsync(K key, V oldValue, V newValue, long lifespan, TimeUnit unit)
BasicCache.replace(Object, Object, Object, long, TimeUnit)
. This method does not block on
remote calls, even if your cache mode is synchronous.key
- key to removeoldValue
- value to overwritenewValue
- value to storelifespan
- lifespan of entryunit
- time unit for lifespanCompletableFuture<Boolean> replaceAsync(K key, V oldValue, V newValue, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit)
BasicCache.replace(Object, Object, Object, long, TimeUnit, long, TimeUnit)
. This method
does not block on remote calls, even if your cache mode is synchronous.key
- key to removeoldValue
- value to overwritenewValue
- value to storelifespan
- lifespan of entrylifespanUnit
- time unit for lifespanmaxIdle
- the maximum amount of time this key is allowed to be idle for before it is considered as
expiredmaxIdleUnit
- time unit for max idle timeCompletableFuture<V> getAsync(K key)
Map.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 Map.get(Object)
.key
- key to retrieveMap.get(Object)
default CompletableFuture<Boolean> containsKeyAsync(K key)
Map.containsKey(Object)
key
- key to retrievedefault CompletableFuture<Map<K,V>> getAllAsync(Set<?> keys)
CompletableFuture<V> computeAsync(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
ConcurrentMap.compute(Object, BiFunction)
. This method does not block on remote
calls, even if your cache mode is synchronous.CompletableFuture<V> computeAsync(K key, BiFunction<? super K,? super V,? extends V> remappingFunction, long lifespan, TimeUnit lifespanUnit)
BasicCache.compute(Object, BiFunction, long, TimeUnit)
. This method does not block on remote
calls, even if your cache mode is synchronous.CompletableFuture<V> computeAsync(K key, BiFunction<? super K,? super V,? extends V> remappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit)
BasicCache.compute(Object, BiFunction, long, TimeUnit, long, TimeUnit)
. This method does not block on remote
calls, even if your cache mode is synchronous.CompletableFuture<V> computeIfAbsentAsync(K key, Function<? super K,? extends V> mappingFunction)
ConcurrentMap.computeIfAbsent(Object, Function)
. This method does not block on remote
calls, even if your cache mode is synchronous.CompletableFuture<V> computeIfAbsentAsync(K key, Function<? super K,? extends V> mappingFunction, long lifespan, TimeUnit lifespanUnit)
BasicCache.computeIfAbsent(Object, Function, long, TimeUnit)
. This method does not block on remote
calls, even if your cache mode is synchronous.CompletableFuture<V> computeIfAbsentAsync(K key, Function<? super K,? extends V> mappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit)
BasicCache.computeIfAbsent(Object, Function, long, TimeUnit, long, TimeUnit)
. This method does not block on remote
calls, even if your cache mode is synchronous.CompletableFuture<V> computeIfPresentAsync(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
ConcurrentMap.computeIfPresent(Object, BiFunction)
. This method does not block on
remote calls, even if your cache mode is synchronous.CompletableFuture<V> computeIfPresentAsync(K key, BiFunction<? super K,? super V,? extends V> remappingFunction, long lifespan, TimeUnit lifespanUnit)
BasicCache.computeIfPresent(Object, BiFunction, long, TimeUnit)
. This method does not block on
remote calls, even if your cache mode is synchronous.CompletableFuture<V> computeIfPresentAsync(K key, BiFunction<? super K,? super V,? extends V> remappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit)
BasicCache.computeIfPresent(Object, BiFunction, long, TimeUnit, long, TimeUnit)
. This method does not block on
remote calls, even if your cache mode is synchronous.CompletableFuture<V> mergeAsync(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
ConcurrentMap.merge(Object, Object, BiFunction)
. This method does not block on remote
calls, even if your cache mode is synchronous.CompletableFuture<V> mergeAsync(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction, long lifespan, TimeUnit lifespanUnit)
BasicCache.merge(Object, Object, BiFunction, long, TimeUnit)
. This method does not
block on remote calls, even if your cache mode is synchronous.CompletableFuture<V> mergeAsync(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit)
BasicCache.merge(Object, Object, BiFunction, long, TimeUnit, long, TimeUnit)
. This
method does not block on remote calls, even if your cache mode is synchronous.Copyright © 2021 JBoss by Red Hat. All rights reserved.