java map缓存
2024-08-21 13:52:52
/** * 缓存池 * @author xiaoquan * @create 2015年3月13日 上午10:32:13 * @see */ public class CachePool { private static CachePool instance; //缓存池唯一实例 private static Map<String,Object> cacheItems; //缓存Map private CachePool(){ cacheItems = new HashMap<String,Object>(); } /** * 得到唯一实例 * @return */ public synchronized static CachePool getInstance(){ if (instance == null ){ instance = new CachePool(); } return instance; } /** * 清除所有Item缓存 */ public synchronized void clearAllItems(){ cacheItems.clear(); } /** * 获取缓存实体 * @param name * @return */ public synchronized Object getCacheItem(String name){ if (!cacheItems.containsKey(name)){ return null ; } CacheItem cacheItem = (CacheItem) cacheItems.get(name); if (cacheItem.isExpired()){ return null ; } return cacheItem.getEntity(); } /** * 存放缓存信息 * @param name * @param obj * @param expires */ public synchronized void putCacheItem(String name,Object obj, long expires){ if (!cacheItems.containsKey(name)){ cacheItems.put(name, new CacheItem(obj, expires)); } CacheItem cacheItem = (CacheItem) cacheItems.get(name); cacheItem.setCreateTime( new Date()); cacheItem.setEntity(obj); cacheItem.setExpireTime(expires); } public synchronized void putCacheItem(String name,Object obj){ putCacheItem(name,obj,- 1 ); } /** * 移除缓存数据 * @param name */ public synchronized void removeCacheItem(String name){ if (!cacheItems.containsKey(name)){ return ; } cacheItems.remove(name); } /** * 获取缓存数据的数量 * @return */ public int getSize(){ return cacheItems.size(); } } public class CacheItem {
private Date createTime = new Date(); //创建缓存的时间 private long expireTime = 1 ; //缓存期满的时间 private Object entity; //缓存的实体 public CacheItem(Object obj, long expires){ this .entity = obj; this .expireTime = expires; } public boolean isExpired(){ return (expireTime != - 1 && new Date().getTime()-createTime.getTime() > expireTime); } /** * 省略getter、setter方法 */ } |
原文: http://www.cnblogs.com/quanenmin/p/4335278.html
最新文章
- PHP中的特殊类,接口类和抽象类(都不能直接实例化)
- echarts在IE8下遮挡其他组件的问题
- 如何用Unity制作自定义字体——Custom Font
- QQ在线客服JS代码,自适应漂浮在网页右侧
- HTML--JS练习小游戏(别踩白块儿)
- MinStack
- Linux -RAID
- 使用Spring开发第一个HelloWorld应用
- .net(c#) winform文本框只能输入数字,不能其他非法字符
- Guava文档翻译之 Service
- http://blog.csdn.net/majian_1987/article/details/44939911
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.2
- 用Windows Live Writer 2012发博客
- 浅谈JS中的高级函数
- iOS 通过UIControl,自定义控件
- Git学习之忽略特殊文件.gitignore的配置
- 深入理解内存模型JMM
- 使用Pyquery+selenium抓取淘宝商品信息
- gitlab分支代码本地拉取及jenkins关联gitlab分支
- C语言上机练习二