CacheService.java 3.13 KB
package com.yoho.search.cache;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yoho.search.dal.model.SearchParam;
import com.yoho.search.utils.MD5Util;
import com.yoho.search.utils.SearchParamUtils;

@Service
public class CacheService {
	
	private static final Logger logger = LoggerFactory.getLogger(CacheService.class);
	
	private Map<String,CacheObject> cache = new ConcurrentHashMap<String,CacheObject>();
	private CacheCount cacheCount = new CacheCount();
	private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
	
	@PostConstruct
	void init(){
		//每5分钟记录下缓存命中率
		scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				logger.info("do log cache matchPercent,[{}]",cacheCount);
				cacheCount.clear();
			}
		}, 1, 5, TimeUnit.MINUTES);
		
		//每30分钟清除缓存对象,防止内存爆掉
		scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				logger.info("do clear cacheObject...");
				cache = new ConcurrentHashMap<String,CacheObject>();
			}
		}, 30, 30, TimeUnit.MINUTES);
	}
	
	private Object getObjectFromCache(String key){
		cacheCount.incTotalCount();
		CacheObject cacheObject = cache.get(key);
		if(cacheObject==null){
			return null;
		}
		if(cacheObject.getExpireTime() < System.currentTimeMillis()){
			cache.remove(key);
			return null;
		}
		cacheCount.incMatchCount();
		return cacheObject.getObject();
	}
	
	private void addObjectToCache(String key,Object object,long expireInSecond){
		CacheObject cacheObject = new CacheObject(object, expireInSecond);
		cache.put(key, cacheObject);
	}
	
	/**
	 * 根据SearchParam生成的ES报文
	 * @param searchParam
	 * @return
	 */
	private String genSearchParamString(String indexName,SearchParam searchParam){
		StringBuilder sb = new StringBuilder();
		//拼装索引名称
		sb.append("indexName:").append(indexName).append(";");
		//拼装搜索类型
		sb.append("searchType:").append(searchParam.getSearchType()==null?"":searchParam.getSearchType().name()).append(";");
		//拼装报文
		SearchSourceBuilder searchSourceBuilder = SearchParamUtils.genSearchSourceBuilderFromSearchParam(searchParam);
		sb.append("searchSource:").append(searchSourceBuilder.toString()).append(";");
		//打印拼装结果
		logger.info("cacheKey is [{}]",sb.toString());
		return MD5Util.string2MD5(sb.toString());
	}
	
	public Object getObjectFromCache(String indexName,SearchParam searchParam){
		String key = this.genSearchParamString(indexName,searchParam);
		return this.getObjectFromCache(key);
	}
	
	public void addObjectToCache(String indexName,SearchParam searchParam,Object object){
		String key = this.genSearchParamString(indexName,searchParam);
		this.addObjectToCache(key, object, 5 * 60);
	}
	
}