用OSCache进行缓存对象

来源:百度文库 编辑:神马文学网 时间:2024/04/24 04:29:31

用OSCache进行缓存对象

1、OSCache是什么?
     OSCache标记库由OpenSymphony设计,它是一种开创性的缓存方案,它提供了在现有JSP页面之内实现内存缓存的功能。OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存解决方案。
2、OSCache的特点
    (1) 缓存任何对象:你可以不受限制的缓存部分jsp页面或HTTP请求,任何java对象都可以缓存。
    (2) 拥有全面的API:OSCache API允许你通过编程的方式来控制所有的OSCache特性。
    (3) 永久缓存:缓存能被配置写入硬盘,因此允许在应用服务器的多次生命周期间缓存创建开销昂贵的数据。
    (4) 支持集群:集群缓存数据能被单个的进行参数配置,不需要修改代码。
    (5) 缓存过期:你可以有最大限度的控制缓存对象的过期,包括可插入式的刷新策略(如果默认性能不能满足需要时)。
3、OSCache的安装与配置
    网上已经有一个不错的使用教程:http://blog.csdn.net/ezerg/archive/2004/10/14/135769.aspx
4、有关“用OSCache进行缓存对象”的研究
    这个是我今天要说的东西。网上对于OSCache缓存Web页面很多说明和例子,但对于缓存对象方面说得不多,我就把自已写得一些东西放出来,让大家看一看是怎样缓存对象的!
    我基于GeneralCacheAdministrator类来写的BaseCache类
   

view plaincopy to clipboardprint?
  1. package com.klstudio.cache;   
  2.   
  3. import java.util.Date;   
  4.   
  5. import com.opensymphony.oscache.base.NeedsRefreshException;   
  6. import com.opensymphony.oscache.general.GeneralCacheAdministrator;   
  7.   
  8. public class BaseCache extends GeneralCacheAdministrator {   
  9.     //过期时间(单位为秒);   
  10.     private int refreshPeriod;   
  11.     //关键字前缀字符;   
  12.     private String keyPrefix;   
  13.        
  14.     private static final long serialVersionUID = -4397192926052141162L;   
  15.        
  16.     public BaseCache(String keyPrefix,int refreshPeriod){   
  17.         super();   
  18.         this.keyPrefix = keyPrefix;   
  19.         this.refreshPeriod = refreshPeriod;   
  20.     }   
  21.     //添加被缓存的对象;   
  22.     public void put(String key,Object value){   
  23.         this.putInCache(this.keyPrefix+"_"+key,value);   
  24.     }   
  25.     //删除被缓存的对象;   
  26.     public void remove(String key){   
  27.         this.flushEntry(this.keyPrefix+"_"+key);   
  28.     }   
  29.     //删除所有被缓存的对象;   
  30.     public void removeAll(Date date){   
  31.         this.flushAll(date);   
  32.     }   
  33.        
  34.     public void removeAll(){   
  35.         this.flushAll();   
  36.     }   
  37.     //获取被缓存的对象;   
  38.     public Object get(String key) throws Exception{   
  39.         try{   
  40.             return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);   
  41.         } catch (NeedsRefreshException e) {   
  42.             this.cancelUpdate(this.keyPrefix+"_"+key);   
  43.             throw e;   
  44.         }   
  45.   
  46.     }   
  47.        
  48. }   
  49.   
  50.   

Java代码

  1. package com.klstudio.cache;   
  2.   
  3. import java.util.Date;   
  4.   
  5. import com.opensymphony.oscache.base.NeedsRefreshException;   
  6. import com.opensymphony.oscache.general.GeneralCacheAdministrator;   
  7.   
  8. public class BaseCache extends GeneralCacheAdministrator {   
  9.     //过期时间(单位为秒);   
  10.     private int refreshPeriod;   
  11.     //关键字前缀字符;   
  12.     private String keyPrefix;   
  13.        
  14.     private static final long serialVersionUID = -4397192926052141162L;   
  15.        
  16.     public BaseCache(String keyPrefix,int refreshPeriod){   
  17.         super();   
  18.         this.keyPrefix = keyPrefix;   
  19.         this.refreshPeriod = refreshPeriod;   
  20.     }   
  21.     //添加被缓存的对象;   
  22.     public void put(String key,Object value){   
  23.         this.putInCache(this.keyPrefix+"_"+key,value);   
  24.     }   
  25.     //删除被缓存的对象;   
  26.     public void remove(String key){   
  27.         this.flushEntry(this.keyPrefix+"_"+key);   
  28.     }   
  29.     //删除所有被缓存的对象;   
  30.     public void removeAll(Date date){   
  31.         this.flushAll(date);   
  32.     }   
  33.        
  34.     public void removeAll(){   
  35.         this.flushAll();   
  36.     }   
  37.     //获取被缓存的对象;   
  38.     public Object get(String key) throws Exception{   
  39.         try{   
  40.             return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);   
  41.         } catch (NeedsRefreshException e) {   
  42.             this.cancelUpdate(this.keyPrefix+"_"+key);   
  43.             throw e;   
  44.         }   
  45.   
  46.     }   
  47.        
  48. }  

   通过CacheManager类来看怎样缓存对象的,这个类中所用的News只是具体功能的类,我就不贴出来了,你可以自己写一个!
   

 

view plaincopy to clipboardprint?
  1. package com.klstudio;   
  2.   
  3. import com.klstudio.News;   
  4. import com.klstudio.cache.BaseCache;   
  5.   
  6. public class CacheManager {   
  7.        
  8.     private BaseCache newsCache;   
  9.   
  10.        
  11.     private static CacheManager instance;   
  12.     private static Object lock = new Object();   
  13.        
  14.     public CacheManager() {   
  15.         //这个根据配置文件来,初始BaseCache而已;   
  16.         newsCache = new BaseCache("news",1800);        
  17.     }   
  18.        
  19.     public static CacheManager getInstance(){   
  20.         if (instance == null){   
  21.             synchronized( lock ){   
  22.                 if (instance == null){   
  23.                     instance = new CacheManager();   
  24.                 }   
  25.             }   
  26.         }   
  27.         return instance;   
  28.     }   
  29.   
  30.     public void putNews(News news) {   
  31.         // TODO 自动生成方法存根   
  32.         newsCache.put(news.getID(),news);   
  33.     }   
  34.   
  35.     public void removeNews(String newsID) {   
  36.         // TODO 自动生成方法存根   
  37.         newsCache.remove(newsID);   
  38.     }   
  39.   
  40.     public News getNews(String newsID) {   
  41.         // TODO 自动生成方法存根   
  42.         try {   
  43.             return (News) newsCache.get(newsID);   
  44.         } catch (Exception e) {   
  45.             // TODO 自动生成 catch 块   
  46.             System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());   
  47.             News news = new News(newsID);   
  48.             this.putNews(news);   
  49.             return news;   
  50.         }   
  51.     }   
  52.   
  53.     public void removeAllNews() {   
  54.         // TODO 自动生成方法存根   
  55.         newsCache.removeAll();   
  56.     }   
  57.   
  58. }   
  59.   

Java代码

  1. package com.klstudio;   
  2.   
  3. import com.klstudio.News;   
  4. import com.klstudio.cache.BaseCache;   
  5.   
  6. public class CacheManager {   
  7.        
  8.     private BaseCache newsCache;   
  9.   
  10.        
  11.     private static CacheManager instance;   
  12.     private static Object lock = new Object();   
  13.        
  14.     public CacheManager() {   
  15.         //这个根据配置文件来,初始BaseCache而已;   
  16.         newsCache = new BaseCache("news",1800);        
  17.     }   
  18.        
  19.     public static CacheManager getInstance(){   
  20.         if (instance == null){   
  21.             synchronized( lock ){   
  22.                 if (instance == null){   
  23.                     instance = new CacheManager();   
  24.                 }   
  25.             }   
  26.         }   
  27.         return instance;   
  28.     }   
  29.   
  30.     public void putNews(News news) {   
  31.         // TODO 自动生成方法存根   
  32.         newsCache.put(news.getID(),news);   
  33.     }   
  34.   
  35.     public void removeNews(String newsID) {   
  36.         // TODO 自动生成方法存根   
  37.         newsCache.remove(newsID);   
  38.     }   
  39.   
  40.     public News getNews(String newsID) {   
  41.         // TODO 自动生成方法存根   
  42.         try {   
  43.             return (News) newsCache.get(newsID);   
  44.         } catch (Exception e) {   
  45.             // TODO 自动生成 catch 块   
  46.             System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());   
  47.             News news = new News(newsID);   
  48.             this.putNews(news);   
  49.             return news;   
  50.         }   
  51.     }   
  52.   
  53.     public void removeAllNews() {   
  54.         // TODO 自动生成方法存根   
  55.         newsCache.removeAll();   
  56.     }   
  57.   
  58. }