---
title: "利用Spring的AOP来配置和管理你的二级缓存(EHCache)"
author: "freeze06"
date: "2011-03-22T13:30:24Z"
tags: []
categories: ["编程语言"]
slug: "spring-aop-ehcache"
draft: false
lastmod: "2011-03-22T13:30:24Z"
id: 970444
url: "https://freeze06.iteye.com/blog/970444"
---

利用Spring的AOP来配置和管理你的二级缓存(EHCache) 2008-03-29 17:12:20

&lt;!--showHead end--&gt;

标签：[配置](http://blog.51cto.com/tagindex.php?keyword=%C5%E4%D6%C3) [管理](http://blog.51cto.com/tagindex.php?keyword=%B9%DC%C0%ED) [Spring](http://blog.51cto.com/tagindex.php?keyword=Spring) [AOP](http://blog.51cto.com/tagindex.php?keyword=AOP)

原创作品，允许转载，转载时请务必以超链接形式标明文章 [原始出处](/340746/68789) 、作者信息和本声明。否则将追究法律责任。[http://weijie.blog.51cto.com/340746/68789](/340746/68789)

&lt;!--正文 begin--&gt;

**利用Spring的AOP来配置和管理你的二级缓存(EHCache)**

       如果我们的项目中采用的是Spring+hibernate来构建的，在缓存方面，我们一定会首先想到Spring自带的EHCache缓存工具，在Spring中集成了目前比较流行的缓存策略EHCache，现在用的比较多的还有像OSCache,MemCached.这些应该是当前用的最多的缓存工具了。

       在Spring+hibernate的这样的框架中，EHCache应该属于二级缓存了，我们知道在Hibernate中已经默认的使用了一级缓存，也就是在Session中。二级缓存应该是SessionFactory的范围了。二级缓存默认不会起作用的，这就需要我们简单的配置一下就可以了。

       在配置之前，我先说明一点，缓存从理论上来说是可以提高你网站系统的性能，但前提就是你要保证你有一个良好的架构设计。比如用Spring+Hibernate构建的系统，如果用单个服务器，用Spring自带的EHCache来做二级缓存是再好不过了。如果你的系统是分布式的系统，有多台服务器，那么MemCached是最好的选择了，一般来说MemCached在做缓存这一块，要比EHCache和OSCache的性能要好点，但是并不是所有的网站用MemCached都能达到事半功倍的，它虽然是比较好，但它有一个前提，那就是你有多台服务器，是分布式的。这样用MemCached对系统的性能一定OK。因为Memcached是“分布式”的内存对象缓存系统，那么就是说，那些不需要“分布”的，不需要共享的，或者干脆规模小到只有一台服务器的应用， MemCached不会带来任何好处，相反还会拖慢系统效率，因为网络连接同样需要资源 .OSCache这个缓存机制的限制就比较少了。它和EHCache差不多。

        在Spring+Hibernate中整合EHCache只需简单的三步。

        第一步：配置缓存文件ehcache.xml，默认放到src目录下。下面是简单的配置。

   &lt;ehcache&gt;  
    &lt;!—设置缓存文件 .data 的创建路径。

         如果该路径是 Java 系统参数，当前虚拟机会重新赋值。

         下面的参数这样解释：  
         user.home – 用户主目录  
         user.dir      – 用户当前工作目录  
         java.io.tmpdir – 默认临时文件路径，就是在tomcat的temp目录 --&gt;  
    &lt;diskStore path="java.io.tmpdir"/&gt;

    &lt;!—缺省缓存配置。CacheManager 会把这些配置应用到程序中。

        下列属性是 defaultCache 必须的：

        maxInMemory           - 设定内存中创建对象的最大值。  
        eternal                        - 设置元素（译注：内存中对象）是否永久驻留。如果是，将忽略超  
                                              时限制且元素永不消亡。  
        timeToIdleSeconds  - 设置某个元素消亡前的停顿时间。  
                                              也就是在一个元素消亡之前，两次访问时间的最大时间间隔值。  
                                              这只能在元素不是永久驻留时有效（译注：如果对象永恒不灭，则  
                                              设置该属性也无用）。  
                                              如果该值是 0 就意味着元素可以停顿无穷长的时间。  
        timeToLiveSeconds - 为元素设置消亡前的生存时间。  
                                               也就是一个元素从构建到消亡的最大时间间隔值。  
                                               这只能在元素不是永久驻留时有效。  
        overflowToDisk        - 设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘  
                                               上。  
        --&gt;  
     &lt;!--timeToLiveSeconds的时间一定要大于等于timeToIdleSeconds的时间按--&gt;  
    &lt;cache name="DEFAULT\_CACHE"  
        maxElementsInMemory="1000"  
        eternal="false"  
        timeToIdleSeconds="500"  
        timeToLiveSeconds="500"  
        overflowToDisk="true"  
        /&gt;  
&lt;/ehcache&gt;

  上面有一个默认的缓存配置，还有一个我们自己配置的缓存，在应用程序中如果不指明缓存的话，就会默认的使用默认的配置属性。

    第二步：用Spring中的强大机制，面向切面的设计AOP.来编写两个类文件，MethodCacheAfterAdvice.java(主要是对脏东西的同步更新)和MethodCacheInterceptor.java（主要使用拦截器来为要缓存的对象建立缓存并缓存）。拦截器的实现机制其实就是我们常用的过滤器。它和过滤器的工作原理一样。以下是这两个文件。

**MethodCacheInterceptor.java**

 public class MethodCacheInterceptor implements MethodInterceptor,  
  InitializingBean {  
 private static final Log logger = LogFactory  
   .getLog(MethodCacheInterceptor.class);

 private Cache cache;

 public void setCache(Cache cache) {  
  this.cache = cache;  
 }

 public MethodCacheInterceptor() {  
  super();  
 }

 /\**  
  * 拦截Service/DAO的方法，并查找该结果是否存在，如果存在就返回cache中的值， 否则，返回数据库查询结果，并将查询结果放入cache  
  \*/  
 public Object invoke(MethodInvocation invocation) throws Throwable {  
  String targetName = invocation.getThis().getClass().getName();  
  String methodName = invocation.getMethod().getName();  
  Object\[] arguments = invocation.getArguments();  
  Object result;  
  logger.debug("Find object from cache is " + cache.getName());  
  String cacheKey = getCacheKey(targetName, methodName, arguments);  
  Element element = cache.get(cacheKey);  
  long startTime = System.currentTimeMillis();  
  if (element == null) {  
   logger  
     .debug("Hold up method , Get method result and create cache........!");  
   result = invocation.proceed();  
   element = new Element(cacheKey, (Serializable) result);  
   cache.put(element);  
   long endTime = System.currentTimeMillis();  
   logger.info(targetName + "." + methodName + " 方法被首次调用并被缓存。耗时"  
     + (endTime - startTime) + "毫秒" + " cacheKey:"  
     + element.getKey());  
  } else {  
   long endTime = System.currentTimeMillis();  
   logger.info(targetName + "." + methodName + " 结果从缓存中直接调用。耗时"  
     + (endTime - startTime) + "毫秒" + " cacheKey:"  
     + element.getKey());  
  }  
  return element.getValue();  
 }

 /\**  
  * 获得cache key的方法，cache key是Cache中一个Element的唯一标识 cache key包括 包名+类名+方法名+参数  
  \*/  
 private String getCacheKey(String targetName, String methodName,  
   Object\[] arguments) {  
  StringBuffer sb = new StringBuffer();  
  sb.append(targetName).append(".").append(methodName);  
  if ((arguments != null) && (arguments.length != 0)) {  
   for (int i = 0; i &lt; arguments.length; i++) {  
    sb.append(".").append(arguments\[i]);  
   }  
  }  
  return sb.toString();  
 }

 /\**  
  * implement InitializingBean，检查cache是否为空  
  \*/  
 public void afterPropertiesSet() throws Exception {  
  Assert.notNull(cache,  
    "Need a cache. Please use setCache(Cache) create it.");  
 }

}

   这个方法实现了两个接口，一个是MethodInterceptor（方法拦截），它主要是在方法的调用前后都可以执行。另一个InitializingBean （初始化Bean）它主要在方法调用之后做一下简单的检查，主要实现写在afterPropertiesSet()中，就可以了 。

**MethodCacheAfterAdvice** **.java**  
public class MethodCacheAfterAdvice implements AfterReturningAdvice,  
  InitializingBean {  
 private static final Log logger = LogFactory  
   .getLog(MethodCacheAfterAdvice.class);

 private Cache cache;

 public void setCache(Cache cache) {  
  this.cache = cache;  
 }

 public MethodCacheAfterAdvice() {  
  super();  
 }

 public void afterReturning(Object arg0, Method arg1, Object\[] arg2,  
   Object arg3) throws Throwable {  
  String className = arg3.getClass().getName();  
  List list = cache.getKeys();  
  for (int i = 0; i &lt; list.size(); i++) {  
   String cacheKey = String.valueOf(list.get(i));  
   if (cacheKey.startsWith(className)) {  
    cache.remove(cacheKey);  
    logger.debug("remove cache " + cacheKey);  
   }  
  }  
 }

 public void afterPropertiesSet() throws Exception {  
  Assert.notNull(cache,  
    "Need a cache. Please use setCache(Cache) create it.");  
 }  
}  
  这个方法主要是保证缓存的同步，保持与数据库的数据一致性。  
第三步：配置Bean了，applicationContext-ehcache.xml文件就是Spring中的Ioc（控制反转容器）的描述了。上面的只是简单的写了两个方法，具体的能起到什么作用，以及何时起作用，以及怎样用声明式的方式(AOP)和Bean结合。

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans xmlns="[\[url\]http://www.springframework.org/schema/beans\[/url\]](http://www.springframework.org/schema/beans)"  
 xmlns:xsi="[\[url\]http://www.w3.org/2001/XMLSchema-instance\[/url\]](http://www.w3.org/2001/XMLSchema-instance)"  
 xsi:schemaLocation="[\[url\]http://www.springframework.org/schema/beans\[/url\]](http://www.springframework.org/schema/beans) [\[url\]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd\[/url\]](http://www.springframework.org/schema/beans/spring-beans-2.0.xsd)"&gt;

 &lt;!-- 利用BeanNameAutoProxyCreator自动创建事务代理 --&gt;  
 &lt;bean id="transactionInterceptor"  
  class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;  
  &lt;property name="transactionManager"&gt;  
   &lt;ref bean="transactionManager" /&gt;  
  &lt;/property&gt;  
  &lt;!-- 配置事务属性 --&gt;  
  &lt;property name="transactionAttributes"&gt;  
   &lt;props&gt;  
    &lt;prop key="delete\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
    &lt;prop key="update\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
    &lt;prop key="save\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
    &lt;prop key="find\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
    &lt;prop key="get\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
   &lt;/props&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
   
 &lt;!-- 引用ehCache的配置 --&gt;  
 &lt;bean id="defaultCacheManager"  
  class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"&gt;  
  &lt;property name="configLocation"&gt;  
   &lt;value&gt;classpath:ehcache.xml&lt;/value&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

 &lt;!-- 定义ehCache的工厂，并设置所使用的Cache name --&gt;  
 &lt;bean id="ehCache"  
  class="org.springframework.cache.ehcache.EhCacheFactoryBean"&gt;  
  &lt;property name="cacheManager"&gt;  
   &lt;ref local="defaultCacheManager" /&gt;  
  &lt;/property&gt;  
  &lt;property name="cacheName"&gt;  
   &lt;value&gt;DEFAULT\_CACHE&lt;/value&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

 &lt;!-- find/create cache拦截器 --&gt;  
 &lt;bean id="methodCacheInterceptor"  
  class="com.w3cs.cache.ehcache.MethodCacheInterceptor"&gt;  
  &lt;property name="cache"&gt;  
   &lt;ref local="ehCache" /&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
 &lt;!-- flush cache拦截器 --&gt;  
 &lt;bean id="methodCacheAfterAdvice"  
  class="com.w3cs.cache.ehcache.MethodCacheAfterAdvice"&gt;  
  &lt;property name="cache"&gt;  
   &lt;ref local="ehCache" /&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

 &lt;bean id="methodCachePointCut"  
  class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"&gt;  
  &lt;property name="advice"&gt;  
   &lt;ref local="methodCacheInterceptor" /&gt;  
  &lt;/property&gt;  
  &lt;property name="patterns"&gt;  
   &lt;list&gt;  
    &lt;value&gt;.\*find.\*&lt;/value&gt;  
    &lt;value&gt;.\*get.\*&lt;/value&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
 &lt;bean id="methodCachePointCutAdvice"  
  class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"&gt;  
  &lt;property name="advice"&gt;  
   &lt;ref local="methodCacheAfterAdvice" /&gt;  
  &lt;/property&gt;  
  &lt;property name="patterns"&gt;  
   &lt;list&gt;  
    &lt;value&gt;.\*create.\*&lt;/value&gt;  
    &lt;value&gt;.\*update.\*&lt;/value&gt;  
    &lt;value&gt;.\*delete.\*&lt;/value&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

 &lt;!-- 自动代理 --&gt;  
 &lt;bean id="autoproxy"  
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;  
  &lt;!-- 可以是Service或DAO层（最好是针对业务层\*Service） --&gt;  
  &lt;property name="beanNames"&gt;  
   &lt;list&gt;  
    &lt;value&gt;\*DAO&lt;/value&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
  &lt;property name="interceptorNames"&gt;  
   &lt;list&gt;  
    &lt;value&gt;methodCachePointCut&lt;/value&gt;  
    &lt;value&gt;methodCachePointCutAdvice&lt;/value&gt;  
    &lt;value&gt;transactionInterceptor&lt;/value&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
&lt;/beans&gt;

        上面我是针对DAO层进行拦截并缓存的，最好是能在业务层进行拦截会更好，你可以根据你的系统具体的设计，如果没有业务层的话，对DAO层拦截也是可以的。拦截采用的是用正规表达式配置的。对find,get的方法只进行缓存，如果 create，update,delete方法进行缓存的同步。对一些频繁的操作最好不要用缓存，缓存的作用就是针对那些不经常变动的操作。

         只需这简单的三部就可以完成EHCache了。最好亲自试一试。我并没有针对里面对方法过细的讲解。其实都很简单，多看看就会明白了。不当之处，敬请原谅。

 

本文出自 “[魏杰的技术专栏](http://weijie.blog.51cto.com)” 博客，请务必保留此出处[http://weijie.blog.51cto.com/340746/68789](http://weijie.blog.51cto.com/340746/68789)