Posted on 2008-04-10 18:13
一步一步努力向上爬 閱讀(819)
評論(0) 編輯 收藏 所屬分類:
J2SE學(xué)習(xí)
Java]用OSCache進(jìn)行緩存對象
1、OSCache是什么?
OSCache標(biāo)記庫由OpenSymphony設(shè)計,它是一種開創(chuàng)性的緩存方案,它提供了在現(xiàn)有JSP頁面之內(nèi)實(shí)現(xiàn)內(nèi)存緩存的功能。OSCache是個
一個被廣泛采用的高性能的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ù)能被單個的進(jìn)行參數(shù)配置,不需要修改代碼。
(5) 緩存過期:你可以有最大限度的控制緩存對象的過期,包括可插入式的刷新策略(如果默認(rèn)性能不能滿足需要時)。
3、OSCache的安裝與配置
網(wǎng)上已經(jīng)有一個不錯的使用教程:
http://blog.csdn.net/ezerg/archive/2004/10/14/135769.aspx
4、有關(guān)“用OSCache進(jìn)行緩存對象”的研究
這個是我今天要說的東西。網(wǎng)上對于OSCache緩存Web頁面很多說明和例子,但對于緩存對象方面說得不多,我就把自已寫得一些東西放出來,讓大家看一看是怎樣緩存對象的!
我基于GeneralCacheAdministrator類來寫的BaseCache類
- package com.klstudio.cache;
-
- import java.util.Date;
-
- import com.opensymphony.oscache.base.NeedsRefreshException;
- import com.opensymphony.oscache.general.GeneralCacheAdministrator;
-
- public class BaseCache extends GeneralCacheAdministrator {
- //過期時間(單位為秒);
- private int refreshPeriod;
- //關(guān)鍵字前綴字符;
- private String keyPrefix;
-
- private static final long serialVersionUID = -4397192926052141162L;
-
- public BaseCache(String keyPrefix,int refreshPeriod){
- super();
- this.keyPrefix = keyPrefix;
- this.refreshPeriod = refreshPeriod;
- }
- //添加被緩存的對象;
- public void put(String key,Object value){
- this.putInCache(this.keyPrefix+"_"+key,value);
- }
- //刪除被緩存的對象;
- public void remove(String key){
- this.flushEntry(this.keyPrefix+"_"+key);
- }
- //刪除所有被緩存的對象;
- public void removeAll(Date date){
- this.flushAll(date);
- }
-
- public void removeAll(){
- this.flushAll();
- }
- //獲取被緩存的對象;
- public Object get(String key) throws Exception{
- try{
- return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);
- } catch (NeedsRefreshException e) {
- this.cancelUpdate(this.keyPrefix+"_"+key);
- throw e;
- }
-
- }
-
- }
-
-
通過CacheManager類來看怎樣緩存對象的,這個類中所用的News只是具體功能的類,我就不貼出來了,你可以自己寫一個!
- package com.klstudio;
-
- import com.klstudio.News;
- import com.klstudio.cache.BaseCache;
-
- public class CacheManager {
-
- private BaseCache newsCache;
-
-
- private static CacheManager instance;
- private static Object lock = new Object();
-
- public CacheManager() {
- //這個根據(jù)配置文件來,初始BaseCache而已;
- newsCache = new BaseCache("news",1800);
- }
-
- public static CacheManager getInstance(){
- if (instance == null){
- synchronized( lock ){
- if (instance == null){
- instance = new CacheManager();
- }
- }
- }
- return instance;
- }
-
- public void putNews(News news) {
- // TODO 自動生成方法存根
- newsCache.put(news.getID(),news);
- }
-
- public void removeNews(String newsID) {
- // TODO 自動生成方法存根
- newsCache.remove(newsID);
- }
-
- public News getNews(String newsID) {
- // TODO 自動生成方法存根
- try {
- return (News) newsCache.get(newsID);
- } catch (Exception e) {
- // TODO 自動生成 catch 塊
- System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());
- News news = new News(newsID);
- this.putNews(news);
- return news;
- }
- }
-
- public void removeAllNews() {
- // TODO 自動生成方法存根
- newsCache.removeAll();
- }
-
- }
-