Authored by hugufei

fix AbstractCacheComponent

package com.yoho.search.cache.beans;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.cache.impls.EhCache;
import com.yoho.search.cache.impls.SearchRedis;
... ... @@ -13,7 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
public abstract class AbstractCacheComponent<T> {
... ... @@ -23,7 +19,7 @@ public abstract class AbstractCacheComponent<T> {
@Autowired
private SearchRedis searchRedis;
public T queryWithCache(ParamQueryFilter paramQueryFilter,Map<String,String> paramMap) {
public T queryWithCache(ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) throws Exception {
//1、生成RedisKeyBuilder
RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(paramQueryFilter);
if (redisKeyBuilder == null) {
... ... @@ -40,11 +36,11 @@ public abstract class AbstractCacheComponent<T> {
this.addValueToEhcache(redisKeyBuilder, result);
return result;
}
return this.doInnerQuery(redisKeyBuilder, paramQueryFilter,paramMap);
return this.doInnerQuery(redisKeyBuilder, paramQueryFilter, paramMap);
}
private T doInnerQuery(RedisKeyBuilder redisKeyBuilder, ParamQueryFilter paramQueryFilter,Map<String,String> paramMap) {
T result = this.doRealQuery(paramQueryFilter,paramMap);
private T doInnerQuery(RedisKeyBuilder redisKeyBuilder, ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) throws Exception {
T result = this.doRealQuery(paramQueryFilter, paramMap);
if (result == null) {
return result;
}
... ... @@ -57,8 +53,10 @@ public abstract class AbstractCacheComponent<T> {
return result;
}
private T getValueFromEhcache(RedisKeyBuilder redisKeyBuilder) {
if(!useEhcache()){
protected abstract T doRealQuery(ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) throws Exception;
private T getValueFromEhcache(RedisKeyBuilder redisKeyBuilder) throws Exception {
if (!useEhcache()) {
return null;
}
CacheObject cacheObject = ehCache.get(redisKeyBuilder);
... ... @@ -74,14 +72,14 @@ public abstract class AbstractCacheComponent<T> {
}
private T getValueFromRedis(RedisKeyBuilder redisKeyBuilder) {
if(!useRedis()){
if (!useRedis()) {
return null;
}
CacheObject cacheObject = searchRedis.get(redisKeyBuilder);
if (cacheObject == null) {
return null;
}
String redisValue = (String)cacheObject.toObject();
String redisValue = (String) cacheObject.toObject();
Type superClass = getClass().getGenericSuperclass();
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
return JSON.parseObject(redisValue, type);
... ... @@ -97,8 +95,6 @@ public abstract class AbstractCacheComponent<T> {
protected abstract int cacheTimeInMinute();
protected abstract T doRealQuery(ParamQueryFilter paramQueryFilter,Map<String,String> paramMap);
protected boolean useEhcache() {
return false;
}
... ...
... ... @@ -62,6 +62,10 @@ public class SearchParamHelper {
return buildParamQueryFilter(paramMap,false,mustFilter,null);
}
public ParamQueryFilter buildParamQueryFilter(Map<String, String> paramMap,BoolQueryBuilder mustFilter,String filterParamName) throws Exception {
return buildParamQueryFilter(paramMap,false,mustFilter,filterParamName);
}
public ParamQueryFilter buildParamQueryFilter(Map<String, String> paramMap, boolean needPersional, BoolQueryBuilder mustFilter,String filterParamName) throws Exception {
// 1、构造filter
BoolQueryBuilder filter = searchQueryHelper.constructFilterBuilder(paramMap, filterParamName);
... ...
... ... @@ -19,6 +19,8 @@ import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
... ... @@ -27,6 +29,8 @@ import java.util.*;
@Component
public class CachePersionalFactorComponent extends AbstractCacheComponent<PagePersonalFactor> {
private static final Logger logger = LoggerFactory.getLogger(CachePersionalFactorComponent.class);
@Autowired
private SearchCommonService searchCommonService;
... ... @@ -37,7 +41,12 @@ public class CachePersionalFactorComponent extends AbstractCacheComponent<PagePe
* @return
*/
public PagePersonalFactor queryPagePersionalFactor(ParamQueryFilter paramQueryFilter) {
return super.queryWithCache(paramQueryFilter,null);
try {
return super.queryWithCache(paramQueryFilter, null);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new PagePersonalFactor();
}
}
@Override
... ... @@ -56,7 +65,7 @@ public class CachePersionalFactorComponent extends AbstractCacheComponent<PagePe
}
@Override
protected PagePersonalFactor doRealQuery(ParamQueryFilter paramQueryFilter,Map<String,String> paramMap){
protected PagePersonalFactor doRealQuery(ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) throws Exception {
//1、构造参数
SearchParam searchParam = new SearchParam();
searchParam.setQuery(paramQueryFilter.getParamQuery());
... ...
... ... @@ -3,6 +3,7 @@ package com.yoho.search.service.recall.models.personal;
import com.yoho.search.core.personalized.models.SortBrand;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
... ... @@ -17,6 +18,9 @@ public class PagePersonalFactor implements Serializable {
private List<SortBrand> sortBrandList;
public PagePersonalFactor() {
this.misortIds = new ArrayList<>();
this.brandIds = new ArrayList<>();
this.sortBrandList = new ArrayList<>();
}
public PagePersonalFactor(List<Integer> misortIds, List<Integer> brandIds, List<SortBrand> sortBrandList) {
... ...
... ... @@ -65,7 +65,7 @@ public class ActivityShopBrandListService extends AbstractCacheComponent<List<Ac
}
@Override
protected List<ActivityShopBrand> doRealQuery(ParamQueryFilter paramQueryFilter,Map<String, String> paramMap) {
protected List<ActivityShopBrand> doRealQuery(ParamQueryFilter paramQueryFilter,Map<String, String> paramMap) throws Exception {
//1、构造参数
SearchParam searchParam = new SearchParam();
searchParam.setQuery(paramQueryFilter.getParamQuery());
... ...
... ... @@ -75,7 +75,7 @@ public class DiscountService extends AbstractCacheComponent<JSONObject> implemen
}
@Override
protected JSONObject doRealQuery(ParamQueryFilter paramQueryFilter,Map<String, String> paramMap) {
protected JSONObject doRealQuery(ParamQueryFilter paramQueryFilter,Map<String, String> paramMap) throws Exception{
// 构造查询参数
SearchParam searchParam = searchParamHelper.buildSearchParam(paramQueryFilter);
... ...
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;
import com.yoho.search.common.utils.SearchApiResultUtils;
import com.yoho.search.core.es.agg.IAggregation;
import com.yoho.search.core.es.model.SearchParam;
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.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Map;
@Service
public class AggBrandListService extends AbstractCacheComponent<JSONArray> {
private static final Logger logger = LoggerFactory.getLogger(AggBrandListService.class);
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private AggregationFactory aggregationFactory;
@Autowired
private AggregationsService aggregationsService;
public SearchApiResult aggAllBrandList(Map<String, String> paramMap) {
return this.aggAllBrandList(paramMap, null);
}
public SearchApiResult aggAllBrandList(Map<String, String> paramMap, BoolQueryBuilder mustFilter) {
try {
// 1、构造带filter和query的SearchParam
boolean needPreAggregation = "Y".equals(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_AGG_WITH_PARAM_BRAND, "N")) ? false : true;
String filterParamName = needPreAggregation ? "brand" : null;
ParamQueryFilter paramFilter = searchParamHelper.buildParamQueryFilter(paramMap, mustFilter, filterParamName);
JSONArray brandJSONArray = super.queryWithCache(paramFilter, paramMap);
return new SearchApiResult().setData(brandJSONArray);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return SearchApiResultUtils.defaultErrorSearchApiResult(e);
}
}
@Override
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("AGG_BRAND_LIST:").appendVar(paramQueryFilter.getParamMd5Key());
}
@Override
protected int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_30_MINUTE;
}
@Override
protected JSONArray doRealQuery(ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) throws Exception {
// 1、构造带filter和query的SearchParam
SearchParam searchParam = new SearchParam();
searchParam.setQuery(paramQueryFilter.getParamQuery());
searchParam.setFiter(paramQueryFilter.getParamFilter());
// 2、构造aggrations
IAggregation brandAggregation = aggregationFactory.getBrandAggregation(paramMap);
searchParam.setAggregationBuilders(Arrays.asList(brandAggregation.getBuilder()));
// 3、从ES中获取
JSONObject jsonObject = aggregationsService.getAggNameAndResponse(brandAggregation, searchParam);
// 4、生成结果
JSONArray brandJSONArray = jsonObject.getJSONArray(brandAggregation.aggName());
return brandJSONArray;
}
}
... ...