yl-backend/src/main/java/com/guwan/backend/config/CaffeineConfig.java

38 lines
1.2 KiB
Java

package com.guwan.backend.config;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.guwan.backend.controller.monitor.CacheMonitor;
import lombok.extern.slf4j.Slf4j;
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;
import java.util.concurrent.TimeUnit;
import static com.guwan.backend.constant.CacheConstants.CACHE_LIST;
@Slf4j
@EnableCaching
@Configuration
public class CaffeineConfig {
@Bean
@Primary
public CacheManager caffeineCacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCaffeine(Caffeine.newBuilder()
.recordStats()
.expireAfterAccess(60, TimeUnit.MINUTES)
.initialCapacity(100)
.maximumSize(1000));
cacheManager.setCacheNames(CACHE_LIST);
return cacheManager;
}
@Bean
public CacheMonitor cacheMonitor(CacheManager cacheManager) {
return new CacheMonitor(cacheManager);
}
}