package com.gkhy.exam.common.config; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import org.springframework.cache.CacheManager; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; public class CaffeineConfig { /** * 系统默认缓存TTL时间:10分钟 */ public static final long SYSTEM_DEFAULT_EXPIRES = 10 * 60 * 1000; @Bean public Cache caffeineCache(){ return Caffeine.newBuilder() // 设置最后一次写入或访问后经过固定时间过期 .expireAfterWrite(SYSTEM_DEFAULT_EXPIRES, TimeUnit.MILLISECONDS) // 初始的缓存空间大小 .initialCapacity(100) // 缓存的最大条数 .maximumSize(10000) .build(); } //@Bean public CacheManager cacheManager() { // create and return an instance of your preferred cache manager, for example: return new ConcurrentMapCacheManager(); } }