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

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

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

    kapok

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

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      455 隨筆 :: 0 文章 :: 76 評論 :: 0 Trackbacks

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

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

    方法結果的例子,感覺很不錯,充分體會到AOP的強大力量 :)

    首先配置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實現了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;

     

    /**

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

     *

     * @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方法中,首先根據key查詢緩存(key=className + methodName + arguments)

    ,緩存中存在則返回,否之調用invocation.proceed()返回結果.

    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是一個正規表達式切入點,

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

      <property name="target">

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

      </property>

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

    methodCachePointCut

    <value>.*methodOne</value>

    <value>.*methodTwo</value>

    則是指定的模式匹配方法,對應于org.taha.beans.MyBean中的方法. 這里指定了2個方法需要做緩存處理.

    呵呵,就是這么簡單.這樣每次對org.taha.beans.MyBeanmethodOne方法進行調用,都會首先從緩存查找,

    其次才會查詢數據庫. 這樣我就不需要在xx.hbm.xml來指定討厭的cache.也不需要在開發階段來關心緩存.

    一切AOP搞定.. ^_^

    posted on 2005-04-17 23:05 笨笨 閱讀(2784) 評論(0)  編輯  收藏 所屬分類: J2EEHibernateAndSpringALL
    主站蜘蛛池模板: 亚洲av无码不卡久久| 亚洲国产精品久久久久婷婷软件| 亚洲精品在线免费观看视频| 精品国产免费一区二区三区香蕉| 亚洲成av人片天堂网老年人| 五月婷婷免费视频| 一级毛片高清免费播放| 亚洲第一页综合图片自拍| 美女18毛片免费视频| 亚洲国产精品自在拍在线播放| 色欲aⅴ亚洲情无码AV| 日韩成人在线免费视频| 在线亚洲精品视频| 亚洲精品岛国片在线观看| 国产无遮挡裸体免费视频在线观看| 亚洲精品无码午夜福利中文字幕| 亚洲男人的天堂久久精品| 成年在线网站免费观看无广告| 亚洲综合一区国产精品| 又粗又硬又黄又爽的免费视频| 一级毛片正片免费视频手机看| 亚洲国产三级在线观看| 精品熟女少妇a∨免费久久| 亚洲av成人综合网| 免费一级一片一毛片| 久久最新免费视频| 亚洲精品在线网站| 国产免费爽爽视频免费可以看| 一区二区三区免费精品视频| 亚洲男人天堂av| 毛片免费观看网站| 国产免费久久久久久无码| 亚洲人成影院在线| 手机看片久久国产免费| 在线观看黄片免费入口不卡| 亚洲福利一区二区精品秒拍| 日韩伦理片电影在线免费观看| 美女网站在线观看视频免费的 | 国产免费小视频在线观看| 中文永久免费观看网站| 亚洲免费人成视频观看|