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);
         }
}

 

posted @ 2017-06-27 15:00  烟雨观春柳  阅读(4436)  评论(0编辑  收藏  举报