OSCache是當前運用最廣的緩存方案。其主被用的最廣泛功能是緩存頁面,這里主要是用其緩存文件對象。
運用OScache的步驟:
1.取得oscache.jar 文件放到 /WEB-INF/lib 或相應類庫目錄 目錄中。
2.oscache.jar依賴commons-collections.jar包。如果你的jdk版本為1.3,
建議在lib中加入Apache Common Lib 的commons-collections.jar包。
如jdk是1.4以上則不必要。
3.src根目錄或發布環境的/WEB-INF/classes 目錄下放入oscache.properties。
cache.memory
值為true 或 false ,默認為在內存中作緩存,
如設置為false,那cache只能緩存到數據庫或硬盤中,那cache還有什么意義:)
cache.capacity
緩存元素個數
cache.persistence.class
持久化緩存類,如此類打開,則必須設置cache.path信息
cache.cluster 相關
為集群設置信息。
如cache.cluster.multicast.ip為廣播IP地址
cache.cluster.properties為集群屬性
cache.path
硬盤持久化時存放文件的目錄。如果目錄不存在OSCache會自動創建。
Windows系統:c:\\myapp\\cache。其它:/opt/myapp/cache
cache.persistence.overflow.only*
是否只有當指定的內存緩存已經滿時才進行持久化。推薦使用true,flase是為向后兼容。
cache.unlimited.disk
硬盤緩存是否有限制。缺省為cache.capacity指定的值
運用:
com.opensymphony.oscache.general.GeneralCacheAdministrator
GeneralCacheAdministrator主要對實現持久化對象的保存以及取出的相關的操作。
Object getFromCache(String key) //根據key獲取緩存對象
Object getFromCache(String key , int refreshInterval)//refreshInterval時間內,根據key獲取緩存對象
void putInCache(String key ,Object obj) //保存被緩存對象
void flushAll() //刪除所有被緩存的對象
void flushAll(Date date) //在指定的時間去刪除所有被緩存的對象
void cancelUpdate(String key) //取消未確定的更新
java 代碼
- package com.iflytek;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
-
- import com.opensymphony.oscache.base.NeedsRefreshException;
- import com.opensymphony.oscache.general.GeneralCacheAdministrator;
-
-
- public class DisplayChart extends HttpServlet {
-
-
-
-
- public DisplayChart() {
- super();
- }
-
-
-
-
-
-
- public void init() throws ServletException {
- return;
- }
-
-
- public static GeneralCacheAdministrator cacheAdmin = new GeneralCacheAdministrator();
- public void service(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException {
-
- String path = getServletContext().getRealPath("/");
- File file = null;
- SimpleDateFormat sdf= new SimpleDateFormat("hh-mm-ss");
- try {
- file = (File)cacheAdmin.getFromCache(sdf.format(new Date()));
- System.out.println("來自緩存!"+ sdf.format(new Date()));
- } catch (NeedsRefreshException e) {
- file = new File(path+"xmls\\Pipe11.xml");
- cacheAdmin.putInCache(sdf.format(new Date()), file);
- System.out.println("--緩存沒有!"+sdf.format(new Date()));
- }
- sendResponse(file,response);
- return;
- }
-
-
-
-
-
-
- public void sendResponse(File file,HttpServletResponse response) throws IOException{
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
- BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
- byte[] input = new byte[1024];
- boolean eof = false;
- while (!eof) {
- int length = bis.read(input);
- if (length == -1) {
- eof = true;
- }
- else {
- bos.write(input, 0, length);
- }
- }
- bos.flush();
- bis.close();
- bos.close();
- }
-
- }
|