/**
 * 缓存池
 * @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

 

最新文章

  1. PHP中的特殊类,接口类和抽象类(都不能直接实例化)
  2. echarts在IE8下遮挡其他组件的问题
  3. 如何用Unity制作自定义字体——Custom Font
  4. QQ在线客服JS代码,自适应漂浮在网页右侧
  5. HTML--JS练习小游戏(别踩白块儿)
  6. MinStack
  7. Linux -RAID
  8. 使用Spring开发第一个HelloWorld应用
  9. .net(c#) winform文本框只能输入数字,不能其他非法字符
  10. Guava文档翻译之 Service
  11. http://blog.csdn.net/majian_1987/article/details/44939911
  12. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.2
  13. 用Windows Live Writer 2012发博客
  14. 浅谈JS中的高级函数
  15. iOS 通过UIControl,自定义控件
  16. Git学习之忽略特殊文件.gitignore的配置
  17. 深入理解内存模型JMM
  18. 使用Pyquery+selenium抓取淘宝商品信息
  19. gitlab分支代码本地拉取及jenkins关联gitlab分支
  20. C语言上机练习二

热门文章

  1. OAF_解决OAF与Windows版本不兼容黑屏
  2. PLSQL_性能优化系列17_Oracle Merge Into和Update更新效率
  3. AP_AP系列 - 发票管理分析(案例)
  4. php解压 tar.gz 格式文件
  5. [物理学与PDEs]第5章习题参考解答
  6. 深入ThreadLocal之三(ThreadLocal可能引起的内存泄露)
  7. Python补充05 字符串格式化 (%操作符)
  8. Wix安装包权限问题
  9. ThinkPHP CURD返回结果参考
  10. minicom的安装及使用