41 lines
1.0 KiB
Java
41 lines
1.0 KiB
Java
package cn.hezhaohui.thumb.manager.cache;
|
|||
|
|
|
||
|
|
import com.github.benmanes.caffeine.cache.Cache;
|
||
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.springframework.context.annotation.Bean;
|
||
|
|
import org.springframework.stereotype.Component;
|
||
|
|
|
||
|
|
import java.util.concurrent.TimeUnit;
|
||
|
|
|
||
|
|
@Component
|
||
|
|
@Slf4j
|
||
|
|
public class CacheManager {
|
||
|
|
private TopK hotKeyDetector;
|
||
|
|
private Cache<String, Object> localCache;
|
||
|
|
|
||
|
|
@Bean
|
||
|
|
public TopK getHotKeyDetector() {
|
||
|
|
return hotKeyDetector = new HeavyKeeper(
|
||
|
|
// 监控 Top 100 Key
|
||
|
|
100,
|
||
|
|
// 宽度
|
||
|
|
100000,
|
||
|
|
// 深度
|
||
|
|
5,
|
||
|
|
// 衰减系数
|
||
|
|
0.92,
|
||
|
|
// 最小出现 10 次才记录
|
||
|
|
10
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Bean
|
||
|
|
public Cache<String, Object> localCache() {
|
||
|
|
return localCache = Caffeine.newBuilder()
|
||
|
|
.maximumSize(1000)
|
||
|
|
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||
|
|
.build();
|
||
|
|
}
|
||
|
|
}
|