线程安全,带失效时间的Map

线程安全,带失效时间的Map

具体使用按照自己的需要修改或调整,当然也可以直接使用

/**
 * 带失效时间的Map,线程安全
 */
public class CacheMap<K,T> {

    private static CacheMap cacheMap = null;

    private Map<K,BufferData<T>> map = new ConcurrentHashMap<>();

    public static CacheMap getInstance(){
        if(cacheMap == null){
            addInstance();
        }
        return cacheMap;
    }

    private static synchronized void addInstance(){
        if(cacheMap == null){
            cacheMap = new CacheMap();
        }
    }

    private CacheMap() {
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
            this.clearExpiry();
        },1L,1L, TimeUnit.SECONDS);
    }

    public T get(K key){
        BufferData<T> bufferData = map.get(key);
        if(bufferData == null){
            return null;
        }
        return bufferData.data;
    }

    public BufferData getBufferData(K key){
        BufferData<T> bufferData = map.get(key);
        if(bufferData == null){
            return null;
        }
        return bufferData;
    }

    public BufferData remove(K key){
        BufferData<T> bufferData = map.get(key);
        if(bufferData != null){
            map.remove(bufferData);
        }
        return bufferData;
    }

    public BufferData remove(BufferData<T> value){
        if(value == null){
            return value;
        }
        map.remove(value);
        return value;
    }

    public boolean containsKey(K key){
        return map.containsKey(key);
    }

    public boolean containsValue(T data){
        for (BufferData<T> bufferData : map.values()) {
            if(bufferData.data.equals(data)){
                return true;
            }
        }
        return false;
    }

    public Set<K> keySet(){
        return map.keySet();
    }

    public Collection values(){
        return map.values();
    }

    public void clear(){
        map.clear();
    }

    public boolean isEmpty(){
        return map.isEmpty();
    }

    /**
     * 清除失效数据
     */
    private void clearExpiry(){
        Iterator<BufferData<T>> iterator = map.values().iterator();
        while(iterator.hasNext()){
            BufferData<T> bufferData = iterator.next();
            if(bufferData.isExpiry()){
                iterator.remove();
            }
        }
    }

    /**
     * 缓存数据对象
     * @param <T>
     */
    public class BufferData<T>{
        private T data;//数据
        private long expiryTime;//失效时间

        public BufferData() {
        }

        public BufferData(T data, long expiryTime) {
            this.data = data;
            this.expiryTime = expiryTime;
        }

        public T getData() {
            return data;
        }

        public long getExpiryTime() {
            return expiryTime;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            BufferData<?> that = (BufferData<?>) o;
            return this.hashCode() == that.hashCode();
        }

        @Override
        public int hashCode() {
            return Objects.hash(data);
        }

        /**
         * 判断当前数据是否失效
         * @return  boolean
         */
        private boolean isExpiry(){
            if(System.currentTimeMillis() > expiryTime){
                return true;
            }
            return false;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值