<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    kapok

    垃圾桶,嘿嘿,我藏的這么深你們還能找到啊,真牛!

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      455 隨筆 :: 0 文章 :: 76 評(píng)論 :: 0 Trackbacks

    我們都知道Hibernate可以用ehcache來作為Second Level Cache.主要是針對(duì)POJO的緩存,而且緩存的讀取

    Hibernate中是寫死.實(shí)際運(yùn)用中感覺很不靈活.今天看到一篇介紹利用Spring Interceptor 來緩存指定

    方法結(jié)果的例子,感覺很不錯(cuò),充分體會(huì)到AOP的強(qiáng)大力量 :)

    首先配置ehcache.xml

     <ehcache>

        <diskStore path="java.io.tmpdir"/>

        <cache name="org.taha.cache.METHOD_CACHE"

            maxElementsInMemory="300"

            eternal="false"

            timeToIdleSeconds="500"

            timeToLiveSeconds="500"

            overflowToDisk="true"

            />

    </ehcache>

     

    接下在Spring配置文件中定義Ehcache組件

     

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

      <property name="configLocation">

        <value>classpath:ehcache.xml</value>

      </property>

    </bean>

     

    <bean id="methodCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">

      <property name="cacheManager">

        <ref local="cacheManager"/>

      </property>

      <property name="cacheName">

        <value>org.taha.cache.METHOD_CACHE</value>

      </property>

    </bean>

    建立我們自己的方法攔截器MethodCacheInterceptor.

    MethodCacheInterceptor實(shí)現(xiàn)了org.aopalliance.intercept.MethodInterceptor接口.

    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.springframework.beans.factory.InitializingBean;

     

    /**

     * 攔截器,用于緩存方法返回結(jié)果.

     *

     * @version $Id: MethodCacheInterceptor.java v 1.0 2004-11-28 14:57:00 Znjq Exp $

     * @author <a href="mailto:znjq1980@etang.com">Znjq </a>

     */

    public class MethodCacheInterceptor implements MethodInterceptor,

            InitializingBean {

        private Cache cache;

     

        /**

         * sets cache name to be used

         */

        public void setCache(Cache cache) {

            this.cache = cache;

        }

     

        /*

         * (non-Javadoc)

         *

         * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)

         */

        public Object invoke(MethodInvocation invocation) throws Throwable {

           String targetName = invocation.getThis().getClass().getName();

            String methodName = invocation.getMethod().getName();

            Object[] arguments = invocation.getArguments();

            Object result;

     

            String cacheKey = getCacheKey(targetName, methodName, arguments);

            Element element = cache.get(cacheKey);

            if (element == null) {

                //call target/sub-interceptor

                result = invocation.proceed();

     

                //cache method result

                element = new Element(cacheKey, (Serializable) result);

                cache.put(element);

            }

            return element.getValue();

        }

     

        /**

         * creates cache key: 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();

        }

     

        /*

         * (non-Javadoc)

         *

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

         */

        public void afterPropertiesSet() throws Exception {

            // TODO Auto-generated method stub

     

        }

    }

    invoke方法中,首先根據(jù)key查詢緩存(key=className + methodName + arguments)

    ,緩存中存在則返回,否之調(diào)用invocation.proceed()返回結(jié)果.

    Spring配置文件中定義攔截器

    <bean id="methodCacheInterceptor" class="org.taha.interceptor.MethodCacheInterceptor">

      <property name="cache">

        <ref local="methodCache" />

      </property>

    </bean>

     

    <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

      <property name="advice">

        <ref local="methodCacheInterceptor"/>

      </property>

      <property name="patterns">

        <list>

          <value>.*methodOne</value>

          <value>.*methodTwo</value>

        </list>

      </property>

    </bean>

     

    <bean id="myBean" class="org.springframework.aop.framework.ProxyFactoryBean">

      <property name="target">

       <bean class="org.taha.beans.MyBean"/>

      </property>

      <property name="interceptorNames">

        <list>

          <value>methodCachePointCut</value>

        </list>

      </property>

    </bean>

    這里org.springframework.aop.support.RegexpMethodPointcutAdvisor是一個(gè)正規(guī)表達(dá)式切入點(diǎn),

    使用Perl 5的正規(guī)表達(dá)式的語法, Jakarta ORO(有空寫個(gè)文檔,自己研究一下).

      <property name="target">

       <bean class="org.taha.beans.MyBean"/>

      </property>

    org.taha.beans.MyBean是我們需要做緩存處理的類.

    methodCachePointCut

    <value>.*methodOne</value>

    <value>.*methodTwo</value>

    則是指定的模式匹配方法,對(duì)應(yīng)于org.taha.beans.MyBean中的方法. 這里指定了2個(gè)方法需要做緩存處理.

    呵呵,就是這么簡單.這樣每次對(duì)org.taha.beans.MyBeanmethodOne方法進(jìn)行調(diào)用,都會(huì)首先從緩存查找,

    其次才會(huì)查詢數(shù)據(jù)庫. 這樣我就不需要在xx.hbm.xml來指定討厭的cache.也不需要在開發(fā)階段來關(guān)心緩存.

    一切AOP搞定.. ^_^

    posted on 2005-04-17 23:05 笨笨 閱讀(2781) 評(píng)論(0)  編輯  收藏 所屬分類: J2EEHibernateAndSpringALL
    主站蜘蛛池模板: 日韩一区二区三区免费播放| 亚洲αv在线精品糸列| 久久亚洲中文字幕无码| 情人伊人久久综合亚洲| 国产男女猛烈无遮挡免费视频网站 | 国产精品免费看香蕉| 妞干网免费视频观看| 男女啪啪永久免费观看网站| 日韩免费视频在线观看| 国产免费拔擦拔擦8x| 亚洲精品视频免费| 成人免费视频网址| 国产美女无遮挡免费网站| 免费在线观看毛片| 久久伊人亚洲AV无码网站| 国产极品粉嫩泬免费观看| 免费jjzz在线播放国产| 亚洲乱亚洲乱少妇无码| 亚洲精品无码mv在线观看网站| 国产成人免费手机在线观看视频 | 免费大香伊蕉在人线国产 | 最近免费视频中文字幕大全| 国产精品九九久久免费视频| 又硬又粗又长又爽免费看| a级毛片毛片免费观看久潮喷| 久久黄色免费网站| 国产日韩一区二区三免费高清| 暖暖免费中文在线日本| 亚洲av成人一区二区三区在线播放| 国产精品久久久久久亚洲小说| 亚洲国产成a人v在线观看| 亚洲AV无码片一区二区三区| 四虎精品成人免费视频| 人人玩人人添人人澡免费| 国产妇乱子伦视频免费| 免费能直接在线观看黄的视频| 在线看片人成视频免费无遮挡| 亚洲AV蜜桃永久无码精品| 亚洲AV无码一区二区乱子伦| 亚洲AV无码一区二区三区在线| 看一级毛片免费观看视频|