Authored by hugufei

fix

package com.yoho.search.recall.scene.cache;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.recall.scene.models.ICacheRequest;
public class CacheRequestResponse<K,V> {
public class CacheRequestResponse<K extends ICacheRequest,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){
public CacheRequestResponse(K request ,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;
}
... ... @@ -31,10 +24,6 @@ public class CacheRequestResponse<K,V> {
return response;
}
public RedisKeyBuilder getRequestRedisKeyBuilder() {
return redisKeyBuilder;
}
public Transfer<String, V> getToResponseTransfer() {
return toResponseTransfer;
}
... ... @@ -52,9 +41,4 @@ public class CacheRequestResponse<K,V> {
return needRecache;
}
public int getCacheTimeInSecond() {
return cacheTimeInSecond;
}
}
... ...
... ... @@ -5,6 +5,7 @@ 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.ICacheRequest;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -31,11 +32,11 @@ public class CacheRequestResponseComponent {
* @param <V>
* @param <T>
*/
public <K, V, T extends CacheRequestResponse<K, V>> void batchFillResponseFromCache(final List<T> cacheRequestResponses, boolean useEhCache) {
public <K extends ICacheRequest, 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();
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequest().redisKeyBuilder();
keys.add(redisKeyBuilder);
}
List<String> cachedValues = this.mutiGetFromCache(keys, useEhCache);
... ... @@ -60,7 +61,7 @@ public class CacheRequestResponseComponent {
* @param <T>
* @return
*/
public <K, V, T extends CacheRequestResponse<K, V>> List<T> filterMissCacheRequests(List<T> cacheRequestResponses){
public <K extends ICacheRequest, 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) {
... ... @@ -78,12 +79,12 @@ public class CacheRequestResponseComponent {
* @param <V>
* @param <T>
*/
public <K, V, T extends CacheRequestResponse<K, V>> void batchFillResponseWithQueryResults(List<T> cacheRequestResponses,Map<RedisKeyBuilder,V> queryResults){
public <K extends ICacheRequest, 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();
RedisKeyBuilder redisKeyBuilder = sknInfoRequestResponse.getRequest().redisKeyBuilder();
V response = queryResults.get(redisKeyBuilder);
if(response!=null){
sknInfoRequestResponse.setResponse(response,true);
... ... @@ -99,12 +100,12 @@ public class CacheRequestResponseComponent {
* @param <V>
* @param <T>
*/
public <K, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache) {
public <K extends ICacheRequest, 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();
return t.getRequest().cacheTimeInSecond();
}
});
//2、按缓存时间大小直接加入缓存
... ... @@ -113,14 +114,14 @@ public class CacheRequestResponseComponent {
}
}
private <K, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache, int timeOutInSecond) {
private <K extends ICacheRequest, 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();
RedisKeyBuilder redisKeyBuilder = requestResponse.getRequest().redisKeyBuilder();
V response = requestResponse.getResponse();
if(redisKeyBuilder==null || response==null){
continue;
... ...
... ... @@ -9,6 +9,7 @@ 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.SknInfoRequest;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import org.apache.commons.collections.MapUtils;
... ... @@ -26,8 +27,6 @@ import java.util.Map;
@Component
public class BacthSknInfoComponent {
private static final Logger logger = LoggerFactory.getLogger(BacthSknInfoComponent.class);
@Autowired
private SearchCommonService searchCommonService;
@Autowired
... ... @@ -37,7 +36,6 @@ public class BacthSknInfoComponent {
private static final boolean useEhCache = true;
/**
* 按skn查询并按顺序返回
*
... ... @@ -64,7 +62,7 @@ public class BacthSknInfoComponent {
//1、构建请求与返回结果
final List<CacheSknInfoRequestResponse> sknInfoCacheRequestRespons = new ArrayList<>();
for (Integer productSkn : productSkns) {
sknInfoCacheRequestRespons.add(new CacheSknInfoRequestResponse(productSkn, CacheTimeConstants.SKN_INFO));
sknInfoCacheRequestRespons.add(new CacheSknInfoRequestResponse(new SknInfoRequest(productSkn)));
}
//2、批量从缓存中获取
cacheRequestResponseComponent.batchFillResponseFromCache(sknInfoCacheRequestRespons,useEhCache);
... ... @@ -88,7 +86,7 @@ public class BacthSknInfoComponent {
//2、获取skn
List<Integer> productSkns = new ArrayList<>();
for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse : notCachedRequestResponse) {
productSkns.add(sknInfoCacheRequestResponse.getRequest());
productSkns.add(sknInfoCacheRequestResponse.getRequest().getProductSkn());
}
//3、构建SearchParam
SearchParam searchParam = new SearchParam();
... ... @@ -104,8 +102,8 @@ public class BacthSknInfoComponent {
productTempMap.put(MapUtils.getIntValue(product,"product_skn",0),product);
}
//5、构造最终结果
for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse :notCachedRequestResponse ) {
results.put(sknInfoCacheRequestResponse.getRequestRedisKeyBuilder(),productTempMap.get(sknInfoCacheRequestResponse.getRequest()));
for (CacheSknInfoRequestResponse requestResponse :notCachedRequestResponse ) {
results.put(requestResponse.getRequest().redisKeyBuilder(),productTempMap.get(requestResponse.getRequest().getProductSkn()));
}
return results;
}
... ...
... ... @@ -7,29 +7,22 @@ import com.yoho.search.recall.scene.cache.CacheRequestResponse;
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();
}
};
public CacheRecallRequestResponse(RecallRequest request) {
super(request, toResponseTransfer,fromResponseTransfer);
}
static Transfer<String,RecallResponse> toResponseTransfer = new Transfer<String, RecallResponse>() {
private 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>() {
private 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);
}
}
... ...
... ... @@ -9,14 +9,11 @@ import com.yoho.search.recall.scene.cache.CacheRequestResponse;
import java.util.HashMap;
import java.util.Map;
public class CacheSknInfoRequestResponse extends CacheRequestResponse<Integer, Map<String, Object>> {
public class CacheSknInfoRequestResponse extends CacheRequestResponse<SknInfoRequest, 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);
}
};
public CacheSknInfoRequestResponse(SknInfoRequest sknInfoRequest) {
super(sknInfoRequest, toResponseTransfer, fromResponseTransfer);
}
static Transfer<String, Map<String, Object>> toResponseTransfer = new Transfer<String, Map<String, Object>>() {
@Override
... ... @@ -34,8 +31,4 @@ public class CacheSknInfoRequestResponse extends CacheRequestResponse<Integer, M
}
};
public CacheSknInfoRequestResponse(Integer productSkn, int cacheTimeInSecond) {
super(productSkn, requestRedisKeyBuilder, cacheTimeInSecond , toResponseTransfer,fromResponseTransfer);
}
}
... ...
package com.yoho.search.recall.scene.models;
import com.yoho.search.base.utils.Transfer;
public interface ICacheResponse<V> {
Transfer<String,V> toResponseTransfer();
Transfer<V,String> fromResponseTransfer();
}
... ...
package com.yoho.search.recall.scene.models;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.recall.scene.constants.CacheTimeConstants;
public class SknInfoRequest implements ICacheRequest{
private Integer productSkn;
public SknInfoRequest(Integer productSkn){
this.productSkn = productSkn;
}
@Override
public RedisKeyBuilder redisKeyBuilder() {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN:").appendVar(productSkn);
}
@Override
public int cacheTimeInSecond() {
return CacheTimeConstants.SKN_INFO;
}
public Integer getProductSkn() {
return productSkn;
}
}
... ...
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();
}