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

背景

         声明,如果你不关心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的包一般项目里会有,换成最新的版本即可),下载地址如下:

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

 

  1. 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.    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>  

 

  1.     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>  

 

  1.     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里面的方法上是可以的。

 

  1.    2. 清除缓存

 

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

  

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

4.      测试

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

 

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

 

  1.     3.    请求清除缓存方法。

 

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

 

5.      其他

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

 

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

 

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

 

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

 

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

---------------------------------------------------

 

缓存与饼干,spring mvc使用ehcache

需要用到的jar包

?
1
2
3
4
ehcache- 2.7 . 5 .jar(主程序)
ehcache-spring-annotations- 1.2 . 0 .jar(注解)
guava-r09.jar(依赖)
slf4j-api- 1.6 . 6 .jar(依赖)

配置文件

spring配置中需要添加如下内容

头部

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
xmlns:cache= "http://www.springframework.org/schema/cache"
xsi:schemaLocation
http: //www.springframework.org/schema/cache 
http: //www.springframework.org/schema/cache/spring-cache-3.2.xsd 
 
<!-- 缓存配置 -->
<!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
<cache:annotation-driven cache-manager= "cacheManager" />
 
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3. 1 开始提供的) -->
<!-- <bean id= "cacheManager" class = "org.springframework.cache.support.SimpleCacheManager" >
     <property name= "caches" > <set> <bean name= "myCache" class = "org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" />
     </set> </property> </bean> -->
 
<!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
<!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
<bean id= "cacheManagerFactory"
     class = "org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
     <property name= "configLocation" value= "classpath:ehcache.xml" />
</bean>
<bean id= "cacheManager" class = "org.springframework.cache.ehcache.EhCacheCacheManager" >
     <property name= "cacheManager" ref= "cacheManagerFactory" />
</bean>
ehcache.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<ehcache>
<diskStore path= "java.io.tmpdir" />
<defaultCache
        maxElementsInMemory= "1000"
        eternal= "false"
        timeToIdleSeconds= "120"
        timeToLiveSeconds= "120"
        overflowToDisk= "false" />
<cache name= "myCache"
        maxElementsOnDisk= "20000"
        maxElementsInMemory= "2000"
        eternal= "false"
        overflowToDisk= "true"
        diskPersistent= "true" />
<cache name= "cacheTest"
        maxElementsOnDisk= "20000"
        maxElementsInMemory= "2000"
        eternal= "false"
        overflowToDisk= "true"
        diskPersistent= "true" />
</ehcache>

示例代码

cache一般用在和数据库交互的地方service

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
示例
 
package com.service;
 
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.aft.site.yey.dao.NoticeDao;
import com.aft.site.yey.entity.Notice;
import com.app.jdbc.core.ToolUtil;
 
/**
  * @author: yangyang 2013年10月21日
  * @since JDK 1.6
  */
@Service ( "XXXNoticeService" )
public class NoticeService {
     private static Log logger = LogFactory.getLog(NoticeService. class );
     @Resource (name = "XXXNoticeDao" )
     private NoticeDao dao;
 
     /**
      * status = 0 指未删除
      * */
     @Cacheable (value = "cacheTest" ,key= "'noticelist'" )
     public List<Notice> topN( int begin, int end) {
         LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
         orderby.put( "publish_time" , "desc" );
         Map<String, String> where = new HashMap<String, String>();
         where.put(dao.STATUS + " = ? " , dao.NORMAL_CODE.toString());
         //TODO:delete
         System.out.println( "list:" );
         logger.info( "[list ]" );
         return dao.find(where, orderby, begin, end);
     }
 
     @CacheEvict (value = "cacheTest" ,key= "'noticelist'" )
     public void delete(String id) {
         //TODO:delete
         System.out.println( "delete:" );
         logger.info( "delete " );
         dao.delete(id, false );
     }
 
     @CacheEvict (value = "cacheTest" , allEntries = true )
     public void save(Notice notice) {
         notice.setRowid(ToolUtil.getUUID());
         notice.setStatus(dao.NORMAL_CODE);
         notice.setPublish_time( new Date());
         // TODO:yeyid的获得方式
         notice.setYey_id( "123" );
         dao.insert(notice);
         //TODO:delete
         System.out.println( "save:" );
         logger.info( "save " );
     }
 
     public Notice get(String id) {
         return dao.findById(id);
     }
 
     //@CachePut(value = "cacheTest",key="#notice.getRowid()")
     public void update(Notice notice) {
         Map<String, String> set = new HashMap<String, String>();
         LinkedHashMap<String, String> where = new LinkedHashMap<String, String>();
         set.put( "title" , notice.getTitle());
         set.put( "author" , notice.getAuthor());
         set.put( "content" , notice.getContent());
         where.put(dao.getIdColumnName() + "=?" , notice.getRowid());
         dao.update(set, where);
         System.out.println( "update:" );
         logger.info( "update " );
     }
 
}

使用说明

cache主要注解使用:@Cacheable,@CacheEvict,@CachePut

缓存是这样的,取值时在方法(A)调用前查一下缓存中是否有目标值,缓存存在的话直接从缓存中拿出不再去执行方法(A),这也是最基本的@Cacheable的概念;

缓存中有值需要更新怎么办?使用@CacheEvict来更新,这个注解的意思是删除掉缓存里面的某个值,从而达到更新缓存的效果。关于缓存更新,例如,取topN个对象,第一次取的时候比如是前1~10个,缓存中存这1~10的一个集合对象,第二次取的时候直接从缓存中拿,这没问题,现在是这样的,假设数据库中删除了1~10个元素中的任意一个值,这样数据库中的topN与缓存中的topN就不同步了,下次你在前台取topN的时候,因为缓存里面有这个对象,根据之前的介绍(取值时在方法(A)调用前查一下缓存中是否有目标值,缓存存在的话直接从缓存中拿出不再去执行方法(A)),方法A被略过,查的值不是真正的topN了,因此需要在add或者delete之后删除掉原来的缓存,保持数据一致。其他情景请自行考虑。

根据缓存的特性,如何做到既要保证方法被调用,又希望结果被缓存呢?直接使用@CachePut,他与@Cacheable的区别就在与方法是会被执行的。

注解里面属性解释,@Cacheable 与@CachePut一样, @CacheEvict还有和删除有关的两个属性:

  1. value:缓存的名称,在spring配置文件中定义,必须指定至少一个

    • 例如:@Cacheable(value=”mycache”) 或者 
      @Cacheable(value={”cache1”,”cache2”}
  2. key:缓存的key,(缓存是键值对儿)可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

    • 例如: 
      @Cacheable(value=”testcache”,key=”#userName”)
    • 使用字符串”'sss'”
    • 调用对象getName方法key=”#userName.getName()”
  3. condition:缓存的条件,可以为空,使用SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

    • 例如: 
      @Cacheable(value=”testcache”,condition=”#userName.length()>2”)
  4. allEntries:是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存

    • 例如: 
      @CachEvict(value=”testcache”,allEntries=true)
  5. beforeInvocation:是否在方法执行前就清空,缺省为 false,如果指定为 true,则在*方法还没有执行的时候就清空缓存,缺省情况下为false,这样如果方法执行抛出异常,则不会清空缓存

    • 例如: 
      @CachEvict(value=”testcache”,beforeInvocation=true)

下一步

集群的同步问题,未完待续。

相关链接

非常详细的spring mvc和cache的使用博客

http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/

官网文档

http://docs.spring.io/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html

ehcache介绍

http://my.oschina.net/coolfire368/blog/123377

spring mvc整合 ehcache

http://blog.csdn.net/jadyer/article/details/12257865

 

------------------------------------------------------------

由于这两天用的springmvc  和 mybatis 的搭建的web 框架  然后准备用缓存数据,就简单记录下

准备:

googlecode 的ehcache

 这个可以在https://code.google.com/p/ehcache-spring-annotations/  下载,下载之后拿出来要用到的jar包

    

下载的压缩包中的注解包

ehcache-spring-annotations-1.2.0.jar  

下载的压缩包中lib 目录下的

ehcache-core-2.4.5.jar
guava-r09.jar 

  由于其他的一些包都在spring mvc   中已经有了,只需要添加这几个进去。

配置ehcache.xml

在src  目录下新增配置 ehcache.xml  如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
/**
 * 
 * 缓存配置
 * @author yq 
 * @date 2014.9.10
 * 
 */ -->
 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  
 updateCheck="false">  
 <diskStore path="java.io.tmpdir" />  
 <defaultCache eternal="false"   
   maxElementsInMemory="1000"  
   overflowToDisk="false"   
   diskPersistent="false"   
   timeToIdleSeconds="0"  
   timeToLiveSeconds="600"   
   memoryStoreEvictionPolicy="LRU" />  
  
 <cache name="CustomerCache"   
   eternal="false"  
   maxElementsInMemory="100"  
   overflowToDisk="false"  
   diskPersistent="false"   
   timeToIdleSeconds="0"  
   timeToLiveSeconds="300"  
   memoryStoreEvictionPolicy="LRU" />  
  
</ehcache>  

此配置中 可以有多个cache ,方便管理我们程序中的cache。

配置spring.xml

1  <ehcache:annotation-driven cache-manager="ehCacheManager" />   
2  <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
3        <property name="configLocation" value="classpath:ehcache.xml" />    
4    </bean>  

 

接下来就到我们的代码 Service 层 试用缓存了:

@Cacheable(cacheName = "CustomerCache")
    public Customer GetCustomerByCid(int cid){
        System.out.print("----------------------------------------------------------");
        return cd.GetCustomerByCid(cid);
    }
    //修改客户
    @TriggersRemove(cacheName ={"CustomerCache"},removeAll=true)
    public int UpdateCustomer(Customer cus){
        return cd.UpdateCustomer(cus);
    }

 

注意包的import  ,是这两个

import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.TriggersRemove;

 

以上就已经搭建好了。 

当我们两次调用 service 中的 GetCustomerByCid 方法时候控制台只有一次sql打印表示成功了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值