32.6 配置缓存存储

开箱即用,缓存抽象提供了多种存储集成。要使用它们,需要简单地声明一个适当的CacheManager - 一个控制和管理Caches,可用于检索这些存储。

32.6.1 JDK ConcurrentMap-based Cache

基于JDK的Cache实现位于org.springframework.cache.concurrent包下。它允许使用ConcurrentHashMap作为后备缓存存储。

<!-- simple cache manager -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
        <set>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="books"/>
        </set>
    </property>
</bean>

上面的代码片段使用SimpleCacheManager创建一个CacheManager,为两个嵌套的ConcurrentMapCache实例命名为defaultbooks。请注意,这些名称是为每个缓存直接配置的。

由于缓存是由应用程序创建的,因此它必须与其生命周期一致,使其适用于基本用例,测试或简单应用程序。缓存规模好,速度快,但不提供任何管理、持久化能力或驱逐合同。

32.6.2 Ehcache-based Cache(基于Ehcache的缓存)

Ehcache 3.x完全符合JSR-107标准,不需要专门的支持。

Ehcache 2.x的实现位于org.springframework.cache.ehcache包下。要使用它,只需要声明适当的CacheManager

<bean id="cacheManager"
      class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>

<!-- EhCache library setup -->
<bean id="ehcache"
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="ehcache.xml"/>

此设置引导Spring IoC(通过ehcache bean)中的ehcache库,然后将其连接到专用的CacheManager实现中。请注意,整个ehcache特定的配置是从ehcache.xml读取的。

32.6.3 Caffeine Cache

Caffeine是Java 8的重写Guava缓存,其实现位于org.springframework.cache.caffeine包下,并提供了对Caffeine的几项功能的访问。

根据需要,配置创建缓存的CacheManager很简单:

<bean id="cacheManager"
      class="org.springframework.cache.caffeine.CaffeineCacheManager"/>

还可以明确提供使用的缓存。在这种情况下,只有manager才能提供:

<bean id="cacheManager" class="org.springframework.cache.caffeine.CaffeineCacheManager">
    <property name="caches">
        <set>
            <value>default</value>
            <value>books</value>
        </set>
    </property>
</bean>

Caffeine CacheManager也支持自定义的CaffeineCacheLoader。查看Caffeine documentation获取更多信息。

32.6.4 GemFire-based Cache(基于GemFire的缓存)

GemFire是面向内存/磁盘支持,弹性可扩展,持续可用,主动(内置基于模式的订阅通知),全局复制数据库,并提供功能齐全的边缘缓存。有关如何使用GemFire作为CacheManager(及更多)的更多信息,请参考 Spring Data GemFire reference documentation

32.6.5 JSR-107 Cache

Spring的缓存抽象也可以使用兼容JSR-107的缓存。JCache实现位于org.springframework.cache.jcache包下。
要使用它,只需要声明适当的CacheManager

<bean id="cacheManager"
      class="org.springframework.cache.jcache.JCacheCacheManager"
      p:cache-manager-ref="jCacheManager"/>

<!-- JSR-107 cache manager setup  -->
<bean id="jCacheManager" .../>

32.6.6 Dealing with caches without a backing store(处理没有后端存储的缓存)

有时在切换环境或进行测试时,可能会有缓存声明,而不配置实际的后备缓存。由于这是一个无效的配置,因此在运行时将会抛出异常,因为缓存基础结构无法找到合适的存储。在这种情况下,而不是删除缓存声明(这可以证明是乏味的),可以连接一个不执行缓存的简单的虚拟缓存,也就是强制每次执行缓存的方法:

<bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager">
    <property name="cacheManagers">
        <list>
            <ref bean="jdkCache"/>
            <ref bean="gemfireCache"/>
        </list>
    </property>
    <property name="fallbackToNoOpCache" value="true"/>
</bean>

上面的CompositeCacheManager链接多个CacheManagers,另外,通过fallbackToNoOpCache标志,添加了一个no op缓存,用于所有不被配置的缓存管理器处理的定义。也就是说,在jdkCachegemfireCache(上面配置)中找不到的每个缓存定义都将由no op缓存来处理,这不会存储任何导致目标方法每次执行的信息。