package com.kwan.springbootkwan.config; 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; @Configuration @EnableCaching public class CacheConfig { @Bean public Caffeine caffeineConfig() { return Caffeine.newBuilder() .expireAfterWrite(10, TimeUnit.MINUTES) // 设置缓存失效时间为10分钟 .maximumSize(10_000) // 设置缓存的最大容量为10_000 .recordStats(); // 启用统计信息,可通过Cache.stats()获取缓存命中率等统计数据 } @Bean public CacheManager cacheManager(Caffeine caffeine) { CaffeineCacheManager cacheManager = new CaffeineCacheManager(); cacheManager.setCaffeine(caffeine); return cacheManager; } }