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

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

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

import com.centit.support.algorithm.DatetimeOpt;

import java.util.Date;
import java.util.function.Function;

/**
 * 缓存 有 ID 的对象
 * @param  id 键值类型
 * @param  target 缓存对象的类型
 */
public class CachedIdentifiedObject {

    private T target;
    private boolean evicted;
    private Date refreshTime;
    private long freshPeriod;
    private Function refresher;

    public CachedIdentifiedObject(Function refresher, T target, long freshPeriod){
        assert target!=null :"输入的 target 值不能为null ";
        this.target = target;
        this.evicted = false;
        refreshTime = DatetimeOpt.currentUtilDate();
        this.refresher = refresher;
        this.freshPeriod = freshPeriod;
    }
    /**
     *
     * @param refresher 重新获取代码的接口
     * @param freshPeriod 保鲜时间,单位为分钟
     */
    public CachedIdentifiedObject(Function refresher, long freshPeriod){
        this.target = null;
        this.evicted = true;
        this.refresher = refresher;
        this.freshPeriod = freshPeriod;
    }

    public CachedIdentifiedObject(Function refresher){
        //默认时间一个月
        this(refresher,43200L);
    }


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

    public synchronized void evictObject(){
        evicted = true;
    }

    public synchronized T getCachedObject(K key){
        if(this.target == null || this.evicted ||
                System.currentTimeMillis() > refreshTime.getTime() + freshPeriod * 60000 ){
            target = refresher.apply(key);
            refreshTime = DatetimeOpt.currentUtilDate();
            this.evicted = false;
        }
        return target;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy