CacheMatchLogger.java 1.2 KB
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();
	}

}