SpringBoot集成Redis以及常用操作
1、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
2、application.yml配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/cms?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
username: root
password: 123456
redis:
host: 192.168.21.139
port: 6379
password: 123456
jedis:
pool:
max-active: 20
max-idle: 8
min-idle: 0
max-wait: 2000
3、单元测试
package com.chengya.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisListCommands;
import org.springframework.data.redis.core.*;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void redisKeyTest() {
// 查找指定格式的key,比如查找所有以"test"结尾的key
Set<String> keys = stringRedisTemplate.keys("*test");
// 设置key的过期时间,比如设置key在10小时后过期
stringRedisTemplate.expire("key_01",10,TimeUnit.HOURS);
stringRedisTemplate.expire("key_01",Duration.ofHours(10));
// 设置key在指定的时间过期
stringRedisTemplate.expireAt("key_01",new Date());
// 查看key的剩余过期时间,单位分钟
Long expireSeconds = stringRedisTemplate.getExpire("key_01",TimeUnit.SECONDS);
// 删除指定的key
stringRedisTemplate.delete("key_01");
// 批量删除指定的key
List<String> keyList = Arrays.asList("key_01","key_02");
stringRedisTemplate.delete(keyList);
// 重命名key
stringRedisTemplate.rename("old_key","new_key");
// 如果新的key不存在,则重命名
stringRedisTemplate.renameIfAbsent("old_key","new_key");
// 返回任意的key
String key = stringRedisTemplate.randomKey();
}
@Test
void redisStringTest() {
ValueOperations<String,String> valueOperations = stringRedisTemplate.opsForValue();
// 设置key的值
valueOperations.set("string_01","value_01");
// 将值追加到指定key值的末尾
valueOperations.append("string_01","_value_02");
// 获取key的值
String str01 = valueOperations.get("string_01");
// 设置key-value,并且设置超时时间为1000毫秒
valueOperations.set("string_01","value_01",1000,TimeUnit.MILLISECONDS);
// 设置key-value,并且设置超时时间为1000毫秒
valueOperations.set("string_01","value_01",Duration.ofMillis(1000));
}
@Test
void redisHashMapTest() {
HashOperations<String,String,String> hashOperations = stringRedisTemplate.opsForHash();
Map<String,String> map = new HashMap<>();
map.put("field_01","value_01");
map.put("field_02","value_02");
map.put("field_03","value_03");
// 把整个map放进redis
hashOperations.putAll("hashMap_01",map);
// 为指定的map设置字段
hashOperations.put("hashMap_01","field_04","value_04");
// 如果map的字段不存在,则设置字段的值
hashOperations.putIfAbsent("hashMap_01","field_05","value_05");
// 删除map的部分字段
hashOperations.delete("hashMap_01","field_01","field_02");
// 返回哈希表中指定key的值
hashOperations.get("hashMap_01","field_04");
// 返回哈希表的所有key
Set<String> keys = hashOperations.keys("hashMap_01");
// 返回哈希表的所有value
List<String> values = hashOperations.values("hashMap_01");
// 返回哈希表的所有键值对
Map<String,String> redisMap = hashOperations.entries("hashMap_01");
// 判断哈希表中是否存在指定的key
hashOperations.hasKey("hashMap_01","field_01");
// 返回哈希表中的任意一对键值对
Map.Entry<String,String> entry = hashOperations.randomEntry("hashMap_01");
// 返回哈希表中的任意三对键值对
hashOperations.randomEntries("hashMap_01",3);
// 返回哈希表中的任意一个key
hashOperations.randomKey("hashMap_01");
// 返回哈希表中的任意三个key
hashOperations.randomKeys("hashMap_01",3);
}
/**
* Redis列表是有序的
*/
@Test
void redisListTest() {
ListOperations<String,String> listOperations = stringRedisTemplate.opsForList();
// 将多个值放入list
listOperations.leftPushAll("list_01","value_01","value_02","value_03");
// 将整个集合放入list
List<String> list = Arrays.asList("value_04","value_05","value_06");
listOperations.leftPushAll("list_01",list);
// 向list添加一个元素
listOperations.leftPush("list_01","value_07");
listOperations.rightPushAll("list_01","value_18","value_17");
listOperations.rightPushAll("list_01",list);
listOperations.rightPush("list_01","value_19");
// 截取list,这将改变list的大小
listOperations.trim("list_01",0,4);
// 返回list指定区间内的元素集
listOperations.range("list_01",0,4);
// 返回list的大小
listOperations.size("list_01");
// 返回list指定索引位置的元素
listOperations.index("list_01",2);
// 返回指定元素的索引位置
listOperations.indexOf("list_01","value_02");
// 弹出list的第一个元素
listOperations.leftPop("list_01");
// 弹出list的最后一个元素
listOperations.rightPop("list_01");
// 弹出list的第一个元素,并设置超时时间为1000毫秒
listOperations.leftPop("list_01",1000, TimeUnit.MILLISECONDS);
// 弹出list的最后一个元素,并设置超时时间为1000毫秒
listOperations.rightPop("list_01",1000,TimeUnit.MILLISECONDS);
// 从list头部弹出三个元素
listOperations.leftPop("list_01",3);
// 从list尾部弹出三个元素
listOperations.rightPop("list_01",3);
// 将list_01的头部元素移动到list_02的头部
listOperations.move(ListOperations.MoveFrom.fromHead("list_01"),ListOperations.MoveTo.toHead("list_02"));
// 将list_01的头部元素移动到list_02的头部,并设置超时时间为2秒
listOperations.move(ListOperations.MoveFrom.fromHead("list_01"),ListOperations.MoveTo.toHead("list_02"), Duration.ofSeconds(2));
// 将list_01的头部元素移动到list_02的尾部
listOperations.move("list_01", RedisListCommands.Direction.LEFT,"list_02", RedisListCommands.Direction.RIGHT);
// 移除指定的元素,可能有多个相同元素,将顺序匹配到的前count个元素移除
listOperations.remove("list_01",2,"value_03");
// 设置指定索引位置的元素
listOperations.set("list_01",2,"value_02");
}
/**
* Redis集合是无序的
*/
@Test
void redisSetTest() {
SetOperations<String,String> setOperations = stringRedisTemplate.opsForSet();
// 向集合中添加成员
setOperations.add("set_01","element_01","element_02","element_03");
// 判断element_01是否为集合set_01的成员
Boolean isMember = setOperations.isMember("set_01","element_01");
// 判断element_01,element_02是否为集合set_01的成员
Map<Object, Boolean> isMemberMap = setOperations.isMember("set_01","element_01","element_02");
// 获取set集合
Set<String> set = setOperations.members("set_01");
// 返回集合中的任意一个成员
String randomValue = setOperations.randomMember("set_01");
// 返回集合中的任意count个成员
List<String> randomSet = setOperations.randomMembers("set_01",3);
// 随机地弹出集合中的一个成员
String popValue = setOperations.pop("set_01");
// 随机地弹出集合中的多个成员
List<String> popValues = setOperations.pop("set_01",3);
// 将集合set_01中的元素element_01移动到集合set_02
Boolean isMove = setOperations.move("set_01","element_01","set_02");
// 返回集合的差集,即set_01里面有的,set_02里面没有的
Set<String> otherKeys = new HashSet<>();
otherKeys.add("set_02");
otherKeys.add("set_03");
otherKeys.add("set_04");
Set<String> diffSet = setOperations.difference("set_01","set_02");
// 返回集合的差集,即set_01里面有的,set_02里面没有的,并且将差集存为destination_set
setOperations.differenceAndStore("set_01","set_02","destination_set");
// 返回集合的差集,即set_01集合里面有的,其他集合里面没有的
Set<String> diffElements = setOperations.difference("set_01",otherKeys);
// 返回集合的差集,即set_01集合里面有的,其他集合里面没有的,并且将差集存为destination_set
setOperations.differenceAndStore("set_01",otherKeys,"destination_set");
// 返回集合的交集
Set<String> interSet = setOperations.intersect("set_01","set_02");
Set<String> interOtherSet = setOperations.intersect("set_01",otherKeys);
Set<String> interOthers = setOperations.intersect(otherKeys);
// 返回集合的交集,并将交集存为destination_set
setOperations.intersectAndStore("set_01","set_02","destination_set");
setOperations.intersectAndStore("set_01",otherKeys,"destination_set");
setOperations.intersectAndStore(otherKeys,"destination_set");
// 返回集合的并集
Set<String> unionSet = setOperations.union("set_01","set_02");
Set<String> unionOtherSet = setOperations.union("set_01",otherKeys);
Set<String> unionOthers = setOperations.union(otherKeys);
// 返回集合的并集,并将并集存为destination_set
setOperations.unionAndStore("set_01","set_02","destination");
setOperations.unionAndStore("set_01",otherKeys,"destination");
setOperations.unionAndStore(otherKeys,"destination");
}
}