• ehCache 配置


    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> 
  • 相关阅读:
    C++小知识之Vector用法
    云计算和大数据入门
    C++解析JSON之JsonCPP
    OSS研究
    linux内核--进程地址空间(三)
    学习笔记:修改网吧计费系统
    学习笔记:找回电脑开机密码
    例说C#深拷贝与浅拷贝
    JBossESB教程(一)——开发环境的搭建
    Java集合---ConcurrentHashMap原理分析
  • 原文地址:https://www.cnblogs.com/yy123/p/4613408.html
Copyright © 2020-2023  润新知