-
Notifications
You must be signed in to change notification settings - Fork 985
Asynchronous API (4.0)
lettuce is built on netty that is a multithreaded, event-driven IO framework. All communication is handled asynchronously. Every command sent to Redis using the asynchronous API creates a CompleteableFuture
that can be cancelled, awaited and listened. You obtain the async API by calling the connectAsync()
method of the RedisClient
(or RedisClusterClient.connectClusterAsync
).
There are several ways how to wait or get notified in case a future completes:
- RedisFuture.get() / RedisFuture.get(long timeout, TimeUnit unit)
- while(!RedisFuture.isDone())
- RedisFuture.await(long timeout, TimeUnit unit)
- RedisFuture.thenAccept(Consumer<? super T> action)
Futures of lettuce 4.0 also carry exceptions, if any occurred. If you call the get()
method and an exception occurred, this exception will be rethrown wrapped within an ExecutionException
(this is different to lettuce 3.x). You can find more details within the Javadoc on CompletionStage
You can fire this way one or more commands and wait until the execution completes.
If you use RedisFuture.get()
the system will wait indefinitely and block your call.
RedisFuture.get(long timeout, TimeUnit unit)
will wait at most until the specified timeout. If the result comes back earlier, so you call will also continue earlier. If the timeout exceeds, you will receive a TimeoutException
.
In this style, you are using the Future whether it's done. You can poll, wait or even do other things
until your future comes back. isDone
is a non-blocking call.
You can specify a maximal waiting time, like in the get(long timeout, TimeUnit unit)
example. The difference to get()
is, that await(long timeout, TimeUnit unit)
will come back with a boolean
value in every case. So if the execution takes longer than the timeout, await
will return false
instead of a TimeoutException
. After a result of true
you can call get()
and you'll receive the return value immediately.
You can design your flow also completely asynchronous using functions and consumers. You subscribe to an individual future using CompletionStage methods.
Any Redis errors will cause to throw an ExecutionException
on calling get()
.
Basic operations
RedisStringAsyncCommands<String, String> async = client.connect().async();
RedisFuture<String> set = async.set("key", "value");
RedisFuture<String> get = async.get("key");
set.get() == "OK"
get.get() == "value"
Waiting for a future with a timeout
RedisStringAsyncCommands<String, String> async = client.connect().async();
RedisFuture<String> set = async.set("key", "value");
RedisFuture<String> get = async.get("key");
set.await(1, SECONDS) == true
set.get() == "OK"
get.get(1, TimeUnit.MINUTES) == "value"
Using listeners for a future
RedisStringAsyncCommands<String, String> async = client.connect().async();
RedisFuture<String> set = async.set("key", "value");
Runnable listener = new Runnable() {
@Override
public void run() {
...;
}
};
set.thenRun(listener);
Lettuce documentation was moved to https://redis.github.io/lettuce/overview/
Intro
Getting started
- Getting started
- Redis URI and connection details
- Basic usage
- Asynchronous API
- Reactive API
- Publish/Subscribe
- Transactions/Multi
- Scripting and Functions
- Redis Command Interfaces
- FAQ
HA and Sharding
Advanced usage
- Configuring Client resources
- Client Options
- Dynamic Command Interfaces
- SSL Connections
- Native Transports
- Unix Domain Sockets
- Streaming API
- Events
- Command Latency Metrics
- Tracing
- Stateful Connections
- Pipelining/Flushing
- Connection Pooling
- Graal Native Image
- Custom commands
Integration and Extension
Internals