基于spring @Cacheable 注解的spel表达式解析执行逻辑
作者:二哈_8fd0 发布时间:2023-07-03 19:46:45
日常使用中spring的 @Cacheable 大家一定不陌生,基于aop机制的缓存实现,并且可以选择cacheManager具体提供缓存的中间件或者进程内缓存,类似于 @Transactional 的transactionManager ,都是提供了一种多态的实现,抽象出上层接口,实现则供客户端选择,或许这就是架构吧,抽象的设计,使用interface对外暴露可扩展实现的机制,使用abstract 整合类似实现。
那么我们就看看 @Cacheable提供的一种方便的机制,spel表达式取方法 参数的逻辑,大家都写过注解,但是注解逻辑需要的参数可以使用spel动态取值是不是好爽~
直接进入主题 跟随spring的调用链
直接看 @Cacheable 注解就可以了
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cacheable {
// spring的别名机制,这里不讨论,和cacheNames作用一致
@AliasFor("cacheNames")
String[] value() default {};
@AliasFor("value")
String[] cacheNames() default {};
// 今天的主角,就从他入手
String key() default "";
// 拼接key的 抽象出来的接口
String keyGenerator() default "";
// 真正做缓存这件事的人,redis,caffine,还是其他的都可以,至于内存还是进程上层抽象的逻辑不关心,如果你使用caffine
//就需要自己考虑 多服务实例的一致性了
String cacheManager() default "";
String cacheResolver() default "";
// 是否可以执行缓存的条件 也是 spel 如果返回结果true 则进行缓存
String condition() default "";
// 如果spel 返回true 则不进行缓存
String unless() default "";
// 是否异步执行
boolean sync() default false;
}
接下来看 key获取是在哪里
SpringCacheAnnotationParser#parseCacheableAnnotation 解析注解,还好就一个地方
没有任何逻辑就是一个组装
继续跟踪上述方法 SpringCacheAnnotationParser#parseCacheAnnotations 走到这里,
@Nullable
private Collection<CacheOperation> parseCacheAnnotations(
DefaultCacheConfig cachingConfig, AnnotatedElement ae, boolean localOnly) {
Collection<? extends Annotation> anns = (localOnly ?
AnnotatedElementUtils.getAllMergedAnnotations(ae, CACHE_OPERATION_ANNOTATIONS) :
AnnotatedElementUtils.findAllMergedAnnotations(ae, CACHE_OPERATION_ANNOTATIONS));
if (anns.isEmpty()) {
return null;
}
final Collection<CacheOperation> ops = new ArrayList<>(1);
anns.stream().filter(ann -> ann instanceof Cacheable).forEach(
ann -> ops.add(parseCacheableAnnotation(ae, cachingConfig, (Cacheable) ann)));
anns.stream().filter(ann -> ann instanceof CacheEvict).forEach(
ann -> ops.add(parseEvictAnnotation(ae, cachingConfig, (CacheEvict) ann)));
anns.stream().filter(ann -> ann instanceof CachePut).forEach(
ann -> ops.add(parsePutAnnotation(ae, cachingConfig, (CachePut) ann)));
anns.stream().filter(ann -> ann instanceof Caching).forEach(
ann -> parseCachingAnnotation(ae, cachingConfig, (Caching) ann, ops));
return ops;
}
也没有太多逻辑,将当前拦截到的方法可能存在的多个 SpringCache的注解解析为集合返回,那就是支持多个SpringCache注解同时放到一个方法喽。
@Override
@Nullable
public Collection<CacheOperation> parseCacheAnnotations(Class<?> type) {
// 到上边发现这里入参是一个类,那么可以推断这里调用是启动或者类加载时进行注解解析,然后缓存注解的写死的参数返回
DefaultCacheConfig defaultConfig = new DefaultCacheConfig(type);
return parseCacheAnnotations(defaultConfig, type);
}
//------------还有一个方法是对方法的解析也是对注解的解析返回------------------
@Override
@Nullable
public Collection<CacheOperation> parseCacheAnnotations(Method method) {
DefaultCacheConfig defaultConfig = new DefaultCacheConfig(method.getDeclaringClass());
return parseCacheAnnotations(defaultConfig, method);
}
再上边 AnnotationCacheOperationSource#findCacheOperations ,两个重载方法
@Override
@Nullable
protected Collection<CacheOperation> findCacheOperations(Class<?> clazz) {
return determineCacheOperations(parser -> parser.parseCacheAnnotations(clazz));
}
@Override
@Nullable
protected Collection<CacheOperation> findCacheOperations(Method method) {
return determineCacheOperations(parser -> parser.parseCacheAnnotations(method));
}
AbstractFallbackCacheOperationSource#computeCacheOperations
这里有点看不懂暂时不细做追溯,目的就是spelAbstractFallbackCacheOperationSource#getCacheOperations
还是处理解析注解返回
调用getCacheOperations方法的地方
如上图直接查看第一个调用
CacheAspectSupport#execute 查看这个execute调用方是CacheInterceptor#invoke 实现的MethodInterceptor接口,那不用看其他的了,这里就是执行方法拦截的地方,在这里会找到spel的动态解析噢
顺便看一下拦截方法中的执行逻辑
了解一下@Cacheable的拦截顺序
@Override
@Nullable
public Object invoke(final MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
// 这是个一个 函数式接口作为回调,这里并没有执行,先执行下面execute方法 即CacheAspectSupport#execute
CacheOperationInvoker aopAllianceInvoker = () -> {
try {
return invocation.proceed();
}
catch (Throwable ex) {
throw new CacheOperationInvoker.ThrowableWrapper(ex);
}
};
try {
return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments());
}
catch (CacheOperationInvoker.ThrowableWrapper th) {
throw th.getOriginal();
}
}
接下来看 execute方法
@Nullable
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);
CacheOperationSource cacheOperationSource = getCacheOperationSource();
if (cacheOperationSource != null) {
Collection<CacheOperation> operations = cacheOperationSource.getCacheOperations(method, targetClass);
if (!CollectionUtils.isEmpty(operations)) {
return execute(invoker, method,
new CacheOperationContexts(operations, method, args, target, targetClass));
}
}
}
// 方法逻辑是后执行噢,先进行缓存
return invoker.invoke();
}
再看 重载方法execute
@Nullable
private Object execute(final CacheOperationInvoker invoker, Method method, CacheOperationContexts contexts) {
// 注解上的是否异步的字段这里决定是否异步执行
if (contexts.isSynchronized()) {
CacheOperationContext context = contexts.get(CacheableOperation.class).iterator().next();
if (isConditionPassing(context, CacheOperationExpressionEvaluator.NO_RESULT)) {
Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT);
Cache cache = context.getCaches().iterator().next();
try {
return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker))));
}
catch (Cache.ValueRetrievalException ex) {
// Directly propagate ThrowableWrapper from the invoker,
// or potentially also an IllegalArgumentException etc.
ReflectionUtils.rethrowRuntimeException(ex.getCause());
}
}
else {
// No caching required, only call the underlying method
return invokeOperation(invoker);
}
}
// -------------同步执行缓存逻辑--------------
// --------------------下面各种注解分别执行,可以看出来springCache注解之间的顺序 缓存删除(目标方法invoke前)并执行、缓存增
//加(猜测是先命中一次缓存,如果没有命中先存入空数据的缓存,提前占住缓存数据,尽量减少并发缓存带来的缓存冲洗问题)、
//缓存增加(带有数据的)、上述两个缓存增加的真正执行 、缓存删除(目标方法invoke 后)并执行
//当然这个 是 invoke前执行 或者后执行 是取决于@CacheEvict 中的 beforeInvocation 配置,默认false在后面执行如果前面执行unless就拿不到结果值了
// 那么spring cache 不是 延时双删噢,高并发可能存在数据过期数据重新灌入
// Process any early evictions
processCacheEvicts(contexts.get(CacheEvictOperation.class), true,
CacheOperationExpressionEvaluator.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<>();
if (cacheHit == null) {
collectPutRequests(contexts.get(CacheableOperation.class),
CacheOperationExpressionEvaluator.NO_RESULT, cachePutRequests);
}
// 方法入参解析 用于 key condition
Object cacheValue;
// 方法结果 解析 用于 unless
Object returnValue;
if (cacheHit != null && !hasCachePut(contexts)) {
// If there are no put requests, just use the cache hit
cacheValue = cacheHit.get();
returnValue = wrapCacheValue(method, cacheValue);
}
else {
// Invoke the method if we don't have a cache hit
returnValue = invokeOperation(invoker);
cacheValue = unwrapReturnValue(returnValue);
}
// Collect any explicit @CachePuts
collectPutRequests(contexts.get(CachePutOperation.class), cacheValue, cachePutRequests);
// Process any collected put requests, either from @CachePut or a @Cacheable miss
for (CachePutRequest cachePutRequest : cachePutRequests) {
cachePutRequest.apply(cacheValue);
}
// Process any late evictions
processCacheEvicts(contexts.get(CacheEvictOperation.class), false, cacheValue);
return returnValue;
}
不详细探究执行逻辑了,来看看生成key的逻辑,private 方法 generateKey
// 可以看出没有生成key 会抛出异常,不允许null
private Object generateKey(CacheOperationContext context, @Nullable Object result) {
Object key = context.generateKey(result);
if (key == null) {
throw new IllegalArgumentException("Null key returned for cache operation (maybe you are " +
"using named params on classes without debug info?) " + context.metadata.operation);
}
if (logger.isTraceEnabled()) {
logger.trace("Computed cache key '" + key + "' for operation " + context.metadata.operation);
}
return key;
}
//------------------------继续------------
/**
* Compute the key for the given caching operation.
*/
@Nullable
protected Object generateKey(@Nullable Object result) {
if (StringUtils.hasText(this.metadata.operation.getKey())) {
// 终于看到 spring核心包之一 org.springframework.expression 包里的类了。。。T.T
EvaluationContext evaluationContext = createEvaluationContext(result);
return evaluator.key(this.metadata.operation.getKey(), this.metadata.methodKey, evaluationContext);
}
return this.metadata.keyGenerator.generate(this.target, this.metadata.method, this.args);
}
可以看到使用的 evaluator 是CacheOperationExpressionEvaluator类这个成员变量,类加载时便生成,里面有生成待解析实例的方法,有解析 key condition unless 的三个方法及ConcurrentMap 成员变量缓存到内存中,将所有的Cache注解的 spel表达式缓存于此,默认 64的大小,主要方法如下
public EvaluationContext createEvaluationContext(Collection<? extends Cache> caches,
Method method, Object[] args, Object target, Class<?> targetClass, Method targetMethod,
@Nullable Object result, @Nullable BeanFactory beanFactory) {
CacheExpressionRootObject rootObject = new CacheExpressionRootObject(
caches, method, args, target, targetClass);
CacheEvaluationContext evaluationContext = new CacheEvaluationContext(
rootObject, targetMethod, args, getParameterNameDiscoverer());
if (result == RESULT_UNAVAILABLE) {
evaluationContext.addUnavailableVariable(RESULT_VARIABLE);
}
else if (result != NO_RESULT) {
evaluationContext.setVariable(RESULT_VARIABLE, result);
}
if (beanFactory != null) {
evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
}
return evaluationContext;
}
@Nullable
public Object key(String keyExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
return getExpression(this.keyCache, methodKey, keyExpression).getValue(evalContext);
}
public boolean condition(String conditionExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
return (Boolean.TRUE.equals(getExpression(this.conditionCache, methodKey, conditionExpression).getValue(
evalContext, Boolean.class)));
}
public boolean unless(String unlessExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
return (Boolean.TRUE.equals(getExpression(this.unlessCache, methodKey, unlessExpression).getValue(
evalContext, Boolean.class)));
}
然后就返回想要的key了。
来源:https://www.jianshu.com/p/3e4eb47e72ce


猜你喜欢
- 使用Java生成图片验证码完全解析在 Java 中开发图片验证码功能,您可以使用 Java 图形处理库,比如 Java Advanced I
- 1. 编译错误//代码1public static void test() throws Exception {throw ne
- JavaConfig,是在 Spring 3.0 开始从一个独立的项目并入到 Spring 中的。JavaConfig 可以看成一个用于完成
- 什么是Aop主要介绍springboot中aop的使用,用过Spring框架的都知道,aop是spring框架的两大核心功能之一,还有一个就
- 问题原因今天在看集合源码的时候,突然看到接口继承接口,觉得有点差异,以前写代码也就是类继承一个类,实现接口。这样写的多了,突然看到接口继承接
- 本文实例讲述了C#利用System.Uri转URL为绝对地址的方法。分享给大家供大家参考。具体分析如下:在使用ASPOSE.Word生成Wo
- 提示:这里咱们要说的常量池,常量池就是咱们面试中所说的常量池,谈谈你对常量池的认识?面试官一问咱们就懵逼了,你要记得你脑子中有一张图!!!
- 通用配置#下面介绍的整合JDBC和整合MyBatis都需要添加的实体类和配置数据库表#CREATE TABLE `user` ( `id`
- 一、Steam的优势java8中Stream配合Lambda表达式极大提高了编程效率,代码简洁易懂(可能刚接触的人会觉得晦涩难懂),不需要写
- 题目题目要求思路:模拟用一个哈希表记录可出现的字母,然后逐一遍历每个单词每个字母,符合条件则结果加一。Javaclass Solution
- 生成随机数可以用伪随机数发生器Random,受种子控制生成伪随机数,默认以当前时间值为种子。如果程序运行的很快,就会导致在几乎同一时刻运行多
- 本文实例讲述了Java Spring开发环境搭建及简单入门示例。分享给大家供大家参考,具体如下:前言虽然之前用过Spring,但是今天试着去
- /*最小树形图图模版-朱刘算法模版说明:点标号必须0-(N-1) 必须去除到自身的点(到自身的边的边权赋无限大)*/
- SpringBoot @ConditionalOnBean实现原理在SpringBoot1.5.X时判断条件是OR,SpringBoot2.
- 在本文中,笔者向大家介绍下Java中一个非常重要也非常有趣的特性,就是自动装箱与拆箱,并从源码中解读自动装箱与拆箱的原理,同时这种特性也留有
- 本文较为详细的描述了重载运算符的方法。一般来说,重载运算符在实际的项目开发中会经常的用到,但如果某些自定义类型通过简短几行代码重载一些常用的
- Knife4j就相当于是swagger的升级版,对于我来说,它比swagger要好用得多1、在pom.xml引入依赖包<!-- Swa
- Unsupported major.minor version 51.0解决办法今天偶然间同事遇到一个问题,也加深了自己对eclipse中b
- 一、什么是HTTP协议HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应
- DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数