OSCache 在java对象中使用

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>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值