听说OSCache的性能不行,我没测试过,但看到在应用过程中使用却非常广泛,因此对OSCache的使用看了一些资料,现对片断摘录如下:
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;
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=778146