论坛首页 Java企业应用论坛

Spring3.1 缓存cache配置

浏览 10083 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-01-13  
最近准备把spring3.1的缓存功能引入到系统中,正好做到数据字典(类似下拉菜单管理)需要用到缓存技术,于是初步使用了一把。
步骤:
1.必须引入ehcache-core-2.5.0.jar
2.applicationContext.xml配置文件中需要加入
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
  http://www.springframework.org/schema/cache
          http://www.springframework.org/schema/cache/spring-cache.xsd"
3.applicationContext.xml配置
<!-- 缓存 -->
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:/ehcache.xml" />
4.
	@Cacheable(value="DICT_CACHE",key="#dictId + 'getSystemDictByDictId'")
	public SystemDict getSystemDictByDictId(String dictId){
		Search search = new Search().addFilterEqual("dictId", dictId);
		return systemDictDAO.searchUnique(search);
	}
	
	@Cacheable(value="DICT_CACHE",key="#parentDictId + 'getSystemDictByDictId'")
	public List<SystemDict> findSystemDictByParentDictId(String parentDictId){
		Search search = new Search().addFilterEqual("parentDictId", parentDictId);
		return systemDictDAO.search(search);
	}
	
	@Cacheable(value="DICT_CACHE",key="#parentDictId + #status + 'getSystemDictByDictId'")
	public List<SystemDict> findSystemDictByParentDictId(String parentDictId,Integer status){
		Search search = new Search().addFilterEqual("parentDictId", parentDictId);
			search.addFilterEqual("status", status);
		return systemDictDAO.search(search);
	}
	@CacheEvict(value="DICT_CACHE",allEntries=true)
	public void saveSystemDict(SystemDict systemDict){
		systemDictDAO.save(systemDict);
	}
	
	@CacheEvict(value="DICT_CACHE",allEntries=true)
	public void deleteSystemDict(String dictId){
		systemDictDAO.deleteSystemDict(dictId);
	}

   发表时间:2012-01-14  
虽然我也是用spring3.1,缓存我是用ehcache。不过,自己封装了cachemanager。感觉要比用注解灵活些,随意怎么存,用了注解虽说方便,但只能进行方法级缓存。貌似,以前还有帖说spring3.1和ehcache整合有点问题,楼主使用中有没有发现什么异常情况?
0 请登录后投票
   发表时间:2012-01-16  
现在可以用<mvc:ehcache/>的标签来做的了,很方便的,
0 请登录后投票
   发表时间:2012-01-16  
spring缓存目前买没用过,

mark
0 请登录后投票
   发表时间:2012-01-16   最后修改:2012-01-16
我也用到了数据字典,回来把你的代码加上试试!
0 请登录后投票
   发表时间:2012-01-16  
用mybatis的童鞋可以在mybatis的sql映射文件里面简单的写一个<cache/>标签就可以搞定了。
0 请登录后投票
   发表时间:2012-01-17  
用分布式部署吗
0 请登录后投票
   发表时间:2012-01-17  
没用过,先看看!
0 请登录后投票
   发表时间:2012-01-17  
用用原生的JCS试试
0 请登录后投票
   发表时间:2012-01-17  
我是来看例子的, 不错.

我是自己写的cachemanager来管理缓存的.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics