package com.lanyuanxiaoyao.service.configuration;

import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 缓存提供
 *
 * @author lanyuanxiaoyao
 * @date 2023-04-23
 */
@Configuration
@EnableCaching
public class CacheProvider {
    private static final Logger logger = LoggerFactory.getLogger(CacheProvider.class);

    @Primary
    @Bean("normal-cache")
    public CacheManager normalCache() {
        CaffeineCacheManager manager = new CaffeineCacheManager();
        manager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS));
        return manager;
    }

    @Bean("long-cache")
    public CacheManager longCache() {
        CaffeineCacheManager manager = new CaffeineCacheManager();
        manager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(30, TimeUnit.SECONDS));
        return manager;
    }
}
