spring mvc缓存设置

版本:Spring3.0.6

准备工作:

下载 ehcache-spring-annotations-1.2.0 http://code.google.com/p/ehcache-spring-annotations/downloads/list 

下载完加压后里面的lib下的jar统统添加到classpath中

在资源文件夹下(通常是src/main/resources) 添加 ehcache.xml 内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  
  4.     updateCheck="false">  
  5.       
  6.     <diskStore path="java.io.tmpdir" />  
  7.       
  8.     <defaultCache eternal="false"   
  9.         maxElementsInMemory="1000"  
  10.         overflowToDisk="false"   
  11.         diskPersistent="false"   
  12.         timeToIdleSeconds="0"  
  13.         timeToLiveSeconds="600"   
  14.         memoryStoreEvictionPolicy="LFU" />  
  15.   
  16.     <cache name="baseCache"   
  17.         eternal="false"   
  18.         maxElementsInMemory="500"  
  19.         overflowToDisk="false"   
  20.         diskPersistent="false"   
  21.         timeToIdleSeconds="0"  
  22.         timeToLiveSeconds="300"   
  23.         memoryStoreEvictionPolicy="LFU" />  
  24.   
  25. </ehcache>  

这里是定义缓存策略

eternal="false"   // 元素是否永恒,如果是就永不过期(必须设置)
  maxElementsInMemory="1000" // 缓存容量的内存最大值(必须设置)
  overflowToDisk="false"  // 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)
  diskPersistent="false"  // 磁盘缓存在VM重新启动时是否保持(默认为false)
  timeToIdleSeconds="0" // 导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0
  timeToLiveSeconds="600" // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期
  memoryStoreEvictionPolicy="LFU" // 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU

 

然后在添加 cache-config.xml 内容如下:

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  6.     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring     
  7.     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">  
  8.   
  9.     <ehcache:annotation-driven />  
  10.   
  11.     <ehcache:config cache-manager="cacheManager">  
  12.         <ehcache:evict-expired-elements  
  13.             interval="60" />  
  14.     </ehcache:config>  
  15.   
  16.     <bean id="cacheManager"  
  17.         class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
  18.         <property name="configLocation" value="classpath:ehcache.xml" />  
  19.     </bean>  
  20.   
  21. </beans>  


然后在DAO层做如下配置:

  1.         @TriggersRemove(cacheName="baseCache",removeAll=true)  
  2. public Entity save(Entity entity) throws CrudException {  
  3.     return entity;  
  4. }  
  5.   
  6. @TriggersRemove(cacheName="baseCache",removeAll=true)  
  7. public Entity update(Entity entity) throws CrudException {  
  8.     return entity;  
  9. }  
  10.   
  11. @TriggersRemove(cacheName="baseCache",removeAll=true)  
  12. public void del(Entity entity) throws CrudException {  
  13.         }  
  14.   
  15. @Cacheable(cacheName="baseCache")   @SuppressWarnings("unchecked")  
  16. public List<Entity> findAll() throws SearchException {  
  17.     return list;  
  18. }  

 

@Cacheable(cacheName="baseCache")

这个注解就是做到缓存数据,cacheName对应ehcache.xml 中配置


 @TriggersRemove(cacheName="baseCache",removeAll=true)

这个注解的作用就是当数据发生变化的时候清除缓存,做到数据同步

 

如何知道有没生效:

新建一个单元测试,在类中添加两个搜索的方法,然后执行这个单元测试,然后查看两个方法执行的时间(Eclipse的单元测试可以查看)

如果第二个搜索方法的运行时间为0那就说吗成功了,如图

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值