package com.njzscloud.common.cache.config;

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

@Slf4j
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(CacheProperties.class)
public class CacheAutoConfiguration {

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