SpringBoot整合redis(二)
SpringBoot+redis+@Cache
准备工作:
1、理解springboot缓存机制
链接:https://blog.csdn.net/SuchASilly/article/details/106102893
2、springboot整合redis的配置参考
链接:https://blog.csdn.net/SuchASilly/article/details/106103069
一、@EanblCache
这个注解要到Application启动类标明(开启缓存注解)
package com.jinv.studentinfo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @EnableCaching//启动缓存注解 @SpringBootApplication @MapperScan("com.jinv.studentinfo.dao")/*配置MapperScan来扫描dao(Mapper)接口*/ public class StudentinfoApplication { public static void main(String[] args) { SpringApplication.run(StudentinfoApplication.class, args); } }
二、serviceImpl(service层正常写)
package com.jinv.studentinfo.service.impl; import com.jinv.studentinfo.dao.CourseDao; import com.jinv.studentinfo.domain.Course; import com.jinv.studentinfo.service.CourseService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class CourseServiceImpl implements CourseService { @Autowired private CourseDao courseDao; @Override @Cacheable(value = "course",key = "#courseId") public Course findCourseById(String courseId) { return courseDao.findCourseById(courseId); } @Override @CachePut(value = "course",key = "#result.courseId",unless = "#result==null") public Course updateCourse(Course course) { courseDao.updateCourse(course); return courseDao.findCourseById(course.getCourseId()); } @Override @CacheEvict(value="course",key = "#courseId") public void deleteCourseById(String courseId) { courseDao.deleteCourseById(courseId); } }
注意:这里有个坑——更新的语句,我本来写的返回值是void空,这样子就会报错
Cache ‘course’ does not allow ‘null’ values. Avoid storing null via ‘@Cacheable(unless="#result == null")’ or configure RedisCache to allow ‘null’ via RedisCacheConfiguration
原因分析:
因为@CachePut其实就是@CacheEvict和@Cacheable的联合,先清空再添加缓存达到更新效果,而且这个注解的参数就是指向返回值,所以返回值为空的话,就会找不到对应的keyvalue来执行@Cacheable
@CachePut 注释,这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中,实现缓存与数据库的同步更新。
三、web
package com.jinv.studentinfo.web; import com.alibaba.fastjson.JSON; import com.jinv.studentinfo.domain.Course; import com.jinv.studentinfo.service.CourseService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class CourseController { @Autowired private CourseService courseService; @GetMapping("/testRedis/find/{courseId}") public String findCourse(@PathVariable String courseId){ Course course = courseService.findCourseById(courseId); String result = JSON.toJSONString(course); return result; } @GetMapping("/testRedis/update/{courseName}") public String updateCourse(@PathVariable String courseName){ Course course = courseService.findCourseById("1"); course.setCourseName(courseName); courseService.updateCourse(course); return "成功!"; } @GetMapping("/testRedis/delete/{courseId}") public String deleteCourse(@PathVariable String courseId){ courseService.deleteCourseById(courseId); return "萨达"; } }
四、启动测试
1.测试@Cacheable(增)
查看redis
2.测试@CachePut(改)
redis
mysql
3.测试@CacheEvict(删)
redis
mysql
五、关于怎么才能让springboot项目不选用simpleCacheAutoConfiguration而是用Redis的Configuration
这个细节我也不清楚,应该是RedisConfig的配置使得cache的自动配置选择redis条件condition达到了就不会选择默认的,详情还是要看springboot缓存机制
总结:使用@CachePut的目标方法时update数据库的,而且必须有返回值,返回更新后的实体类
- springboot整合redis报错
- springboot 整合 redis 主从同步 sentinel哨兵 实现商品抢购秒杀
- SpringBoot2.x系列教程50--NoSQL之SpringBoot整合Redis
- Spring Boot整合redis笔记
- SpringBoot整合Redis集群
- springBoot整合redis
- SpringBoot进阶教程(二十九)整合Redis 发布订阅
- spring boot整合redis
- spring boot整合redis
- Springboot 使用 Jedis、RedisTemplate 整合 Redis 实现并发锁
- SpringBoot(六):SpringBoot整合Redis
- SpringBoot 2.x版本整合Redis集群
- Spring Boot (二) 整合 Redis
- Spring Boot 2.x整合Redis
- Redis 和 Springboot基础整合
- spring-boot整合redis 总结
- SpringBoot整合Redis实现一套CRUD的简单缓存
- springboot整合redis(入门)
- spring boot中spring cache 整合redis
- (七)Spring Boot 整合Redis