package cn.reghao.autodop.dmaster.spring.config;

import com.github.benmanes.caffeine.cache.*;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * 缓存配置
 *
 * @author reghao
 * @date 2020-03-06 10:58:04
 */
@Configuration
@EnableCaching
public class CacheConfig {
    @Bean(name = "caffeineCacheManager")
    public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .initialCapacity(1000)
                .maximumSize(10000)
                .expireAfterAccess(60, TimeUnit.SECONDS));
        return cacheManager;
    }

    @Bean("caffeineCache")
    public Cache<String, Object> cache() {
        return Caffeine.newBuilder()
                .initialCapacity(1000)
                .maximumSize(10000)
                .expireAfterAccess(60, TimeUnit.SECONDS)
                .build();
    }
}
