CacheProperties.java 2.12 KB
package com.infoloop.tianting.cache;

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

/**
 * 缓存配置属性
 */
@Data
@ConfigurationProperties(prefix = "cache")
public class CacheProperties {

    /**
     * 是否启用缓存
     */
    private boolean enabled = true;

    /**
     * 默认缓存过期时间(秒)
     */
    private int defaultTtl = 300;

    /**
     * Caffeine本地缓存配置
     */
    private CaffeineProperties caffeine = new CaffeineProperties();

    /**
     * Redis缓存配置
     */
    private RedisProperties redis = new RedisProperties();

    /**
     * 监控配置
     */
    private MetricsProperties metrics = new MetricsProperties();

    @Data
    public static class CaffeineProperties {
        /**
         * 初始容量
         */
        private int initialCapacity = 100;

        /**
         * 最大容量
         */
        private int maximumSize = 10000;

        /**
         * 写入后过期时间(秒)
         */
        private int expireAfterWrite = 60;

        /**
         * 访问后过期时间(秒),0表示不启用
         */
        private int expireAfterAccess = 0;

        /**
         * 是否记录统计信息
         */
        private boolean recordStats = true;
    }

    @Data
    public static class RedisProperties {
        /**
         * 是否启用Redis缓存
         */
        private boolean enabled = true;

        /**
         * Redis主机
         */
        private String host = "localhost";

        /**
         * Redis端口
         */
        private int port = 6379;

        /**
         * Redis密码
         */
        private String password;

        /**
         * Redis数据库索引
         */
        private int database = 1;

        /**
         * 连接超时时间(毫秒)
         */
        private int timeout = 2000;
    }

    @Data
    public static class MetricsProperties {
        /**
         * 是否启用监控
         */
        private boolean enabled = true;

        /**
         * 监控步长
         */
        private String step = "1m";
    }
}