目录
1、先测试一下StringRedisTemplate,即键值都为string类型
2、再测试一下RedisTemplate,可以用于存放对象,object>
一、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、配置文件
# redis配置
spring:
redis:
host: xxx.xxx.xxx.xxx
port: 6379
password: 123456
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 500
min-idle: 0
lettuce:
shutdown-timeout: 0
三、测试
1、先测试一下StringRedisTemplate,即键值都为string类型
@Autowired
StringRedisTemplate redisTemplate;
@Test
public void set(){
// 插入c,值为123
redisTemplate.opsForValue().set("c","123");
// 打印c
System.err.println(redisTemplate.opsForValue().get("c"));
// 删除c
redisTemplate.delete("c");
// 检查是否有c
System.err.println(redisTemplate.hasKey("c"));
}
2、再测试一下RedisTemplate<Object,Object>,可以用于存放对象
编写一个student学生类
@Data
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
String name = "小明";
int age = 15;
}
写一个service类
@Service
public class RedisService {
@Resource
RedisTemplate<Object,Object> template;
@PostConstruct
public void init() {
template.setEnableTransactionSupport(true); // 需要开启事务
}
@Transactional // 依旧需要开启事务注解
public void test(){
template.multi();
template.opsForValue().set("f",new Student());
template.exec();
}
}
到测试类里进行测试
@Autowired
RedisService redisService;
@Test
public void set(){
redisService.test();
System.out.println("值为"+redisTemplate.opsForValue().get("f"));
}
运行测试,进入可视化工具查看:
这是jdk自带的序列方法,也可以使用json序列方法
将service层里的开启事务换成
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
@PostConstruct
public void init() {
//template.setEnableTransactionSupport(true); // 需要开启事务
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
}
结果为:
四、缓存配置类
实现Cache,并重写各个方法
//实现Mybatis的Cache接口
public class MybatisRedisCache implements Cache {
private final String id;
private static RedisTemplate<Object, Object> template;
//注意构造方法必须带一个String类型的参数接收id
public MybatisRedisCache(String id){
this.id = id;
}
//初始化时通过配置类将RedisTemplate给过来
public static void setTemplate(RedisTemplate<Object, Object> template) {
MybatisRedisCache.template = template;
}
@Override
public String getId() {
return id;
}
@Override
public void putObject(Object o, Object o1) {
//这里直接向Redis数据库中丢数据即可,o就是Key,o1就是Value,60秒为过期时间
template.opsForValue().set(o, o1, 60, TimeUnit.SECONDS);
}
@Override
public Object getObject(Object o) {
//这里根据Key直接从Redis数据库中获取值即可
return template.opsForValue().get(o);
}
@Override
public Object removeObject(Object o) {
//根据Key删除
return template.delete(o);
}
@Override
public void clear() {
//由于template中没封装清除操作,只能通过connection来执行
template.execute((RedisCallback<Void>) connection -> {
//通过connection对象执行清空操作
connection.flushDb();
return null;
});
}
@Override
public int getSize() {
//这里也是使用connection对象来获取当前的Key数量
return template.execute(RedisServerCommands::dbSize).intValue();
}
}
五、Mybatis二级缓存配置类
@Configuration
public class MybatisRedisConfig {
@Autowired
RedisTemplate<Object, Object> template;
@PostConstruct
public void init(){
//把RedisTemplate给到RedisMybatisCache
MybatisRedisCache.setTemplate(template);
}
}
六、mapper文件
编写一个mapper文件,用于查询数据库
@Mapper
@CacheNamespace(implementation = MybatisRedisCache.class)
public interface AccountMapper extends BaseMapper<Account>{
@Select("select oper_no from t_inner_oper where oper_no = 1 ")
String getR();
}
七、测试mapper接口
连续调用三次后,去redis可视化工具里查看,是可以发现有缓存的,缓存时间为设定的一分钟
@Autowired
AccountMapper accountMapper;
@Test
public void set(){
// 连续调用三次
accountMapper.getR();
accountMapper.getR();
accountMapper.getR();
}
可视化工具结果: