All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.centit.support.common.CachedMap Maven / Gradle / Ivy

There is a newer version: 5.3.2302
Show newest version
package com.centit.support.common;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;

/**
 * 用 ConcurrentHashMap 缓存对象,每个对象 自己维护缓存策略
 * @param  id 键值类型
 * @param  target 缓存对象的类型
 */
public class CachedMap {

    private ConcurrentMap> targetMap;
    private Date refreshTime;
    private long freshPeriod;
    private Function refresher;

    /**
     *
     * @param refresher 重新获取代码的接口
     * @param freshPeriod 保鲜时间,单位为分钟
     */
    public CachedMap(Function refresher, long freshPeriod , int initialCapacity){
        this.targetMap = new ConcurrentHashMap<>(initialCapacity);
        this.refresher = refresher;
        this.freshPeriod = freshPeriod;
    }

    public CachedMap(Function refresher){
        this(refresher, 43200L,16);
    }

    public CachedMap(Function refresher, int initialCapacity){
        this(refresher, 43200L,initialCapacity);
    }


    public void setFreshPeriod(int freshPeriod) {
        this.freshPeriod = freshPeriod;
    }

    public synchronized void evictObject(K key){
        CachedIdentifiedObject identifiedObject =  targetMap.get(key);
        if(identifiedObject!=null){
            identifiedObject.evictObject();
        }
    }

    public synchronized T getCachedObject(K key){
        CachedIdentifiedObject identifiedObject =  targetMap.get(key);
        if(identifiedObject != null){
            return identifiedObject.getCachedObject(key);
        }

        T target = refresher.apply(key);
        if(target != null) {
            targetMap.put(key,
                    new CachedIdentifiedObject(refresher, target, freshPeriod));
        }
        return target;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy