🔄Update: 实现 HeavyKeeper 算法

This commit is contained in:
2025-10-23 18:37:47 +08:00
parent 1ec560dde5
commit a7db0ff0ba
4 changed files with 255 additions and 0 deletions
@@ -0,0 +1,40 @@
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();
}
}