zoukankan      html  css  js  c++  java
  • java map缓存

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

     
  • 相关阅读:
    python数组查找算法---bisect二分查找插入
    pandas的连接函数concat()函数
    python dataframe astype 字段类型转换
    Pandas Dataframe增、删、改、查、去重、抽样基本操作
    pandas入门——loc与iloc函数
    API:详解 pandas.read_csv
    python的复制,深拷贝和浅拷贝的区别
    代码详解生成器、迭代器
    Python学习笔记(4):容器、迭代对象、迭代器、生成器、生成器表达式
    mysql 安装参考
  • 原文地址:https://www.cnblogs.com/liangbo-/p/5169979.html
Copyright © 2011-2022 走看看