采用静态Map、ConcurrentHashMap处理缓存

考虑到在使用HashMap在并发时会出现不正确行为,根据网上资料自己编写了采用ConcurrentHashMap来完成静态缓存的处理,目的是为了能够用来处理高并发的线程安全类,如有问题请各位大侠指教:

[java]  view plain  copy
  1. package com.zengms.cache;  
  2.   
  3. import java.util.Map;  
  4. import java.util.concurrent.ConcurrentHashMap;  
  5.   
  6. import org.apache.commons.logging.Log;  
  7. import org.apache.commons.logging.LogFactory;  
  8.   
  9. public class MapCacheManager {  
  10.   
  11.     private final static Log log = LogFactory.getLog(MapCacheManager.class);  
  12.   
  13.     private volatile long updateTime = 0L;// 更新缓存时记录的时间  
  14.   
  15.     private volatile boolean updateFlag = true;// 正在更新时的阀门,为false时表示当前没有更新缓存,为true时表示当前正在更新缓存  
  16.   
  17.     private volatile static MapCacheManager mapCacheObject;// 缓存实例对象  
  18.   
  19.     private static Map<String, String> cacheMap = new ConcurrentHashMap<String, String>();// 缓存容器  
  20.   
  21.     private MapCacheManager() {  
  22.         this.LoadCache();// 加载缓存  
  23.         updateTime = System.currentTimeMillis();// 缓存更新时间  
  24.   
  25.     }  
  26.   
  27.     /** 
  28.      * 采用单例模式获取缓存对象实例 
  29.      *  
  30.      * @return 
  31.      */  
  32.     public static MapCacheManager getInstance() {  
  33.         if (null == mapCacheObject) {  
  34.             synchronized (MapCacheManager.class) {  
  35.                 if (null == mapCacheObject) {  
  36.                     mapCacheObject = new MapCacheManager();  
  37.                 }  
  38.             }  
  39.         }  
  40.         return mapCacheObject;  
  41.     }  
  42.   
  43.     /** 
  44.      * 装载缓存 
  45.      */  
  46.     private void LoadCache() {  
  47.   
  48.         this.updateFlag = true;// 正在更新  
  49.   
  50.         /********** 数据处理,将数据放入cacheMap缓存中 **begin ******/  
  51.         cacheMap.put("key1""value1");  
  52.         cacheMap.put("key2""value2");  
  53.         cacheMap.put("key3""value3");  
  54.         cacheMap.put("key4""value4");  
  55.         cacheMap.put("key5""value5");  
  56.         /********** 数据处理,将数据放入cacheMap缓存中 ***end *******/  
  57.   
  58.         this.updateFlag = false;// 更新已完成  
  59.   
  60.     }  
  61.   
  62.     /** 
  63.      * 返回缓存对象 
  64.      *  
  65.      * @return 
  66.      */  
  67.     public Map<String, String> getMapCache() {  
  68.   
  69.         long currentTime = System.currentTimeMillis();  
  70.   
  71.         if (this.updateFlag) {// 前缓存正在更新  
  72.             log.info("cache is Instance .....");  
  73.             return null;  
  74.   
  75.         }  
  76.   
  77.         if (this.IsTimeOut(currentTime)) {// 如果当前缓存正在更新或者缓存超出时限,需重新加载  
  78.             synchronized (this) {  
  79.                 this.ReLoadCache();  
  80.                 this.updateTime = currentTime;  
  81.             }  
  82.         }  
  83.   
  84.         return this.cacheMap;  
  85.     }  
  86.   
  87.     private boolean IsTimeOut(long currentTime) {  
  88.   
  89.         return ((currentTime - this.updateTime) > 1000000);// 超过时限,超时  
  90.     }  
  91.   
  92.     /** 
  93.      * 获取缓存项大小 
  94.      * @return 
  95.      */  
  96.     private int getCacheSize() {  
  97.         return cacheMap.size();  
  98.     }  
  99.   
  100.     /** 
  101.      * 获取更新时间 
  102.      * @return 
  103.      */  
  104.     private long getUpdateTime() {  
  105.         return this.updateTime;  
  106.     }  
  107.   
  108.     /** 
  109.      * 获取更新标志 
  110.      * @return 
  111.      */  
  112.     private boolean getUpdateFlag() {  
  113.         return this.updateFlag;  
  114.     }  
  115.   
  116.     /** 
  117.      * 重新装载 
  118.      */  
  119.     private void ReLoadCache() {  
  120.         this.cacheMap.clear();  
  121.         this.LoadCache();  
  122.     }  
  123.   
  124. }  

测试代码:

[java]  view plain  copy
  1. package com.zengms.cache;  
  2.   
  3. import java.util.Iterator;  
  4. import java.util.Map;  
  5. import java.util.Set;  
  6. import java.util.concurrent.ConcurrentHashMap;  
  7.   
  8. public class CacheTest {  
  9.   
  10.     public static void main(String[] args) {  
  11.           
  12.         MapCacheManager cache = MapCacheManager.getInstance();  
  13.         Map<String, String> cacheMap = new ConcurrentHashMap<String, String>();  
  14.           
  15.         cacheMap = cache.getMapCache();  
  16.         Set<String> set = cacheMap.keySet();  
  17.         Iterator<String> it = set.iterator();  
  18.           
  19.         while(it.hasNext()){  
  20.             String key = it.next();  
  21.             System.out.println(key+"="+cacheMap.get(key));  
  22.         }  
  23.     }  
  24.   
  25. }  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值