157. Description of Spring Boot @cacheput [Learn Spring Boot from scratch]

 

【Video & Communication Platform】

à SpringBoot Video 

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à  SpringCloud Video

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot source code 

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot communication platform 

http://412887952-qq-com.iteye.com/blog/2321532

 

The latest update of NetEase Cloud Classroom video :

Chapter 11 Spring Boot Logging

1, spring boot log - theory

2. Spring Boot log-logback

3. Spring Boot log-log4j2

Chapter 12 Spring Boot Knowledge Point 2

1. Spring boot service configuration and deployment

2. Spring Boot custom URL matching rules

 

 

Origin of need:

       37. Spring Boot integrates EHCache to implement caching mechanism [Learn Spring Boot from scratch ]

In this blog, a netizen commented:

Brother Xian, in the ehcache article, is the description about cacheput wrong? The blog said that the method marked by cacheput will not check whether the previous execution result exists in the cache before execution, and the method is executed every time. I tested the cacheput to check, if there is a cache, it will not be executed.

       For technology, the best way to resolve confusion is to test:

 

Ready to work:

We have two methods here:

    @Cacheable(value=DEMO_CACHE_NAME,key="'demoInfo_'+#id")
    @Override
    public DemoInfo findById(Long id){
       System.err.println("findById-->No cache!"+id);
       return demoInfoRepository.findOne(id);
    }
    @CachePut(value=DEMO_CACHE_NAME,key="'demoInfo_'+#id")
    @Override
    public DemoInfo findById2(Long id){
       System.err.println("findById2-->No cache!"+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)验证1findById1走缓存,findById2不走缓存

操作步骤:

(a)访问1次:http://127.0.0.1:8080/findById1?id=57
(b)访问2次:http://127.0.0.1:8080/findById1?id=57
(c)查看控制台的打印:
第一次请求:findById1请求-->id=57
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
从这里可以看出第一次数据是从数据库获取,然后进行缓存的;第二次是直接从缓存中进行获取的。
(d)清空控制台信息
(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)验证1findById2执行结果能以键值对的形式存入指定的缓存中

操作步骤:

a)重启应用程序

b)访问1次:http://127.0.0.1:8080/findById2?id=57

c)访问1次:http://127.0.0.1:8080/findById1?id=57

d)查看控制台的打印信息:

请求1findById2请求à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=?

请求2findById1请求-->id=57

 

结论

1)对于使用@Cacheable标注的方法,Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。

2@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中

       过程可以忽略,结论大家要记住哦。

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326719830&siteId=291194637