您的位置:首页 > 其它

ehCache 配置

2015-07-01 15:39 302 查看
package com.jy.modules.cms;

import java.io.Serializable;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

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;
//编写自定义拦截器
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 Object invoke(MethodInvocation invocation)
throws Throwable
{
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();

logger.debug("Find object from cache is " + this.cache.getName());

String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = this.cache.get(cacheKey);

if (element == null) {
logger.debug("Hold up method , Get method result and create cache........!");
Object result = invocation.proceed();
element = new Element(cacheKey, (Serializable)result);
this.cache.put(element);
}
return element.getValue();
}

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 < arguments.length; i++) {
sb.append(".").append(arguments[i]);
}
}
return sb.toString();
}

public void afterPropertiesSet()
throws Exception
{
Assert.notNull(this.cache, "Need a cache. Please use setCache(Cache) create it.");
}
}


<!-- 采用ehCache的工厂进行缓存配置  start -->
<!-- 定义ehCache的工厂,并设置所使用的Cache name -->
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="ehcacheManager"/>
<property name="cacheName">
<value>SYS_DATE_CACHE</value>
</property>
</bean>

<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"/>
</bean>

<!-- find/create cache拦截器   -->
<bean id="methodCacheInterceptor" class="com.jy.modules.cms.MethodCacheInterceptor">
<property name="cache" ref="ehCache" />
</bean>
<!-- flush cache拦截器   -->
<bean id="methodCacheAfterAdvice" class="com.jy.platform.core.ehcache.MethodCacheAfterAdvice">
<property name="cache" ref="ehCache" />
</bean>

<!-- 采用ehCache的工厂进行缓存配置  end -->

<bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="methodCacheInterceptor"/>
<property name="patterns">
<list>
<value>com.jy.modules.platform.sysorg.service.*query.*</value>
<value>com.jy.modules.platform.sysorg.service.*find.*</value>
<value>com.jy.modules.platform.sysorg.service.*search.*</value>
<value>com.jy.modules.platform.sysconfig.service.*query.*</value>
<value>com.jy.modules.platform.sysconfig.service.*find.*</value>
<value>com.jy.modules.platform.sysconfig.service.*search.*</value>
<value>com.jy.modules.platform.sysdict.service.*query.*</value>
<value>com.jy.modules.platform.sysdict.service.*find.*</value>
<value>com.jy.modules.platform.sysdict.service.*search.*</value>
</list>
</property>
</bean>
<bean id="methodCachePointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="methodCacheAfterAdvice"/>
<property name="patterns">
<list>
<value>com.jy.modules.platform.sysorg.service.*update.*</value>
<value>com.jy.modules.platform.sysorg.service.*delete.*</value>
<value>com.jy.modules.platform.sysorg.service.*insert.*</value>
<value>com.jy.modules.platform.sysconfig.service.*update.*</value>
<value>com.jy.modules.platform.sysconfig.service.*delete.*</value>
<value>com.jy.modules.platform.sysconfig.service.*insert.*</value>
<value>com.jy.modules.platform.sysdict.service.*update.*</value>
<value>com.jy.modules.platform.sysdict.service.*delete.*</value>
<value>com.jy.modules.platform.sysdict.service.*insert.*</value>
</list>
</property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: