Authored by hugufei

fix

... ... @@ -8,7 +8,6 @@ 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.CacheRecallRequestResponse;
import com.yoho.search.recall.scene.models.RecallRequest;
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,8 +6,9 @@ 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.cache.CacheSknInfoRequestResponse;
import com.yoho.search.recall.scene.models.CacheSknInfoRequestResponse;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import org.apache.commons.collections.MapUtils;
... ... @@ -32,7 +33,7 @@ public class BacthSknInfoComponent {
@Autowired
private ProductIndexBaseService productIndexBaseService;
@Autowired
private CacheRequestResponseComponent cacheRequestResponseHelper;
private CacheRequestResponseComponent cacheRequestResponseComponent;
private static final boolean useEhCache = true;
... ... @@ -66,15 +67,15 @@ public class BacthSknInfoComponent {
sknInfoCacheRequestRespons.add(new CacheSknInfoRequestResponse(productSkn, CacheTimeConstants.SKN_INFO));
}
//2、批量从缓存中获取
cacheRequestResponseHelper.batchFillResponseFromCache(sknInfoCacheRequestRespons,useEhCache);
cacheRequestResponseComponent.batchFillResponseFromCache(sknInfoCacheRequestRespons,useEhCache);
//3、获取未命中缓存的请求
List<CacheSknInfoRequestResponse> missCacheRequests = cacheRequestResponseHelper.filterMissCacheRequests(sknInfoCacheRequestRespons);
List<CacheSknInfoRequestResponse> missCacheRequests = cacheRequestResponseComponent.filterMissCacheRequests(sknInfoCacheRequestRespons);
//4、执行批量查询
Map<RedisKeyBuilder,Map<String, Object>> queryResults = this.batchQueryMissCacheRequests(missCacheRequests);
//5、填充查询结果
cacheRequestResponseHelper.batchFillResponseWithQueryResults(sknInfoCacheRequestRespons,queryResults);
cacheRequestResponseComponent.batchFillResponseWithQueryResults(sknInfoCacheRequestRespons,queryResults);
//6、将CacheRequestResponse中需要缓存的key加入缓存
cacheRequestResponseHelper.batchAddResponseToCache(sknInfoCacheRequestRespons,useEhCache);
cacheRequestResponseComponent.batchAddResponseToCache(sknInfoCacheRequestRespons,useEhCache);
return sknInfoCacheRequestRespons;
}
... ...
... ... @@ -5,8 +5,9 @@ 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.CacheRecallRequestResponse;
import com.yoho.search.recall.scene.models.RecallRequest;
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);
}
}
}
package com.yoho.search.recall.scene.models;
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;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.model.SearchParam;
... ... @@ -17,18 +16,6 @@ public interface IRecallRequest {
String requestType();
/**
* 缓存key
* @return
*/
RedisKeyBuilder redisKeyBuilder();
/**
* 缓存时间
* @return
*/
int cacheTimeInSecond();
/**
* 真实的查询参数
* @return
*/
... ...
... ... @@ -3,8 +3,6 @@ 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;
... ...
... ... @@ -4,7 +4,6 @@ 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;
... ...
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.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);
}
}