Authored by hugufei

添加service缓存

... ... @@ -9,6 +9,8 @@ public class CacheObject {
super();
this.object = object;
this.expireTime = System.currentTimeMillis() + expireInSecond * 1000L;
System.out.println(System.currentTimeMillis());
System.out.println(expireTime);
}
public Object getObject() {
... ... @@ -18,5 +20,12 @@ public class CacheObject {
public long getExpireTime() {
return expireTime;
}
public static void main(String[] args) {
long current = System.currentTimeMillis();
System.out.println(current);
System.out.println(current + 3000 * 60);
}
}
... ...
package com.yoho.search.cache;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
... ... @@ -14,6 +16,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.yoho.search.dal.model.SearchParam;
import com.yoho.search.dal.model.SearchResult;
import com.yoho.search.utils.MD5Util;
import com.yoho.search.utils.SearchParamUtils;
... ... @@ -26,9 +29,9 @@ public class CacheService {
private CacheCount cacheCount = new CacheCount();
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
private static final int cacheTime = 3;//结果缓存的时间
private static final int logPeriod = 5;//记录缓存命中的时间
private static final int clearCachePeriod = 15;//清除整个缓存对象的时间
private static final int cacheTimeInMinute = 3;//结果缓存的时间
private static final int logPeriodInMinute = 3;//记录缓存命中的时间
private static final int clearCachePeriodInMinute = 15;//清除整个缓存对象的时间
@PostConstruct
void init(){
... ... @@ -36,10 +39,10 @@ public class CacheService {
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
logger.info("do log cache matchPercent,logPeriod is [{}],[{}]",logPeriod,cacheCount);
logger.info("do log cache matchPercent,logPeriodInMinute is [{}],[{}]",logPeriodInMinute,cacheCount);
cacheCount.clear();
}
}, logPeriod, logPeriod, TimeUnit.MINUTES);
}, logPeriodInMinute, logPeriodInMinute, TimeUnit.MINUTES);
//每15分钟清除缓存对象,防止内存爆掉
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
... ... @@ -48,7 +51,7 @@ public class CacheService {
logger.info("do clear cacheObject...");
cache = new ConcurrentHashMap<String,CacheObject>();
}
}, clearCachePeriod, clearCachePeriod, TimeUnit.MINUTES);
}, clearCachePeriodInMinute, clearCachePeriodInMinute, TimeUnit.MINUTES);
}
private Object getObjectFromCache(String key){
... ... @@ -65,38 +68,59 @@ public class CacheService {
return cacheObject.getObject();
}
private void addObjectToCache(String key,Object object,long expireInSecond){
CacheObject cacheObject = new CacheObject(object, expireInSecond);
private void addObjectToCache(String key,Object object){
CacheObject cacheObject = new CacheObject(object, cacheTimeInMinute * 60);
cache.put(key, cacheObject);
}
/**
* 根据SearchParam生成的ES报文
* @param searchParam
* @return
*/
private String genSearchParamString(String indexName,SearchParam searchParam){
/***********************************for SearchParam and SearchResult*****************************************/
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(";");
//拼装报文
// 拼装搜索类型
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());
// 打印拼装结果
// logger.info("cacheKey is [{}]",sb.toString());
return MD5Util.string2MD5(sb.toString());
}
public SearchResult getSearchResultFromCache(String indexName, SearchParam searchParam) {
String key = this.genSearchParamString(indexName, searchParam);
return (SearchResult) this.getObjectFromCache(key);
}
public void addSearchResultToCache(String indexName, SearchParam searchParam, SearchResult result) {
String key = this.genSearchParamString(indexName, searchParam);
this.addObjectToCache(key, result);
}
/***********************************for multiGet*****************************************/
private String genMultiGetParamString(String indexName, Set<String> idList, List<String> fields) {
StringBuilder sb = new StringBuilder();
// 拼装索引名称
sb.append("indexName:").append(indexName).append(";");
// 拼装搜索类型
sb.append("idList:").append(idList.toString()).append(";");
// 拼装报文
sb.append("fields:").append(fields==null?"":fields.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);
@SuppressWarnings("unchecked")
public List<Map<String, Object>> getMultiGetResultFromCache(String indexName, Set<String> idList, List<String> fields) {
String key = this.genMultiGetParamString(indexName, idList, fields);
return (List<Map<String, Object>>) this.getObjectFromCache(key);
}
public void addObjectToCache(String indexName,SearchParam searchParam,Object object){
String key = this.genSearchParamString(indexName,searchParam);
this.addObjectToCache(key, object, cacheTime);
public void addMultiGetResultResultToCache(String indexName, Set<String> idList, List<String> fields, List<Map<String, Object>> result) {
String key = this.genMultiGetParamString(indexName, idList, fields);
this.addObjectToCache(key, result);
}
}
... ...
... ... @@ -2,6 +2,7 @@ package com.yoho.search.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
... ... @@ -26,6 +27,7 @@ public class SearchCommonService {
private IndexService indexService;
@Autowired
private CacheService cacheService;
/**
* 通用的查询接口
... ... @@ -36,9 +38,9 @@ public class SearchCommonService {
*/
public SearchResult doSearch(final String indexName, final SearchParam searchParam) {
// 1、先从缓存中取结果
Object objectFromCache = cacheService.getObjectFromCache(indexName, searchParam);
if (objectFromCache != null) {
return (SearchResult) objectFromCache;
SearchResult resultFromCache = cacheService.getSearchResultFromCache(indexName, searchParam);
if (resultFromCache != null) {
return resultFromCache;
}
// 2、取不到再从ES获取结果
SearchResult searchResult = null;
... ... @@ -52,7 +54,9 @@ public class SearchCommonService {
}, firstIndex);
}
// 3、将ES中的缓存结果放进缓存中
cacheService.addObjectToCache(indexName, searchParam, searchResult);
if (searchResult != null) {
cacheService.addSearchResultToCache(indexName, searchParam, searchResult);
}
return searchResult;
}
... ... @@ -89,40 +93,44 @@ public class SearchCommonService {
* @param fields
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Map<String, Object>> doMultiGet(final String indexName, final String[] idList, final List<String> fields) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if (idList == null || idList.length == 0) {
return list;
}
Index firstIndex = indexService.getIndex(indexName);
if (firstIndex != null) {
list = indexService.execute(new SearchCallback<List>() {
@Override
public List<Map<String, Object>> execute(IndexClient indexClient) {
return indexClient.multiGet(indexName, idList, fields);
}
}, firstIndex);
Set<String> idSet = new HashSet<String>();
for (String id : idList) {
idSet.add(id);
}
return list;
return this.doMultiGet(indexName, idList, fields);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Map<String, Object>> doMultiGet(final String indexName, final Set<String> idList, final List<String> fields) throws Exception {
List<Map<String, Object>> list = new LinkedList<Map<String, Object>>();
if (idList==null || idList.size() == 0){
return list;
List<Map<String, Object>> result = new LinkedList<Map<String, Object>>();
if (idList == null || idList.size() == 0) {
return result;
}
// 1、先从缓存中取结果
List<Map<String, Object>> resultFromCache = cacheService.getMultiGetResultFromCache(indexName, idList, fields);
if (resultFromCache != null) {
return resultFromCache;
}
// 2、先ES中批量获取结果
Index firstIndex = indexService.getIndex(indexName);
if (firstIndex != null) {
list = indexService.execute(new SearchCallback<List>() {
result = indexService.execute(new SearchCallback<List>() {
@Override
public List<Map<String, Object>> execute(IndexClient indexClient) {
return indexClient.multiGet(indexName, idList, fields);
}
}, firstIndex);
}
return list;
// 3、将搜索结果加入到缓存中
if (result != null) {
cacheService.addMultiGetResultResultToCache(indexName, idList,fields, result);
}
return result;
}
}
... ...