OSCache 缓存对象的总结

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代码 
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("来自缓存!"+ 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("--缓存没有!"+sdf.format(new Date()));              
55         }   
56         sendResponse(file,response);   
57         return;   
58     }   
59     /**  
60      * 把文件用响应流写出  
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 }   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值