SpringMVC + ehcache( ehcache-spring-annotations)基于注解的服务器端数据缓存

背景

         声明,如果你不关心java缓存解决方案的全貌,只是急着解决问题,请略过背景部分。

在互联网应用中,由于并发量比传统的企业级应用会高出很多,所以处理大并发的问题就显得尤为重要。在硬件资源一定的情况下,在软件层面上解决高并发问题会比较经济实惠一些。解决并发的根本在于提高系统的响应时间与单位时间的吞吐量。解决问题的思路可分两个维度,一是提高系统的单位时间内的运算效率(比如集群),二是减少系统不必要的开支(比如缓存)。缓存又会分为客户端缓存与服务器端缓存,本文就javaEE项目的服务器端缓存方案展开介绍。

根据网上资料Java缓存解决方案有多种,用的比较多,中文资料也比较多的有:oscacheehcache。如何选择参考了网上前辈们的评价。如下:

http://blog.sina.com.cn/s/blog_46d5caa40100ka9z.html

由于我们系统采用springMVC,所以在选定ehcache后,就着重研究ehcacheSpringMVC的整合,注解当然是要用到的。参考资料:

http://hanqunfeng.iteye.com/blog/603719

在了解了springehcache整合之后发现这东西是不是很简单,但是笔者在查找资料的时候又发现了更简单解决方案,googlespringehcache整合提供了封装包,参考资料如下:

http://luck332.iteye.com/blog/1001783

http://code.google.com/p/ehcache-spring-annotations/

http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/(推荐参考,虽然是e文的,但是基本可以看懂)

方案

         进入正题。

SpringMVC + ehcachegoogle  ehcache-spring-annotations),基于注解解决服务器端数据缓存。

1.      下载所需jar

1.         Ehcachejar

ehcache-2.7.1.jar

slf4j-api-1.6.6.jar

slf4j-jdk14-1.6.6.jar

down下来之后lib里面会有以上三个包(这两个slf4j的包一般项目里会有,换成最新的版本即可),下载地址如下:

http://ehcache.org/downloads/destination?name=ehcache-2.7.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache-2.7.2-distribution.tar.gz

 

2.         ehcache-spring-annotations jar

ehcache-spring-annotations-1.2.0.jar

ehcache-spring-annotations-1.2.0-sources.jar

down下来之后,从文件夹取以上2个包,其他的包忽视。下载地址:

http://code.google.com/p/ehcache-spring-annotations/downloads/detail?name=ehcache-spring-annotations-1.2.0-nodep.tar.gz

当然google也提供了开发版的包,里面提供了源码。jar还支持二维码下载,下载到手机上?不知道google咋想的。。。

2.      配置

   1.   ehcacheApplication.xml,该文件里面配置基本不变,需要将该文件加入web.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!--   
  3. /**  
  4.  *   
  5.  * 缓存配置  
  6.  * @author zyz   
  7.  * @date 2013年7月2日  
  8.  *   
  9.  */ -->  
  10. <beans xmlns="http://www.springframework.org/schema/beans"    
  11.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  12.     xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"    
  13.     xsi:schemaLocation="    
  14.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  15.     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring  
  16.     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">  
  17.                                          
  18.     <ehcache:annotation-driven />    
  19.         
  20.     <ehcache:config cache-manager="cacheManager">    
  21.         <ehcache:evict-expired-elements interval="60" />    
  22.     </ehcache:config>    
  23.         
  24.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
  25.         <property name="configLocation"  value="classpath:spring/ehcache.xml"/>    
  26.     </bean>  
  27. </beans>  

 

    2. web.xml

Xml代码   收藏代码
  1. <context-param>  
  2.         <param-name>contextConfigLocation</param-name>  
  3.         <param-value>classpath:spring/applicationContext.xml,classpath:spring/ehcacheApplication.xml</param-value>  
  4.     </context-param>  

 

    3.   ehcache.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!--   
  3. /**  
  4.  *   
  5.  * 缓存配置  
  6.  * @author zyz   
  7.  * @date 2013年7月2日  
  8.  *   
  9.  */ -->  
  10. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">    
  11.         
  12.     <diskStore path="user.home/web_ehcache/" />     
  13.         
  14.     <defaultCache    
  15.             maxElementsInMemory="3000"    
  16.             eternal="false"    
  17.             timeToIdleSeconds="3600"    
  18.             timeToLiveSeconds="3600"    
  19.             overflowToDisk="true"    
  20.             diskPersistent="false"    
  21.             diskExpiryThreadIntervalSeconds="100"    
  22.             memoryStoreEvictionPolicy="LRU"    
  23.             />    
  24.     <cache name="mallListCache"    
  25.            maxElementsInMemory="3000"    
  26.            eternal="false"    
  27.            overflowToDisk="true"    
  28.            timeToIdleSeconds="36000"    
  29.            timeToLiveSeconds="36000"    
  30.            memoryStoreEvictionPolicy="LFU"    
  31.             />    
  32. </ehcache>  

 

3.      代码

1.  缓存

 

给你需要缓存的方法加上如下这句

 

Java代码   收藏代码
  1. @Cacheable(cacheName = "mallListCache")  
  2. public int find() {  
  3.  List<MallBean> list=mallDao.findMallResBean();  
  4.     Return list.size();  
  5. }  

  

cacheName里面对应ehcache.xml中配置的<cache name="mallListCache"  

 

这里我想说,网上很多人都是加到dao上的,我是直接加到service(业务层)里面的方法上,因为我的业务层方法会对数据做处理,而我需要缓存整个处理结果,所以加到service里面的方法上是可以的。

 

   2. 清除缓存

 

Java代码   收藏代码
  1. @TriggersRemove(cacheName="mallListCache",removeAll=true)   
  2.     public void flush(){  
  3.        log.info("清除缓存!");  
  4.     }  

  

cacheName里面对应缓存里面的名称,removeAll=true 这句是必须加的,否则无法清除缓存。

4.      测试

    1.     我的测试方法是,在控制层提供了两个方法,缓存和清除缓存,分别调用service中的两个方法findflush,缓存方法将service中返回的size返给页面,在浏览器里面去请求这两个方法。

 

    2.    首次请求缓存方法,发现后台打印sql查询信息,再在数据库中添加数据,再次请求缓存方法,发现后台不打印sql查询信息,页面显示listsize不变。证明缓存成功。

 

    3.    请求清除缓存方法。

 

    4.   再次请求缓存方法,会发现后台打印sql查询信息,页面显示listsize发生变化。证明清除缓存成功。

 

5.      其他

    1.对于清除缓存的方法,ehcache提供了两种,一种是在ehcache.xml中配置的时间过后自动清除,一种是在数据发生变化后触发清除。个人感觉第二种比较好。可以将

 

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

 

这句代码加到service里面的添加、删除、修改方法上。这样只要这几个方法有调用,缓存自动清除。

 

    2. 但是我这块的情况比较特殊,缓存的这个系统A只是提供查询功能,相当于一个中间服务。而添加、删除、修改方法是在另外一个系统B中做的。所以我为这两个项目提供http接口,当然也可以是其他形式的接口比如webservice,当B系统中调用添加、删除、修改方法时调用接口来清除A中的缓存。

 

    3. 如果是通过触发的方式清除缓存,那时间可以配置为永不过期。在ehcache.xml中将eternal="true"  设置为true,超时设置将被忽略,在未触发清除缓存方法之前,对象从不过期。

 

猜你喜欢

转载自yzyzero.iteye.com/blog/2008189