CacheMatchLogger.java
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.yoho.search.cache.model;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.yoho.search.cache.model.CacheCount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CacheMatchLogger {
private static final Logger CACHE_MATCH = LoggerFactory.getLogger("CACHE_MATCH");
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
private CacheCount cacheCount = new CacheCount();
public void init(String cacheName,int cacheInMinute,int logPeriodInMinute) {
// 每1分钟记录下缓存命中率
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// 记录cacheService的缓存命中率
if(cacheCount.getTotalCount().get()>0){
CACHE_MATCH.info("cacheName is [{}] , cacheInMinute is [{}],[{}]",cacheName,cacheInMinute,cacheCount);
}
cacheCount.clear();
}
}, logPeriodInMinute, logPeriodInMinute, TimeUnit.MINUTES);
}
public void incTotalCount(){
cacheCount.incTotalCount();
}
public void incMatchCount(){
cacheCount.incMatchCount();
}
}