关闭

OSCache 在java对象中使用

标签: javaobjectcacheimportnullinput
3691人阅读 评论(1) 收藏 举报

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
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:42090次
    • 积分:668
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    最新评论