http://blog.csdn.net/upyaya/archive/2007/05/21/1619411.aspx
导言
从 Spring 1.1.1 开始,
EHCache 就作为一种通用缓存解决方案集成进 Spring。
我将示范拦截器的例子,它能把方法返回的结果缓存起来。
利用
Spring IoC
配置
EHCache
在 Spring 里配置 EHCache 很简单。你只需一个 ehcache.xml 文件,该文件用于配置 EHCache:
<
ehcache
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
—设置缓存文件 .data 的创建路径。
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
如果该路径是 Java 系统参数,当前虚拟机会重新赋值。
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
下面的参数这样解释:
user.home – 用户主目录
user.dir – 用户当前工作目录
java.io.tmpdir – 默认临时文件路径 --
>
<
diskStore
path
="java.io.tmpdir"
/>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
—缺省缓存配置。CacheManager 会把这些配置应用到程序中。
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
下列属性是 defaultCache 必须的:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
maxInMemory - 设定内存中创建对象的最大值。
eternal - 设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超
时限制且元素永不消亡。
timeToIdleSeconds - 设置某个元素消亡前的停顿时间。
也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。
这只能在元素不是永久驻留时有效(译注:如果对象永恒不灭,则
设置该属性也无用)。
如果该值是 0 就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds - 为元素设置消亡前的生存时间。
也就是一个元素从构建到消亡的最大时间间隔值。
这只能在元素不是永久驻留时有效。
overflowToDisk - 设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘
上。
--
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
cache
name
="org.taha.cache.METHOD_CACHE"
maxElementsInMemory
="300"
eternal
="false"
timeToIdleSeconds
="500"
timeToLiveSeconds
="500"
overflowToDisk
="true"
/>
</
ehcache
>
拦截器将使用 ”org.taha.cache.METHOD_CACHE” 区域缓存方法返回结果。下面利用 Spring IoC 让 bean 来访问这一区域。
<!--
====================== 缓存 =======================
-->
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
bean
id
="cacheManager"
class
="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
>
<
property
name
="configLocation"
>
<
value
>
classpath:ehcache.xml
</
value
>
</
property
>
</
bean
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
bean
id
="methodCache"
class
="org.springframework.cache.ehcache.EhCacheFactoryBean"
>
<
property
name
="cacheManager"
>
<
ref
local
="cacheManager"
/>
</
property
>
<
property
name
="cacheName"
>
<
value
>
org.taha.cache.METHOD_CACHE
</
value
>
</
property
>
</
bean
>
构建我们的
MethodCacheInterceptor
com.ph.serviceportal.infoboard.util.MethodCacheInterceptor
package
com.ph.serviceportal.infoboard.util;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.io.Serializable;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
net.sf.ehcache.Cache;
import
net.sf.ehcache.Element;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.aopalliance.intercept.MethodInterceptor;
import
org.aopalliance.intercept.MethodInvocation;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.springframework.beans.factory.InitializingBean;
import
org.springframework.util.Assert;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
class
MethodCacheInterceptor
implements
MethodInterceptor,
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
InitializingBean
...
{
private static final Log logger = LogFactory
.getLog(MethodCacheInterceptor.class);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private Cache cache;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setCache(Cache cache) ...{
this.cache = cache;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
*
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public MethodCacheInterceptor() ...{
super();
// TODO 自动生成构造函数存根
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* 主方法
* 如果某方法可被缓存就缓存其结果
* 方法结果必须是可序列化的(serializable)
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Object invoke(MethodInvocation invocation) throws Throwable ...{
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();
Object result;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
logger.debug("在缓存中查找方法返回的对象!");
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = cache.get(cacheKey);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (element == null) ...{
logger.debug("正在拦截方法!");
result = invocation.proceed();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
logger.debug("正在缓存对象!");
element = new Element(cacheKey, (Serializable)result);
cache.put(element);
}
return element.getValue();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
*创建一个缓存对象的标识: targetName.methodName.argument0.argument1...
*/
private String getCacheKey(String targetName, String methodName,
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Object[] arguments) ...{
StringBuffer sb = new StringBuffer();
sb.append(targetName).append(".").append(methodName);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ((arguments != null) && (arguments.length != 0)) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int i = 0; i < arguments.length; i++) ...{
sb.append(".").append(arguments[i]);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return sb.toString();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//* (非 Javadoc)
* @see org.springframework.beans.factory.InitializingBeanafterPropertiesSet()
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void afterPropertiesSet() throws Exception ...{
Assert.notNull(cache, "需要一个缓存. 使用setCache(Cache)分配一个.");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
}
com.ph.serviceportal.infoboard.util.MethodCacheAfterAdvice
package
com.ph.serviceportal.infoboard.util;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.lang.reflect.Method;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
net.sf.ehcache.Cache;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.springframework.aop.AfterReturningAdvice;
import
org.springframework.beans.factory.InitializingBean;
import
org.springframework.util.Assert;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
class
MethodCacheAfterAdvice
implements
AfterReturningAdvice,
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
InitializingBean
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private Cache cache;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setCache(Cache cache) ...{
this.cache = cache;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
*
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public MethodCacheAfterAdvice() ...{
super();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* (非 Javadoc)
*
* @see org.springframework.aop.AfterReturningAdviceafterReturning(java.lang.Object,
* java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
*/
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Object arg3) throws Throwable ...{
StringBuffer buffer = new StringBuffer();
buffer.append(arg3.getClass().getName()).append(".").append(
arg1.getName());
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (arg2 != null && arg2.length != 0) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int i = 0; i < arg2.length; i++) ...{
buffer.append(".").append(arg2[i]);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
cache.remove(buffer);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* (非 Javadoc)
*
* @see org.springframework.beans.factory.InitializingBeanafterPropertiesSet()
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void afterPropertiesSet() throws Exception ...{
Assert.notNull(cache, "需要一个缓存. 使用setCache(Cache)分配一个.");
}
}
MethodCacheInterceptor 代码说明了:
- 默认条件下,所有方法返回结果都被缓存了(methodNames 是 null)
- 缓存区利用 IoC 形成
- cacheKey 的生成还包括方法参数的因素(译注:参数的改变会影响 cacheKey)
使用
MethodCacheInterceptor
下面摘录了怎样配置 MethodCacheInterceptor and MethodCacheAfterAdvice:
<
bean
id
="methodCacheInterceptor"
class
="com.ph.serviceportal.infoboard.util.MethodCacheInterceptor"
>
<
property
name
="cache"
>
<
ref
local
="methodCache"
/>
</
property
>
</
bean
>
<
bean
id
="methodCacheAfterAdvice"
class
="com.ph.serviceportal.infoboard.util.MethodCacheAfterAdvice"
>
<
property
name
="cache"
>
<
ref
local
="methodCache"
/>
</
property
>
</
bean
>
<
bean
id
="methodCachePointCut"
class
="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
>
<
property
name
="advice"
>
<
ref
local
="methodCacheInterceptor"
/>
</
property
>
<
property
name
="patterns"
>
<
list
>
<
value
>
.*find.*
</
value
>
<
value
>
.*get.*
</
value
>
</
list
>
</
property
>
</
bean
>
<
bean
id
="methodCacheAdvicePointCut"
class
="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
>
<
property
name
="advice"
>
<
ref
local
="methodCacheAfterAdvice"
/>
</
property
>
<
property
name
="patterns"
>
<
list
>
<
value
>
.*upd.*
</
value
>
<
value
>
.*save.*
</
value
>
<
value
>
.*delete.*
</
value
>
</
list
>
</
property
>
</
bean
>
<
bean
name
="infoboardService"
class
="com.ph.serviceportal.infoboard.service.InfoBoardServiceImpl"
>
<
property
name
="dataRetriever"
ref
="infoboardDAO"
/>
</
bean
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
bean
name
="infoboardDAO"
class
="com.ph.serviceportal.infoboard.dao.InfoboardDAOImpl"
>
<
property
name
="infoboardDAO"
>
<
ref
local
="infoboardServiceRpcProxy"
/>
</
property
>
</
bean
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
bean
id
="infoboardServiceRpcProxy"
class
="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean"
>
<
property
name
="serviceInterface"
>
<
value
>
com.hp.serviceportal.infoboard.dao.IInfoboardDAO
</
value
>
</
property
>
<
property
name
="wsdlDocumentUrl"
>
<
value
>
http://qatest17.mro.cpqcorp.net/infoboard_ws_1100/infoboard.asmx?wsdl
</
value
>
</
property
>
<
property
name
="namespaceUri"
>
<
value
>
http://tempuri.org/InfoBoard_WS/Service1
</
value
>
</
property
>
<
property
name
="serviceName"
>
<
value
>
Service1
</
value
>
</
property
>
<
property
name
="portName"
>
<
value
>
Service1Soap
</
value
>
</
property
>
<
property
name
="portInterface"
>
<
value
>
org.tempuri.InfoBoard_WS.Service1.Service1Soap
</
value
>
</
property
>
</
bean
>
对infoboardservice进行增强:
<
bean
id
="infoboardServiceCacheProxy"
class
="org.springframework.aop.framework.ProxyFactoryBean"
>
<
property
name
="proxyInterfaces"
>
<
value
>
com.ph.serviceportal.infoboard.service.IInfoBoardService
</
value
>
</
property
>
<
property
name
="target"
>
<
ref
local
="infoboardService"
/>
</
property
>
<
property
name
="interceptorNames"
>
<
list
>
<
value
>
methodCachePointCut
</
value
>
<
value
>
methodCacheAfterAdvice
</
value
>
</
list
>
</
property
>
</
bean
>
<完>