国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
利用Spring aop 自帶的ehcache來緩存對象。
1.采用ehcache來緩存得到的對象結(jié)合Spring aop實(shí)現(xiàn)通過MethodCacheInterceptor類攔截器來實(shí)現(xiàn):
java代碼: 

/*
* 創(chuàng)建日期 2005-3-15
*
* TODO 要更改此生成的文件的模板,請轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
*/

package com.cnsi.softer.interceptor.MethodCacheInterceptor;



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;


/**
* @author Administrator
*
* TODO 要更改此生成的類型注釋的模板,請轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
*/

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();
                // TODO 自動(dòng)生成構(gòu)造函數(shù)存根
        }

          /**
           * 主方法
           * 如果某方法可被緩存就緩存其結(jié)果
           * 方法結(jié)果必須是可序列化的(serializable)
           */

          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("在緩存中查找方法返回的對象!");
            String cacheKey = getCacheKey(targetName, methodName, arguments);
            Element element = cache.get(cacheKey);
            if (element == null) {
                 logger.debug("正在攔截方法!");
                 result = invocation.proceed();

                 logger.debug("正在緩存對象!");
                 element = new Element(cacheKey, (Serializable) result);
                 cache.put(element);
            }
            return element.getValue();
          }

          /**
           *創(chuàng)建一個(gè)緩存對象的標(biāo)識: targetName.methodName.argument0.argument1...
           */

          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();
          }



        /* £ǚÇ Javadoc)
        * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
        */

        public void afterPropertiesSet() throws Exception {
                Assert.notNull(cache, "需要一個(gè)緩存. 使用setCache(Cache)分配一個(gè).");

        }

}


當(dāng)程序中的如create方法,update方法,delete方法修改了內(nèi)存中的對象值時(shí),通過如下的通知來除去此對象:
java代碼: 

/*
* 創(chuàng)建日期 2005-3-15
*
* TODO 要更改此生成的文件的模板,請轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
*/

package com.cnsi.softer.interceptor.MethodCacheInterceptor;

import java.lang.reflect.Method;

import net.sf.ehcache.Cache;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

/**
* @author Administrator
*
* TODO 要更改此生成的類型注釋的模板,請轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - 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();
        }

        /* (非 Javadoc)
        * @see org.springframework.aop.AfterReturningAdvice#afterReturning(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
        */

        public void afterReturning(Object arg0, Method arg1, Object[] arg2,
                        Object arg3) throws Throwable {
                    StringBuffer buffer = new StringBuffer();
                    buffer.append(arg3.getClass().getName()).append(".").append(arg1.getName());
                    if (arg2 != null&&arg2.length != 0){
                              for (int i=0; i<arg2.length; i++) {
                                buffer.append(".")
                                  .append(arg2[i]);
                              }
                           
                    }
                    cache.remove(buffer);
        }

        /* (非 Javadoc)
        * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
        */

        public void afterPropertiesSet() throws Exception {
                Assert.notNull(cache, "需要一個(gè)緩存. 使用setCache(Cache)分配一個(gè).");
    }

}


2.我的配置文件如下:
由于發(fā)現(xiàn)直接把文件貼出來,定義的bean標(biāo)簽都不見了,所以還是上傳一個(gè)文件附件吧,大家打開文件看看

假若可以這樣配置是不是在serviceCache和serviceCacheAdvice對象中就會(huì)存在多個(gè)target對象,對內(nèi)存的要求就會(huì)增大呢?
3.這樣配置后,就可以了對巴,用不用在其他地方注入newsServiceCache,和newsServiceCacheAdvice。是自動(dòng)執(zhí)行的對巴
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
springboot+mybatis集成自定義緩存ehcache用法筆記
hibernate4 二級緩存demo實(shí)例
java并發(fā)(二十九)構(gòu)建高效且可伸縮的結(jié)果緩存
ehcache
Mybatis 整合 ehcache緩存
Hibernate 緩存機(jī)制
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服