Ehcache(四)-–在Spring中运用Ehcache

主要使用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实现ehcache有很多方法,很多都是利用aop来实现,我认为采用注解的方式更灵活,配置也更简洁。下面就是我利用spring-modules-0.9实现的注解缓存。

配置文件如下:

<?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">
  <ehcache:config configlocation="classpath:ehcache.xml" />       
   <ehcache:annotations>       
   <ehcache:caching id="testCache" cachename="testCache" />        
  <ehcache:flushing id="testFlush" cachenames="testCache" />        
  </ehcache:annotations>        
</beans>

里一定要注意:

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

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

我就在这里花了很长时间,查了很多资料。网上的很多资料说这是spring-moduls的bug。很多朋友在这里总是报找到xsd文件。

在ehcache.xml中加入

      <cache name="testCache" memorystoreevictionpolicy="LFU" overflowtodisk="true" eternal="true" maxelementsondisk="1000" maxelementsinmemory="20000" />

需要实现缓存的业务逻辑类

public class SampleServiceImpl extends BaseServiceIbatisImpl implements ISampleService { 
  @Cacheable(modelId="testCache")
   public List cacheTest(){
     List list=new ArrayList();
     list.add("a");
     return list;
   } 
  @CacheFlush(modelId ="testFlush")
   public List cacheFlushTest(){
     List list=new ArrayList();
    list.add("b");
    return list;
  }
}

测试类:

public class SampleServiceTest extends BaseTestCase{
  @Resource
  Private ISampleService sampleService;
  @Test
  public void testCacheTest(){
        List<string> list=this.sampleService.cacheTest();
    for(String str:list){
      System.out.println(str);
    }
     List<string> cachelist=this.sampleService.cacheTest();
    for(String str:cachelist){
      System.out.println(str);
    } 
    List<string> cachenewlist=this.sampleService.cacheFlushTest();
    for(String str:cachenewlist){
      System.out.println(str);
    }   
    List<string> cachelist1=this.sampleService.cacheTest();
    for(String str:cachelist1){
      System.out.println(str);
    }
  } 
}
 
yybean ps:
 

想不到网上关于spring-modules的资料居然还不多,搜来搜去都是那几篇,上面那篇来自csdn http://blog.csdn.net/gaoligaoli/archive/2009/06/19/4282403.aspx

算是比较详细的。官方文档也太不详细了。

注解是必须居于目标环境是Java 5平台的

对于缓存,Spring Modules提供了两个注解:

@Cacheable:声明一个方法的返回值应该被缓存。

@CacheFlush:声明一个方法是清空缓存的触发器。

设置缓存模型与刷新模型的不同之处在于,刷新模型不仅决定要清空哪个缓存,还决定了何时清空。在默认情况下,缓存是在@CacheFlush注解的方法被调用之后清空的,但我们可以通过指定<ehcache:flushing>的when属性来改变:

<ehcache:annotations>

  <ehcache:caching id="testCache" cacheName="testCache" />

  <ehcache:flushing id="testFlush" cacheNames="testCache" when=“before”/>

</ehcache:annotations>

把when属性设置为before之后,缓存就会@CacheFlush注解的方法被调用之前清空。



posted on 2010-09-21 11:51  缘^_^  阅读(3491)  评论(0)    收藏  举报

导航