posts - 9, comments - 4, trackbacks - 0, articles - 21
          Java]用OSCache進(jìn)行緩存對象

          1、OSCache是什么?
               OSCache標(biāo)記庫由OpenSymphony設(shè)計(jì),它是一種開創(chuàng)性的緩存方案,它提供了在現(xiàn)有JSP頁面之內(nèi)實(shí)現(xiàn)內(nèi)存緩存的功能。OSCache是個(gè) 一個(gè)被廣泛采用的高性能的J2EE緩存框架,OSCache還能應(yīng)用于任何Java應(yīng)用程序的普通的緩存解決方案。
          2、OSCache的特點(diǎn)
              (1) 緩存任何對象:你可以不受限制的緩存部分jsp頁面或HTTP請求,任何java對象都可以緩存。
              (2) 擁有全面的API:OSCache API允許你通過編程的方式來控制所有的OSCache特性。
              (3) 永久緩存:緩存能被配置寫入硬盤,因此允許在應(yīng)用服務(wù)器的多次生命周期間緩存創(chuàng)建開銷昂貴的數(shù)據(jù)。
              (4) 支持集群:集群緩存數(shù)據(jù)能被單個(gè)的進(jìn)行參數(shù)配置,不需要修改代碼。
              (5) 緩存過期:你可以有最大限度的控制緩存對象的過期,包括可插入式的刷新策略(如果默認(rèn)性能不能滿足需要時(shí))。
          3、OSCache的安裝與配置
              網(wǎng)上已經(jīng)有一個(gè)不錯(cuò)的使用教程:http://blog.csdn.net/ezerg/archive/2004/10/14/135769.aspx
          4、有關(guān)“用OSCache進(jìn)行緩存對象”的研究
              這個(gè)是我今天要說的東西。網(wǎng)上對于OSCache緩存Web頁面很多說明和例子,但對于緩存對象方面說得不多,我就把自已寫得一些東西放出來,讓大家看一看是怎樣緩存對象的!
              我基于GeneralCacheAdministrator類來寫的BaseCache類
             
          1. package com.klstudio.cache;   
          2.   
          3. import java.util.Date;   
          4.   
          5. import com.opensymphony.oscache.base.NeedsRefreshException;   
          6. import com.opensymphony.oscache.general.GeneralCacheAdministrator;   
          7.   
          8. public class BaseCache extends GeneralCacheAdministrator {   
          9.     //過期時(shí)間(單位為秒);   
          10.     private int refreshPeriod;   
          11.     //關(guān)鍵字前綴字符;   
          12.     private String keyPrefix;   
          13.        
          14.     private static final long serialVersionUID = -4397192926052141162L;   
          15.        
          16.     public BaseCache(String keyPrefix,int refreshPeriod){   
          17.         super();   
          18.         this.keyPrefix = keyPrefix;   
          19.         this.refreshPeriod = refreshPeriod;   
          20.     }   
          21.     //添加被緩存的對象;   
          22.     public void put(String key,Object value){   
          23.         this.putInCache(this.keyPrefix+"_"+key,value);   
          24.     }   
          25.     //刪除被緩存的對象;   
          26.     public void remove(String key){   
          27.         this.flushEntry(this.keyPrefix+"_"+key);   
          28.     }   
          29.     //刪除所有被緩存的對象;   
          30.     public void removeAll(Date date){   
          31.         this.flushAll(date);   
          32.     }   
          33.        
          34.     public void removeAll(){   
          35.         this.flushAll();   
          36.     }   
          37.     //獲取被緩存的對象;   
          38.     public Object get(String key) throws Exception{   
          39.         try{   
          40.             return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);   
          41.         } catch (NeedsRefreshException e) {   
          42.             this.cancelUpdate(this.keyPrefix+"_"+key);   
          43.             throw e;   
          44.         }   
          45.   
          46.     }   
          47.        
          48. }   
          49.   
          50.   

             通過CacheManager類來看怎樣緩存對象的,這個(gè)類中所用的News只是具體功能的類,我就不貼出來了,你可以自己寫一個(gè)!
             
          1. package com.klstudio;   
          2.   
          3. import com.klstudio.News;   
          4. import com.klstudio.cache.BaseCache;   
          5.   
          6. public class CacheManager {   
          7.        
          8.     private BaseCache newsCache;   
          9.   
          10.        
          11.     private static CacheManager instance;   
          12.     private static Object lock = new Object();   
          13.        
          14.     public CacheManager() {   
          15.         //這個(gè)根據(jù)配置文件來,初始BaseCache而已;   
          16.         newsCache = new BaseCache("news",1800);        
          17.     }   
          18.        
          19.     public static CacheManager getInstance(){   
          20.         if (instance == null){   
          21.             synchronized( lock ){   
          22.                 if (instance == null){   
          23.                     instance = new CacheManager();   
          24.                 }   
          25.             }   
          26.         }   
          27.         return instance;   
          28.     }   
          29.   
          30.     public void putNews(News news) {   
          31.         // TODO 自動生成方法存根   
          32.         newsCache.put(news.getID(),news);   
          33.     }   
          34.   
          35.     public void removeNews(String newsID) {   
          36.         // TODO 自動生成方法存根   
          37.         newsCache.remove(newsID);   
          38.     }   
          39.   
          40.     public News getNews(String newsID) {   
          41.         // TODO 自動生成方法存根   
          42.         try {   
          43.             return (News) newsCache.get(newsID);   
          44.         } catch (Exception e) {   
          45.             // TODO 自動生成 catch 塊   
          46.             System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());   
          47.             News news = new News(newsID);   
          48.             this.putNews(news);   
          49.             return news;   
          50.         }   
          51.     }   
          52.   
          53.     public void removeAllNews() {   
          54.         // TODO 自動生成方法存根   
          55.         newsCache.removeAll();   
          56.     }   
          57.   
          58. }   
          59.   
          主站蜘蛛池模板: 锦州市| 平江县| 辽宁省| 资源县| 玉环县| 麟游县| 辉南县| 开化县| 仁寿县| 延川县| 小金县| 襄樊市| 垦利县| 安图县| 酉阳| 祥云县| 巴中市| 利津县| 溧阳市| 紫阳县| 合水县| 临沭县| 称多县| 株洲市| 哈巴河县| 清涧县| 开化县| 筠连县| 仙居县| 资兴市| 连山| 将乐县| 叶城县| 嘉善县| 什邡市| 灵武市| 阳原县| 陵川县| 鹿泉市| 大港区| 河津市|