`
IT民工%
  • 浏览: 45647 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

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 version="1.0" encoding="UTF-8"?>  
<!-- 
/**
 * 
 * 缓存配置
 * @author zyz 
 * @date 2013年7月2日
 * 
 */ -->
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"  
    xsi:schemaLocation="  
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
                                       
    <ehcache:annotation-driven />  
      
    <ehcache:config cache-manager="cacheManager">  
		<ehcache:evict-expired-elements interval="60" />  
    </ehcache:config>  
      
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        <property name="configLocation"  value="classpath:spring/ehcache.xml"/>  
    </bean>
</beans>

 

    2. web.xml

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext.xml,classpath:spring/ehcacheApplication.xml</param-value>
	</context-param>

 

    3.   ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
/**
 * 
 * 缓存配置
 * @author zyz 
 * @date 2013年7月2日
 * 
 */ -->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">  
      
    <diskStore path="user.home/web_ehcache/" />   
      
    <defaultCache  
            maxElementsInMemory="3000"  
            eternal="false"  
            timeToIdleSeconds="3600"  
            timeToLiveSeconds="3600"  
            overflowToDisk="true"  
            diskPersistent="false"  
            diskExpiryThreadIntervalSeconds="100"  
            memoryStoreEvictionPolicy="LRU"  
            />  
    <cache name="mallListCache"  
           maxElementsInMemory="3000"  
           eternal="false"  
           overflowToDisk="true"  
           timeToIdleSeconds="36000"  
           timeToLiveSeconds="36000"  
           memoryStoreEvictionPolicy="LFU"  
            />  
</ehcache>

 

3.      代码

1.  缓存

 

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

 

@Cacheable(cacheName = "mallListCache")
public int find() {
 List<MallBean> list=mallDao.findMallResBean();
	Return list.size();
}

  

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

 

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

 

   2. 清除缓存

 

@TriggersRemove(cacheName="mallListCache",removeAll=true) 
    public void flush(){
       log.info("清除缓存!");
    }

  

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,超时设置将被忽略,在未触发清除缓存方法之前,对象从不过期。

分享到:
评论
2 楼 achenbj 2013-11-27  
挺好的,就是我配置的怎么都不生效.
1 楼 a137268431 2013-09-24  
不错!!!挺好的有过研究!!!就是不知道这么做和给hibernate做缓存有什么区别

相关推荐

Global site tag (gtag.js) - Google Analytics