期待更好更穩(wěn)定的開源FrameWork的出現(xiàn),讓我們一起努力吧!  
          日歷
          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234
          統(tǒng)計(jì)
          • 隨筆 - 78
          • 文章 - 1
          • 評(píng)論 - 29
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案(42)

          文章檔案(37)

          相冊(cè)

          搜索

          •  

          積分與排名

          • 積分 - 45228
          • 排名 - 1064

          最新隨筆

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

           

          今天下午,想了好久,終于決定要用OSCache來(lái)緩存我的報(bào)表統(tǒng)計(jì)了,要不覺得效率很低。呵呵,當(dāng)然我是選擇了opensymphony的OSCache,它可以緩存任何Java對(duì)象以及JSP程序,以下是今天下午的成果,寫了一個(gè)通用類,再在action里面緩存一下。試了一下,效果還算不錯(cuò)。
          package com.finegold.digimus.comm;

          import com.opensymphony.oscache.base.persistence.CachePersistenceException;

          /**
           * @author 汪心利 2007-7-5
          *@copyRight WWW.FINEGOLD.COM.CN
          *
          */
          public interface Cache {
           /**
            * 根據(jù)key獲取cache里的對(duì)象
            *
            * @param key
            * @return
            * @throws CachePersistenceException
            */
           Object get(Object key) throws CachePersistenceException;

           /**
            * 根據(jù)key以及time獲取cache里的對(duì)象,對(duì)應(yīng) inputOSCache(Object key, Object value, int
            * i)方法添加進(jìn)去的對(duì)象
            *
            * @param key
            * @param time
            * @return
            * @throws CachePersistenceException
            */
           Object get(Object key, int time) throws CachePersistenceException;

           /**
            * 將object添加cache
            *
            * @param key
            * @param value
            * @throws CachePersistenceException
            */
           void inputOSCache(Object key, Object value)
             throws CachePersistenceException;

           /**
            * 將object添加cache
            *
            * @param key
            * @param value
            * @param i
            * @throws CachePersistenceException
            */
           void inputOSCache(Object key, Object value, int i)
             throws CachePersistenceException;

           /**
            * 根據(jù)key刪除object
            *
            * @param key
            * @throws CachePersistenceException
            */
           void remove(Object key) throws CachePersistenceException;

           /**
            * 清空cache
            *
            * @throws CachePersistenceException
            */
           void clear() throws CachePersistenceException;

           /**
            * 銷毀cache
            *
            * @throws CachePersistenceException
            */
           void destroy() throws CachePersistenceException;

           /**
            * 根據(jù)time 獲取key
            *
            * @param time
            * @return
            * @throws CachePersistenceException
            */
           Object getkey(int time) throws CachePersistenceException;
          }

          接口的實(shí)現(xiàn)類:
          package com.finegold.digimus.comm;

          /**
           * @author BlueSKy_itwangxinli 2007-7-5
          *@copyRight WWW.FINEGOLD.COM.CN
           */
          import java.util.Properties;

          import org.apache.commons.lang.RandomStringUtils;

          import com.opensymphony.oscache.base.EntryRefreshPolicy;
          import com.opensymphony.oscache.base.NeedsRefreshException;
          import com.opensymphony.oscache.base.persistence.CachePersistenceException;
          import com.opensymphony.oscache.general.GeneralCacheAdministrator;
          import com.opensymphony.oscache.web.filter.ExpiresRefreshPolicy;

          /**
           * OSCache 緩存共用類
           *
           * @author 汪心利 2007-7-5
           */
          public class OSCache implements Cache {
           /**
            * 通用緩存管理類 administrator
            */
           private GeneralCacheAdministrator cache;

           private static OSCache instance;

           public OSCache() {
            this.cache = new GeneralCacheAdministrator();
           }

           /**
            * 指定加載加載cache.propeties fiel 默認(rèn)情況下加載classes目錄下的OScache.properties
            *
            * @param prop
            */
           public OSCache(Properties prop) {
            this.cache = new GeneralCacheAdministrator(prop);
           }

           /**
            * 返回OSCache 的Instance 單態(tài)模式
            *
            * @return
            */
           public synchronized static OSCache getInstance() {

            if (instance == null) {
             instance = new OSCache();
            }
            return instance;
           }

           /**
            * 設(shè)置緩存容量 default value 請(qǐng)查看oscache配置文件,OSCache系統(tǒng)本身默認(rèn)值unlimited
            *
            * @param cacheCapacity
            */
           public void setCacheCapacity(int cacheCapacity) {
            this.cache.setCacheCapacity(cacheCapacity);
           }

           /**
            * 根據(jù)Key獲取cache里緩存的object
            *
            * @param key
            *            查找關(guān)鍵字
            */
           public Object get(Object key) throws CachePersistenceException {
            try {
             return this.cache.getFromCache(String.valueOf(key));
            } catch (NeedsRefreshException e) {
             cache.cancelUpdate(String.valueOf(key));
             return null;
            }
           }

           /**
            * 根據(jù)key和time獲取緩存的object
            *
            * @param key
            *            查找的key
            * @param time
            *            (最準(zhǔn)確的含義-->)How long the object can stay in cache in seconds
            */
           public Object get(Object key, int time) throws CachePersistenceException {
            try {
             return this.cache.getFromCache(String.valueOf(key), time);
            } catch (NeedsRefreshException e) {
             cache.cancelUpdate(String.valueOf(key));
             return null;
            }
           }

           /**
            * 盡量不要使用該方法 根據(jù)time 獲取key
            *
            * @param time
            *            時(shí)間
            */
           public Object getkey(int time) throws CachePersistenceException {
            String key = RandomStringUtils.randomAlphanumeric(10);
            try {
             while (this.cache.getFromCache(key) != null) {
              key = RandomStringUtils.randomAlphanumeric(10);
             }
             return key;
            } catch (NeedsRefreshException e) {
             return key;
            }
           }

           /**
            * 緩存對(duì)象
            */
           public void inputOSCache(Object key, Object value)
             throws CachePersistenceException {
            this.cache.putInCache(String.valueOf(key), value);
           }

           /**
            * 緩存對(duì)象
            *
            * @param key
            *            緩存對(duì)象的key
            * @param value
            *            緩存對(duì)象的value
            * @param n
            *            緩存對(duì)象有效時(shí)間
            */
           public void inputOSCache(Object key, Object value, int n)
             throws CachePersistenceException {
            EntryRefreshPolicy Policy = new ExpiresRefreshPolicy(n);
            this.cache.putInCache(String.valueOf(key), value, Policy);
           }

           /**
            * 根據(jù)key從cache里 刪除object
            *
            * @param 要?jiǎng)h除緩存對(duì)象的key
            */
           public void remove(Object key) throws CachePersistenceException {
            this.cache.flushEntry(String.valueOf(key));
           }

           /**
            * 清空所有的緩存
            */
           public void clear() throws CachePersistenceException {
            this.cache.flushAll();
           }

           /**
            * 銷毀緩存
            */
           public void destroy() throws CachePersistenceException {
            this.cache.destroy();
           }
          }
          配置web.xml
           <filter>
                <filter-name>CacheFilter</filter-name>
          <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
           <init-param>
                   <param-name>time</param-name>
                   <param-value>600</param-value>
                </init-param>
                <init-param>
                   <param-name>scope</param-name>
                   <param-value>session</param-value>
                </init-param>
          </filter>



          posted on 2007-07-05 17:53 BlueSky_itwangxinli 閱讀(663) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
           
          Copyright © BlueSky_itwangxinli Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 崇信县| 新龙县| 修武县| 韶山市| 甘洛县| 庆城县| 句容市| 鱼台县| 琼结县| 长沙市| 登封市| 红桥区| 千阳县| 万盛区| 淳安县| 育儿| 通江县| 山东省| 普洱| 青州市| 达孜县| 淮北市| 静宁县| 清河县| 周宁县| 武鸣县| 林口县| 东方市| 汉阴县| 邳州市| 武平县| 全南县| 崇义县| 济阳县| 松原市| 尚志市| 枞阳县| 淳化县| 黎川县| 恭城| 台安县|