一、Spring Cache Abstraction
Spring Cache 的使用
@Cacheable 运行流程
CacheAutoConfiguration是缓存的自动配置类
自动配置类使用@Import注解导入CacheConfigurationImportSelector类
这个类会往容器中导入一些缓存组件
打上断点,开启Debug模式
往容器中导入了10个组件,都是各种缓存配置类,导入的这些类都会有一些规则
在application.yml增加配置,打印出容器中的类,看到底哪些缓存配置类生效了
debug: true 复制代码
只有SimpleCacheConfiguration是生效的,其他的都没有匹配上
而SimpleCacheConfiguration通过@Bean注解往容器中导入了一个ConcurrentMapCacheManager
ConcurrentMapCacheManager实现了CacheManager接口
在getCache()方法打断点,查看如何根据缓存名字获取缓存
缓存是存在Map中的,是一个ConcurrentHashMap
getCache()方法可以根据缓存名字获取缓存,如果根据缓存名字查询不到缓存就通过createConcurrentMapCache()方法创建一个缓存并返回
createConcurrentMapCache()方法通过new关键字创建了ConcurrentMapCache
ConcurrentMapCache作用就是将缓存的存储在CurrentMap中,其实就是一个Map,该类中包含了一个lookup方法用来从CurrentMap中查询缓存以及put()方法将缓存保存在CurrentMap中,底层是调用了Map的get()方法和put()方法
在目标方法、lookup()、put()以及ConCurrentMapCacheManager类上的getCache()方法上打断点,重新启动Debug查看@Cacheable注解的运行流程
在浏览器发送请求 /tesla/1166057520
之后会先获取name为tesla的缓存
继续Step Over, 获取到的缓存为null,这是就会创建name为tesla的缓存
















