码迷,mamicode.com
首页 > 系统相关 > 详细

ehCache 配置

时间:2015-07-01 17:45:19      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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> 

 

ehCache 配置

标签:

原文地址:http://www.cnblogs.com/yy123/p/4613408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!