一、Spring boot cache原理
第一步、自动配置类;
自动启动类:CacheAutoConfiguration
属性配置:CacheProperties
主启动类添加:@EnableCaching注解
cache POM添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
第二步、从缓存的配置类 中获取 多个cache
CacheConfigurationImportSelector.selectImports()方法获取
static class CacheConfigurationImportSelector implements ImportSelector { public String[] selectImports(AnnotationMetadata importingClassMetadata) { CacheType[] types = CacheType.values(); String[] imports = new String[types.length]; for (int i = 0; i < types.length; i++) { imports[i] = CacheConfigurations.getConfigurationClass(types[i]); } return imports; } }
获取结果:SimpleCacheConfiguration 默认cache
org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration org.springframework.boot.autoconfigure.cache.GuavaCacheConfiguration org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration【默认】 org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration
第三步:SimpleCacheConfiguration.cacheManager()
此方法中给容器中注册了一个CacheManager组件:类型为ConcurrentMapCacheManager