使用Spring Cache

本人用的是Spring3.2,所以如果你用的是其他版本,则本文不能保证完全正确。

Spring的cache是在Spring-context中实现的,你额外的引入的spring-context包即可。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.             <groupId>org.springframework</groupId>  
  3.             <artifactId>spring-core</artifactId>  
  4.             <version>${spring.framework.version}</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.springframework</groupId>  
  8.             <artifactId>spring-context</artifactId>  
  9.             <version>${spring.framework.version}</version>  
  10.         </dependency>  

我们采用的是注解式的,所以spring的bean配置添加了如下两个组件:
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="false" />  
  2.   
  3. <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">  
  4.     <property name="caches">  
  5.         <set>  
  6.             <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="realEstateCache" />  
  7.             <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="schoolCache" />  
  8.             <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="infoCache" />  
  9.             <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="chartDataCache" />  
  10.         </set>  
  11.     </property>  
  12. </bean>  
第一个很好理解,但是有个地方要注意:proxy-target-class="false"      这个地方,如果你程序中已经采用了一层代理,比如事务,则此处只能是false;否则会出现(我用了hibernate)“HibernateException: no session in current thread”的问题。 原因以及含义不解释, 程序员能谈到代理/缓存的,相信能理解这个话题。
说第二个,它里面需要配置一些caches,概念类似于ehcache的cache配置,这里我按照业务需要,为四种业务对象配置了4个cache池。大家知道凡是cache都要考虑缓存失效问题,按业务需要配置缓存,可以在必要的时候分别失效各个缓存池。缓存的实现类我就简单的使用了ConcurrentMapCacheFactoryBean。或许有别的实现。

在service类的接口或实现类的方法上添加如下的注解:

schoolCache就是上面配置文件xml里面的一个p:name对应的值

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Cacheable(value="schoolCache", key="'findTop10Primary'")  
  2. public List<School> findTop10Primary();  
  3. @Cacheable(value="schoolCache", key="'findTop10Middle'")  
  4. public List<School> findTop10Middle();  
注意value就是你在spring配置文件里面配置的缓存名称,key是可区分相互的一个常量或者表达式,我这里就用了和方法名一样的一个常量。
为了实现缓存失效,我在每个需要缓存的service都添加了如下的方法:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @CacheEvict(value="schoolCache", allEntries=true)  
  2. public void removeAllCache();  

在我需要缓存失效的时候,我在web层调用一下这些方法即可。




关于失效时间


Spring Cache是要看你使用的实现,具体在SpringContext中是cacheManager Bean。默认内置3种实现,EHCache,Redis,ConcurrentHashMap。使用Spring对于EHCache可以使用如下的配置,可以有实效的。

(  <defaultCachemaxElementsInMemory="10000"eternal="false"
        timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="false"/>

timeToLiveSeconds====缓存数据在失效前的允许存活时间(单位:秒)
timeToIdleSeconds====缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用)


使用Redis在缓存的时候也可以使用实效的,但是Spring Cache没有提供便利的API,因此还无法利用Redis的默认缓存时限,需要自己更改Sprng context源码


ConcurrentHashMap实现的更无法使用时效限制。


三种配置如下:


EHCache:





<cache:annotation-driven/>
 
    <!-- EhCache library setup -->
    <beanid="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
          p:config-location="classpath:ehcache.xml"
          p:shared="true"/>
 
    <beanid="cacheManager"
          class="org.springframework.cache.ehcache.EhCacheCacheManager"
          p:cache-manager-ref="ehcache"/>



Redis:

<beanid="cacheManager"     class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-argindex="0"ref="redisTemplate"/>
    </bean>



Java  ConcurrentHashMap:

<beanid="cacheManager"
          class="org.springframework.cache.concurrent.ConcurrentMapCacheManager">
    </bean>
Spring Cache默认自带的基于ConcurrentHashMap的CacheManager实现是没有自动过期这一功能的,需要手工通过@CacheEvict 来剔除缓存数据。TTL、TTI等高级特性等高级特性需要使用EhCache来实现







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值