• redis的安装

    在笔者之前的文章中有介绍redis的安装,不会的可以去看 笔者之前写的文章redis安装
  • 完成安装后如果不熟悉redis的操作,redis官方文档也有基本操作指南,redis基本操作,如果觉得没问题了就可以开始对redis的整合
  1. maven安装依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

redis自动会吧cache的依赖带过来,所有不用配置,如图

  1. 启动类增加@EnableCaching 注解
@SpringBootApplication
@MapperScan("com.tanoak.mapper")
@EnableCaching
public class BootRedisApplication {
public static void main(String[] args) {
SpringApplication.run(BootRedisApplication.class, args);
}
}
  1. service层增加@Cacheable 注解
@Override
@Cacheable(cacheNames= "tea")
public Teacher getTeaById(Integer id) {
logger.info("进行查询实体 ID为"+id);
return teacherMapper.getTeaById(id) ;
}
  1. controller 查询
@GetMapping("/tea/{id}")
public Teacher getTea(@PathVariable("id")Integer id){
return teacherService.getTeaById(id) ;
}

RedisCacheManager 配置

在SpringBoot2.x中,移除了1.x中的配置,因此要配置Json序列化与1.x的差别很大,看代码


@Configuration
@EnableCaching
public class MyRedisConfig extends CachingConfigurerSupport { /*
*自定义键生成策略
*/
@Bean
public KeyGenerator KeyGenerator() {
return (target, method, params) -> {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
};
} @Bean
public RedisCacheConfiguration redisCacheConfiguration() {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(jackson2JsonRedisSerializer)
//设置默认超过期时间是30秒
).entryTtl(Duration.ofMinutes(30)); return redisCacheConfiguration;
} }



没有打印sql,说明缓存成功,与redis集成就完成了

最新文章

  1. 简单递推 HDU-2108
  2. poj 1236 Network of Schools(连通图)
  3. ABAP 指針常用语法
  4. Html-Css-设置DIV边框圆滑
  5. C编译错误解决方法
  6. hibernate添加数据,默认字段为null的问题解决
  7. [React] React Fundamentals: Integrating Components with D3 and AngularJS
  8. localStorage点击次数存储
  9. 你也可以玩转Skype -- 基于Skype API开发外壳程序入门
  10. PHP 中 static 和 self 的区别
  11. 定期清空log文件
  12. 笔记:Spring Cloud Eureka 服务发现与消费
  13. 不规则的JSON解析(一)
  14. Linux系统组成
  15. js 弹窗广告24小时显示一次
  16. checkbox选择
  17. asp.net core 发布到docker 极简步骤
  18. MJRefresh原理分析
  19. [转]ASP.NET MVC 5 - 验证编辑方法(Edit method)和编辑视图(Edit view)
  20. 大型发布会现场的 Wi-Fi 应该如何搭建(密集人群部署wifi抗干扰)?

热门文章

  1. 1.7 Systemd初始化进程
  2. shell基础之bus实战(if 练习)
  3. Linux进阶之排错
  4. Windows上能看朋友圈的微信来了 | 附下载地址
  5. Python数模笔记-StatsModels 统计回归(4)可视化
  6. 【补档_STM32单片机】脉搏波采集显示硬件设计
  7. Go语言web开发---Beego的session
  8. RMAN-20208: UNTIL CHANGE is before RESETLOGS change
  9. 定位服务API案例
  10. .NET平台系列24:从.NET Framework迁移到.NET Core/.NET5的技术指南