今天下午,想了好久,終于決定要用OSCache來(lái)緩存我的報(bào)表統(tǒng)計(jì)了,要不覺得效率很低。呵呵,當(dāng)然我是選擇了opensymphony的OSCache,它可以緩存任何Java對(duì)象以及JSP程序,以下是今天下午的成果,寫了一個(gè)通用類,再在action里面緩存一下。試了一下,效果還算不錯(cuò)。
package com.finegold.digimus.comm;
import com.opensymphony.oscache.base.persistence.CachePersistenceException;
/**
* @author 汪心利 2007-7-5
*@copyRight WWW.FINEGOLD.COM.CN
*
*/
public interface Cache {
/**
* 根據(jù)key獲取cache里的對(duì)象
*
* @param key
* @return
* @throws CachePersistenceException
*/
Object get(Object key) throws CachePersistenceException;
/**
* 根據(jù)key以及time獲取cache里的對(duì)象,對(duì)應(yīng) inputOSCache(Object key, Object value, int
* i)方法添加進(jìn)去的對(duì)象
*
* @param key
* @param time
* @return
* @throws CachePersistenceException
*/
Object get(Object key, int time) throws CachePersistenceException;
/**
* 將object添加cache
*
* @param key
* @param value
* @throws CachePersistenceException
*/
void inputOSCache(Object key, Object value)
throws CachePersistenceException;
/**
* 將object添加cache
*
* @param key
* @param value
* @param i
* @throws CachePersistenceException
*/
void inputOSCache(Object key, Object value, int i)
throws CachePersistenceException;
/**
* 根據(jù)key刪除object
*
* @param key
* @throws CachePersistenceException
*/
void remove(Object key) throws CachePersistenceException;
/**
* 清空cache
*
* @throws CachePersistenceException
*/
void clear() throws CachePersistenceException;
/**
* 銷毀cache
*
* @throws CachePersistenceException
*/
void destroy() throws CachePersistenceException;
/**
* 根據(jù)time 獲取key
*
* @param time
* @return
* @throws CachePersistenceException
*/
Object getkey(int time) throws CachePersistenceException;
}
接口的實(shí)現(xiàn)類:
package com.finegold.digimus.comm;
/**
* @author BlueSKy_itwangxinli 2007-7-5
*@copyRight WWW.FINEGOLD.COM.CN
*/
import java.util.Properties;
import org.apache.commons.lang.RandomStringUtils;
import com.opensymphony.oscache.base.EntryRefreshPolicy;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.base.persistence.CachePersistenceException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
import com.opensymphony.oscache.web.filter.ExpiresRefreshPolicy;
/**
* OSCache 緩存共用類
*
* @author 汪心利 2007-7-5
*/
public class OSCache implements Cache {
/**
* 通用緩存管理類 administrator
*/
private GeneralCacheAdministrator cache;
private static OSCache instance;
public OSCache() {
this.cache = new GeneralCacheAdministrator();
}
/**
* 指定加載加載cache.propeties fiel 默認(rèn)情況下加載classes目錄下的OScache.properties
*
* @param prop
*/
public OSCache(Properties prop) {
this.cache = new GeneralCacheAdministrator(prop);
}
/**
* 返回OSCache 的Instance 單態(tài)模式
*
* @return
*/
public synchronized static OSCache getInstance() {
if (instance == null) {
instance = new OSCache();
}
return instance;
}
/**
* 設(shè)置緩存容量 default value 請(qǐng)查看oscache配置文件,OSCache系統(tǒng)本身默認(rèn)值unlimited
*
* @param cacheCapacity
*/
public void setCacheCapacity(int cacheCapacity) {
this.cache.setCacheCapacity(cacheCapacity);
}
/**
* 根據(jù)Key獲取cache里緩存的object
*
* @param key
* 查找關(guān)鍵字
*/
public Object get(Object key) throws CachePersistenceException {
try {
return this.cache.getFromCache(String.valueOf(key));
} catch (NeedsRefreshException e) {
cache.cancelUpdate(String.valueOf(key));
return null;
}
}
/**
* 根據(jù)key和time獲取緩存的object
*
* @param key
* 查找的key
* @param time
* (最準(zhǔn)確的含義-->)How long the object can stay in cache in seconds
*/
public Object get(Object key, int time) throws CachePersistenceException {
try {
return this.cache.getFromCache(String.valueOf(key), time);
} catch (NeedsRefreshException e) {
cache.cancelUpdate(String.valueOf(key));
return null;
}
}
/**
* 盡量不要使用該方法 根據(jù)time 獲取key
*
* @param time
* 時(shí)間
*/
public Object getkey(int time) throws CachePersistenceException {
String key = RandomStringUtils.randomAlphanumeric(10);
try {
while (this.cache.getFromCache(key) != null) {
key = RandomStringUtils.randomAlphanumeric(10);
}
return key;
} catch (NeedsRefreshException e) {
return key;
}
}
/**
* 緩存對(duì)象
*/
public void inputOSCache(Object key, Object value)
throws CachePersistenceException {
this.cache.putInCache(String.valueOf(key), value);
}
/**
* 緩存對(duì)象
*
* @param key
* 緩存對(duì)象的key
* @param value
* 緩存對(duì)象的value
* @param n
* 緩存對(duì)象有效時(shí)間
*/
public void inputOSCache(Object key, Object value, int n)
throws CachePersistenceException {
EntryRefreshPolicy Policy = new ExpiresRefreshPolicy(n);
this.cache.putInCache(String.valueOf(key), value, Policy);
}
/**
* 根據(jù)key從cache里 刪除object
*
* @param 要?jiǎng)h除緩存對(duì)象的key
*/
public void remove(Object key) throws CachePersistenceException {
this.cache.flushEntry(String.valueOf(key));
}
/**
* 清空所有的緩存
*/
public void clear() throws CachePersistenceException {
this.cache.flushAll();
}
/**
* 銷毀緩存
*/
public void destroy() throws CachePersistenceException {
this.cache.destroy();
}
}
配置web.xml
<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>600</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>session</param-value>
</init-param>
</filter>