1、OSCache是什么?
????
OSCache標記庫由OpenSymphony設計,它是一種開創性的緩存方案,它提供了在現有JSP頁面之內實現內存緩存的功能。OSCache是個
一個被廣泛采用的高性能的J2EE緩存框架,OSCache還能應用于任何Java應用程序的普通的緩存解決方案。
2、OSCache的特點
????(1) 緩存任何對象:你可以不受限制的緩存部分jsp頁面或HTTP請求,任何java對象都可以緩存。
????(2) 擁有全面的API:OSCache API允許你通過編程的方式來控制所有的OSCache特性。
????(3) 永久緩存:緩存能被配置寫入硬盤,因此允許在應用服務器的多次生命周期間緩存創建開銷昂貴的數據。
????(4) 支持集群:集群緩存數據能被單個的進行參數配置,不需要修改代碼。
????(5) 緩存過期:你可以有最大限度的控制緩存對象的過期,包括可插入式的刷新策略(如果默認性能不能滿足需要時)。
3、OSCache的安裝與配置
????網上已經有一個不錯的使用教程:http://blog.csdn.net/ezerg/archive/2004/10/14/135769.aspx
4、有關“用OSCache進行緩存對象”的研究
????這個是我今天要說的東西。網上對于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; ??
-
????
??
-
????
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; ??
-
????????} ??
-
??
-
????} ??
-
???? ??
-
} ??
-
??
-
??
-
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;??
-
??????
-
????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()?{ ??
- ??????????
- ????????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)?{ ??
- ??????????
- ????????newsCache.put(news.getID(),news); ??
- ????} ??
- ??
- ????public?void?removeNews(String?newsID)?{ ??
- ??????????
- ????????newsCache.remove(newsID); ??
- ????} ??
- ??
- ????public?News?getNews(String?newsID)?{ ??
- ??????????
- ????????try?{ ??
- ????????????return?(News)?newsCache.get(newsID); ??
- ????????}?catch?(Exception?e)?{ ??
- ??????????????
- ????????????System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage()); ??
- ????????????News?news?=?new?News(newsID); ??
- ????????????this.putNews(news); ??
- ????????????return?news; ??
- ????????} ??
- ????} ??
- ??
- ????public?void?removeAllNews()?{ ??
- ??????????
- ????????newsCache.removeAll(); ??
- ????} ??
- ??
- } ??
- ??
- 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()?{??
- ??????????
- ????????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)?{??
- ??????????
- ????????newsCache.put(news.getID(),news);??
- ????}??
- ??
- ????public?void?removeNews(String?newsID)?{??
- ??????????
- ????????newsCache.remove(newsID);??
- ????}??
- ??
- ????public?News?getNews(String?newsID)?{??
- ??????????
- ????????try?{??
- ????????????return?(News)?newsCache.get(newsID);??
- ????????}?catch?(Exception?e)?{??
- ??????????????
- ????????????System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());??
- ????????????News?news?=?new?News(newsID);??
- ????????????this.putNews(news);??
- ????????????return?news;??
- ????????}??
- ????}??
- ??
- ????public?void?removeAllNews()?{??
- ??????????
- ????????newsCache.removeAll();??
- ????}??
- ??
- }?