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

io.github.dengchen2020.cache.properties.CacheSpecBuilder Maven / Gradle / Ivy

package io.github.dengchen2020.cache.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * 缓存配置
 * @author dengchen
 * @since 2024/5/30
 */
@ConfigurationProperties(prefix = "dc.cache")
public class CacheSpecBuilder {

    /**
     * 缓存属性配置
     */
    private Map specs = new HashMap<>();

    /**
     * 多久后过期
     */
    private Duration expireTime = Duration.ofSeconds(60);

    /**
     * 最大缓存元素个数(不适用于redis)
     */
    private int max = 200;

    /**
     * 是否读取后一段时间过期,默认写入后一段时间过期(不适用于redis)
     */
    private boolean isExpireAfterAccess = false;

    public void setSpecs(Map specs) {
        this.specs = specs;
    }

    public Map getSpecs() {
        return specs;
    }

    public Duration getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Duration expireTime) {
        this.expireTime = expireTime;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public boolean isExpireAfterAccess() {
        return isExpireAfterAccess;
    }

    public void setExpireAfterAccess(boolean expireAfterAccess) {
        isExpireAfterAccess = expireAfterAccess;
    }

    public static class CacheSpec {
        /**
         * 多久后过期
         */
        private Duration expireTime;
        /**
         * 最大缓存元素个数(不适用于redis)
         */
        private Integer max;
        /**
         * 是否读取后一段时间过期,默认写入后一段时间过期(不适用于redis)
         */
        private Boolean expireAfterAccess;

        public Duration getExpireTime() {
            if (expireTime == null) return null;
            return expireTime.getSeconds() <= 0 ? Duration.ofSeconds(1) : expireTime;
        }

        public Integer getMax() {
            if (max == null) return null;
            return max < 1 ? 1 : max;
        }

        public void setExpireTime(Duration expireTime) {
            this.expireTime = expireTime;
        }

        public void setMax(int max) {
            this.max = max;
        }

        public Boolean isExpireAfterAccess() {
            return expireAfterAccess;
        }

        public void setExpireAfterAccess(boolean expireAfterAccess) {
            this.expireAfterAccess = expireAfterAccess;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy