dfrank
2014-06-26 18:55:47
原证
java Map实现的cache manager
一个模仿memcached的JAVA虚拟缓存工具,可以缓存java对象;
突然想到就搞出来了,具体好像也没有什么用......汗
更新:
感谢牛大大的提醒,老的代理确实存在线程方面的问题,下面是更新的版本,老的版本放入到附件中了
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.concurrent.ConcurrentHashMap; import java.util.Map; /** * java Map cache manager 改进版 * 特点:无线程,取出时判断过期,系列化实现的深度克隆,覆盖原则 * 问题: 线程安全,克隆带来的时效损耗 * @author frank * */ public class VirtualCache{ /** * 开发模式 */ private boolean isdev = false; private static String ERROR_SET = "VirtualCache缓存数据异常:key="; private static String ERROR_GET= "VirtualCache取出数据异常:key="; /** * 虚拟缓存 */ private Map<String,Object> cache = new ConcurrentHashMap<String, Object>(); /** * 过期时间 */ private Map<String,Long> tasks = new ConcurrentHashMap<String,Long>(); public VirtualCache(boolean isdev){ this.isdev = isdev; } public VirtualCache(){ } /** * 深度克隆 */ private Object clone(String key,Object obj,boolean isSet){ if(obj == null) return null; ByteArrayOutputStream bo = null; ObjectOutputStream oo = null; ByteArrayInputStream bi = null; ObjectInputStream oi = null; Object value = null; try { bo=new ByteArrayOutputStream(); oo = new ObjectOutputStream(bo); oo.writeObject(obj); bi=new ByteArrayInputStream(bo.toByteArray()); oi=new ObjectInputStream(bi); value = oi.readObject(); } catch (Exception e) { this.printError(e, key,isSet); } finally{ if(oo != null) try { oo.close(); } catch (Exception e) { this.printError(e, key,isSet); } if(oi != null) try { oi.close(); } catch (Exception e) { this.printError(e, key,isSet); } } return value; } /** * 日志输出 */ private void printError(Exception e,String key,boolean isSet){ if(isSet) System.err.println(ERROR_SET + key); else System.err.println(ERROR_GET + key); if(this.isdev) e.printStackTrace(); } /** * 存入缓存 */ public void set(String key,Object value,long timeout){ this.delete(key); this.cache.put(key, this.clone(key,value,true)); if(timeout > 0l) this.tasks.put(key,timeout*1000l + System.currentTimeMillis()); } /** * 获取缓存 */ public Object get(String key){ Long timeout = this.tasks.get(key); if(timeout != null && timeout <= System.currentTimeMillis()) this.delete(key); return this.clone(key,this.cache.get(key),false); } /** * 删除缓存 */ public void delete(String key){ this.cache.remove(key); this.tasks.remove(key); } }
官方验证:
这个cache实现和该代码类似,但存在线程安全问题
由最代码官方编辑于2016-12-16 10:22:36
猜你喜欢
- 分享一个java Map实现的cache manager,定时清除缓存起来的值
- java自定义加密算法
- 【小C出品】对java Map的操作
- java自定义注解实例
- java map用法实例
- java缓存工具类,支持主流的缓存memcache redis两种缓存系统
- java map通过值由大到下进行排序
- java自定义MVC框架共享
- java多线程类ReentrantReadWriteLock读写锁实例代码及内存缓存工具类
- java基于反射的Map转Bean的工具类
- 自己在项目中写的简单的仿 freemarker 模板引擎工具 V2 版本,支持迭代 template4javaniu
- 自己在项目中写的简单的仿freemarker模板引擎工具
请下载代码后再发表评论


qcxdld LV1
2020年9月23日
gan857569302 LV9
2020年6月23日
luohaipeng LV23
2019年11月20日
低调人 LV38
2019年8月4日
Tiger_Peppa LV2
2019年6月19日
幽幻梦之韩霜 LV10
2018年9月4日
1324488732 LV27
2017年12月22日
silent LV12
2017年9月27日
zl_lin LV2
2017年1月3日
zyl LV34
2016年6月22日

CrystalQ LV8
2023年5月30日
hxx123456789 LV10
2021年8月13日
qcxdld LV1
2020年9月23日
wkc LV21
2020年7月29日
gan857569302 LV9
2020年6月22日
luv583774 LV1
2020年6月11日
weixiao LV6
2020年5月18日
1033755143 LV17
2020年5月8日
弥尘123456 LV4
2020年2月19日
luohaipeng LV23
2019年11月20日