java hashMap缓存简单实现

直接上代码,干货:

import java.util.HashMap;
import java.util.Map;

/**
 * map缓存
 * @author ming
 *
 * @param <K>
 * @param <V>
 */
public class MapDataCache {

    private static Map<String, Object> cacheMap;  
      
    public static Object getCache(String key, Object defaultValue) {  
        Object obj = getCacheMap().get(key);  
        return obj==null?defaultValue:obj;  
    }  
      
    public static void putCache(String key, Object value) {  
        getCacheMap().put(key, value);  
    }  
      
    public static void removeCache(String key) {  
        getCacheMap().remove(key);  
    }  
      
      
    public static Map<String, Object> getCacheMap() {  
        if (cacheMap==null){  
            cacheMap = new HashMap<String, Object>();  
        }  
        return cacheMap;  
    }  }

 

posted @ 2016-12-12 16:57  全力以赴001  阅读(2210)  评论(0编辑  收藏  举报