Spring4 Cache + Ehcache配置

Spring4 Cache + Ehcache配置

从Spring 3.1之后 引入了基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使用的抽象,通过在既有代码中添加少量它定义的各种 annotation,即能够达到缓存方法的返回对象的效果来定义缓存的 key 和各种 condition,还提供开箱即用的缓存临时存储方案,也支持和主流的专业缓存

例如 EHCache 集成其特点总结如下:

通过少量的配置 annotation 注释即可使得既有代码支持缓存支持开箱即用 Out-Of-The-Box,即不用安装和部署额外第三方组件即可使用缓存

支持 SpringEL,能使用对象的任何属性或者方法来定义缓存的 key 和 condition支持 AspectJ,并通过其实现任何方法的缓存支持支持自定义 key 和自定义缓存管理者,具有相当的灵活性和扩展性

首先我们需要配置ehcache.xml

配置如下:

<ehcache>
	<diskStore path="java.io.tmpdir"/>
    <!-- 
                    配置自定义缓存
        maxElementsInMemory:缓存中允许创建的最大对象数
        eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
        timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,
                    两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,
                    如果该值是 0 就意味着元素可以停顿无穷长的时间。
        timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,
                    这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
        overflowToDisk:内存不足时,是否启用磁盘缓存。
        memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
    -->
    <defaultCache  
            maxElementsInMemory="5000"  
            eternal="true"  
            overflowToDisk="false"  
            memoryStoreEvictionPolicy="LFU"/>  
    <!-- 用户cache -->
    <cache name="user_Cache"
            maxElementsInMemory="10000"  
            eternal="false"
            timeToIdleSeconds="1000"
            timeToLiveSeconds="18000"
            overflowToDisk="false"  
            memoryStoreEvictionPolicy="LFU"/>
</ehcache>  

 然后我们需要在sping配置文件中增加cacheManager配置

<beans xmlns="http://www.springframework.org/schema/beans"
 	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">  
    <!--配置spring自动拦截带cache注解的注解方法-->
    <cache:annotation-driven />
    <!--配置cache管理工厂-->
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:resources/ehcache.xml" p:shared="false"/>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="cacheManagerFactory"/>
    <!-- generic cache manager
    	使用spring自带的多线程map集合,如果不适用ecache等缓存软件的,可以适用这个
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">  
        <property name="caches">  
            <set>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="user_Cache"/>  
             </set>  
        </property>  
    </bean>
     -->
</beans>  

    接着,我们就可以在serviceImpl中需要的方法上添加@CacheEvict @Cacheable  @Caching

其中

@CacheEvict 是在调用方法时删除某一个缓存,可以指定 beforeInvocation = true,在方法调用前删除,默认为调用后删除

参数: value 指定缓存的cache名字,可以指定多个,key是缓存中key的名字,beforeInvocation 是否在执行前删除缓存,默认为false,allEntries 为是否清楚cacha中所有的缓存

@Cacheable  是指定当前方法需要缓存结果

参数: value 指定缓存的cacha名字,可以指定多个, key为缓存的key名字,默认为hash,不过在我使用过程中发现有时候不指定,返回的cache有时候会是其他缓存的结果,我一直都设置.也比较简单.

key执行springEL.设置如下:

比如下面方法

@Cacheable(value="user_Cache",key=" 'findForList' + #userid+ '_' + #pwd ")

findForList(String userid,String pwd)

 如果参数为对象,可以按照如下设置,比如TmUser中有userid

@Cacheable(value="user_Cache",key=" 'findForList' + #user.userid")

findForList(TmUser user)

如果我们想使用Hash来做key,首先需要自己做一个方法比如:com.common.util.HashUtil,里面有一个方法hash(Object obj)

@Cacheable(value="user_Cache",key=" 'findForList'+T(com.common.util.HashUtil).hash(#user)")

findForList(TmUser user)

@Caching 此方法可以一次指定多个cache和evict,即,在调用方法时缓存多个对象和除去多个对象.

配置如下:

   @Caching(cacheable={@Cacheable(value="a_Cache"),@Cacheable(value="b_Cache")},evict={@CacheEvict(value="a_cache",key="a"),@CacheEvict(value="b_cache",key="b")})

将spring的配置文件在web.xml中加载,添加ehcache相关jar包,即可发现缓存已生效

注:如果在Action或Controller中控制cache,可以通过注入EhCacheCacheManager对象,id为前面标红的ID

通过 cacheManager.getCache(cacheName)获得对应Cache

通过 cache.get(缓存的key, 缓存的对象类型)得到对应的对象.

通过 cache.put(要缓存的key,要缓存的对象)来缓存对象

猜你喜欢

转载自weixiao2013.iteye.com/blog/2229509