OSCache是一种缓存,在web页面上面设置很简单,这里就不多说了。
首先把OScache.jar包引入你的程序里面。
这里做了几个常用方法的封装:
OSCache.java
import java.util.Properties;
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;
import com.util.*;
public class OSCache implements Cache {
private GeneralCacheAdministrator cache;
private static OSCache instance;
public OSCache() {
this.cache = new GeneralCacheAdministrator();
}
public OSCache(Properties prop) {
this.cache = new GeneralCacheAdministrator(prop);
}
public synchronized static OSCache getInstance() {
if (instance == null) {
instance = new OSCache();
}
return instance;
}
public void setCacheCapacity(int cacheCapacity) {
this.cache.setCacheCapacity(cacheCapacity);
}
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;
}
}
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;
}
}
public Object getkey(int time) throws CachePersistenceException {
String key = StrUtils.randomAlphanumeric(10);
try {
while (this.cache.getFromCache(key) != null) {
key = StrUtils.randomAlphanumeric(10);
}
return key;
} catch (NeedsRefreshException e) {
return key;
}
}
public void input(Object key, Object value)
throws CachePersistenceException {
this.cache.putInCache(String.valueOf(key), value);
}
public void input(Object key, Object value, int n)
throws CachePersistenceException {
EntryRefreshPolicy Policy = new ExpiresRefreshPolicy(n);
this.cache.putInCache(String.valueOf(key), value, Policy);
}
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();
}
}
//Cache.java接口
import com.opensymphony.oscache.base.persistence.CachePersistenceException;
public interface Cache {
Object get(Object key) throws CachePersistenceException;
Object get(Object key, int time) throws CachePersistenceException;
void input(Object key, Object value) throws CachePersistenceException;
void input(Object key, Object value, int i)
throws CachePersistenceException;
void remove(Object key) throws CachePersistenceException;
void clear() throws CachePersistenceException;
void destroy() throws CachePersistenceException;
Object getkey(int time) throws CachePersistenceException;
}
//CacheManager .java
import java.util.*;
public class CacheManager {
private static Map cacheMap = new HashMap();
// private static Config config = ConfigManager.getConfig();
private CacheManager() {
}
public static Cache getCache(Class clazz) {
return getCache(clazz.getName());
}
public static Cache getCache() {
return getCache();
}
public static Cache getCache(String cacheId) {
Cache cache = null;
cache = (Cache) cacheMap.get(cacheId);
if (cache == null) {
// cache = new OSCache(config.getProperties());
cacheMap.put(cacheId, cache);
}
return cache;
}
}
web.xml里面 配置
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<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>application</param-value>
</init-param>
</filter>
<taglib>
<taglib-uri>oscache</taglib-uri>
<taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>
</taglib>
</web-app>