Redis客户端编程
在 Java 中调用 Redis,通常使用 Jedis 或 Lettuce(Spring Data Redis 默认客户端)、Redisson进行操作。
Jedis | Spring Data Redis | Redisson | |
说明 | Redis官方提供 | SpringBoot集成 | 提供很多分布式相关服务 |
1. Jedis
1.1 添加 Maven 依赖
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>5.1.0</version> <!-- 使用最新版本 -->
</dependency>
1.2 基本操作示例
import redis.clients.jedis.Jedis;public class JedisExample {public static void main(String[] args) {// 1. 连接 RedisJedis jedis = new Jedis("localhost", 6379); // 默认端口 6379// 2. 设置和获取 Stringjedis.set("name", "Alice");String name = jedis.get("name");System.out.println("name: " + name); // 输出: Alice// 3. 设置过期时间(TTL)jedis.setex("tempKey", 10, "expires in 10 sec"); // 10秒后自动删除// 4. 操作 Listjedis.lpush("mylist", "item1", "item2", "item3");System.out.println(jedis.lrange("mylist", 0, -1)); // 输出: [item3, item2, item1]// 5. 操作 Hashjedis.hset("user:1", "name", "Bob");jedis.hset("user:1", "age", "30");System.out.println(jedis.hgetAll("user:1")); // 输出: {name=Bob, age=30}// 6. 删除 Keyjedis.del("name");// 7. 关闭连接jedis.close();}
}
2. 使用 Spring Data Redis(Lettuce)
Spring Data Redis 和 Lettuce 的关系是框架与底层客户端的关系, Spring Data Redis 将操作委托给 Lettuce(或 Jedis,需手动切换)
2.1 添加 Maven 依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.2 配置 Redis 连接
在 application.properties
或 application.yml
中配置:
spring.redis.host=localhost
spring.redis.port=6379
-- Lettuce 专属配置
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
2.3 使用 RedisTemplate
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.Map;@Component
public class RedisExample {@Autowiredprivate RedisTemplate<String, String> redisTemplate;public void runRedisOperations() {// 1. 设置和获取 StringredisTemplate.opsForValue().set("name", "Alice");String name = redisTemplate.opsForValue().get("name");System.out.println("name: " + name); // Alice// 2. 设置过期时间redisTemplate.opsForValue().set("tempKey", "expires in 10 sec", 10, TimeUnit.SECONDS);// 3. 操作 ListredisTemplate.opsForList().leftPushAll("mylist", "item1", "item2", "item3");List<String> list = redisTemplate.opsForList().range("mylist", 0, -1);System.out.println(list); // [item3, item2, item1]// 4. 操作 HashredisTemplate.opsForHash().put("user:1", "name", "Bob");redisTemplate.opsForHash().put("user:1", "age", "30");Map<Object, Object> user = redisTemplate.opsForHash().entries("user:1");System.out.println(user); // {name=Bob, age=30}// 5. 删除 KeyredisTemplate.delete("name");}
}
3. Redisson
3.1 添加maven依赖
<dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.23.4</version> <!-- 使用最新版本 -->
</dependency>
3.2 基本操作
package com.example.testspringboot.redis.client.demo;import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;import java.util.Map;public class RedissonExample {public static void main(String[] args) {// 1. 创建配置Config config = new Config();config.useSingleServer().setAddress("redis://127.0.0.1:6379")
// .setPassword("yourPassword") // 如果有密码.setDatabase(0);// 2. 创建Redisson客户端RedissonClient redisson = Redisson.create(config);try {// 3. 执行各种操作...// 3.1 string操作RBucket<String> bucket = redisson.getBucket("simpleKey");// 设置值bucket.set("value1");// 获取值String value = bucket.get();System.out.println("Value: " + value);// 3.1 hash操作RMap<String, String> map = redisson.getMap("userMap");// 添加元素map.put("name", "John");map.put("age", "30");// 获取元素String name = map.get("name");System.out.println("Name: " + name);// 批量操作Map<String, String> allEntries = map.readAllMap();System.out.println("All entries: " + allEntries);} finally {// 4. 关闭客户端redisson.shutdown();}}
}
参考文章
jedis是什么,为什么是线程不安全的_jedis线程安全吗-CSDN博客