java Map实现的cache manager

一个模仿memcached的JAVA虚拟缓存工具,可以缓存java对象

  1 import java.io.ByteArrayInputStream;
  2 import java.io.ByteArrayOutputStream;
  3 import java.io.ObjectInputStream;
  4 import java.io.ObjectOutputStream;
  5 import java.util.concurrent.ConcurrentHashMap;
  6 import java.util.Map;
  7 
  8 /**
  9  * java Map cache manager 改进版
 10  * 特点:无线程,取出时判断过期,系列化实现的深度克隆,覆盖原则
 11  * 问题: 线程安全,克隆带来的时效损耗
 12  * @author frank
 13  *
 14  */
 15 public class VirtualCache{
 16     
 17     /**
 18      * 开发模式
 19      */
 20     private boolean isdev = false;
 21     
 22     private static String ERROR_SET = "VirtualCache缓存数据异常:key=";
 23     
 24     private static String ERROR_GET= "VirtualCache取出数据异常:key=";
 25     
 26     /**
 27      * 虚拟缓存
 28      */
 29     private Map<String,Object> cache = new ConcurrentHashMap<String, Object>();
 30     
 31     /**
 32      * 过期时间
 33      */
 34     private Map<String,Long> tasks = new ConcurrentHashMap<String,Long>();
 35     
 36     public VirtualCache(boolean isdev){
 37         this.isdev = isdev;
 38     }
 39     
 40     public VirtualCache(){
 41         
 42     }
 43     
 44     /**
 45      * 深度克隆
 46      */
 47     private Object clone(String key,Object obj,boolean isSet){
 48         if(obj == null)
 49             return null;
 50         ByteArrayOutputStream bo = null;
 51         ObjectOutputStream oo = null;
 52         ByteArrayInputStream bi = null;
 53         ObjectInputStream oi = null;
 54         Object value = null;
 55         try {
 56             bo=new ByteArrayOutputStream();
 57             oo = new ObjectOutputStream(bo);
 58             oo.writeObject(obj);
 59             bi=new ByteArrayInputStream(bo.toByteArray());
 60             oi=new ObjectInputStream(bi);
 61             value = oi.readObject();
 62         } catch (Exception e) {
 63             this.printError(e, key,isSet);
 64         } finally{
 65             if(oo != null)
 66                 try {
 67                     oo.close();
 68                 } catch (Exception e) {
 69                     this.printError(e, key,isSet);
 70                 }
 71             if(oi != null)
 72                 try {
 73                     oi.close();
 74                 } catch (Exception e) {
 75                     this.printError(e, key,isSet);
 76                 }
 77         }
 78         return value;
 79     }
 80     
 81     /**
 82      * 日志输出
 83      */
 84     private void printError(Exception e,String key,boolean isSet){
 85         if(isSet)
 86             System.err.println(ERROR_SET + key);
 87         else
 88             System.err.println(ERROR_GET + key);
 89         if(this.isdev)
 90             e.printStackTrace();
 91     }
 92     
 93     /**
 94      * 存入缓存
 95      */
 96     public void set(String key,Object value,long timeout){
 97         this.delete(key);
 98         this.cache.put(key, this.clone(key,value,true));
 99         if(timeout > 0l)
100             this.tasks.put(key,timeout*1000l + System.currentTimeMillis());
101     }
102     
103     /**
104      * 获取缓存
105      */
106     public Object get(String key){
107         Long timeout = this.tasks.get(key);
108         if(timeout != null && timeout <= System.currentTimeMillis())
109             this.delete(key);
110         return this.clone(key,this.cache.get(key),false);
111     }
112 
113     /**
114      * 删除缓存
115      */
116     public void delete(String key){
117         this.cache.remove(key);
118         this.tasks.remove(key);
119     }
120     
121 }
原文地址:https://www.cnblogs.com/codeOfLife/p/5183886.html