背景
声明,如果你不关心java缓存解决方案的全貌,只是急着解决问题,请略过背景部分。
在互联网应用中,由于并发量比传统的企业级应用会高出很多,所以处理大并发的问题就显得尤为重要。在硬件资源一定的情况下,在软件层面上解决高并发问题会比较经济实惠一些。解决并发的根本在于提高系统的响应时间与单位时间的吞吐量。解决问题的思路可分两个维度,一是提高系统的单位时间内的运算效率(比如集群),二是减少系统不必要的开支(比如缓存)。缓存又会分为客户端缓存与服务器端缓存,本文就javaEE项目的服务器端缓存方案展开介绍。
根据网上资料Java缓存解决方案有多种,用的比较多,中文资料也比较多的有:oscache、ehcache。如何选择参考了网上前辈们的评价。如下:
http://blog.sina.com.cn/s/blog_46d5caa40100ka9z.html
由于我们系统采用springMVC,所以在选定ehcache后,就着重研究ehcache与SpringMVC的整合,注解当然是要用到的。参考资料:
http://hanqunfeng.iteye.com/blog/603719
在了解了spring与ehcache整合之后发现这东西是不是很简单,但是笔者在查找资料的时候又发现了更简单解决方案,google为spring与ehcache整合提供了封装包,参考资料如下:
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 + ehcache(google ehcache-spring-annotations),基于注解解决服务器端数据缓存。
1. 下载所需jar包
1. Ehcache的jar:
ehcache-2.7.1.jar
slf4j-api-1.6.6.jar
slf4j-jdk14-1.6.6.jar
down下来之后lib里面会有以上三个包(这两个slf4j的包一般项目里会有,换成最新的版本即可),下载地址如下:
2. ehcache-spring-annotations 的jar:
ehcache-spring-annotations-1.2.0.jar
ehcache-spring-annotations-1.2.0-sources.jar
down下来之后,从文件夹取以上2个包,其他的包忽视。下载地址:
当然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中的两个方法find与flush,缓存方法将service中返回的size返给页面,在浏览器里面去请求这两个方法。
2. 首次请求缓存方法,发现后台打印sql查询信息,再在数据库中添加数据,再次请求缓存方法,发现后台不打印sql查询信息,页面显示list的size不变。证明缓存成功。
3. 请求清除缓存方法。
4. 再次请求缓存方法,会发现后台打印sql查询信息,页面显示list的size发生变化。证明清除缓存成功。
5. 其他
1.对于清除缓存的方法,ehcache提供了两种,一种是在ehcache.xml中配置的时间过后自动清除,一种是在数据发生变化后触发清除。个人感觉第二种比较好。可以将
@TriggersRemove(cacheName="mallListCache",removeAll=true)
这句代码加到service里面的添加、删除、修改方法上。这样只要这几个方法有调用,缓存自动清除。
2. 但是我这块的情况比较特殊,缓存的这个系统A只是提供查询功能,相当于一个中间服务。而添加、删除、修改方法是在另外一个系统B中做的。所以我为这两个项目提供http接口,当然也可以是其他形式的接口比如webservice,当B系统中调用添加、删除、修改方法时调用接口来清除A中的缓存。
3. 如果是通过触发的方式清除缓存,那时间可以配置为永不过期。在ehcache.xml中将eternal="true" 设置为true,超时设置将被忽略,在未触发清除缓存方法之前,对象从不过期。
相关推荐
<artifactId>mybatis-spring <version>1.3.2 <!-- MyBatis End --> ``` #### 六、其他配置 除了上述依赖外,还需要配置数据库连接池、Spring 配置文件等。这里推荐使用 Druid 作为数据库连接池,并通过 Spring ...
1)Spring 【AOP核心】 com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar spring-aspects-4.0.0.RELEASE.jar ...
springmvc 各种jar包 apache springmvc 等 0,activation-1.1.1.jar 1,aliyun-openservices-1.0.12.jar 2,antlr-2.7.7.jar 3,aopalliance-1.0.jar 4,asm-all-3.1.jar 5,bcprov-jdk15on-147.jar 6,c3p0-0.9.2.1....
spring springmvc mybatis 整合 【spring 核心】 commons-logging-1.1.3.jar spring-aop-4.3.11.RELEASE.jar spring-aspects-4.3.11.RELEASE.jar spring-beans-4.3.11.RELEASE.jar spring-context-4.3.11....
10. **Integration with Other Technologies**: Spring MVC 可以与各种数据库技术(如 Hibernate, JPA)、缓存(如 Ehcache, Redis)、消息队列(如 RabbitMQ, ActiveMQ)等无缝集成,增强了其在复杂企业级应用中的...