【视频 & 交流平台】
http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
https://gitee.com/happyangellxq520/spring-boot
http://412887952-qq-com.iteye.com/blog/2321532
网易云课堂视频最新更新:
第十一章 Spring Boot 日志
1、spring boot日志—理论
2、Spring Boot日志-logback
3、Spring Boot日志-log4j2
第十二章 Spring Boot 知识点2
1、spring boot 服务配置和部署
2、Spring Boot 定制URL匹配规则
需求缘起:
37. Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】
在这篇博客中,有一网友留言:
纤哥,ehcache那一篇,关于cacheput的说明,是不是有误?博客说cacheput标注的方法执行前不会去检查缓存中是否存在之前执行结果,每次都执行该方法。我测试下来cacheput也是去检查的,如果缓存有了,不会执行了
对于技术,解决困惑最好的方法就是测试:
准备工作:
我们这里有两个方法:
@Cacheable(value=DEMO_CACHE_NAME,key="'demoInfo_'+#id") @Override public DemoInfo findById(Long id){ System.err.println("findById-->没有走缓存!"+id); return demoInfoRepository.findOne(id); } @CachePut(value=DEMO_CACHE_NAME,key="'demoInfo_'+#id") @Override public DemoInfo findById2(Long id){ System.err.println("findById2-->没有走缓存!"+id); return demoInfoRepository.findOne(id); }
在Controller相应的添加两个请求:
@RequestMapping("/findById1") public DemoInfo findById1(long id){ System.out.println("findById1请求-->id="+id); return demoInfoService.findById(id); } @RequestMapping("/findById2") public DemoInfo findById2(long id){ System.out.println("findById2请求-->id="+id); return demoInfoService.findById2(id); }
测试验证
(1)验证1:findById1走缓存,findById2不走缓存
操作步骤:
(b)访问2次:http://127.0.0.1:8080/findById1?id=57
(c)查看控制台的打印:
findById-->没有走缓存!57
Hibernate: select demoinfo0_.id as id1_0_0_, demoinfo0_.name as name2_0_0_, demoinfo0_.pwd as pwd3_0_0_, demoinfo0_.state as state4_0_0_ from demo_info demoinfo0_ where demoinfo0_.id=?
第二次请求:findById1请求-->id=57
从这里可以看出第一次数据是从数据库获取,然后进行缓存的;第二次是直接从缓存中进行获取的。
(e)访问1次:http://127.0.0.1:8080/findById2?id=58
(f)访问2次:http://127.0.0.1:8080/findById2?id=58
(g)观察控制台的打印信息:
第一次请求:findById2请求-->id=58
findById2-->没有走缓存!58
Hibernate: select demoinfo0_.id as id1_0_0_, demoinfo0_.name as name2_0_0_, demoinfo0_.pwd as pwd3_0_0_, demoinfo0_.state as state4_0_0_ from demo_info demoinfo0_ where demoinfo0_.id=?
第二次请求:findById2请求-->id=58
findById2-->没有走缓存!58
Hibernate: select demoinfo0_.id as id1_0_0_, demoinfo0_.name as name2_0_0_, demoinfo0_.pwd as pwd3_0_0_, demoinfo0_.state as state4_0_0_ from demo_info demoinfo0_ where demoinfo0_.id=?
从这里可以看出两次都是从数据库进行查询的。
(2)验证1:findById2执行结果能以键值对的形式存入指定的缓存中
操作步骤:
(a)重启应用程序
(b)访问1次:http://127.0.0.1:8080/findById2?id=57
(c)访问1次:http://127.0.0.1:8080/findById1?id=57
(d)查看控制台的打印信息:
请求1:findById2请求àid=57
findById2à没有走缓存!57
Hibernate: select demoinfo0_.id as id1_0_0_, demoinfo0_.name as name2_0_0_, demoinfo0_.pwd as pwd3_0_0_, demoinfo0_.state as state4_0_0_ from demo_info demoinfo0_ where demoinfo0_.id=?
请求2:findById1请求-->id=57
结论
(1)对于使用@Cacheable标注的方法,Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。
(2)@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
过程可以忽略,结论大家要记住哦。