Cacheable注解是典型的Spring AOP实现,在Spring里,aop可以简单的理解为代理(AspectJ除外),我们声明了@Cacheable的方法的类,都会被代理,在代理中,实现缓存的查询与设置操作。

 

在CacheAspectSupport中找到

源码追踪Cacheable注解的执行过程

而在setCacheManager方法中,了解到将该cacheManager使用SimpleCacheResolver的构造方法赋值给了SimpleCacheResolver。


在执行Aspect的invoke方法追踪下来,发现寻找Cache使用的是该语句:


接着向下寻找实际调用逻辑,在AbstractCacheResolver中终于发现了实际操作是使用CacheManager中的getCache方法。而AbstractCacheResolver是刚才SimpleCacheResolver的父类,由此我们明白,spring创建了CacheManager的对象并且在resolve中使用getCache(cacheName)。

源码追踪Cacheable注解的执行过程

找到Cahce后,程序返回Cache.get(Object key, Callable valueLoader)方法的返回值。并且通过wrapCacheValue方法进行了一个非null值的包装。

private Object wrapCacheValue(Method method, Object cacheValue) {
    return method.getReturnType() != javaUtilOptionalClass || cacheValue != null && cacheValue.getClass() == javaUtilOptionalClass ? cacheValue : CacheAspectSupport.OptionalUnwrapper.wrap(cacheValue);
}

相关文章:

  • 2021-04-16
  • 2023-01-10
  • 2021-04-04
  • 2021-09-15
  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
猜你喜欢
  • 2022-03-05
  • 2021-07-19
  • 2021-12-24
  • 2021-06-23
  • 2021-08-28
  • 2021-04-12
  • 2021-07-25
相关资源
相似解决方案