# 31.1 支持的缓存提供商

缓存抽象不提供实际的存储，而是依赖于`org.springframework.cache.Cache`和`org.springframework.cache.CacheManager`接口的实现。

如果你还没有定义一个`CacheManager`类型的bean，或一个名为`cacheResolver`的`CacheResolver`（查看`CachingConfigurer`），Spring Boot将尝试以下提供商（按这个顺序)：

1. [Generic](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-generic)
2. [JCache (JSR-107)](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-jcache)(EhCache 3, Hazelcast, Infinispan, etc)
3. [EhCache 2.x](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-ehcache2)
4. [Hazelcast](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-hazelcast)
5. [Infinispan](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-infinispan)
6. [Couchbase](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-couchbase)
7. [Redis](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-redis)
8. [Caffeine](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-caffeine)
9. [Simple](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-simple)

   **提示** `spring.cache.type`属性可强制指定使用的缓存提供商，如果需要在一些环境（比如，测试）中[禁用全部缓存](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-none)也可以使用该属性。

   **提示** 使用`spring-boot-starter-cache`“Starter”来快速添加基础的缓存依赖。“Starter”带来了`spring-context-support`：如果你手动添加依赖，为了使用JCache、EhCache2.x或者Guava的支持，你必须添加`spring-context-support`。

   如果`CacheManager`是Spring Boot自动配置的，你可以在它完全初始化前，通过实现`CacheManagerCustomizer`接口进一步配置，以下设置标志表明空值应当传递给底层映射：

   ```java
   @Bean
   public CacheManagerCustomizer<ConcurrentMapCacheManager> cacheManagerCustomizer() {
   return new CacheManagerCustomizer<ConcurrentMapCacheManager>() {
       @Override
       public void customize(ConcurrentMapCacheManager cacheManager) {
           cacheManager.setAllowNullValues(false);
       }
   };
   }
   ```

   **注** 在以上示例中，希望得到一个自动配置好的`ConcurrentMapCacheManager`。如果没有配置（要么你提供你自己的配置，要么一个不同的缓存提供商被自动配置），则自定义器（customizer）将不会被调用。自定义器你添加多少都可以，并可以使用`@Order`或`Ordered`对它们进行排序。
