zoukankan      html  css  js  c++  java
  • map全局缓存demo

    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    import org.apache.log4j.Logger;
    public class MapCache {
        protected Logger log=Logger.getLogger(getClass().getName());
        private static Map<Object, Object> cacheMap = new ConcurrentHashMap<Object, Object>();
    
        public static void destoryCacheMap() {
          cacheMap = null;
         }
    
         public static Map<Object, Object> getCacheMap() {
          return cacheMap;
         }
    
         public static void set(Object key, Object values) {
          cacheMap.put(key, values);
         }
    
         public static Object get(Object key) {
          return cacheMap.get(key);
         }
    
         public static String getString(Object key) {
          return (String) cacheMap.get(key);
         }
    
         public static Object getToEmpty(Object key) {
          Object o = cacheMap.get(key);
          if (o == null)
           return "";
          else
           return o;
         }
    
         public static void remove(Object key) {
          cacheMap.remove(key);
         }
    
         public static void clear() {
          cacheMap.clear();
         } 
    }
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    public class CachePool {
        private static CachePool cachePool;
        private Map<Object, Object> cacheItems;
        private CachePool() {
            cacheItems =new ConcurrentHashMap<Object, Object>();
        }
        /**
         * 获取唯一实例
         * @return instance
         */
        public static CachePool getInstance() {
            if (cachePool ==null) {
                synchronized (CachePool.class) {
                    if (cachePool ==null) {
                        cachePool =new CachePool();
                    }
                }
            }
            return cachePool;
        }
        
        /**
         * 获取所有cache信息
         * @return cacheItems
         */
        public Map<Object, Object> getCacheItems() {
            return this.cacheItems;
        }
        
        /**
         * 清空cache
         */
        public void clearAllItems() {
            cacheItems.clear();
        }
        
        /**
         * 获取指定cache信息
         * @return cacheItem
         */
        public Object getCacheItem(Object key) {
            if (cacheItems.containsKey(key)) {
                return cacheItems.get(key);
            }
            return null;
        }
        
        /**
         * 存放cache信息
         */
        public void putCacheItem(Object key,Object value) {
            if (!cacheItems.containsKey(key)) {
                cacheItems.put(key, value);
            }
        }
        
        /**
         * 删除一个cache
         */
        public void removeCacheItem(Object key) {
            if (cacheItems.containsKey(key)) {
                cacheItems.remove(key);
            }
        }
        
        /**
         * 获取cache长度
         * @return size
         */
        public int getSize() {
            return cacheItems.size();
        }
        
    }
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    public class CacheTime {
        private static CacheTime cacheTime;
        private Map<Object, Object> cacheTimes;
        private CacheTime() {
            cacheTimes =new ConcurrentHashMap<Object, Object>();
        }
        /**
         * 获取唯一实例
         * @return instance
         */
        public static CacheTime getInstance() {
            if (cacheTime ==null) {
                synchronized (CacheTime.class) {
                    if (cacheTime ==null) {
                        cacheTime =new CacheTime();
                    }
                }
            }
            return cacheTime;
        }
        
        /**
         * 获取所有cache信息
         * @return cacheItems
         */
        public Map<Object, Object> getCacheItems() {
            return this.cacheTimes;
        }
        
        /**
         * 清空cache
         */
        public void clearAllItems() {
            cacheTimes.clear();
        }
        
        /**
         * 获取指定cache信息
         * @return cacheItem
         */
        public Object getCacheItem(Object key) {
            if (cacheTimes.containsKey(key)) {
                return cacheTimes.get(key);
            }
            return null;
        }
        
        /**
         * 存放cache信息
         */
        public void putCacheItem(Object key,Object value) {
            if (!cacheTimes.containsKey(key)) {
                cacheTimes.put(key, value);
            }
        }
        
        /**
         * 删除一个cache
         */
        public void removeCacheItem(Object key) {
            if (cacheTimes.containsKey(key)) {
                cacheTimes.remove(key);
            }
        }
        
        /**
         * 获取cache长度
         * @return size
         */
        public int getSize() {
            return cacheTimes.size();
        }
        
    }
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    import org.apache.log4j.Logger;
    
    //此Map缓存针对处理排行榜数据接口
    public class timeMapUtil {
    protected Logger log=Logger.getLogger(getClass().getName());
    private static Map<Integer,String> cacheMap=new ConcurrentHashMap<Integer,String>();
    private static Map<Integer,String> daymap=new ConcurrentHashMap<Integer,String>();
    private static Map<Integer,String> weekmap=new ConcurrentHashMap<Integer,String>();
    private static Map<Integer,String> monthmap=new ConcurrentHashMap<Integer,String>();
    private static Map<Integer,String> floorday=new ConcurrentHashMap<Integer,String>();
    private static Map<Integer,String> floorweek=new ConcurrentHashMap<Integer,String>();
    private static Map<Integer,String> floormonth=new ConcurrentHashMap<Integer,String>();
    public static void destoryCacheMap(){
        cacheMap=null;
    }
    public static Map<Integer,String> getCacheMap() {
          return cacheMap;
         }
    
         public static void set(int key, String values) {
          cacheMap.put(key, values);
         }
    
         public static String get(int key) {
          return cacheMap.get(key);
         }
    
         public static String getString(int key) {
          return  (String) cacheMap.get(key);
         }
    
         public static Object getToEmpty(int key) {
          Object o = cacheMap.get(key);
          if (o == null)
           return "";
          else
           return o;
         }
    
         public static void remove(int key) {
          cacheMap.remove(key);
         }
    
         public static void clear() {
          cacheMap.clear();
         } 
         //今日
         public static String getday(int key) {
              return  (String) daymap.get(key);
             }
         public static void setday(int key, String values) {
             daymap.put(key, values);
             }
         //本周
         public static String getweek(int key) {
              return  (String) weekmap.get(key);
             }
         public static void setweek(int key, String values) {
             weekmap.put(key, values);
             }
         //本月
         public static String getmonth(int key) {
              return  (String) monthmap.get(key);
             }
         public static void setmonth(int key, String values) {
             monthmap.put(key, values);
             }
         //昨日
         public static String getfloorday(int key) {
              return  (String) floorday.get(key);
             }
         public static void setfloorday(int key, String values) {
             floorday.put(key, values);
             }
         //上周
         public static String getfloorweek(int key) {
              return  (String) floorweek.get(key);
             }
         public static void setfloorweek(int key, String values) {
             floorweek.put(key, values);
             }
         //上月
         public static String getfloormonth(int key) {
              return  (String) floormonth.get(key);
             }
         public static void setfloormonth(int key, String values) {
             floormonth.put(key, values);
             }
    }
  • 相关阅读:
    Java中编写代码出现异常,如何抛出异常,如何捕获异常
    用Java制作斗地主
    Java—Map接口中的常用方法
    Java—增强for循环与for循环的区别/泛型通配符/LinkedList集合
    Java—包装类/System类/Math类/Arrays类/大数据运算/Collection接口/Iterator迭代器
    Java—时间的原点 计算时间所使用的 Date类/DateFormat类/Calendar类
    Java—匿名对象/内部类/访问修饰符/代码块
    Java—构造方法及this/super/final/static关键字
    Java—接口
    URL Protocol打开应用程序并传递程序启动参数(Windows、Mac)
  • 原文地址:https://www.cnblogs.com/emperorking/p/7085259.html
Copyright © 2011-2022 走看看