Preface
Redis is a high-performance key-value storage database that is widely used in caching, message queues, session storage and other scenarios. Java, as a widely used programming language, provides a variety of ways to connect and operate Redis. This article will introduce two commonly used Java connections to Redis: Jedis and Lettuce, and explain in detail how they are used.
1. Jedis
Jedis is a lightweight Java Redis client that provides a simple and easy-to-use API to operate Redis. It is suitable for most Redis operating scenarios and is easy to get started.
1. Add dependencies
First, add Jedis's dependencies to the project. Taking the Maven project as an example, add the following dependencies to the file:
<dependency> <groupId></groupId> <artifactId>jedis</artifactId> <version>4.2.3</version> </dependency>
2. Connect to Redis
Connecting Redis with Jedis is very simple. You just need to create a Jedis object and specify the address and port of the Redis server:
import ; public class JedisExample { public static void main(String[] args) { // Create a Jedis object, connect to the local Redis server, default port 6379 Jedis jedis = new Jedis("localhost", 6379); // Test connection ("Connected successfully"); ("Service is running: " + ()); // Set key-value pairs ("name", "Redis with Jedis"); // Get the value String value = ("name"); ("Get the value: " + value); // Close the connection (); } }
3. Connection pool
For performance improvements, you can use the Jedis connection pool to manage Redis connections:
import ; import ; public class JedisPoolExample { public static void main(String[] args) { // Configure the connection pool JedisPoolConfig poolConfig = new JedisPoolConfig(); (10); // Maximum number of connections (5); // Maximum number of idle connections // Create a connection pool JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379); // Get connection from the connection pool try (Jedis jedis = ()) { // Test connection ("Connected successfully"); ("Service is running: " + ()); // Set key-value pairs ("name", "Redis with Jedis Pool"); // Get the value String value = ("name"); ("Get the value: " + value); } // Close the connection pool (); } }
2. Lettuce
Lettuce is a high-performance Java Redis client based on Netty implementation and supports asynchronous and responsive programming models. It is suitable for high concurrency scenarios and provides richer features.
1. Add dependencies
Add Lettuce's dependencies to the project. Taking the Maven project as an example, add the following dependencies to the file:
<dependency> <groupId></groupId> <artifactId>lettuce-core</artifactId> <version>6.2.</version> </dependency>
2. Connect to Redis
Connecting Redis with Lettuce is also very simple:
import ; import ; import ; public class LettuceExample { public static void main(String[] args) { // Create RedisClient RedisClient redisClient = ("redis://localhost:6379"); // Get the connection StatefulRedisConnection<String, String> connection = (); // Get the synchronous operation interface RedisCommands<String, String> syncCommands = (); // Test connection ("Connected successfully"); ("Service is running: " + ()); // Set key-value pairs ("name", "Redis with Lettuce"); // Get the value String value = ("name"); ("Get the value: " + value); // Close the connection (); (); } }
3. Asynchronous operation
Lettuce supports asynchronous operation and is suitable for high concurrency scenarios:
import ; import ; import ; import ; public class LettuceAsyncExample { public static void main(String[] args) { // Create RedisClient RedisClient redisClient = ("redis://localhost:6379"); // Get the connection StatefulRedisConnection<String, String> connection = (); // Get the asynchronous operation interface RedisAsyncCommands<String, String> asyncCommands = (); // Asynchronously set key-value pairs CompletableFuture<String> future = ("name", "Redis with Lettuce Async"); // Get the value asynchronously (result -> ("name")) .thenAccept(value -> ("Get the value: " + value)); // Close the connection (); (); } }
Ending
This article introduces two common ways to connect Redis in Java: Jedis and Lettuce. Jedis is simple and easy to use and is suitable for most scenarios; while Lettuce has higher performance and supports asynchronous and responsive programming, suitable for high concurrency scenarios. Developers can choose the right tools to operate Redis according to actual needs.
This is the end of this article about the two ways to connect Java to Redis. For more related content on Java to Redis, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!