助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
# 31.1 支持的缓存提供商 ### 31.1 支持的缓存提供商 缓存抽象不提供实际的存储,而是依赖于`org.springframework.cache.Cache`和`org.springframework.cache.CacheManager`接口的实现。只要通过`@EnableCaching`注解开启缓存支持,Spring Boot就会根据实现自动配置一个合适的`CacheManager`。 **注** 如果你使用的缓存设施beans不是基于接口的,确保启用`proxyTargetClass`,并设置其属性为`@EnableCaching`。 **注** 使用`spring-boot-starter-cache`‘Starter’可以快速添加所需缓存依赖,如果你是手动添加依赖,需要注意一些实现只有`spring-context-support` jar才提供。 如果你还没有定义一个`CacheManager`类型的bean,或一个名为`cacheResolver`的`CacheResolver`(查看`CachingConfigurer`),Spring Boot将尝试以下提供商(按这个顺序): - [Generic](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-generic) - [JCache (JSR-107)](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-jcache)(EhCache 3, Hazelcast, Infinispan, etc) - [EhCache 2.x](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-ehcache2) - [Hazelcast](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-hazelcast) - [Infinispan](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-infinispan) - [Couchbase](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-couchbase) - [Redis](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-redis) - [Caffeine](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-caffeine) - [Guava](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-guava) - [Simple](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-simple) **注**`spring.cache.type`属性可强制指定使用的缓存提供商,如果需要在一些环境(比如,测试)中[禁用全部缓存](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-caching-provider-none)也可以使用该属性。 如果`CacheManager`是Spring Boot自动配置的,你可以在它完全初始化前,通过实现`CacheManagerCustomizer`接口进一步配置,以下设置使用的缓存name: ``` @Bean public CacheManagerCustomizer<ConcurrentMapCacheManager> cacheManagerCustomizer() { return new CacheManagerCustomizer<ConcurrentMapCacheManager>() { @Override public void customize(ConcurrentMapCacheManager cacheManager) { cacheManager.setCacheNames(Arrays.asList("one", "two")); } }; } ``` **注** 在以上示例中,需要配置一个`ConcurrentMapCacheManager`,如果没有配置,则自定义器(customizer)将不会被调用。自定义器你添加多少都可以,并可以使用`@Order`或`Ordered`对它们进行排序。