package cn.stylefeng.team.generator.sdk.template.config;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.team.generator.api.pojo.template.BizGeneratorTemplateGroup;
import cn.stylefeng.team.generator.api.pojo.template.GeneratorTemplateGroup;
import cn.stylefeng.team.generator.sdk.template.cache.TemplateMemoryCache;
import cn.stylefeng.team.generator.sdk.template.cache.biz.BizTemplateMemoryCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static cn.stylefeng.roses.kernel.cache.api.constants.CacheConstants.NONE_EXPIRED_TIME;

@Configuration
@ConditionalOnMissingClass("org.springframework.data.redis.connection.RedisConnectionFactory")
public class TemplateCacheAutoConfiguration {

    @Bean
    public CacheOperatorApi<GeneratorTemplateGroup> generatorTemplateGroupCache() {
        TimedCache<String, GeneratorTemplateGroup> themeCache = CacheUtil.newTimedCache(NONE_EXPIRED_TIME);
        return new TemplateMemoryCache(themeCache);
    }

    @Bean
    public CacheOperatorApi<BizGeneratorTemplateGroup> bizGeneratorTemplateGroupCache() {
        TimedCache<String, BizGeneratorTemplateGroup> themeCache = CacheUtil.newTimedCache(NONE_EXPIRED_TIME);
        return new BizTemplateMemoryCache(themeCache);
    }

}
