欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
利用Spring aop 自帶的ehcache來(lái)緩存對象。
1.采用ehcache來(lái)緩存得到的對象結合Spring aop實(shí)現通過(guò)MethodCacheInterceptor類(lèi)攔截器來(lái)實(shí)現:
java代碼: 

/*
* 創(chuàng )建日期 2005-3-15
*
* TODO 要更改此生成的文件的模板,請轉至
* 窗口 - 首選項 - 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 要更改此生成的類(lèi)型注釋的模板,請轉至
* 窗口 - 首選項 - 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)生成構造函數存根
        }

          /**
           * 主方法
           * 如果某方法可被緩存就緩存其結果
           * 方法結果必須是可序列化的(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è)緩存對象的標識: 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è).");

        }

}


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

/*
* 創(chuàng )建日期 2005-3-15
*
* TODO 要更改此生成的文件的模板,請轉至
* 窗口 - 首選項 - 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 要更改此生成的類(lèi)型注釋的模板,請轉至
* 窗口 - 首選項 - 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ā)現直接把文件貼出來(lái),定義的bean標簽都不見(jiàn)了,所以還是上傳一個(gè)文件附件吧,大家打開(kāi)文件看看

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

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久