Files
thumb-up/src/main/java/cn/hezhaohui/thumb/manager/cache/CacheManager.java
T

94 lines
2.7 KiB
Java
Raw Normal View History

2025-10-23 18:37:47 +08:00
package cn.hezhaohui.thumb.manager.cache;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
2025-10-23 18:49:53 +08:00
import jakarta.annotation.Resource;
2025-10-23 18:37:47 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
2025-10-23 18:49:53 +08:00
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
2025-10-23 18:37:47 +08:00
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
public class CacheManager {
private TopK hotKeyDetector;
private Cache<String, Object> localCache;
2025-10-23 18:49:53 +08:00
@Resource
private RedisTemplate<String, Object> redisTemplate;
2025-10-23 18:37:47 +08:00
@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();
}
2025-10-23 18:49:53 +08:00
// 构造复合 Key
private String buildCacheKey(String hashKey, String key) {
return hashKey + ":" + key;
}
public Object get(String hashKey, String key) {
String compositeKey = this.buildCacheKey(hashKey, key);
// 1. 查本地缓存
Object value = localCache.getIfPresent(compositeKey);
if (value != null) {
log.info("本地缓存获取到数据 {} = {}", compositeKey, value);
// 记录访问次数
hotKeyDetector.add(key, 1);
return value;
}
// 2. 本地缓存未命中,查询 Redis
Object redisValue = redisTemplate.opsForHash().get(hashKey, key);
if (redisValue == null) {
return null;
}
// 3. 记录访问次数
AddResult addResult = hotKeyDetector.add(key, 1);
// 4. 缓存数据 热Key
if (addResult.isHotKey()) {
localCache.put(compositeKey, redisValue);
}
return redisValue;
}
public void putIfPresent(String hashKey, String key, Object value) {
String compositeKey = this.buildCacheKey(hashKey, key);
Object object = localCache.getIfPresent(compositeKey);
if (object == null) {
return;
}
localCache.put(compositeKey, value);
}
// 定时清理过期的 HotKey 数据
@Scheduled(fixedRate = 20, timeUnit = TimeUnit.SECONDS)
public void cleanHotKeys() {
hotKeyDetector.fading();
}
2025-10-23 18:37:47 +08:00
}