Java使用RedisTemplate操作Redis
1.RedisTemplate 的介绍
- RedisTemplate方式是SpringBoot集成Redis的客户端方式,在使用时与Jedis的区别是是否使用使用RedisTemplage的时候一定要配置对象的序列化和反序列化方式。
- 我们的代码中使用的序列化与反序列化是由 FastJSON 提供的
2.SpringBoot 项目使用 RedisTemplate
2.1 配置依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
2.2 SpringBoot 配置文件
spring:
application:
name: springbootredis
redis:
host: 192.168.159.133
port: 6379
database: 0
jedis:
pool:
max-active: 10
max-idle: 5
min-idle: 0
max-wait: 60000
2.3 业务层代码注入 RedisTemplate 对象
@Autowired
private RedisTemplate<String, String> redisTemplate;
2.4 String 类型操作
public void StringMethod(){
System.out.println("=========[查询所有]=========");
Set<String> keys = redisTemplate.keys("jedis*");
for (String key : keys) {
System.out.println("键:" + key + ", 键的类型:" + redisTemplate.opsForValue().get(key) + ", 值:" + redisTemplate.opsForValue().get(key));
}
System.out.println("=========[设置键值对]=========");
redisTemplate.opsForValue().set("jedis1", "1");
redisTemplate.opsForValue().set("jedis2", "2");
redisTemplate.opsForValue().set("jedis3", "3");
keys = redisTemplate.keys("jedis*");
for (String key : keys) {
System.out.println("键:" + key + ", 键的类型:" + redisTemplate.opsForValue().get(key) + ", 值:" + redisTemplate.opsForValue().get(key));
}
System.out.println("=========[设置有效期限的键,获取 ttl 值]=========");
redisTemplate.opsForValue().set("expire1", "ttl key");
redisTemplate.expire("expire1", 60, TimeUnit.SECONDS);
try {
Thread.sleep(5000);
} catch (Exception e){
System.out.println(e.getMessage());
}
System.out.println("休眠 5 秒钟之后,ttl 值为:" + redisTemplate.getExpire("expire1"));
System.out.println("=========[同时设置多个键值对,并获取]=========");
Map<String, String> stringStringMap = new LinkedHashMap<>();
stringStringMap.put("java-1", "6");
stringStringMap.put("java-2", "8");
stringStringMap.put("java-3", "10");
redisTemplate.opsForValue().multiSet(stringStringMap);
List<String> multiGet = redisTemplate.opsForValue().multiGet(stringStringMap.keySet());
System.out.println("同时获取多个键的值:" + multiGet.toString());
System.out.println("=========[对 数值型的 键值对进行增减操作]=========");
redisTemplate.opsForValue().increment("java-1", 3L);
redisTemplate.opsForValue().decrement("java-3", 5L);
multiGet = redisTemplate.opsForValue().multiGet(stringStringMap.keySet());
System.out.println("同时获取多个键的值:" + multiGet.toString());
System.out.println("=========[返回旧的数据,更新新的数据]=========");
String andSet = redisTemplate.opsForValue().getAndSet("java-2", "new-value");
System.out.println("java-2 键的原来的值:" + andSet + ", 新的值:" + redisTemplate.opsForValue().get("java-2"));
System.out.println("=========[对值进行字符串操作]=========");
redisTemplate.opsForValue().append("java-2", "-aaa");
System.out.println("java-2 键的原来的值:" + andSet + ", 新的值:" + redisTemplate.opsForValue().get("java-2"));
System.out.println("=========[删除元素]=========");
Set<String> keys1 = redisTemplate.keys("*");
redisTemplate.delete(keys1);
}
2.5 List 类型操作
public void ListMethod(){
System.out.println("=========[从左侧和右侧 添加元素]=========");
List<String> nameList = new LinkedList<>();
nameList.add("Mike");
nameList.add("Peter");
nameList.add("Jack");
redisTemplate.opsForList().leftPushAll("list-java-1", nameList);
nameList.clear();
nameList.add("aaa");
nameList.add("bbb");
nameList.add("ccc");
redisTemplate.opsForList().rightPushAll("list-java-1", nameList);
System.out.println("=========[根据索引获取元素]=========");
String index = redisTemplate.opsForList().index("list-java-1", 1);
System.out.println("索引为 1 的元素为:" + index);
System.out.println("=========[获取所有元素]=========");
List<String> range = redisTemplate.opsForList().range("list-java-1", 0, -1);
System.out.println("所有元素:" + range.toString());
System.out.println("=========[删除指定数量的某元素]=========");
redisTemplate.opsForList().remove("list-java-1", 2, "Mike");
range = redisTemplate.opsForList().range("list-java-1", 0, -1);
System.out.println("元素数量:" + redisTemplate.opsForList().size("list-java-1") + ", 所有元素:" + range.toString());
System.out.println("=========[删除全部元素]=========");
redisTemplate.opsForList().trim("list-java-1", 0, 0);
redisTemplate.opsForList().leftPop("list-java-1");
range = redisTemplate.opsForList().range("list-java-1", 0, -1);
System.out.println("元素数量:" + redisTemplate.opsForList().size("list-java-1") + ", 所有元素:" + range.toString());
}
2.6 Set 集合类型方法
public void SetMethod(){
System.out.println("=========[添加元素]=========");
redisTemplate.opsForSet().add("set-java-1", "Mike", "Peter", "Jack", "Mike", "Smith");
redisTemplate.opsForSet().add("set-java-2", "Peter", "Jordan", "Amy", "Smith", "Wof");
System.out.println("=========[查询元素]=========");
Set<String> members = redisTemplate.opsForSet().members("set-java-1");
Set<String> members2 = redisTemplate.opsForSet().members("set-java-2");
System.out.println("元素数量:" + redisTemplate.opsForSet().size("set-java-1") + ", set-java-1 的所有元素:" + members.toString());
System.out.println("元素数量:" + redisTemplate.opsForSet().size("set-java-2") + ", set-java-2 的所有元素:" + members2.toString());
System.out.println("=========[判断元素是否在 set 中]=========");
System.out.println(redisTemplate.opsForSet().isMember("set-java-1", "Mike")? "Mike 存在": "Mike 不存在");
System.out.println(redisTemplate.opsForSet().isMember("set-java-2", "Jorge")? "Jorge 存在": "Jorge 不存在");
System.out.println("=========[交集,并集,差集的计算]=========");
Set<String> intersect = redisTemplate.opsForSet().intersect("set-java-1", "set-java-2");
Set<String> union = redisTemplate.opsForSet().union("set-java-1", "set-java-2");
Set<String> difference = redisTemplate.opsForSet().difference("set-java-1", "set-java-2");
System.out.println("交集:" + intersect.toString());
System.out.println("并集:" + union.toString());
System.out.println("差集:" + difference.toString());
System.out.println("=========[删除元素]=========");
redisTemplate.opsForSet().remove("set-java-1", "Mike");
redisTemplate.opsForSet().move("set-java-1", "set-java-2", "Jack");
redisTemplate.opsForSet().pop("set-java-1", redisTemplate.opsForSet().size("set-java-1"));
redisTemplate.opsForSet().pop("set-java-2", redisTemplate.opsForSet().size("set-java-2"));
}
2.7 Sort Set 类型方法(有序集合)
public void SortSetMethod(){
System.out.println("=========[添加元素]=========");
redisTemplate.opsForZSet().add("sortset-java-1", "不及格", 59.0);
redisTemplate.opsForZSet().add("sortset-java-1", "及格", 69.0);
redisTemplate.opsForZSet().add("sortset-java-1", "中等", 79.0);
redisTemplate.opsForZSet().add("sortset-java-1", "良好", 89.0);
redisTemplate.opsForZSet().add("sortset-java-1", "优秀", 99.0);
redisTemplate.opsForZSet().add("sortset-java-1", "满分", 100.0);
System.out.println("=========[查询元素]=========");
System.out.println("元素总数为:" + redisTemplate.opsForZSet().size("sortset-java-1"));
System.out.println("优秀分数为:" + redisTemplate.opsForZSet().score("sortset-java-1", "优秀"));
System.out.println("及格人数为:" + redisTemplate.opsForZSet().count("sortset-java-1", 60, 100) + ", 上面的档次有:" + redisTemplate.opsForZSet().rangeByScore("sortset-java-1", 60, 100).toString());
System.out.println("中等人数为:" + redisTemplate.opsForZSet().count("sortset-java-1", 70, 100) + ", 上面的档次有:" + redisTemplate.opsForZSet().rangeByScore("sortset-java-1", 70, 100).toString());
System.out.println("良好人数为:" + redisTemplate.opsForZSet().count("sortset-java-1", 80, 100) + ", 上面的档次有:" + redisTemplate.opsForZSet().rangeByScore("sortset-java-1", 80, 100).toString());
System.out.println("优秀人数为:" + redisTemplate.opsForZSet().count("sortset-java-1", 90, 100) + ", 上面的档次有:" + redisTemplate.opsForZSet().rangeByScore("sortset-java-1", 90, 100).toString());
System.out.println("=========[删除元素]=========");
redisTemplate.opsForZSet().remove("sortset-java-1", "不及格");
redisTemplate.opsForZSet().removeRangeByScore("sortset-java-1", 60, 100);
}
2.8 Hash 类型方法
public void HashMethod(){
List<User> userList = new LinkedList<>();
userList.add(new User(2022090101, "Mike", 21, "Beijing"));
userList.add(new User(2022090102, "Peter", 24, "Shanghai"));
userList.add(new User(2022090103, "Jack", 22, "Shenzhen"));
userList.add(new User(2022090104, "云梦", 22, "Dalian"));
userList.add(new User(2022090105, "归遥", 20, "Hangzhou"));
Map<String, String> userMap = new LinkedHashMap<>();
for (User user: userList){
userMap.put(String.valueOf(user.getId()), JSON.toJSONString(user));
}
System.out.println("============[增加数据(进行序列化)]===========");
redisTemplate.opsForHash().putAll("hash-java-1", userMap);
System.out.println("============[获取所有的键,值]===========");
Set<Object> keys = redisTemplate.opsForHash().keys("hash-java-1");
List<Object> values = redisTemplate.opsForHash().values("hash-java-1");
System.out.println("所有的键:" + keys.toString());
System.out.println("所有的值:" + values.toString());
System.out.println("============[判断某个键是否存在]===========");
Boolean aBoolean = redisTemplate.opsForHash().hasKey("hash-java-1", "2022090101");
System.out.println("2022090101 是否存在:" + aBoolean);
aBoolean = redisTemplate.opsForHash().hasKey("hash-java-1", "2022090109");
System.out.println("2022090109 是否存在:" + aBoolean);
System.out.println("============[获取并修改某个键对应的值]===========");
if (redisTemplate.opsForHash().hasKey("hash-java-1", "2022090101")){
String userString = (String)redisTemplate.opsForHash().get("hash-java-1", "2022090101");
User user = JSON.parseObject(userString, User.class);
System.out.println("2022090101 对应的数据为:" + user.toString());
user.setAge(30);
user.setAddress("Guangzhou");
redisTemplate.opsForHash().put("hash-java-1", "2022090101", JSON.toJSONString(user));
} else {
System.out.println("2022090101 对应的数据为 null");
}
System.out.println("============[获取所有键值对]===========");
Map<Object, Object> entries = redisTemplate.opsForHash().entries("hash-java-1");
System.out.println("元素数量:" + entries.size());
Set<Map.Entry<Object, Object>> entries1 = entries.entrySet();
for (Map.Entry<Object, Object> entry: entries1){
System.out.println("键:" + entry.getKey() + ", 值:" + entry.getValue());
}
System.out.println("============[删除元素]===========");
redisTemplate.opsForHash().keys("hash-java-1").forEach(key -> redisTemplate.opsForHash().delete("hash-java-1", key));
}
2.9 事务
public void MultiMethod(){
redisTemplate.opsForValue().set("Mike", "100");
redisTemplate.opsForValue().set("Jack", "100");
int transfer = 20;
String mike = redisTemplate.opsForValue().get("Mike");
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.watch(List.of("Mike", "Jack"));
redisTemplate.multi();
try{
if (Integer.parseInt(mike) >= transfer){
redisTemplate.opsForValue().decrement("Mike", (long) transfer);
redisTemplate.opsForValue().increment("Jack", (long) transfer);
redisTemplate.exec();
System.out.println("双方转账成功");
}else {
System.out.println("Mike 账户余额不足,转账失败");
}
}catch (Exception e){
System.out.println("转账过程中出现异常:" + e.getMessage());
redisTemplate.discard();
} finally {
System.out.println("转账后双方余额:Mike:" + redisTemplate.opsForValue().get("Mike") + ", Jack: " + redisTemplate.opsForValue().get("Jack"));
redisTemplate.delete(List.of("Mike", "Jack"));
}
}
3.总结
以上就是 RedisTemplate 操作 Redis 的相关代码
|