Lettuce guide (Java)
Connect your Lettuce application to a Redis database
Lettuce is an advanced Java client for Redis that supports synchronous, asynchronous, and reactive connections. If you only need synchronous connections then you may find the other Java client Jedis easier to use.
The sections below explain how to install Lettuce
and connect your application
to a Redis database.
Lettuce
requires a running Redis or Redis Stack server. See Getting started for Redis installation instructions.
Install
To include Lettuce as a dependency in your application, edit the appropriate dependency file as shown below.
If you use Maven, add the following dependency to your pom.xml
:
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.3.2.RELEASE</version> <!-- Check for the latest version on Maven Central -->
</dependency>
If you use Gradle, include this line in your build.gradle
file:
dependencies {
compileOnly 'io.lettuce:lettuce-core:6.3.2.RELEASE'
}
If you wish to use the JAR files directly, download the latest Lettuce and, optionally, Apache Commons Pool2 JAR files from Maven Central or any other Maven repository.
To build from source, see the instructions on the Lettuce source code GitHub repo.
Connect and test
Connect to a local server using the following code. This example also stores and retrieves a simple string value to test the connection.
import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
public class ConnectBasicTest {
public void connectBasic() {
RedisURI uri = RedisURI.Builder
.redis("localhost", 6379)
.build();
RedisClient client = RedisClient.create(uri);
StatefulRedisConnection<String, String> connection = client.connect();
RedisCommands<String, String> commands = connection.sync();
commands.set("foo", "bar");
String result = commands.get("foo");
System.out.println(result); // >>> bar
connection.close();
client.shutdown();
}
}
More information
The Lettuce reference guide has more examples and an API reference. You may also be interested in the Project Reactor library that Lettuce uses.
See also the other pages in this section for more information and examples: