2024-12-23 16:08:26 +08:00
|
|
|
package com.guwan.backend.config;
|
|
|
|
|
|
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
2024-12-23 17:04:02 +08:00
|
|
|
import com.guwan.backend.controller.monitor.CacheMonitor;
|
2024-12-23 16:08:26 +08:00
|
|
|
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;
|
2024-12-23 17:04:02 +08:00
|
|
|
import static com.guwan.backend.constant.CacheConstants.CACHE_LIST;
|
2024-12-23 16:08:26 +08:00
|
|
|
|
|
|
|
@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));
|
2024-12-23 17:04:02 +08:00
|
|
|
|
|
|
|
cacheManager.setCacheNames(CACHE_LIST);
|
2024-12-23 16:08:26 +08:00
|
|
|
return cacheManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
2024-12-23 17:04:02 +08:00
|
|
|
public CacheMonitor cacheMonitor(CacheManager cacheManager) {
|
|
|
|
return new CacheMonitor(cacheManager);
|
2024-12-23 16:08:26 +08:00
|
|
|
}
|
|
|
|
}
|