Java注解@Cacheable的工作原理-阿里云开发者社区

开发者社区> 开发者小助手-bz4> 正文

Java注解@Cacheable的工作原理

简介: Java注解@Cacheable的工作原理
+关注继续查看

n order to avoid unnecessary query on database it is a common pattern to define a cache in application layer to cache the query result from database. See one example below. Here the application cache is maintained in a custom class CacheContext.

public class AccountService1 {

   private final Logger logger = LoggerFactory.getLogger(AccountService1.class);

   

   private CacheContext accountCacheContext;

 

   public Account getAccountByName(String accountName) {

       Account result = accountCacheContext.get(accountName);

       if (result != null) {

           logger.info("get from cache... {}", accountName);

           return result;

       }

 

       Optional accountOptional = getFromDB(accountName);

       if (!accountOptional.isPresent()) {

           throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));

       }

 

       Account account = accountOptional.get();

       accountCacheContext.addOrUpdateCache(accountName, account);

       return account;

   }In Spring there is an annotation @Cacheable which can make the cache managed by Spring instead of application developer. See improved version:

public class AccountService2 {

 

   private final Logger logger = LoggerFactory.getLogger(AccountService2.class);

   @Cacheable(value="accountCache")

   public Account getAccountByName(String accountName) {

       logger.info("in method getAccountByName, querying account... {}", accountName);

       Optional accountOptional = getFromDB(accountName);

       if (!accountOptional.isPresent()) {

           throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));

       }

       return accountOptional.get();

   }In this example, there is no more cache evaluation and cache fill logic. All such stuff are taken over by Spring under the hood and completely transparent to application developer, with the help of following bean configuration in xml:

 

 

   

    class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">

   

   

   

    class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">

   

   

 

 

And how to research what magic has been done by Spring to achieve this behavior?

We use the following code to research. It is expected that the request sent by line 31 will directly reach database, while the second request in line 34 will be handled by Spring cache handler.



image.png

image.png

Here in line 31, in debugger we can find that accountService2 is not an instance of application class com.sap.AccountService2, but a dynamic proxy class generated by Spring.



image.png

image.pngAs a result after pressing F5, the intercept method of this dynamic proxy class is called:image.pngIn this method, the execution will be delegated to Spring cache handler class:image.pngIn example 1, the logic of cache evaluation and fill is done by application, and now it is done in method execute below.image.png

First internal cache managed by Spring is checked in line 336 via method findCachedItem. Due to expected cache miss, our application method will be called as usual via reflection, as demonstrated below:



image.png

After application method to retrieve account from database is done, the result, Account instance with id 2495 is filled to Spring cache, the variable contexts below.


image.png

Below is the screenshot for Spring internal cache to store application data, which is based on ConcurrentHashMap. Our cached Account instance with id 2495 could be found there.


image.png

image.pngFor the second query request issued by application, the cached result will be returned by Spring handler:image.pngThe last question is, how and when the dynamic proxy is generated?

Again let’s go to the entry point of Beans initialization:

image.pngHere the dynamic proxy is created based on configuration defined in xml with the help of CGlibAopProxy. For more detail about CGlibAopProxy, please refer to Spring official document.



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
8659 0
Java中动态代理工作流程
  当谈到动态代理就会想到接口,因为接口是一种规范,动态代理对象通过接口便会很清楚地知道他的实现类(被代理对象)是何种类型的(即有哪些方法)。Now,然我们来开始编写一个例子来了解动态代理的全过程: 第一:既然接口那么好使,就先定义一个接口Action package com.lzj.Spring_first.testAgentMyPractice; public interface Action { // 定义方法 public void say(); public void doSomething(); } 第二:既然有了接口,接下来咱们就得有实现类。
747 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
10489 0
javax.ws.rs.Path注解@Path的工作原理解析
javax.ws.rs.Path注解@Path的工作原理解析
8 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
12303 0
关于找工作中Java问题
这个也是一点点更新哈,不会一次写完。 String 和StringBuffer的区别 JAVA平台提供了两个类:String和StringBuffer,它们可以储存和操作字符串,即包含多个字符的字符数据这个String类提供了数值不可改变字符串而这个StringBuffer类提供的字符串进行修改当你知道字符数据要改变的时候你就可以使用StringBuffer典型地,你可以
1011 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
11438 0
2315
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载