spring整合ehcache实现缓存

Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现。它支持注解方式使用缓存,非常方便。

spring本身内置了对Cache的支持,之前记录的是基于Java API的ConcurrentMap的CacheManager配置,现使用ehcache实现。

1、引入相关依赖

    <!-- 引入ehcache缓存 -->
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>${ehcache-version}</version>
    </dependency>

 

2、声明对cache的支持

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
    <cache:annotation-driven/>

</beans>

 

2、配置CacheManager

先使用Spring提供的EhCacheCacheManager来生成一个Spring的CacheManager,让其接收一个Ehcache的CacheManager,因为真正用来存入缓存数据的还是Ehcache。

Ehcache的CacheManager是通过Spring提供的EhCacheManagerFactoryBean来生成的,它可以通过指定ehcache的配置文件位置来生成一个Ehcache的CacheManager.

若未指定则将按照Ehcache的默认规则取classpath根路径下的ehcache.xml文件,若该文件也不存在,则获取Ehcache对应jar包中的ehcache-failsafe.xml文件作为配置文件。

    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:config/ehcache.xml" />
    </bean>

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager"  ref="cacheManagerFactory"/>
    </bean>

 

3、ehcache.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <!-- 默认缓存 -->
    <defaultCache
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"/>

    <!-- 用户详情缓存 -->
    <cache name="userDetailCache"
           maxElementsInMemory="1000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           overflowToDisk="false"
           memoryStoreEvictionPolicy="LRU"/>

</ehcache>
 

 

还有,以上说的是Spring内置的对Cache的支持,也可以通过Spring自己单独的使用Ehcache的CacheManager或Ehcache对象。

在配置文件中配置EhCacheManagerFactoryBean和EhCacheFactoryBean

1、EhCacheManagerFactoryBean

EhCacheManagerFactoryBean是Spring内置的一个可以产生Ehcache的CacheManager对象的FactoryBean。

可以通过属性configLocation指定用于创建CacheManager的Ehcache配置文件的路径,通常是ehcache.xml文件的路径。

如果没有指定configLocation,则将使用默认位置的配置文件创建CacheManager(即如果在classpath根路径下存在ehcache.xml文件,则直接使用该文件作为Ehcache的配置文件,否则将使用ehcache-xxx.jar中的ehcache-failsafe.xml文件作为配置文件来创建Ehcache的CacheManager)。

2、EhCacheFactoryBean

EhCacheFactoryBean是用来产生Ehcache的Ehcache对象的FactoryBean。

cacheManager属性,其可以指定将用来获取或创建Ehcache的CacheManager对象,若未指定则将通过CacheManager.create()获取或创建默认的CacheManager。

cacheName属性,其表示当前EhCacheFactoryBean对应的是CacheManager中的哪一个Ehcache对象,若未指定默认使用beanName作为cacheName。若CacheManager中不存在对应cacheName的Ehcache对象,则将使用CacheManager创建一个名为cacheName的Cache对象。

    <!-- 定义CacheManager -->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <!-- 指定配置文件的位置 -->
        <property name="configLocation" value="classpath:config/ehcache.xml"/>
        <!-- 指定新建的CacheManager的名称 -->
        <property name="cacheManagerName" value="cacheManagerName"/>
    </bean>

    <!-- 定义一个Ehcache -->
    <bean id="userCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
        <property name="cacheName" value="user"/>
        <property name="cacheManager" ref="cacheManager"/>
    </bean>
 

 

posted @ 2017-01-08 20:32  纳豆  阅读(405)  评论(0编辑  收藏  举报