今天下午,想了好久,终于决定要用OSCache来缓存我的报表统计了,要不觉得效率很低。呵呵,当然我是选择了opensymphony的OSCache,它可以缓存任何Java对象以及JSP程序,以下是今天下午的成果,写了一个通用类,再在action里面缓存一下。试了一下,效果还算不错。
package com.finegold.digimus.comm;
import com.opensymphony.oscache.base.persistence.CachePersistenceException;
/**
* @author 汪心利 2007-7-5
*@copyRight WWW.FINEGOLD.COM.CN
*
*/
public interface Cache {
/**
* 根据key获取cache里的对象
*
* @param key
* @return
* @throws CachePersistenceException
*/
Object get(Object key) throws CachePersistenceException;
/**
* 根据key以及time获取cache里的对象,对应 inputOSCache(Object key, Object value, int
* i)方法添加进去的对象
*
* @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;
/**
* 根据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;
/**
* 根据time 获取key
*
* @param time
* @return
* @throws CachePersistenceException
*/
Object getkey(int time) throws CachePersistenceException;
}
接口的实现类:
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 默认情况下加载classes目录下的OScache.properties
*
* @param prop
*/
public OSCache(Properties prop) {
this.cache = new GeneralCacheAdministrator(prop);
}
/**
* 返回OSCache 的Instance 单态模式
*
* @return
*/
public synchronized static OSCache getInstance() {
if (instance == null) {
instance = new OSCache();
}
return instance;
}
/**
* 设置缓存容量 default value 请查看oscache配置文件,OSCache系统本身默认值unlimited
*
* @param cacheCapacity
*/
public void setCacheCapacity(int cacheCapacity) {
this.cache.setCacheCapacity(cacheCapacity);
}
/**
* 根据Key获取cache里缓存的object
*
* @param key
* 查找关键字
*/
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;
}
}
/**
* 根据key和time获取缓存的object
*
* @param key
* 查找的key
* @param time
* (最准确的含义-->)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;
}
}
/**
* 尽量不要使用该方法 根据time 获取key
*
* @param time
* 时间
*/
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;
}
}
/**
* 缓存对象
*/
public void inputOSCache(Object key, Object value)
throws CachePersistenceException {
this.cache.putInCache(String.valueOf(key), value);
}
/**
* 缓存对象
*
* @param key
* 缓存对象的key
* @param value
* 缓存对象的value
* @param n
* 缓存对象有效时间
*/
public void inputOSCache(Object key, Object value, int n)
throws CachePersistenceException {
EntryRefreshPolicy Policy = new ExpiresRefreshPolicy(n);
this.cache.putInCache(String.valueOf(key), value, Policy);
}
/**
* 根据key从cache里 删除object
*
* @param 要删除缓存对象的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>