OSCache 緩存對(duì)象的總結(jié)

          OSCache是當(dāng)前運(yùn)用最廣的緩存方案。其主被用的最廣泛功能是緩存頁(yè)面,這里主要是用其緩存文件對(duì)象。
          運(yùn)用OScache的步驟:
           1.取得oscache.jar 文件放到 /WEB-INF/lib 或相應(yīng)類庫(kù)目錄 目錄中。
           2.oscache.jar依賴commons-collections.jar包。如果你的jdk版本為1.3,
             建議在lib中加入Apache Common Lib 的commons-collections.jar包。
             如jdk是1.4以上則不必要。
           3.src根目錄或發(fā)布環(huán)境的/WEB-INF/classes 目錄下放入oscache.properties。
           
           cache.memory
           值為true 或 false ,默認(rèn)為在內(nèi)存中作緩存,
           如設(shè)置為false,那cache只能緩存到數(shù)據(jù)庫(kù)或硬盤中,那cache還有什么意義:)

           cache.capacity
           緩存元素個(gè)數(shù)

           cache.persistence.class
           持久化緩存類,如此類打開(kāi),則必須設(shè)置cache.path信息

           cache.cluster 相關(guān)
           為集群設(shè)置信息。
           如cache.cluster.multicast.ip為廣播IP地址
             cache.cluster.properties為集群屬性

          cache.path   
          硬盤持久化時(shí)存放文件的目錄。如果目錄不存在OSCache會(huì)自動(dòng)創(chuàng)建。
          Windows系統(tǒng):c:\\myapp\\cache。其它:/opt/myapp/cache

          cache.persistence.overflow.only*   
          是否只有當(dāng)指定的內(nèi)存緩存已經(jīng)滿時(shí)才進(jìn)行持久化。推薦使用true,flase是為向后兼容。

          cache.unlimited.disk   
          硬盤緩存是否有限制。缺省為cache.capacity指定的值

          運(yùn)用:
              com.opensymphony.oscache.general.GeneralCacheAdministrator
          GeneralCacheAdministrator主要對(duì)實(shí)現(xiàn)持久化對(duì)象的保存以及取出的相關(guān)的操作。

          Object getFromCache(String key)    //根據(jù)key獲取緩存對(duì)象
          Object getFromCache(String key , int refreshInterval)//refreshInterval時(shí)間內(nèi),根據(jù)key獲取緩存對(duì)象
          void putInCache(String key ,Object obj) //保存被緩存對(duì)象
          void flushAll()                                              //刪除所有被緩存的對(duì)象
          void flushAll(Date date)                            //在指定的時(shí)間去刪除所有被緩存的對(duì)象
          void cancelUpdate(String key)                //取消未確定的更新

          Java代碼
           1 package com.iflytek;   
           2    
           3 import java.io.BufferedInputStream;   
           4 import java.io.BufferedOutputStream;   
           5 import java.io.File;   
           6 import java.io.FileInputStream;   
           7 import java.io.IOException;   
           8 import java.text.SimpleDateFormat;   
           9 import java.util.Date;   
          10    
          11 import javax.servlet.ServletException;   
          12 import javax.servlet.http.HttpServlet;   
          13 import javax.servlet.http.HttpServletRequest;   
          14 import javax.servlet.http.HttpServletResponse;   
          15 import javax.servlet.http.HttpSession;   
          16    
          17 import com.opensymphony.oscache.base.NeedsRefreshException;   
          18 import com.opensymphony.oscache.general.GeneralCacheAdministrator;   
          19    
          20    
          21 public class DisplayChart extends HttpServlet {   
          22    
          23     /**  
          24      * Default constructor.  
          25      */   
          26     public DisplayChart() {   
          27         super();   
          28     }   
          29    
          30     /**  
          31      * Init method.  
          32      *  
          33      * @throws ServletException never.  
          34      */   
          35     public void init() throws ServletException {   
          36         return;   
          37     }   
          38    
          39       
          40     public static GeneralCacheAdministrator cacheAdmin = new GeneralCacheAdministrator();   
          41     public void service(HttpServletRequest request,    
          42                         HttpServletResponse response)   
          43             throws ServletException, IOException {   
          44        
          45         String path = getServletContext().getRealPath("/");    
          46         File file = null;   
          47         SimpleDateFormat sdf= new SimpleDateFormat("hh-mm-ss");   
          48         try {   
          49             file = (File)cacheAdmin.getFromCache(sdf.format(new Date()));   
          50             System.out.println("來(lái)自緩存!"+ sdf.format(new Date()));   
          51         } catch (NeedsRefreshException e) {   
          52             file = new File(path+"xmls\\Pipe11.xml");   
          53             cacheAdmin.putInCache(sdf.format(new Date()), file);   
          54             System.out.println("--緩存沒(méi)有!"+sdf.format(new Date()));              
          55         }   
          56         sendResponse(file,response);   
          57         return;   
          58     }   
          59     /**  
          60      * 把文件用響應(yīng)流寫出  
          61      * @param file  
          62      * @param response  
          63      * @throws IOException  
          64      */   
          65     public void sendResponse(File file,HttpServletResponse response) throws IOException{   
          66         BufferedInputStream  bis = new BufferedInputStream(new FileInputStream(file));   
          67         BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());   
          68         byte[] input = new byte[1024];   
          69         boolean eof = false;   
          70         while (!eof) {   
          71             int length = bis.read(input);   
          72             if (length == -1) {   
          73                 eof = true;   
          74             }    
          75             else {   
          76                 bos.write(input, 0, length);   
          77             }   
          78         }   
          79         bos.flush();   
          80         bis.close();   
          81         bos.close();   
          82     }   
          83    
          84 }   
          85 

          posted on 2007-07-26 15:58 萬(wàn)博 閱讀(1972) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導(dǎo)航

          統(tǒng)計(jì)

          留言簿(1)

          隨筆檔案(13)

          搜索

          積分與排名

          最新隨筆

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 高尔夫| 隆德县| 紫阳县| 黄骅市| 江安县| 昆山市| 蓬安县| 六盘水市| 墨竹工卡县| 商城县| 延长县| 凌云县| 株洲市| 甘孜县| 阳春市| 金沙县| 大石桥市| 文登市| 通榆县| 读书| 康乐县| 东莞市| 峨山| 潜江市| 奉化市| 武胜县| 平山县| 洪江市| 商洛市| 汶上县| 巴南区| 香港 | 文昌市| 嘉祥县| 佛教| 新巴尔虎右旗| 石屏县| 若羌县| 巴楚县| 横山县| 南华县|