Authored by zhaojun2

Merge branch 'cache_fix' into brandproduct

# Conflicts:
#	service/src/main/java/com/yoho/search/cache/beans/AbstractCacheComponent.java
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;
... ... @@ -8,57 +9,57 @@ import com.yoho.search.cache.impls.EhCache;
import com.yoho.search.cache.impls.SearchRedis;
import com.yoho.search.cache.model.CacheObject;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractPageComponent<T> {
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
public abstract class AbstractCacheComponent<T> {
@Autowired
private EhCache ehCache;
@Autowired
private SearchRedis searchRedis;
public T queryWithCache(ParamQueryFilter paramQueryFilter, Class<T> clazz) {
public T queryWithCache(ParamQueryFilter paramQueryFilter) {
//1、生成RedisKeyBuilder
RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(paramQueryFilter);
if (redisKeyBuilder == null) {
return null;
}
//2、取本地缓存,缓存命中,则直接返回
T result = this.getJavaObjectFromEhcache(redisKeyBuilder, clazz);
T result = this.getValueFromEhcache(redisKeyBuilder);
if (result != null) {
return result;
}
//3、取redis缓存,缓存命中,则回写ehcahce
result = this.getJavaObjectFromRedis(redisKeyBuilder, clazz);
if (result != null) {
this.addJavaObjectToEhcache(redisKeyBuilder, result);
result = this.getValueFromRedis(redisKeyBuilder);
if (result != null && useEhcache()) {
this.addValueToEhcache(redisKeyBuilder, result);
return result;
}
return this.doInnerQuery(redisKeyBuilder, paramQueryFilter);
}
public T queryFromCache(RedisKeyBuilder redisKeyBuilder) {
if (redisKeyBuilder == null) {
return null;
}
T result = this.getJavaObjectFromEhcache(redisKeyBuilder, null);
if (result == null) {
if ((result = this.getFromRedis(redisKeyBuilder, null)) != null) {
this.addJavaObjectToEhcache(redisKeyBuilder, result);
}
}
return result;
}
private T doInnerQuery(RedisKeyBuilder redisKeyBuilder, ParamQueryFilter paramQueryFilter) {
T result = this.doRealQuery(paramQueryFilter);
this.addJavaObjectToEhcache(redisKeyBuilder, result);
this.addJavaObjectToRedis(redisKeyBuilder, result);
if (result == null) {
return result;
}
if (useEhcache()) {
this.addValueToEhcache(redisKeyBuilder, result);
}
if (useRedis()) {
this.addValueToRedis(redisKeyBuilder, result);
}
return result;
}
private T getJavaObjectFromEhcache(RedisKeyBuilder redisKeyBuilder, Class<T> clazz) {
private T getValueFromEhcache(RedisKeyBuilder redisKeyBuilder) {
if(!useEhcache()){
return null;
}
CacheObject cacheObject = ehCache.get(redisKeyBuilder);
if (cacheObject != null) {
Object object = cacheObject.toObject();
... ... @@ -67,45 +68,42 @@ public abstract class AbstractPageComponent<T> {
return null;
}
protected void addJavaObjectToEhcache(RedisKeyBuilder redisKeyBuilder, T result) {
private void addValueToEhcache(RedisKeyBuilder redisKeyBuilder, T result) {
ehCache.addOrUpdate(redisKeyBuilder, new CacheObject(result), this.cacheTimeInMinute());
}
protected T getJavaObjectFromRedis(RedisKeyBuilder redisKeyBuilder, Class<T> clazz) {
protected T getValueFromRedis(RedisKeyBuilder redisKeyBuilder) {
if(!useRedis()){
return null;
}
CacheObject cacheObject = searchRedis.get(redisKeyBuilder);
if (cacheObject == null) {
return null;
}
JSONObject jsonObject = cacheObject.toJSONObject();
T result = JSON.toJavaObject(jsonObject, clazz);
return result;
String redisValue = (String)cacheObject.toObject();
Type superClass = getClass().getGenericSuperclass();
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
return JSON.parseObject(redisValue, type);
}
protected void addJavaObjectToRedis(RedisKeyBuilder redisKeyBuilder, T result) {
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(result));
CacheObject toCacheResult = new CacheObject(jsonObject);
protected void addValueToRedis(RedisKeyBuilder redisKeyBuilder, T result) {
String jsonString = JSON.toJSONString(result);
CacheObject toCacheResult = new CacheObject(jsonString);
searchRedis.addOrUpdate(redisKeyBuilder, toCacheResult, this.cacheTimeInMinute());
}
//TODO
protected T getFromRedis(RedisKeyBuilder redisKeyBuilder, Class<T> tClass) {
String result = searchRedis.getString(redisKeyBuilder);
if (tClass != null) {
return StringUtils.isBlank(result) ? null : JSON.parseObject(result, tClass);
}
// Type superClass = getClass().getGenericSuperclass();
// Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
return StringUtils.isBlank(result) ? null : JSON.parseObject(result, new TypeReference<T>(){});
}
protected void addToRedis(RedisKeyBuilder redisKeyBuilder, String result) {
searchRedis.addOrUpdate(redisKeyBuilder, result, this.cacheTimeInMinute());
}
protected abstract RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter);
protected abstract int cacheTimeInMinute();
protected abstract T doRealQuery(ParamQueryFilter paramQueryFilter);
protected boolean useEhcache() {
return false;
}
protected boolean useRedis() {
return true;
}
}
... ...
... ... @@ -3,7 +3,7 @@ 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.beans.AbstractPageComponent;
import com.yoho.search.cache.beans.AbstractCacheComponent;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.core.personalized.models.SortBrand;
... ... @@ -25,7 +25,7 @@ import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class PagePersionalFactorComponent extends AbstractPageComponent<PagePersonalFactor> {
public class CachePersionalFactorComponent extends AbstractCacheComponent<PagePersonalFactor> {
@Autowired
private SearchCommonService searchCommonService;
... ... @@ -37,13 +37,13 @@ public class PagePersionalFactorComponent extends AbstractPageComponent<PagePers
* @return
*/
public PagePersonalFactor queryPagePersionalFactor(ParamQueryFilter paramQueryFilter) {
Object value = super.queryWithCache(paramQueryFilter, PagePersonalFactor.class);
Object value = super.queryWithCache(paramQueryFilter);
return value == null ? null : (PagePersonalFactor) value;
}
@Override
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("PAGE_FACTORS:").appendVar(paramQueryFilter.getParamMd5Key());
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("PAGE_FACTORS_NEW:").appendVar(paramQueryFilter.getParamMd5Key());
}
@Override
... ...
... ... @@ -24,7 +24,7 @@ public class QueryUserPersionalFactorBean {
private static final Logger RECALL_NEW_LOGGER = LoggerFactory.getLogger("RECALL");
@Autowired
private PagePersionalFactorComponent pageComponent;
private CachePersionalFactorComponent pageComponent;
@Autowired
private UserPersionalFactorComponent userComponent;
@Autowired
... ...
... ... @@ -4,7 +4,7 @@ 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;
import com.yoho.search.cache.beans.AbstractPageComponent;
import com.yoho.search.cache.beans.AbstractCacheComponent;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.service.recall.beans.builder.UserRecallRequestBuilder;
... ... @@ -25,7 +25,7 @@ import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class ActivityShopBrandListService extends AbstractPageComponent<ActivityShopBrandList> {
public class ActivityShopBrandListService extends AbstractCacheComponent<List<ActivityShopBrand>> {
private static final Logger logger = LoggerFactory.getLogger(ActivityShopBrandListService.class);
... ... @@ -43,11 +43,11 @@ public class ActivityShopBrandListService extends AbstractPageComponent<Activity
public List<ActivityShopBrand> queryActivityShopBrandList(Map<String, String> paramMap) {
try {
ParamQueryFilter paramQueryFilter = userRecallRequestBuilder.buildParamQueryFilter(paramMap);
Object value = super.queryWithCache(paramQueryFilter, ActivityShopBrandList.class);
List<ActivityShopBrand> value = super.queryWithCache(paramQueryFilter);
if(value==null){
return new ArrayList<>();
}
return new ArrayList<>(((ActivityShopBrandList) value).getShopBrandList());
return value;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new ArrayList<>();
... ... @@ -56,7 +56,7 @@ public class ActivityShopBrandListService extends AbstractPageComponent<Activity
@Override
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("ACTIVITY_SHOP_BRAND_LIST:").appendVar(paramQueryFilter.getParamMd5Key());
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("ACTIVITY_SHOP_BRANDS:").appendVar(paramQueryFilter.getParamMd5Key());
}
@Override
... ... @@ -65,7 +65,7 @@ public class ActivityShopBrandListService extends AbstractPageComponent<Activity
}
@Override
protected ActivityShopBrandList doRealQuery(ParamQueryFilter paramQueryFilter) {
protected List<ActivityShopBrand> doRealQuery(ParamQueryFilter paramQueryFilter) {
//1、构造参数
SearchParam searchParam = new SearchParam();
searchParam.setQuery(paramQueryFilter.getParamQuery());
... ... @@ -95,7 +95,7 @@ public class ActivityShopBrandListService extends AbstractPageComponent<Activity
return middleSortAggBuilder;
}
private ActivityShopBrandList genActivityBrandShops(Map<String, Aggregation> aggregationMap) {
private List<ActivityShopBrand> genActivityBrandShops(Map<String, Aggregation> aggregationMap) {
//1、获取二层聚合的结果
List<AggKeyCountTwoLevel> aggKeyCountTwoLevelList = AggCommonHelper.getAggKeyCountTwoLevelList(aggregationMap, "shopAgg", "brandAgg");
//2、使用第一层聚合的总数量排序
... ... @@ -124,7 +124,7 @@ public class ActivityShopBrandListService extends AbstractPageComponent<Activity
break;
}
}
return new ActivityShopBrandList(shopBrandList);
return shopBrandList;
}
}
... ...
... ... @@ -6,7 +6,7 @@ import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.cache.CacheTimeConstants;
import com.yoho.search.cache.beans.AbstractPageComponent;
import com.yoho.search.cache.beans.AbstractCacheComponent;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.core.es.agg.IAggregation;
import com.yoho.search.core.es.model.SearchParam;
... ... @@ -28,7 +28,7 @@ import java.util.Arrays;
import java.util.Map;
@Service
public class DiscountService extends AbstractPageComponent<JSONObject> implements ApplicationEventPublisherAware {
public class DiscountService extends AbstractCacheComponent<JSONObject> implements ApplicationEventPublisherAware {
private static final Logger logger = LoggerFactory.getLogger(DiscountService.class);
... ... @@ -55,7 +55,7 @@ public class DiscountService extends AbstractPageComponent<JSONObject> implement
public JSONObject discount(Map<String, String> paramMap, BoolQueryBuilder mustFilter) {
try {
ParamQueryFilter paramQueryFilter = searchParamHelper.buildParamQueryFilter(paramMap, mustFilter);
Object value = super.queryWithCache(paramQueryFilter, JSONObject.class);
Object value = super.queryWithCache(paramQueryFilter);
return value == null ? null : (JSONObject) value;
} catch (Exception e) {
logger.error(e.getMessage(), e);
... ...