package com.ssyf.plugs.cache.properties;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author huwei
 * @date 2022/11/26 21:11
 */
@Data
@Accessors(chain = true)
@Component
@ConditionalOnProperty(value = "cache.enabled")
@ConfigurationProperties(prefix = "cache")
public class CacheProperties {
    /**
     * eh默认配置文件
     */
    private static final String EHCACHE_CONFIG_CLASS_PATH = "default_ehcache.xml";

    /**
     * 缓存更新时通知其他节点的topic名称
     */
    private String topic = "cache:auto:topic";

    /**
     * 开启缓存
     */
    private boolean enabled;

    /**
     * 开启redis缓存
     */
    private boolean redisEnabled;

    /**
     * 开启eh缓存
     */
    private boolean ehcacheEnabled;

    /**
     * 开启二级缓存
     */
    private boolean secondLevel = true;

    /**
     * 存储空值
     */
    private boolean nullValue;

    /**
     * 全局过期时间 ,默认一天
     * 单位：毫秒
     */
    private long expire = TimeUnit.DAYS.toMillis(1);

    /**
     * 开启随机过期时间，过期时间不小于全局过期时间
     */
    private boolean randomExpire = false;

    /**
     * 缓存可动态创建标识
     */
    private boolean dynamic = true;

    /**
     * 所有缓存公用静态缓存名称
     */
    private List<String> cacheNames = new ArrayList<>();

    /**
     * eh缓存共享
     */
    private boolean ehcacheShare;

    /**
     * eh配置文件路径
     */
    private String ehcacheConfig;

    /**
     * ch静态缓存名称
     */
    private List<String> ehcacheCacheNames = new ArrayList<>();

    public String getConfig(){
        return StrUtil.isNotEmpty(ehcacheConfig) ? ehcacheConfig : EHCACHE_CONFIG_CLASS_PATH;
    }
}
