package com.njzscloud.common.cache.config;

import com.njzscloud.common.cache.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty(prefix = "cache", name = "enable")
@EnableConfigurationProperties(CacheProperties.class)
public class CacheAutoConfiguration {

    @Bean
    public Cache cache(CacheProperties properties) {
        CacheProperties.FirstCacheProperties first = properties.getFirst();
        CacheProperties.SecondCacheProperties second = properties.getSecond();
        Cache noCache = new NoCache();
        Cache firstCache = first.isEnabled() ? new FirstCache(first.getCapacity(), first.getTimeout()) : noCache;
        Cache secondCache = second.isEnabled() ? new SecondCache(second.getTimeout()) : noCache;
        return new DualCache(firstCache, secondCache);
    }
}
