package com.whyc.config;
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
import org.springframework.cache.CacheManager;
|
import org.springframework.cache.caffeine.CaffeineCache;
|
import org.springframework.cache.support.SimpleCacheManager;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Primary;
|
|
import java.util.ArrayList;
|
|
/**
|
* Caffeine配置
|
*/
|
@Configuration
|
public class CaffeineConfig {
|
|
public static enum Caches{
|
defaultCache(500),
|
defaultCache2Exp(500,24)
|
;
|
|
Caches() {
|
}
|
|
Caches(int maxSize) {
|
this.maxSize = maxSize;
|
}
|
|
Caches(int maxSize, int ttl) {
|
this.maxSize = maxSize;
|
this.ttl = ttl;
|
}
|
|
private int maxSize;
|
private int ttl;
|
|
public int getMaxSize() {
|
return maxSize;
|
}
|
|
public void setMaxSize(int maxSize) {
|
this.maxSize = maxSize;
|
}
|
|
public int getTtl() {
|
return ttl;
|
}
|
|
public void setTtl(int ttl) {
|
this.ttl = ttl;
|
}
|
}
|
|
@Bean
|
@Primary
|
public CacheManager caffeineCacheManager() {
|
SimpleCacheManager cacheManager = new SimpleCacheManager();
|
ArrayList<CaffeineCache> caffeineCaches = new ArrayList<>();
|
|
for (Caches cache : Caches.values()) {
|
caffeineCaches.add(new CaffeineCache(cache.name(),
|
Caffeine.newBuilder().recordStats()
|
.maximumSize(cache.getMaxSize())
|
.build()
|
));
|
}
|
cacheManager.setCaches(caffeineCaches);
|
return cacheManager;
|
|
}
|
|
/*@Bean(value = "defaultCache")
|
public Cache<String,Object> defaultCache(){
|
return Caffeine.newBuilder().recordStats()
|
.maximumSize(Caches.defaultCache.getMaxSize())
|
.build();
|
}
|
|
@Bean(value = "defaultCache2Exp")
|
public Cache<String,Object> defaultCache2Exp(){
|
return Caffeine.newBuilder().recordStats()
|
.maximumSize(Caches.defaultCache2Exp.getMaxSize())
|
.expireAfterWrite(Caches.defaultCache2Exp.ttl, TimeUnit.HOURS)
|
.build();
|
}*/
|
|
}
|