`
shishi11
  • 浏览: 113908 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring3.1的cache配置试验

 
阅读更多

看得觉得不错,所以试了一下,记一下

要有:

springframework.aop 3.1m2

springframework.context 3.1m2

springframework.aspects 3.1m2

和一个

aopalliance-1.0.jar

这个spring的文档里没提,本来以为有spring的包就行了

 

配置文件applictionContext-cache.xml

<beans xmlns="http://www.springframework.org/schema/beans" 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">
  <cache:annotation-driven />
  <!-- generic cache manager -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
  <property name="caches">
    <set>
      <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>
      <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="books"/>
    </set>
  </property>
</bean>
</beans>
 

只是从原例子上抄的,以后可以自己改

这次想缓存的是DAO类的一个方法,试想如果是hibernate,应该不用在这儿进行缓存吧。

@Cacheable(value="books",key="#checkonworkattendance.getAttendanceYear()+#checkonworkattendance.getAttendanceMonth()")
	public List viewCheckonworkattendance(
			Checkonworkattendance checkonworkattendance)
			throws CheckonworkattendanceException {

 这里遇到的问题是,尽量使用某一数值为key,否则可能是以OBJECT.toString的方式来作为key, 这样就可能缓存method了。

KEY可以用所谓SpEL方式写,我也就是试写了一个吧,要多个参数组合。

试验时,在类内做断点,只运行一次,再运行时会自动跳过,从cache中取了。

 

这样对于非hibernate的简易查询,也可以在数据层面进行缓存了。

另,没看到除了annotation方式外的配置方法,因为是想对原有代码进行AOP方式的调教,所以如果能以antmatcher的**方式进行配置就好了。再看

分享到:
评论
2 楼 liuccc1 2011-12-31  
spring-framework-reference.pdf上 章节28.4 上介绍了xml配置缓存的方式
1 楼 -惜帅- 2011-10-06  
嗯,支持下,我用Spring JDBC的方式,也需要做些cache,也是用3.1 M2的自带注解,所有已存在的缓存要如何管理?

相关推荐

Global site tag (gtag.js) - Google Analytics