@Cacheable的实现原理
如果你用过Spring Cache,你一定对这种配置和代码不陌生:
<cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true" order="1" /> |
@Cacheable(value = "3600", key = "i'm a cache key")public List<Object> getData(){} |
上面两段代码,xml是启用Cache Annotation注解并注册一个cacheManager,第二段代码在getData的时候会先去缓存里取,如果缓存没有再执行getData的真实逻辑。
那么今天的“走进科学”讲的就是Spring是怎么做到仅仅一段xml配置和一个注解就实现方法级别的自动缓存。
我们滋道Spring最牛逼的地方就在于IOC容器对于bean的管理,可以说是Spring牛逼的基石,那么画风一换,到了我们今天讨论的起点就是Spring在启动时对xml里面的各种标签进行解析,比如对应<cache:annotation-driven>标签,负责解析的就是AnnotationDrivenCacheBeanDefinitionParser.parse方法,代码看起来很简单,根据mode属性注册Advisor Component:
我们今天先看默认mode=proxy的情况,进入方法,发现方法里面注册了三个Bean到Context里面,分别是CacheOperationSource、CacheInterceptor和BeanFactoryCacheOperationSourceAdvisor。
熟悉AOP原理的看到Interceptor和Advisor一般都会明白大半了,并且他们共同都有一个属性cacheOperationSources,实现类是org.springframework.cache.annotation.AnnotationCacheOperationSource。
下面我们先来喵两眼这两个类,先看BeanFactoryCacheOperationSourceAdvisor,里面有一个叫CacheOperationSourcePointcut的pointcut,用来匹配方法是否需要走拦截器。通过调用之前注入进去的cacheOperationSources.getCacheOperations获取CacheOperation,代码如下:
这样只有被CacheOperationSourcePointcut匹配的方法才会被拦截,并且通过attributeCache做了缓存。
再来看CacheInterceptor类,先看一眼继承结构:

这个类很简单,只是重写了MethodInterceptor的invoke方法:
下一步是调用CacheAspectSupport
protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args) { // check whether aspect is enabled // to cope with cases where the AJ is pulled in automatically if (this.initialized) { Class<?> targetClass = getTargetClass(target); Collection<CacheOperation> operations = getCacheOperationSource().getCacheOperations(method, targetClass); if (!CollectionUtils.isEmpty(operations)) { return execute(invoker, new CacheOperationContexts(operations, method, args, target, targetClass)); } } return invoker.invoke();} |
其中根据 getCacheOperations获得cacheOperations后调用的execute是关键,其中getCacheOperationSource即是之前说到的bean里面的cacheOperationSources,也就是org.springframework.cache.annotation.AnnotationCacheOperationSource,它负责三个标签的调用:@Cacheable、@CachePut和@CacheEvict。
下面喽一眼execute方法的代码:
private Object execute(CacheOperationInvoker invoker, CacheOperationContexts contexts) { // Process any early evictions processCacheEvicts(contexts.get(CacheEvictOperation.class), true, ExpressionEvaluator.NO_RESULT); // Check if we have a cached item matching the conditions Cache.ValueWrapper cacheHit = findCachedItem(contexts.get(CacheableOperation.class)); // Collect puts from any @Cacheable miss, if no cached item is found List<CachePutRequest> cachePutRequests = new LinkedList<CachePutRequest>(); if (cacheHit == null) { collectPutRequests(contexts.get(CacheableOperation.class), ExpressionEvaluator.NO_RESULT, cachePutRequests); } Cache.ValueWrapper result = null; // If there are no put requests, just use the cache hit if (cachePutRequests.isEmpty() && !hasCachePut(contexts)) { result = cacheHit; } // Invoke the method if don't have a cache hit if (result == null) { result = new SimpleValueWrapper(invokeOperation(invoker)); } // Collect any explicit @CachePuts collectPutRequests(contexts.get(CachePutOperation.class), result.get(), cachePutRequests); // Process any collected put requests, either from @CachePut or a @Cacheable miss for (CachePutRequest cachePutRequest : cachePutRequests) { cachePutRequest.apply(result.get()); } // Process any late evictions processCacheEvicts(contexts.get(CacheEvictOperation.class), false, result.get()); return result.get();} |
这段代码看起来还是比较“简单”的,也是Spring Cache逻辑的核心实现了吧,根据注解执行了方法前和方法后需要的缓存操作,注意对于失效的操作分为early evictions和late evictions,对应标签@CacheEvict中的beforeInvocation属性。自此,Spring cache的逻辑算是执行完毕。
还有两点需要注意的就是
- 上面的实现是通过proxy的形式实现,那么对象的方法是内部调用(即 this 引用)而不是外部引用,则会导致 proxy失效,也就是注解失效。
- 非public方法同上
- @CacheEvict标签不会对抛出异常的方法的缓存进行清空,通过将beforeInvocation设置为true,即在方法执行前
最后的话:
本篇文章并没有讲Spring的AOP实现原理以及Spring Cache的更多细节。
参考:
https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html
https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/
http://www.cnblogs.com/chanedi/p/4552555.html
原文发表于2015年10月16日
@Cacheable的实现原理的更多相关文章
- Hibernate缓存原理与策略
Hibernate缓存原理: 对于Hibernate这类ORM而言,缓存显的尤为重要,它是持久层性能提升的关键.简单来讲Hibernate就是对JDBC进行封装,以实现内部状态的管理,OR关系的映射等 ...
- Django admin 组件 原理分析与扩展使用 之 sites.py (一)
一 . 前言 Django 提供了admin 组件 为项目提供基本的管理后台功能(对数据表的增删改查). 本篇文章通过 admin源码 简单分析admin 内部原理 ,扩展使用方式,为以后进行定制和自 ...
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用(转)
原文地址:https://www.cnblogs.com/fashflying/p/6908028.html 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对 ...
- varnish学习以及CDN的原理
一.varnish学习Web Page Cache: squid --> varnish 程序的运行具有局部性特征: 时间局部性:一个数据被访问过之后,可能很快会被再次访问到: 空间局部性:一个 ...
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- Spring之缓存注解@Cacheable
https://www.cnblogs.com/fashflying/p/6908028.html https://blog.csdn.net/syani/article/details/522399 ...
- webpack-loader原理
loader loader 是导出为一个函数的 node 模块.该函数在 loader 转换资源的时候调用.给定的函数将调用 loader API,并通过 this 上下文访问. loader配置 { ...
- Java面向切面原理与实践
Java面向切面原理与实践 一. 面向切面编程是什么 首先用一句话概括:面向切面编程(AOP)就是对某些具有相似点的代码进行增强. 相似点可以是同一个包.使用相同的注解.public的方法.以Impl ...
- Springboot中的缓存Cache和CacheManager原理介绍
背景理解 什么是缓存,为什么要用缓存 程序运行中,在内存保持一定时间不变的数据就是缓存.简单到写一个Map,里面放着一些key,value数据,就已经是个缓存了 所以缓存并不是什么高大上的技术,只是个 ...
随机推荐
- SQL游标使用及实例
declare my_cursor cursor scroll dynamic for select * from t_msg open my_cursor declare @name sysname ...
- C#简单工厂和抽象类的实例
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...
- Acperience HDU - 5734
Deep neural networks (DNN) have shown significant improvements in several application domains includ ...
- java SpringWeb 接收安卓android传来的图片集合及其他信息入库存储
公司是做APP的,进公司一年了还是第一次做安卓的接口 安卓是使用OkGo.post("").addFileParams("key",File); 通过这种方式传 ...
- JAVA中String类的方法(函数)总结--JAVA基础
1.concat()方法,当参数为两字符串时,可实现字符串的连接: package cn.nxl123.www; public class Test { public static void main ...
- 【最新】Power BI混合现实应用Mixed Reality app预览版正式发布
1.介绍 2018年3月13日,Power BI在官方博客和Docs文档发布了Power BI for Mixed Reality应用预览版的消息, 也就是可以以后在更虚拟的世界中来观察你的报表,想象 ...
- wpf研究之道——自定义Button控件
我们知道WPF中普通的按钮,长得丑,所以自定义按钮,在所难免.我们给按钮添加 MoveBrush,EnterBrush两把刷子,其实就是鼠标经过和鼠标按下的效果.只不过这不是普通的刷子,而是带图片的I ...
- JavaScript方面的书籍
我要向大家推荐两本js方面的书: <JavaScript权威指南> <JavaScript高级程序设计>适合想在js方面有所提高的开发人员 我们读书是为了什么? 有的人可能是兴 ...
- Java并发编程实战(chapter_1)(原子性、可见性)
混混噩噩看了很多多线程的书籍,一直认为自己还不够资格去阅读这本书.有种要高登大堂的感觉,被各种网络上.朋友.同事一顿外加一顿的宣传与传颂,多多少少再自我内心中产生了一种敬畏感.2月28好开始看了之后, ...
- 为 Debian 8 或 Debian 9(64 位)安装 .NET Core
在 Debian 8 或 Debian 9(64 位)上安装 .NET Core 的具体步骤: 备注:必须有用户控制目录,才能通过 tar.gz 在 Linux 系统上进行安装. 1.准备一台刚安装的 ...