关于OSCache的使用

听说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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值