EHCache入门系列(四)–在Spring中运用EHCache

2010年6月1日 由 yybean 留言 »

主要使用Spring提供的springmodules和EHCache来简化程序的开发,通过配置文件来完成提供缓存。参考springmodules的文档。

参考

基于springmodules的缓存方案
一 基于xml配置和拦截

主要步骤

1、配置ehcache.xml文件

2、创建Spring EHCache的配置xml文件

配置文件代码示例

<?xml version="1.0" encoding="UTF-8"?>

 

<beans xmlns="http://www.springframework.org/schema/beans"

 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 

    xmlns:aop="http://www.springframework.org/schema/aop"

 

    xmlns:tx="http://www.springframework.org/schema/tx"

 

    xmlns:ehcache="http://www.springmodules.org/schema/ehcache"

 

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   

 

    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   

 

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd    

 

    http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">

 

 

 

    <bean id="EhCacheFacade"

 

       class="org.springmodules.cache.provider.ehcache.EhCacheFacade">

 

       <property name="failQuietlyEnabled" value="true" />

 

       <property name="cacheManager">

 

           <bean

 

              class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

 

              <property name="configLocation"

 

                  value="classpath:ehcache.xml">

 

              </property>

 

           </bean>

 

       </property>

 

    </bean>

 

 

 

    <bean id="cachingInterceptor"

 

       class="org.springmodules.cache.interceptor.caching.MethodMapCachingInterceptor">

 

       <property name="cacheProviderFacade" ref="EhCacheFacade" />

 

       <property name="cachingModels">

 

           <props>

 

              <prop key="com....test.Manager.get*">

 

                  cacheName=dictCache

 

              </prop>

 

           </props>

 

       </property>

 

    </bean>

 

    

 

    <bean id="flushingInterceptor"

 

       class="org.springmodules.cache.interceptor.flush.MethodMapFlushingInterceptor">

 

       <property name="cacheProviderFacade" ref="EhCacheFacade" />

 

       <property name="flushingModels">

 

           <props>

 

              <prop key="com....test.Manager.update*">

 

                  cacheNames=dictCache

 

              </prop>

 

           </props>

 

       </property>

 

    </bean>

 

    

 

    <bean

 

       class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

 

       <property name="beanNames">

 

           <value>*anager</value>

 

       </property>

 

       <property name="interceptorNames">

 

           <list>

 

              <value>flushingInterceptor</value>

 

              <value>cachingInterceptor</value>

 

           </list>

 

       </property>

 

    </bean>

 

    <bean id="manager" class="com...test.Manager"></bean>

 

</beans>

 

 <cache name="dictCache" 

 

   maxElementsInMemory="50" 

 

   eternal="false" 

 

   timeToIdleSeconds="60"

 

   timeToLiveSeconds="60"

 

   overflowToDisk="false"

 

   memoryStoreEvictionPolicy="LFU">

 

   </cache>

 

 

 

缓存说明:

1、方法不含有参数

时间到期缓存失效;调用flush,缓存失效。

2、方法中含有参数

参数不同则每次都缓存,若缓存中存在相同对象,则调用缓存。

当调用flush,该id对应的缓存都失效。

当缓存时间到期,该id对应的缓存都失效。

建议:对没有关联的缓存对象采取不同的id配置。所以ehcache会有好多的cache-id配置信息。

<props>

 

             <prop key="com....test.Manager.get*">

 

                 cacheName=dictCache

 

             </prop>

 

             ………                      

 

             <prop key="com....test.Manager2.get*">

 

                 cacheName=dictCache2

 

             </prop>

 

          </props>

 

 

 

          <props>

 

             <prop key="com....test.Manager.update*">

 

                 cacheNames=dictCache

 

             </prop>

 

             ………

 

             <prop key="com....test.Manager2.update*">

 

                 cacheNames=dictCache2

 

             </prop>

 

          </props>

 

 

二 基于注解

详见下面的一篇文章,已经很详细了

使用spring-modules-0.9实现注解缓存




发表评论