Authored by hugufei

拆包

... ... @@ -7,6 +7,8 @@ import com.yoho.search.recall.scene.builder.response.RecallResponseBatchBuilder;
import com.yoho.search.recall.scene.builder.response.RecallSknResultBuilder;
import com.yoho.search.recall.scene.component.BatchRecallComponent;
import com.yoho.search.recall.scene.models.*;
import com.yoho.search.recall.scene.models.RecallResponseBatch;
import com.yoho.search.recall.scene.models.cache.CacheRecallRequestResponse;
import com.yoho.search.recall.scene.persional.PersionalFactor;
import com.yoho.search.recall.scene.persional.RecallPersionalService;
import org.springframework.beans.factory.annotation.Autowired;
... ...
package com.yoho.search.recall.scene.builder.response;
import com.yoho.search.recall.scene.models.RecallRequest;
import com.yoho.search.recall.scene.models.CacheRecallRequestResponse;
import com.yoho.search.recall.scene.models.cache.CacheRecallRequestResponse;
import com.yoho.search.recall.scene.models.RecallResponse;
import com.yoho.search.recall.scene.models.RecallResponseBatch;
import com.yoho.search.recall.scene.strategy.StrategyNameEnum;
... ...
... ... @@ -6,9 +6,8 @@ import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.recall.scene.cache.CacheRequestResponseComponent;
import com.yoho.search.recall.scene.constants.CacheTimeConstants;
import com.yoho.search.recall.scene.models.CacheSknInfoRequestResponse;
import com.yoho.search.recall.scene.models.cache.CacheSknInfoRequestResponse;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import org.apache.commons.collections.MapUtils;
... ...
... ... @@ -5,9 +5,8 @@ import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.recall.scene.cache.CacheRequestResponseComponent;
import com.yoho.search.recall.scene.models.RecallRequest;
import com.yoho.search.recall.scene.models.CacheRecallRequestResponse;
import com.yoho.search.recall.scene.models.cache.CacheRecallRequestResponse;
import com.yoho.search.recall.scene.models.RecallResponse;
import com.yoho.search.service.base.SearchCommonService;
import org.apache.commons.collections.MapUtils;
... ...
package com.yoho.search.recall.scene.component;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.common.cache.impls.EhCache;
import com.yoho.search.core.redis.components.YohoSearchRedisComponent;
import com.yoho.search.recall.scene.models.cache.CacheRequestResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class CacheRequestResponseComponent {
private static final Logger logger = LoggerFactory.getLogger(CacheRequestResponseComponent.class);
@Autowired
private YohoSearchRedisComponent yohoSearchRedisComponent;
@Autowired
private EhCache ehCache;
/**
* 从缓存中获取response
* @param cacheRequestResponses
* @param useEhCache
* @param <K>
* @param <V>
* @param <T>
*/
public <K, V, T extends CacheRequestResponse<K, V>> void batchFillResponseFromCache(final List<T> cacheRequestResponses, boolean useEhCache) {
try {
Collection<RedisKeyBuilder> keys = new ArrayList<>();
for (T requestResponse : cacheRequestResponses) {
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequestRedisKeyBuilder();
keys.add(redisKeyBuilder);
}
List<String> cachedValues = this.mutiGetFromCache(keys, useEhCache);
for (int i = 0; i < cacheRequestResponses.size(); i++) {
T requestResponse = cacheRequestResponses.get(i);
String cachedValue = cachedValues.get(i);
if (!StringUtils.isBlank(cachedValue)) {
V response = requestResponse.getToResponseTransfer().transfer(cachedValue);
requestResponse.setResponse(response, false);
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
/**
* 过滤出未命中缓存的结果
* @param cacheRequestResponses
* @param <K>
* @param <V>
* @param <T>
* @return
*/
public <K, V, T extends CacheRequestResponse<K, V>> List<T> filterMissCacheRequests(List<T> cacheRequestResponses){
List<T> notCachedRequests = new ArrayList<>();
for (T requestResponse : cacheRequestResponses) {
if (requestResponse != null && requestResponse.getResponse()==null) {
notCachedRequests.add(requestResponse);
}
}
return notCachedRequests;
}
/**
* 使用查询结果填充response
* @param cacheRequestResponses
* @param queryResults
* @param <K>
* @param <V>
* @param <T>
*/
public <K, V, T extends CacheRequestResponse<K, V>> void batchFillResponseWithQueryResults(List<T> cacheRequestResponses,Map<RedisKeyBuilder,V> queryResults){
for (T sknInfoRequestResponse : cacheRequestResponses) {
if(sknInfoRequestResponse.getResponse()!=null){
continue;
}
RedisKeyBuilder redisKeyBuilder = sknInfoRequestResponse.getRequestRedisKeyBuilder();
V response = queryResults.get(redisKeyBuilder);
if(response!=null){
sknInfoRequestResponse.setResponse(response,true);
}
}
}
/**
* 将未缓存的response添加至缓存
* @param cacheRequestResponses
* @param useEhCache
* @param <K>
* @param <V>
* @param <T>
*/
public <K, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache) {
//1、按缓存时间分组
Map<Integer, List<T>> groupMap = CollectionUtils.toListMap(cacheRequestResponses, new Transfer<T, Integer>() {
@Override
public Integer transfer(T t) {
return t.getCacheTimeInSecond();
}
});
//2、按缓存时间大小直接加入缓存
for (Map.Entry<Integer, List<T>> entry : groupMap.entrySet()) {
this.batchAddResponseToCache(entry.getValue(), useEhCache, entry.getKey());
}
}
private <K, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache, int timeOutInSecond) {
try {
Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>();
for (T requestResponse : cacheRequestResponses) {
if (!requestResponse.isNeedRecache()){
continue;
}
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequestRedisKeyBuilder();
V response = requestResponse.getResponse();
if(redisKeyBuilder==null || response==null){
continue;
}
String cacheValue = requestResponse.getFromResponseTransfer().transfer(response);
toCacheMap.put(redisKeyBuilder, cacheValue);
}
if (toCacheMap.isEmpty()) {
return;
}
this.batchAddToCache(toCacheMap, useEhCache, timeOutInSecond);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
private List<String> mutiGetFromCache(Collection<RedisKeyBuilder> keys, boolean useEhCache) {
if (useEhCache) {
return ehCache.mutiGet(keys);
} else {
return yohoSearchRedisComponent.getValueOperations().multiGet(keys);
}
}
private void batchAddToCache(Map<RedisKeyBuilder, String> toCacheMap, boolean useEhCache, int timeOutInSecond) {
if (useEhCache) {
ehCache.mutiSet(toCacheMap, timeOutInSecond);
} else {
yohoSearchRedisComponent.getRedisTemplate().mset(toCacheMap, timeOutInSecond);
}
}
}
... ...
... ... @@ -16,4 +16,6 @@ public class CacheTimeConstants {
public static final int SKN_INFO = 15 * 60;//SKN的缓存
public static final int USER_RECALL_SKN_LIST = 3 * 60;//召回结果的缓存
}
... ...
... ... @@ -3,20 +3,20 @@ package com.yoho.search.recall.scene.models;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.recall.scene.models.interfaces.ICacheRequest;
import com.yoho.search.recall.scene.models.interfaces.IRecallRequest;
import com.yoho.search.recall.scene.strategy.IStrategy;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import java.util.Arrays;
import java.util.List;
/**
* 召回请求
*/
public class RecallRequest implements IRecallRequest {
public class RecallRequest implements ICacheRequest,IRecallRequest {
private ParamQueryFilter paramQueryFilter;
private IStrategy strategy;
... ... @@ -37,11 +37,6 @@ public class RecallRequest implements IRecallRequest {
}
@Override
public String requestType() {
return this.strategy.nameEnum().name();
}
@Override
public RedisKeyBuilder redisKeyBuilder() {
return this.redisKeyBuilder;
}
... ... @@ -52,6 +47,11 @@ public class RecallRequest implements IRecallRequest {
}
@Override
public String requestType() {
return this.strategy.nameEnum().name();
}
@Override
public SearchParam searchParam() {
SearchParam searchParam = new SearchParam();
searchParam.setQuery(this.paramQueryFilter.getParamQuery());
... ...
package com.yoho.search.recall.scene.models;
import com.yoho.search.recall.scene.models.RecallResponse;
import java.util.List;
public class RecallResponseBatch {
... ...
package com.yoho.search.recall.scene.models;
import com.alibaba.fastjson.JSON;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.recall.scene.constants.CacheTimeConstants;
import com.yoho.search.recall.scene.models.interfaces.ICacheRequest;
import java.util.List;
public class RecallSknParams {
public class RecallSknParams implements ICacheRequest {
private final ParamQueryFilter paramQueryFilter;
private final int pageSize;
private final List<String> firstProductSkns;
private final int uid;
private final String udid;
private RedisKeyBuilder redisKeyBuilder;
public RecallSknParams(ParamQueryFilter paramQueryFilter, int pageSize, List<String> firstProductSkns, int uid, String udid) {
this.paramQueryFilter = paramQueryFilter;
... ... @@ -16,6 +23,28 @@ public class RecallSknParams {
this.pageSize = pageSize;
this.uid = uid;
this.udid = udid;
this.redisKeyBuilder = genRedisKeyBuilder();
}
@Override
public RedisKeyBuilder redisKeyBuilder() {
return this.redisKeyBuilder;
}
private RedisKeyBuilder genRedisKeyBuilder(){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(paramQueryFilter==null?"":paramQueryFilter.getParamMd5Key());
stringBuilder.append(pageSize);
stringBuilder.append(firstProductSkns==null?"": JSON.toJSON(firstProductSkns));
stringBuilder.append(uid);
stringBuilder.append(udid==null?"": udid);
String value = MD5Util.string2MD5(stringBuilder.toString());
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("USER_RECALL_SKN_LIST:").appendVar(value);
}
@Override
public int cacheTimeInSecond() {
return CacheTimeConstants.USER_RECALL_SKN_LIST;
}
public ParamQueryFilter getParamQueryFilter() {
... ...
package com.yoho.search.recall.scene.models.cache;
import com.alibaba.fastjson.JSON;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.recall.scene.models.cache.CacheRequestResponse;
import com.yoho.search.recall.scene.models.RecallRequest;
import com.yoho.search.recall.scene.models.RecallResponse;
public class CacheRecallRequestResponse extends CacheRequestResponse<RecallRequest,RecallResponse> {
static Transfer<RecallRequest,RedisKeyBuilder> requestRedisKeyBuilderTransfer = new Transfer<RecallRequest, RedisKeyBuilder>() {
@Override
public RedisKeyBuilder transfer(RecallRequest recallRequest) {
return recallRequest.redisKeyBuilder();
}
};
static Transfer<String,RecallResponse> toResponseTransfer = new Transfer<String, RecallResponse>() {
@Override
public RecallResponse transfer(String jsonValue) {
return JSON.parseObject(jsonValue, RecallResponse.class);
}
};
static Transfer<RecallResponse,String> fromResponseTransfer = new Transfer<RecallResponse, String>() {
@Override
public String transfer(RecallResponse recallResponse) {
return JSON.toJSONString(recallResponse);
}
};
public CacheRecallRequestResponse(RecallRequest request) {
super(request,requestRedisKeyBuilderTransfer,request.cacheTimeInSecond(), toResponseTransfer,fromResponseTransfer);
}
}
... ...
package com.yoho.search.recall.scene.models.cache;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.Transfer;
public class CacheRequestResponse<K,V> {
private K request;
private RedisKeyBuilder redisKeyBuilder;
private int cacheTimeInSecond;
private V response;
private Transfer<String,V> toResponseTransfer;
private Transfer<V,String> fromResponseTransfer;
private boolean needRecache = false;
public CacheRequestResponse(K request,Transfer<K,RedisKeyBuilder> requestRedisKeyBuilder,int cacheTimeInSecond,Transfer<String,V> toResponseTransfer,Transfer<V,String> fromResponseTransfer){
this.request = request;
this.redisKeyBuilder = requestRedisKeyBuilder.transfer(request);
this.cacheTimeInSecond = cacheTimeInSecond;
this.toResponseTransfer = toResponseTransfer;
this.fromResponseTransfer = fromResponseTransfer;
}
public K getRequest() {
return request;
}
public V getResponse() {
return response;
}
public RedisKeyBuilder getRequestRedisKeyBuilder() {
return redisKeyBuilder;
}
public Transfer<String, V> getToResponseTransfer() {
return toResponseTransfer;
}
public Transfer<V, String> getFromResponseTransfer() {
return fromResponseTransfer;
}
public void setResponse(V response,boolean needRecache) {
this.response = response;
this.needRecache = needRecache;
}
public boolean isNeedRecache() {
return needRecache;
}
public int getCacheTimeInSecond() {
return cacheTimeInSecond;
}
}
... ...
package com.yoho.search.recall.scene.models.cache;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.recall.scene.models.cache.CacheRequestResponse;
import java.util.HashMap;
import java.util.Map;
public class CacheSknInfoRequestResponse extends CacheRequestResponse<Integer, Map<String, Object>> {
static Transfer<Integer, RedisKeyBuilder> requestRedisKeyBuilder = new Transfer<Integer, RedisKeyBuilder>() {
@Override
public RedisKeyBuilder transfer(Integer productSkn) {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN:").appendVar(productSkn);
}
};
static Transfer<String, Map<String, Object>> toResponseTransfer = new Transfer<String, Map<String, Object>>() {
@Override
public Map<String, Object> transfer(String value) {
Map<String, Object> product = new HashMap<>();
product.putAll(JSONObject.parseObject(value));
return product;
}
};
static Transfer<Map<String, Object>, String> fromResponseTransfer = new Transfer<Map<String, Object>, String>() {
@Override
public String transfer(Map<String, Object> product) {
return JSON.toJSONString(product);
}
};
public CacheSknInfoRequestResponse(Integer productSkn, int cacheTimeInSecond) {
super(productSkn, requestRedisKeyBuilder, cacheTimeInSecond , toResponseTransfer,fromResponseTransfer);
}
}
... ...
package com.yoho.search.recall.scene.models.interfaces;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
public interface ICacheRequest {
/**
* 缓存key
* @return
*/
RedisKeyBuilder redisKeyBuilder();
/**
* 缓存时间
* @return
*/
int cacheTimeInSecond();
}
... ...
package com.yoho.search.recall.scene.models.interfaces;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.model.SearchParam;
import java.util.Arrays;
import java.util.List;
public interface IRecallRequest {
/**
* 查询类型
* @return
*/
String requestType();
/**
* 真实的查询参数
* @return
*/
SearchParam searchParam();
/**
* 请求返回的字段
* @return
*/
default List<String> includeFields(){
return Arrays.asList(ProductIndexEsField.productSkn,ProductIndexEsField.brandId,ProductIndexEsField.middleSortId);
}
}
... ...