先介紹用硬盤(pán)緩存.
1.打開(kāi)oscache.properties文件. 改成,cache.memory=false(表示硬盤(pán)緩存.true表示內(nèi)存緩存),然后是路徑,cache.path=/home/search/index/cache(我這個(gè)不是win下)然后cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener選一個(gè),第一會(huì)當(dāng)緩存數(shù)據(jù)在硬盤(pán)后,會(huì)產(chǎn)品一個(gè)文件夾.第二是存數(shù)據(jù)在硬盤(pán)后,會(huì)產(chǎn)生分步的件夾,不會(huì)像第一種那樣把緩存數(shù)據(jù)放在一個(gè)文件夾里.
2.寫(xiě)方法進(jìn)行緩存.
1.寫(xiě)個(gè)類extends GeneralCacheAdministrator,
public class BaseCache extends GeneralCacheAdministrator {
// 過(guò)期時(shí)間(單位為秒);
private int refreshPeriod;
public BaseCache(int refreshPeriod){
super();
this.refreshPeriod=refreshPeriod;
}
// 添加被緩存的對(duì)象;
public void put(String key,Object value){
this.putInCache(key,value);
}
// 刪除被緩存的對(duì)象;
public void remove(String key){
this.flushEntry(key);
}
// 刪除所有被緩存的對(duì)象;
public void removeAll(Date date){
this.flushAll(date);
}
public void removeAll(){
this.flushAll();
}
// 獲取被緩存的對(duì)象;
public Object get(String key) throws Exception{
try{
return this.getFromCache(key,this.refreshPeriod);
} catch (NeedsRefreshException e) {
this.cancelUpdate(key);
throw e;
}
}
}
2.執(zhí)行方法:
在方法里,indexCache = new BaseCache(3600),hii = new cache.Hits(hits),這個(gè)類是自己寫(xiě)的,下面的詳細(xì)代碼,然后調(diào)用put(String key,Object value)方法,key是關(guān)鍵字.下次取的時(shí)候就直接get(key),key我是用的查詢參數(shù)組成的,只要下次當(dāng)別人用到相同條件搜索時(shí),就可以直接從緩存讀數(shù)據(jù),這里所提到的是org.apache.lucene.search.Hits這個(gè)對(duì)像是不能被緩存的,沒(méi)有被序例化,我查了下源碼,也不能被繼承,所以大家自己寫(xiě)個(gè)類implements Serializable
public class Hits implements Serializable {
private org.apache.lucene.search.Hits hits;
private Document[] docs=null;
private boolean flag=true;
/**
* sundc 2007-11-20
*/
public Hits(org.apache.lucene.search.Hits hits) {
this.docs=new Document[hits.length()];
int length=0;
//我這里是定義記錄有只緩存前710多記錄,如果大于710還是查lucene,大家都知道搜索用戶訪問(wèn)都是幾頁(yè),大家可以針對(duì)自己項(xiàng)目進(jìn)行規(guī)化..
if(hits.length()>=710){
length=710;
}
else{
length=hits.length();
}
for(int i=0;i<length;i++){
try {
this.docs[i]=hits.doc(i);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Hits( Document[] doc){
this.docs=doc;
}
public Document doc(int i){
if(!this.flag){
try {
return this.hits.doc(i);
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return this.docs[i];
}
public int length(){
if(this.flag){
return docs.length;
}
return this.hits.length();
}
public Document[] getDocs() {
return this.docs;
}
public void setDocs(Document[] docs) {
this.docs = docs;
}
public org.apache.lucene.search.Hits getHits() {
return this.hits;
}
public void setHits(org.apache.lucene.search.Hits hits) {
this.hits = hits;
}
}
我這里是如果有緩存數(shù)據(jù),就讀緩存,沒(méi)有,就lucene然后放在緩存里.大家可以調(diào)用indexCache.get(key),大家每一次訪問(wèn)的時(shí)候會(huì)相對(duì)很慢.如果緩存已經(jīng)建立了,那么速度會(huì)很快,大家在用的時(shí)候,針對(duì)自己項(xiàng)目的數(shù)據(jù)要求,進(jìn)行緩存.下面介紹內(nèi)存緩存.
2.內(nèi)存緩存可以相對(duì)要快點(diǎn).但是針對(duì)服務(wù)器的本身內(nèi)存大小,大家針對(duì)自己的項(xiàng)目和服務(wù)器的配置選擇好的緩存方式.,把cache.memory=true,這樣就可以了.
3.還可以實(shí)現(xiàn)頁(yè)面級(jí)緩存
你可以在web.xml中定義緩存過(guò)濾器,定義特定資源的緩存。
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>60</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>session</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
上面定義將緩存所有.jsp頁(yè)面,緩存刷新時(shí)間為60秒,緩存作用域?yàn)镾ession,
注意,CacheFilter只捕獲Http頭為200的頁(yè)面請(qǐng)求,即只對(duì)無(wú)錯(cuò)誤請(qǐng)求作緩存,
而不對(duì)其他請(qǐng)求(如500,404,400)作緩存處理.
我這里介紹的不全,只是我在項(xiàng)目中用到了這些.如果有什么問(wèn)題,虛心接受....