REQ-2453: 添加日志, 看一下分类统计的命中率

This commit is contained in:
yanglin 2024-07-09 18:46:57 +08:00
parent cbd697a651
commit bbc6f3589e
3 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package cn.axzo.msg.center.message.service.todo.cache;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author yanglin
*/
@Slf4j
@RequiredArgsConstructor
class CacheMetric {
private final String name;
private final AtomicLong hitCount = new AtomicLong();
private final AtomicLong missCount = new AtomicLong();
void incrHitCount() {
hitCount.incrementAndGet();
}
void incrMissCount() {
missCount.incrementAndGet();
}
/**
* 大概统计一下, 不是100%准确
*/
void print() {
long hitCount = this.hitCount.get();
long missCount = this.missCount.get();
long totalAccesses = hitCount + missCount;
double hitRate = totalAccesses == 0 ? 0 : ((double) hitCount / totalAccesses) * 100;
log.info("CacheMetric[{}]: hitCount={}, missCount={}, hitRate={}%",
name, hitCount, missCount, String.format("%.2f", hitRate));
}
}

View File

@ -35,6 +35,7 @@ public class NodeStatCache {
private final TodoMapper todoMapper;
private final StringRedisTemplate stringRedisTemplate;
private final PendingMessageBizConfig cfg;
private final PeriodCacheMetric cacheMetric = new PeriodCacheMetric("node-stat", 1, TimeUnit.HOURS);
public PendingMessageStatisticResponseV2 getCacheResponseOrReload(
MessageGroupNodeStatisticParam request, Supplier<PendingMessageStatisticResponseV2> loader) {
@ -51,6 +52,7 @@ public class NodeStatCache {
NodeStatCacheValue cacheValue = getCacheValue(request);
boolean reloaded = false;
if (cacheValue == null || cacheValue.shouldReload(templateLatestUpdateTime, todoLatestUpdateTime)) {
cacheMetric.incrMissCount();
log.info("reloading node stat data, request={}", request);
PendingMessageStatisticResponseV2 response = loader.get();
cacheValue = new NodeStatCacheValue();
@ -60,6 +62,7 @@ public class NodeStatCache {
setCacheValue(request, cacheValue);
reloaded = true;
}
cacheMetric.incrHitCount();
PendingMessageStatisticResponseV2 copy = new PendingMessageStatisticResponseV2();
log.info("use cached node stat data, request={}", request);
BeanUtils.copyProperties(cacheValue.getResponse(), copy);

View File

@ -0,0 +1,36 @@
package cn.axzo.msg.center.message.service.todo.cache;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author yanglin
*/
class PeriodCacheMetric {
private final String name;
private final AtomicReference<CacheMetric> metric;
PeriodCacheMetric(String name, int period, TimeUnit timeUnit) {
this.name = name;
this.metric = new AtomicReference<>(new CacheMetric(name));
Executors.newSingleThreadScheduledExecutor()
.scheduleAtFixedRate(this::printAndReset, period, period, timeUnit);
}
void incrHitCount() {
metric.get().incrHitCount();
}
void incrMissCount() {
metric.get().incrMissCount();
}
void printAndReset() {
CacheMetric metric = this.metric.get();
this.metric.set(new CacheMetric(name));
metric.print();
}
}