基于ConcurrentHashMap实现一个自定义的缓存工具

本博客主要内容为基于ConcurrentHashMap实现一个自定义的缓存工具类。

该缓存工具具有以下功能:
  1. 支持初始化容量控制
  2. 支持泛型
  3. 支持插入具有过期时间的key
  4. 支持过期key惰性删除
  5. 支持冷数据淘汰功能

工具类代码如下:

1、缓存工具接口

package com.syx.cache;

import java.util.concurrent.TimeUnit;

/**
 * @Author whh
 * @Date: 2023/02/24/ 18:52
 * @description
 */
public interface Cache<K, V> {


    /**
     * 缓存中添加元素 可设置key的过期时间
     * @param key
     * @param val
     * @param timeout
     * @param timeUnit
     */
    void put(K key, V val, long timeout, TimeUnit timeUnit);


    /**
     *缓存中添加元素
     * @param key
     * @param val
     */
    void put(K key, V val);

    /**
     *清空缓存
     */
    void clear();

    /**
     *检查 key是否过期 不存在或过期 返回true 否则返回false
     * 如果该key过期同时删除key
     * @param key
     * @return
     */
    boolean expire(K key);

    /**
     *检查 key在指定的时间偏移量内是否过期 不存在或过期 返回true 否则返回false
     * @param key
     * @param time
     * @param timeUnit
     * @return
     */
    boolean expireAt(K key, long time, TimeUnit timeUnit);

    /**
     *获取缓存中的值
     * @param key
     * @return
     */
    V get(K key);

    /**
     *缓存中的数据量
     * @return
     */
    int size();
}

2、接口实现工具类

package com.syx.cache;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Author whh
 * @Date: 2023/02/24/ 18:51
 * @description
 */
public class CacheUtil<K, V> implements Cache<K, V> {

    //默认缓存容量
    private static final int DEFAULT_CAPACITY = 50000;

    //设定的缓存容量
    private int capacity;

    //发布缓存工具的实例
    private static CacheUtil INSTANCE;
    //锁对象
    private final Object lock = new Object();

    //具体的缓存容器的具体实现
    private ConcurrentHashMap<K, Value<K, V>> cache;

    //使用私有构造函数避免用户创建实例
    private CacheUtil(int capacity) {
        if (capacity == 0) {
            this.capacity = DEFAULT_CAPACITY;
        }
        this.capacity = capacity;
        this.cache = new ConcurrentHashMap<>(this.capacity);
    }


    /**
     * 缓存中添加元素 可设置key的过期时间
     *
     * @param key
     * @param val
     * @param timeout  key的过期时间
     * @param timeUnit 时间单位
     */
    @Override
    public void put(K key, V val, long timeout, TimeUnit timeUnit) {
        //淘汰元素
        eliminate();
        Value<K, V> value = new Value<>(key, val, timeout, timeUnit);
        cache.put(key, value);

    }


    /**
     * 缓存中添加元素
     *
     * @param key
     * @param val
     */
    @Override
    public void put(K key, V val) {
        //淘汰元素
        eliminate();
        Value<K, V> value = new Value<>(key, val, 0, null);
        cache.put(key, value);
    }

    /**
     * 清空缓存
     */
    @Override
    public void clear() {
        cache.clear();
    }

    /**
     * 检查 key是否过期 不存在或过期 返回true 否则返回false
     * 如果该key过期同时删除key
     *
     * @param key
     * @return
     */
    @Override
    public boolean expire(K key) {
        Value<K, V> value = cache.get(key);
        if (value == null || value.timeout == 0) {
            return false;
        }
        boolean expire = (System.currentTimeMillis() - value.timestamp) > TimeUnit.MILLISECONDS.convert(value.timeout, value.unit);
        if (expire) {
            cache.remove(key, value);
        }
        return expire;
    }


    /**
     * 检查 key在指定的时间偏移量内是否过期 不存在或过期 返回true 否则返回false
     *
     * @param key
     * @param timeOffset
     * @param timeUnit
     * @return
     */
    @Override
    public boolean expireAt(K key, long timeOffset, TimeUnit timeUnit) {
        Value<K, V> value = cache.get(key);
        if (value == null || value.timeout == 0) {
            return false;
        }
        return TimeUnit.MILLISECONDS.convert(timeOffset, timeUnit) > TimeUnit.MILLISECONDS.convert(value.timeout, value.unit);
    }


    /**
     * 获取缓存中的值
     *
     * @param key
     * @return
     */
    @Override
    public V get(K key) {
        if (expire(key)) {
            return null;
        }
        Value<K, V> val = cache.get(key);
        if (val == null) return null;
        val.count.incrementAndGet();
        return val.val;
    }


    /**
     * 缓存中的数据量
     *
     * @return
     */
    @Override
    public int size() {
        return cache.size();
    }


    /**
     * 淘汰策略
     * 1、如果初始的容量小于100
     */
    private void eliminate() {
        if (cache.size() >= capacity) {//如果当前容器中的数据量超过了规定的容量则进行数据淘汰

            synchronized (lock) {

                Collection<Value<K, V>> values = cache.values();
                long expire = values.stream().filter(Value::expire).count();

                if ((expire > capacity / 2)) {//如果容器中的过期数据占比超过1/2则创建新容器并复制数据
                    cache = new ConcurrentHashMap<>(capacity);
                    values.stream().filter(e -> !e.expire()).forEach(data -> {
                        //数据复制
                        cache.put(data.key, data);
                    });
                } else {//否则创建新的容器将原容器中未过期的数据按照访问量降序排列  取列表前1/5的数据并复制到新的容器
                    cache = new ConcurrentHashMap<>(capacity);

                    cache.values().stream()
                            .filter(e -> !e.expire())
                            .sorted((o1, o2) -> o2.count.get() - o1.count.get())
                            .limit(size() / 5).forEach(data -> {
                        //数据复制
                        cache.put(data.key, data);

                    });

                }

            }

        }
    }


    /**
     * top count
     * 查询容器中访问前top的数据 格式为 {key,count}
     *
     * @param top
     * @return
     */
    public List<Map<K, Integer>> top(int top) {
        if (top > 100) throw new IllegalArgumentException("top must no more than 100");
        return cache.values().stream().sorted((o1, o2) -> o2.count.get() - o1.count.get()).limit(top).map(e -> {
            HashMap<K, Integer> map = new HashMap<>();
            map.put(e.key, e.count.get());
            return map;
        }).collect(Collectors.toList());
    }

    public int getCapacity() {
        return capacity;
    }

    public static CacheUtil getINSTANCE(int capacity) {
        if (INSTANCE == null) {
            synchronized (CacheUtil.class) {
                if (INSTANCE == null) {
                    INSTANCE = new CacheUtil(capacity);
                }
            }
        }
        return INSTANCE;
    }

    /**
     * 缓存容器中真正存储的 value数据
     *
     * @param <K>
     * @param <V>
     */
    private static class Value<K, V> {
        private K key;//用户传入缓存的key
        private V val; //用户缓存传入缓存的value
        private long timestamp;//时间戳
        private long timeout;//key的过期时间
        private TimeUnit unit;//时间单位
        private AtomicInteger count;//保存该条数据的访问量

        public Value(K key, V val, long timeout, TimeUnit unit) {
            this.key = key;
            this.val = val;
            this.timestamp = System.currentTimeMillis();
            this.timeout = timeout;
            this.unit = unit;
            this.count = new AtomicInteger();
        }

        /**
         * 是否过期
         *
         * @return
         */
        public boolean expire() {
            return (System.currentTimeMillis() - timestamp) > TimeUnit.MILLISECONDS.convert(timeout, unit);
        }

        public K getKey() {
            return key;
        }

        public V getVal() {
            return val;
        }

        public long getTimestamp() {
            return timestamp;
        }

        public long getTimeout() {
            return timeout;
        }

        public TimeUnit getUnit() {
            return unit;
        }

        public AtomicInteger getCount() {
            return count;
        }

        @Override
        public String toString() {
            return "Value{" +
                    "key=" + key +
                    ", val=" + val +
                    ", timestamp=" + timestamp +
                    ", timeout=" + timeout +
                    ", unit=" + unit +
                    ", count=" + count +
                    '}';
        }
    }
}

3、测试

import com.syx.cache.CacheUtil;

import java.util.concurrent.TimeUnit;

/**
 * @Author whh
 * @Date: 2023/02/24/ 19:36
 * @description
 */
public class CacheTest {
    public static void main(String[] args) throws InterruptedException {
        fn1();

    }


    public static void fn1() throws InterruptedException {
        CacheUtil<String,String> cache = CacheUtil.getINSTANCE(10);


        cache.put("key","whh",10,TimeUnit.MILLISECONDS);


        cache.put("ls","ls");
        cache.put("wz","wz");

        cache.put("ww","ww");
        System.out.println(cache.expire("key"));

        Thread.sleep(12);

        System.out.println(cache.expire("key"));

        System.out.println(cache.size());

        cache.get("wz");

        cache.get("wz");
        cache.get("ls");

        System.out.println(cache.top(10));

    }
}

4、测试结果

在这里插入图片描述

目前该工具还需进一步压测,以测试其性能,如有问题请大家指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值