Authored by hugufei

Merge branch 'cache_fix' into brandproduct

... ... @@ -37,6 +37,8 @@ public class CacheTimeConstants {
public static final int CACHE_30_MINUTE = 30;
public static final int CACHE_15_MINUTE = 15;
public static final int CACHE_10_MINUTE = 10;
}
... ...
... ... @@ -21,7 +21,7 @@ public abstract class AbstractCacheComponent<T> {
public T queryWithCache(ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) throws Exception {
//1、生成RedisKeyBuilder
RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(paramQueryFilter);
RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(paramQueryFilter,paramMap);
if (redisKeyBuilder == null) {
return null;
}
... ... @@ -96,10 +96,19 @@ public abstract class AbstractCacheComponent<T> {
return JSON.parseObject(redisValue, type);
}
protected abstract RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter);
protected abstract int cacheTimeInMinute();
protected abstract String cacheSceneKey();
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter,Map<String,String> paramMap){
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
redisKeyBuilder.appendFixed("YOHOSEARCH").appendFixed(":");//前缀
redisKeyBuilder.appendFixed(cacheSceneKey()).appendFixed(":");//场景
redisKeyBuilder.appendFixed(cacheTimeInMinute()).appendFixed(":");//缓存时间
redisKeyBuilder.appendVar(paramQueryFilter.getParamMd5Key());//报文
return redisKeyBuilder;
}
protected boolean useEhcache() {
return false;
}
... ...
package com.yoho.search.service.recall.beans.persional;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.cache.CacheTimeConstants;
... ... @@ -50,13 +49,13 @@ public class CachePersionalFactorComponent extends AbstractCacheComponent<PagePe
}
@Override
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("PAGE_FACTORS_NEW:").appendVar(paramQueryFilter.getParamMd5Key());
protected int cacheTimeInMinute() {
return CacheTimeConstants.PAGE_PERSIONAL_FACTOR;
}
@Override
protected int cacheTimeInMinute() {
return CacheTimeConstants.PAGE_PERSIONAL_FACTOR;
protected String cacheSceneKey() {
return "PERSIONAL_FACTOR";
}
@Override
... ...
... ... @@ -55,13 +55,13 @@ public class ActivityShopBrandListService extends AbstractCacheComponent<List<Ac
}
@Override
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("ACTIVITY_SHOP_BRANDS:").appendVar(paramQueryFilter.getParamMd5Key());
protected int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_60_MINUTE;
}
@Override
protected int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_60_MINUTE;
protected String cacheSceneKey() {
return "ACTIVITY_SHOP_BRANDS";
}
@Override
... ...
... ... @@ -65,13 +65,13 @@ public class DiscountService extends AbstractCacheComponent<JSONObject> implemen
}
@Override
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("DISCOUNT:").appendVar(paramQueryFilter.getParamMd5Key());
protected int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_15_MINUTE;
}
@Override
protected int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_30_MINUTE;
protected String cacheSceneKey() {
return "DISCOUNT";
}
@Override
... ...
... ... @@ -2,7 +2,6 @@ package com.yoho.search.service.scene.shopbrand;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.cache.CacheTimeConstants;
import com.yoho.search.cache.beans.AbstractCacheComponent;
import com.yoho.search.common.SearchRequestParams;
... ... @@ -53,8 +52,8 @@ public class AggBrandService extends AbstractCacheComponent<JSONArray> {
}
@Override
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("AGG_BRAND_LIST:").appendVar(paramQueryFilter.getParamMd5Key());
protected String cacheSceneKey() {
return "AGG_BRAND_LIST";
}
@Override
... ...
... ... @@ -2,10 +2,9 @@ package com.yoho.search.service.scene.shopbrand;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.cache.beans.AbstractCacheAbleService;
import com.yoho.search.cache.log.SearchCacheMatchLogger;
import com.yoho.search.cache.model.SearchCache;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.cache.CacheTimeConstants;
import com.yoho.search.cache.beans.AbstractCacheComponent;
import com.yoho.search.common.SearchDynamicConfigService;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.common.utils.SearchApiResultUtils;
... ... @@ -15,6 +14,8 @@ import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.aggregations.AggregationsService;
import com.yoho.search.service.aggregations.impls.AggregationFactory;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -24,70 +25,84 @@ import java.util.Arrays;
import java.util.Map;
@Service
public class RecommendBrandService extends AbstractCacheAbleService {
private static final Logger logger = LoggerFactory.getLogger(RecommendBrandService.class);
@Autowired
private AggregationsService aggregationsService;
@Autowired
private AggregationFactory aggregationFactory;
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private SearchDynamicConfigService searchDynamicConfigService;
public SearchCache getSearchCache() {
return searchCacheFactory.getRecommendCache();
}
public SearchApiResult recommendBrand(Map<String, String> paramMap) {
try {
logger.info("[func=aggRecommendBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 0、开关支持是否关闭个性化
if (!searchDynamicConfigService.isRecommendPersionalOpen()) {
paramMap.remove("uid");
}
// 1、获取核心参数
boolean needPreAggregation = true;
String aggWithParamBrand = paramMap.get(SearchRequestParams.PARAM_SEARCH_AGG_WITH_PARAM_BRAND);
if ("Y".equals(aggWithParamBrand)) {
needPreAggregation = false;
}
int recommendBrandCount = Integer.parseInt(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_VIEWNUM, "8"));
// 2、构建带queryBuilder和filter的SearchParam
SearchParam searchParam = searchParamHelper.buildSearchParam(paramMap, true, null, needPreAggregation ? "brand" : null);
// 3、构造聚合
IAggregation recommendBrandAgg = aggregationFactory.getRecommendBrandAggregation(paramMap, recommendBrandCount);
searchParam.setAggregationBuilders(Arrays.asList(recommendBrandAgg.getBuilder()));
// 4、构建offset
searchParam.setOffset(recommendBrandCount);// justForCache
// 5、从缓存中获取
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(searchCache, productIndexName, searchParam);
if (cacheJSONArray != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggRecommendBrand.json", paramMap);
return new SearchApiResult().setData(cacheJSONArray);
}
// 6、从ES中获取
JSONObject recommendBrandResult = aggregationsService.getAggNameAndResponse(recommendBrandAgg, searchParam);
if (recommendBrandResult == null) {
return new SearchApiResult().setData(500).setMessage("exception");
}
// 7、生成结果并且加入缓存
JSONArray brandJSONArray = recommendBrandResult.getJSONArray(recommendBrandAgg.aggName());
if (brandJSONArray != null) {
searchCacheService.addJSONArrayToCache(searchCache, productIndexName, searchParam, brandJSONArray);
}
return new SearchApiResult().setData(brandJSONArray);
} catch (Exception e) {
return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e);
}
}
public class RecommendBrandService extends AbstractCacheComponent<JSONArray> {
private static final Logger logger = LoggerFactory.getLogger(RecommendBrandService.class);
@Autowired
private AggregationsService aggregationsService;
@Autowired
private AggregationFactory aggregationFactory;
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private SearchDynamicConfigService searchDynamicConfigService;
public SearchApiResult recommendBrand(Map<String, String> paramMap) {
try {
logger.info("[func=aggRecommendBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1、开关支持是否关闭个性化
if (!searchDynamicConfigService.isRecommendPersionalOpen()) {
paramMap.remove("uid");
}
// 2、获取核心参数
boolean needPreAggregation = true;
String aggWithParamBrand = paramMap.get(SearchRequestParams.PARAM_SEARCH_AGG_WITH_PARAM_BRAND);
if ("Y".equals(aggWithParamBrand)) {
needPreAggregation = false;
}
// 3、构造paramQueryFilter
ParamQueryFilter paramQueryFilter = searchParamHelper.buildParamQueryFilter(paramMap, true, null, needPreAggregation ? "brand" : null);
// 4、执行查询
JSONArray brandJSONArray = super.queryWithCache(paramQueryFilter, paramMap);
// 5、返回结果
return new SearchApiResult().setData(brandJSONArray);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return SearchApiResultUtils.defaultErrorSearchApiResult(e);
}
}
@Override
protected JSONArray doRealQuery(ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) throws Exception {
// 1、构建带queryBuilder和filter的SearchParam
SearchParam searchParam = searchParamHelper.buildSearchParam(paramQueryFilter);
// 2、构造聚合
int recommendBrandCount = MapUtils.getIntValue(paramMap, SearchRequestParams.PARAM_SEARCH_VIEWNUM, 8);
IAggregation recommendBrandAgg = aggregationFactory.getRecommendBrandAggregation(paramMap, recommendBrandCount);
searchParam.setAggregationBuilders(Arrays.asList(recommendBrandAgg.getBuilder()));
// 3、查询es
JSONObject recommendBrandResult = aggregationsService.getAggNameAndResponse(recommendBrandAgg, searchParam);
if (recommendBrandResult == null) {
return new JSONArray();
}
// 4、生成结果
return recommendBrandResult.getJSONArray(recommendBrandAgg.aggName());
}
@Override
protected String cacheSceneKey() {
return "RECOMMEND_BRAND";
}
@Override
protected int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_15_MINUTE;
}
@Override
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter, Map<String,String> paramMap){
RedisKeyBuilder redisKeyBuilder = super.genRedisKeyBuilder(paramQueryFilter,paramMap);
int recommendBrandCount = MapUtils.getIntValue(paramMap, SearchRequestParams.PARAM_SEARCH_VIEWNUM, 8);
redisKeyBuilder.appendFixed(":").appendVar(recommendBrandCount);
return redisKeyBuilder;
}
}
... ...