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

2010年5月21日 由 yybean 留言 »

 

关于spring实现ehcache有很多方法,很多都是利用aop来实现,我认为采用注解的方式更灵活,配置也更简洁。下面就是我利用spring-modules-0.9实现的注解缓存。

配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans    &#13;&#10;           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   &#13;&#10;           http://www.springframework.org/schema/context   &#13;&#10;           http://www.springframework.org/schema/context/spring-context-2.5.xsd   &#13;&#10;           http://www.springframework.org/schema/aop    &#13;&#10;           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   &#13;&#10;           http://www.springframework.org/schema/tx    &#13;&#10;           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   &#13;&#10;           http://www.springmodules.org/schema/ehcache   &#13;&#10;           http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
 
    <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注解的方法被调用之前清空。





发表评论