oscache 对象缓存

  1. 一、对象缓存   
  2. 1、Cache操作类   
  3. import java.util.Date;   
  4. import com.opensymphony.oscache.base.NeedsRefreshException;   
  5. import com.opensymphony.oscache.general.GeneralCacheAdministrator;   
  6. public class BaseCache extends GeneralCacheAdministrator {       
  7.     private int refreshPeriod; //过期时间(单位为秒);           
  8.     private String keyPrefix; //关键字前缀字符;               
  9.     private static final long serialVersionUID = -4397192926052141162L;          
  10.     public BaseCache(String keyPrefix,int refreshPeriod){      
  11.         super();      
  12.         this.keyPrefix = keyPrefix;      
  13.         this.refreshPeriod = refreshPeriod;      
  14.     }      
  15.     //添加被缓存的对象;      
  16.     public void put(String key,Object value){      
  17.         this.putInCache(this.keyPrefix+"_"+key,value);      
  18.     }      
  19.     //删除被缓存的对象;      
  20.     public void remove(String key){      
  21.         this.flushEntry(this.keyPrefix+"_"+key);      
  22.     }      
  23.     //删除所有被缓存的对象;      
  24.     public void removeAll(Date date){      
  25.         this.flushAll(date);      
  26.     }             
  27.     public void removeAll(){      
  28.         this.flushAll();      
  29.     }      
  30.     //获取被缓存的对象;      
  31.     public Object get(String key) throws Exception{      
  32.         try{      
  33.             returnthis.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);      
  34.         } catch (NeedsRefreshException e) {      
  35.             this.cancelUpdate(this.keyPrefix+"_"+key);      
  36.             throw e;      
  37.         }        
  38.     }              
  39. }      
  40.     
  41. 2、Cache管理类   
  42. public class CacheManager {          
  43.     private BaseCache newsCache;              
  44.     private static CacheManager instance;      
  45.     private static Object lock = new Object();             
  46.     private CacheManager() {      
  47.         //这个根据配置文件来,初始BaseCache而已;      
  48.         newsCache = new BaseCache("news",120);           
  49.     }              
  50.     public static CacheManager getInstance(){      
  51.         if (instance == null){      
  52.             synchronized( lock ){      
  53.                 if (instance == null){      
  54.                     instance = new CacheManager();      
  55.                 }      
  56.             }      
  57.         }      
  58.         return instance;      
  59.     }         
  60.     public void putUser(User news) { newsCache.put(news.getId()+"",news);     }         
  61.     public void removeUser(String newsID) {  newsCache.remove(newsID);      }         
  62.     public User getUser(int newsID) {      
  63.         try {      
  64.             return (User) newsCache.get(newsID+"");      
  65.         } catch (Exception e) {      
  66.             System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());      
  67.             User news = new User(newsID);      
  68.             this.putUser(news);      
  69.             return news;      
  70.         }      
  71.     }         
  72.     public void removeAllNews() {      
  73.         newsCache.removeAll();      
  74.     }         
  75. }   
  76.   
  77. 3、对象Bean   
  78. public class User {   
  79.     private int id;   
  80.     private String name;   
  81.     private String sex;   
  82.     private int age;   
  83.     private Date accessTime; public User(int id) {   
  84.        super();   
  85.        this.id = id;   
  86.        this.accessTime = new Date(System.currentTimeMillis());   
  87.     }   
  88.     public String toString() {   
  89.        return "User info is : id=" + id + "  accessTime="  
  90.               + accessTime.toString();   
  91.     }   
  92.     public User(String name, String sex, int age) {   
  93.        super();   
  94.        this.name = name;   
  95.        this.sex = sex;   
  96.        this.age = age;   
  97.     }   
  98.     public User() {   
  99.     }   
  100.     public int getAge() {   
  101.        return age;   
  102.     }   
  103.     public void setAge(int age) {   
  104.        this.age = age;   
  105.     }   
  106.     public String getName() {   
  107.        return name;   
  108.     }   
  109.     public void setName(String name) {   
  110.        this.name = name;   
  111.     }   
  112.     public String getSex() {   
  113.        return sex;   
  114.     }   
  115.     public void setSex(String sex) {   
  116.        this.sex = sex;   
  117.     }   
  118.     public int getId() {   
  119.        return id;   
  120.     }   
  121.     public void setId(int id) {   
  122.        this.id = id;   
  123.     }   
  124.     public Date getAccessTime() {   
  125.        return accessTime;   
  126.     }   
  127.     public void setAccessTime(Date accessTime) {   
  128.        this.accessTime = accessTime;   
  129.     }   
  130. }   
  131.     
  132. 4、测试类   
  133. public class TestObjectCache {   
  134.     public static void main(String[] args) {   
  135.        CacheManager cm=CacheManager.getInstance();   
  136.          
  137.        TestObjectCache test=new TestObjectCache();   
  138.        test.print(cm);   
  139.     }   
  140.       
  141.     public void print(CacheManager cm){   
  142.        User user=null;   
  143.        for (int i = 0; i < 1000; i++) {   
  144.            user=cm.getUser(100);   
  145.            System.out.println("<<"+i+">>: "+user);            
  146.            if(i==10){   
  147.               //删除缓存id的对象   
  148.               cm.removeUser(100+"");   
  149.            }            
  150.            if(i==20){   
  151.               //删除所有缓存的对象   
  152.               cm.removeAllNews();   
  153.            }            
  154.            // 睡眠部分   
  155.            try {   
  156.               Thread.sleep(30000);   
  157.            } catch (Exception e) {   
  158.            }   
  159.        }   
  160.     }   
  161. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值