Authored by henry

ehcache只對提前集合的結果做緩存,緩存時間5分鐘

package com.yoho.search.service;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
... ... @@ -37,6 +38,8 @@ public class SearchCacheService {
private static final int cacheTimeInMinute = 3;//结果缓存的时间
private static final int logPeriodInMinute = 3;//记录缓存命中的时间
private static final String CACHE_KEY = "PRE_AGG_KEY";
@PostConstruct
void init(){
... ... @@ -126,5 +129,28 @@ public class SearchCacheService {
String key = this.genMultiGetParamString(indexName, idList, fields);
this.addObjectToCache(key, result);
}
public String getRequestMapAsCacheKey(Map<String, String> paramMap){
StringBuilder sb = new StringBuilder(CACHE_KEY);
Iterator iter = paramMap.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
String val = (String) paramMap.get(key);
sb.append("param_Key:").append(key).append(";");
sb.append("param_Value:").append(val).append(";");
}
return MD5Util.string2MD5(sb.toString());
}
public Map<String, Object> getPreAggregationResult(Map<String, String> paramMap){
String key = this.getRequestMapAsCacheKey(paramMap);
return (Map<String, Object>)this.getObjectFromCache(key);
}
public void setPreAggregationResult(Map<String, String> paramMap,Map<String, Object> aggregationResult){
String key = this.getRequestMapAsCacheKey(paramMap);
addObjectToCache(key,aggregationResult);
}
}
... ...
... ... @@ -36,10 +36,10 @@ public class SearchCommonService {
*/
public SearchResult doSearch(final String indexName, final SearchParam searchParam) {
// 1、先从缓存中取结果
SearchResult resultFromCache = cacheService.getSearchResultFromCache(indexName, searchParam);
if (resultFromCache != null) {
return resultFromCache;
}
// SearchResult resultFromCache = cacheService.getSearchResultFromCache(indexName, searchParam);
// if (resultFromCache != null) {
// return resultFromCache;
// }
// 2、取不到再从ES获取结果
SearchResult searchResult = null;
Index firstIndex = indexService.getIndex(indexName);
... ... @@ -52,9 +52,9 @@ public class SearchCommonService {
}, firstIndex);
}
// 3、将ES中的缓存结果放进缓存中
if (searchResult != null) {
cacheService.addSearchResultToCache(indexName, searchParam, searchResult);
}
// if (searchResult != null) {
// cacheService.addSearchResultToCache(indexName, searchParam, searchResult);
// }
return searchResult;
}
... ... @@ -110,10 +110,10 @@ public class SearchCommonService {
return result;
}
// 1、先从缓存中取结果
List<Map<String, Object>> resultFromCache = cacheService.getMultiGetResultFromCache(indexName, idList, fields);
if (resultFromCache != null) {
return resultFromCache;
}
// 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) {
... ... @@ -125,9 +125,9 @@ public class SearchCommonService {
}, firstIndex);
}
// 3、将搜索结果加入到缓存中
if (result != null) {
cacheService.addMultiGetResultResultToCache(indexName, idList,fields, result);
}
// if (result != null) {
// cacheService.addMultiGetResultResultToCache(indexName, idList,fields, result);
// }
return result;
}
... ...
... ... @@ -39,6 +39,8 @@ public class SearchProductsService {
private SearchCommonService searchCommonService;
@Autowired
private AggregationFactoryService aggregationFactoryService;
@Autowired
private SearchCacheService cacheService;
/**
* 搜索接口[商品列表以及一堆聚合结果]
... ... @@ -130,6 +132,10 @@ public class SearchProductsService {
*/
private Map<String, Object> preAggregationSearch(SearchParam searchParam, Map<String, String> paramMap) throws Exception {
Map<String, Object> preAggregationResult = new HashMap<String, Object>();
Map<String, Object> cache_pre_AggregationResult = cacheService.getPreAggregationResult(paramMap);
if(cache_pre_AggregationResult!=null){
return cache_pre_AggregationResult;
}
// 1)年龄层
// if (paramMap.containsKey(ISearchConstans.PARAM_SEARCH_AGELEVEL) &&
// StringUtils.isNotBlank(paramMap.get(ISearchConstans.PARAM_SEARCH_AGELEVEL)))
... ... @@ -177,6 +183,8 @@ public class SearchProductsService {
searchParam.setFiter(searchServiceHelper.constructFilterBuilder(paramMap, ISearchConstans.PARAM_SEARCH_BRAND));
preAggregationResult.put(brandAggregation.aggName(), doPreSearch(searchParam.clone(), paramMap));
}
if(preAggregationResult!=null)
cacheService.setPreAggregationResult(paramMap,preAggregationResult);
return preAggregationResult;
}
... ...
package com.yoho.search.vo;
public class AgeLevelVo {
import java.io.Serializable;
public class AgeLevelVo implements Serializable {
private String id;
private String name;
... ...
package com.yoho.search.vo;
public class SearchApiResult {
import java.io.Serializable;
public class SearchApiResult implements Serializable {
private int code = 200;
private String message = "success";
private Object data;
... ...
package com.yoho.search.vo;
public class SizeInfoVO {
import java.io.Serializable;
public class SizeInfoVO implements Serializable {
private int size_id;
... ...
package com.yoho.search.vo;
public class SizeSortReqBO {
import java.io.Serializable;
public class SizeSortReqBO implements Serializable {
private String isdiscount = "";// 是否打折
private String storage_num = "1";
... ...
package com.yoho.search.vo;
import java.io.Serializable;
import java.util.List;
public class SortWithSizesVO {
public class SortWithSizesVO implements Serializable {
private int small_sort_id;
private int middle_sort_id;
... ...