Java学习之ConcurrentHashMap实现一个本地缓存

ConcurrentHashMap融合了Hashtable和HashMap二者的优势。
  Hashtable是做了线程同步,HashMap未考虑同步。所以HashMap在单线程下效率较高,Hashtable在多线程下同步操作能保证程序的正确性。  但是Hashtable每次执行同步操作都需要锁住整个结构。

  

  ConcurrentHashMap的出现就是为了解决Hashtable同步lock整个数据结构的问题。ConcurrentHashMap锁的方式是细颗粒度。

  ConcurrentHashMap将Hash表分为16个桶(默认值),诸如get/put/remove操作只需要锁着需要的单个桶即可。

    ConcurrentHashMap只有在size等操作的时候才会锁住整个Hash表。

  下面是自己实现的一个ConcurrentHashMap的本地缓存的例子:ConcurrentHashMap 和Guava cache相比,需要自己显示的删除缓存
 

import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapTest {

    private static ConcurrentHashMap<String, String> cacheMap = new ConcurrentHashMap<>();

    /**
     * 获取缓存的对象
     * 
     * @param account
     * @return
     */
    public static String getCache(String account) {

        account = getCacheKey(account);
        // 如果缓冲中有该账号,则返回value
        if (cacheMap.containsKey(account)) {
            return cacheMap.get(account);
        }
        // 如果缓存中没有该账号,把该帐号对象缓存到concurrentHashMap中
        initCache(account);
        return cacheMap.get(account);
    }

    /**
     * 初始化缓存
     * 
     * @param account
     */
    private static void initCache(String account) {
        // 一般是进行数据库查询,将查询的结果进行缓存
        cacheMap.put(account, "18013093863");
    }

    /**
     * 拼接一个缓存key
     * 
     * @param account
     * @return
     */
    private static String getCacheKey(String account) {
        return Thread.currentThread().getId() + "-" + account;
    }

    /**
     * 移除缓存信息
     * 
     * @param account
     */
    public static void removeCache(String account) {
        cacheMap.remove(getCacheKey(account));
    }
}
package com.zsplat.yyzx.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缓存机制
 */
public class CacheUtil {
    private static CacheUtil cacheUtil;
    private static Map<String,Object> cacheMap;

    private CacheUtil(){
        cacheMap = new HashMap<String, Object>();
    }

    public static CacheUtil getInstance(){
        if (cacheUtil == null){
            cacheUtil = new CacheUtil();
        }
        return cacheUtil;
    }

    /**
     * 添加缓存
     * @param key
     * @param obj
     */
    public void addCacheData(String key,Object obj){
        cacheMap.put(key,obj);
    }

    /**
     * 取出缓存
     * @param key
     * @return
     */
    public Object getCacheData(String key){
        return cacheMap.get(key);
    }

    /**
     * 清楚缓存
     * @param key
     */
    public void removeCacheData(String key){
        cacheMap.remove(key);
    }
}

 

package com.zhegui.utils.cache;

public class CacheClientTest {
    public static void main(String[] args) throws InterruptedException {
        ConcurrentHashMapCache cache = ConcurrentHashMapCache.getInstance();
        String value = "test3333";
        String key = "key1";
        cache.put(key, value);

        String key2 = "key2";
        String value2 = "value222";
        cache.put(key2, value2, 30 * 1000);

        String key3 = "key3";
        String value3 = "value33333333";
        cache.put(key3, value3, 2 * 60 * 1000);


        Thread.sleep(60 * 1000);

        System.out.println("key1: value = "+cache.get(key));
        System.out.println("key2: value2 = " + cache.get(key2));
        System.out.println("key3: value3 = " + cache.get(key3));
        System.out.println("-------remove key1 --------");
        cache.remove(key);

        System.out.println("key1: value = "+cache.get(key));
        System.out.println("key2: value2 = " + cache.get(key2));
        System.out.println("key3: value3 = " + cache.get(key3));

        Thread.sleep(90 * 1000);
        System.out.println("key3: value3 = " + cache.get(key3));
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值