Authored by hugufei

Merge branch 'cache_fix' into brandproduct

... ... @@ -32,7 +32,7 @@ public abstract class AbstractCacheComponent<T> {
}
//3、取redis缓存,缓存命中,则回写ehcahce
result = this.getValueFromRedis(redisKeyBuilder);
if (result != null && useEhcache()) {
if (result != null) {
this.addValueToEhcache(redisKeyBuilder, result);
return result;
}
... ... @@ -44,12 +44,8 @@ public abstract class AbstractCacheComponent<T> {
if (result == null) {
return result;
}
if (useEhcache()) {
this.addValueToEhcache(redisKeyBuilder, result);
}
if (useRedis()) {
this.addValueToRedis(redisKeyBuilder, result);
}
this.addValueToEhcache(redisKeyBuilder, result);
this.addValueToRedis(redisKeyBuilder, result);
return result;
}
... ... @@ -68,15 +64,30 @@ public abstract class AbstractCacheComponent<T> {
}
private void addValueToEhcache(RedisKeyBuilder redisKeyBuilder, T result) {
if(!useEhcache()){
return;
}
ehCache.addOrUpdate(redisKeyBuilder, new CacheObject(result), this.cacheTimeInMinute());
}
private void addValueToRedis(RedisKeyBuilder redisKeyBuilder, T result) {
if(!useRedis()){
return;
}
String jsonString = JSON.toJSONString(result);
CacheObject toCacheResult = new CacheObject(jsonString);
searchRedis.addOrUpdate(redisKeyBuilder, toCacheResult, this.cacheTimeInMinute());
}
private T getValueFromRedis(RedisKeyBuilder redisKeyBuilder) {
if (!useRedis()) {
return null;
}
CacheObject cacheObject = searchRedis.get(redisKeyBuilder);
if (cacheObject == null) {
if (cacheObject == null || cacheObject.getValue()==null) {
return null;
}
if(! (cacheObject.getValue() instanceof String)){
return null;
}
String redisValue = (String) cacheObject.toObject();
... ... @@ -85,12 +96,6 @@ public abstract class AbstractCacheComponent<T> {
return JSON.parseObject(redisValue, type);
}
private void addValueToRedis(RedisKeyBuilder redisKeyBuilder, T result) {
String jsonString = JSON.toJSONString(result);
CacheObject toCacheResult = new CacheObject(jsonString);
searchRedis.addOrUpdate(redisKeyBuilder, toCacheResult, this.cacheTimeInMinute());
}
protected abstract RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter);
protected abstract int cacheTimeInMinute();
... ...
... ... @@ -5,6 +5,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.yoho.search.aop.downgrade.PersionalRateLimit;
import com.yoho.search.service.scene.shopbrand.AggBrandService;
import com.yoho.search.service.scene.shopbrand.BrandProductListService;
import com.yoho.search.service.scene.shopbrand.RecommendBrandService;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -25,6 +26,8 @@ public class BrandController {
@Autowired
private BrandListService brandListService;
@Autowired
private AggBrandService aggBrandService;
@Autowired
private BrandProductListService brandProductListService;
@Autowired
private RecommendBrandService recommendBrandService;
... ... @@ -113,7 +116,7 @@ public class BrandController {
@ResponseBody
public SearchApiResult aggBrand(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return brandListService.aggAllBrandList(paramMap);
return aggBrandService.aggAllBrandList(paramMap);
}
/**
... ...
... ... @@ -8,6 +8,7 @@ import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.models.RecommendPromotionAggVO;
import com.yoho.search.service.scene.pages.selections.PageAggregationHelper;
import com.yoho.search.service.scene.pages.selections.PageSelectionsBrandsService;
import com.yoho.search.service.scene.shopbrand.AggBrandService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
... ... @@ -69,6 +70,8 @@ public class PromotionAggregationsService extends AbstractCacheAbleService {
private PageSelectionsBrandsService scenePageSelectionsBrandsService;
@Autowired
private PageAggregationHelper pageAggregationHelper;
@Autowired
private AggBrandService aggBrandService;
@Override
public SearchCache getSearchCache() {
... ... @@ -180,7 +183,7 @@ public class PromotionAggregationsService extends AbstractCacheAbleService {
// 1、设置必须满足的过滤条件
BoolQueryBuilder mustFilterByPromotion = promotionSceneHelper.getMustFilterByPromotion(promotionConditions);
// 2、调聚合品牌的方法
SearchApiResult searchApiResult = brandListService.aggAllBrandList(paramMap, mustFilterByPromotion);
SearchApiResult searchApiResult = aggBrandService.aggAllBrandList(paramMap, mustFilterByPromotion);
return searchApiResult;
}
... ...
... ... @@ -24,8 +24,8 @@ import java.util.Arrays;
import java.util.Map;
@Service
public class AggBrandListService extends AbstractCacheComponent<JSONArray> {
private static final Logger logger = LoggerFactory.getLogger(AggBrandListService.class);
public class AggBrandService extends AbstractCacheComponent<JSONArray> {
private static final Logger logger = LoggerFactory.getLogger(AggBrandService.class);
@Autowired
private SearchParamHelper searchParamHelper;
... ...
... ... @@ -44,25 +44,16 @@ import java.util.*;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
@Service
public class BrandListService extends AbstractCacheAbleService implements ApplicationEventPublisherAware {
public class BrandListService implements ApplicationEventPublisherAware {
private static final Logger logger = LoggerFactory.getLogger(BrandListService.class);
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private AggregationsService aggregationsService;
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
@Autowired
private AggregationFactory aggregationFactory;
@Override
public SearchCache getSearchCache() {
return searchCacheFactory.getBrandRelatedCache();
}
private ApplicationEventPublisher publisher;
... ... @@ -71,48 +62,6 @@ public class BrandListService extends AbstractCacheAbleService implements Applic
this.publisher = applicationEventPublisher;
}
public SearchApiResult aggAllBrandList(Map<String, String> paramMap) {
try {
logger.info("[func=aggBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
return this.aggAllBrandList(paramMap, null);
} catch (Exception e) {
return SearchApiResultUtils.errorSearchApiResult("aggBrand", paramMap, e);
}
}
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;
SearchParam searchParam = searchParamHelper.buildSearchParam(paramMap, false, mustFilter, needPreAggregation ? "brand" : null);
// 2、构造aggrations
IAggregation brandAggregation = aggregationFactory.getBrandAggregation(paramMap);
searchParam.setAggregationBuilders(Arrays.asList(brandAggregation.getBuilder()));
// 3、从缓存中获取
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(this.searchCache, indexName, searchParam);
if (cacheJSONArray != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggBrand.json", paramMap);
return new SearchApiResult().setData(cacheJSONArray);
}
// 4、从ES中获取
JSONObject jsonObject = aggregationsService.getAggNameAndResponse(brandAggregation, searchParam);
if (jsonObject == null) {
return new SearchApiResult().setData(500).setMessage("exception");
}
// 5、生成结果并且加入缓存
JSONArray brandJSONArray = jsonObject.getJSONArray(brandAggregation.aggName());
if (brandJSONArray != null) {
searchCacheService.addJSONArrayToCache(this.searchCache, indexName, searchParam, brandJSONArray);
}
return new SearchApiResult().setData(brandJSONArray);
} catch (Exception e) {
return SearchApiResultUtils.errorSearchApiResult("aggBrand", paramMap, e);
}
}
@SearchCacheAble(cacheInMinute = 30, cacheName = "BRAND_LIST", excludeParams = {"page", "order", "uid", "udid"})
public SearchApiResult brandList(Map<String, String> paramMap) {
try {
... ...