Use this file to discover all available pages before exploring further.
Redis provides two official Java clients: Jedis and Lettuce. Both offer full support for Redis commands and features, but with different architectural approaches.
JedisClientConfig config = DefaultJedisClientConfig.builder() .password("your-password") .ssl(true) .build();JedisPool pool = new JedisPool( new HostAndPort("your-endpoint.redis.cloud", 12345), config);
import java.util.HashMap;import java.util.Map;try (Jedis jedis = pool.getResource()) { // Store user data as a hash Map<String, String> user = new HashMap<>(); user.put("name", "Alice"); user.put("email", "alice@example.com"); user.put("age", "30"); jedis.hset("user:1", user); // Get specific field String name = jedis.hget("user:1", "name"); // Get all fields Map<String, String> userData = jedis.hgetAll("user:1"); // Increment numeric field jedis.hincrBy("user:1", "age", 1);}
try (Jedis jedis = pool.getResource()) { // Add items to a list jedis.rpush("queue", "task1", "task2", "task3"); // Get list length long length = jedis.llen("queue"); // Pop item from list String task = jedis.lpop("queue"); // Get range of items List<String> tasks = jedis.lrange("queue", 0, -1);}
try (Jedis jedis = pool.getResource()) { // Add members to a set jedis.sadd("tags", "python", "redis", "database"); // Check membership boolean isMember = jedis.sismember("tags", "python"); // Get all members Set<String> tags = jedis.smembers("tags"); // Set operations jedis.sadd("tags2", "redis", "cache", "nosql"); Set<String> common = jedis.sinter("tags", "tags2");}
// SET and GET operationscommands.set("user:1:name", "Alice");String name = commands.get("user:1:name");System.out.println("Name: " + name);// Work with numberscommands.set("counter", "0");commands.incr("counter");commands.incrby("counter", 5);String count = commands.get("counter");System.out.println("Counter: " + count);// Expirationcommands.setex("session:abc", 3600, "session_data");
3
Use async operations
Leverage Lettuce’s asynchronous API:
import io.lettuce.core.api.async.RedisAsyncCommands;import io.lettuce.core.RedisFuture;RedisAsyncCommands<String, String> async = connection.async();RedisFuture<String> future = async.get("key");future.thenAccept(value -> { System.out.println("Value: " + value);});// Wait for completion if neededString result = future.get();