package cn.reghao.oss.store.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
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 java.util.concurrent.TimeUnit;

/**
 * 缓存配置
 *
 * @author reghao
 * @date 2023-05-22 16:31:04
 */
@EnableCaching
@Configuration
public class CacheConfig {
    /**
     * 为 Spring Cache 的相关注解提供一个缓存
     *
     * @param
     * @return
     * @date 2024-03-04 16:37:05
     */
    @Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        Caffeine<Object, Object> caffeineCache = Caffeine.newBuilder()
                .initialCapacity(1000)
                .maximumSize(10_000)
                .expireAfterAccess(365, TimeUnit.DAYS);;
        cacheManager.setCaffeine(caffeineCache);
        return cacheManager;
    }

    /**
     * 提供一个全局的独立缓存
     *
     * @param
     * @return
     * @date 2024-03-04 16:37:38
     */
    @Bean("caffeineCache")
    public Cache<String, Object> caffeineCache() {
        return Caffeine.newBuilder()
                .initialCapacity(1000)
                .maximumSize(10_000)
                .expireAfterAccess(365, TimeUnit.DAYS)
                .build();
    }
}
