Authored by hugufei

fix

1 -package com.yoho.search.recall.scene.component;  
2 -  
3 -  
4 -import com.alibaba.fastjson.JSON;  
5 -import com.alibaba.fastjson.JSONObject;  
6 -import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;  
7 -import com.yoho.search.base.utils.ISearchConstants;  
8 -import com.yoho.search.base.utils.ProductIndexEsField;  
9 -import com.yoho.search.base.utils.Transfer;  
10 -import com.yoho.search.common.cache.impls.SearchRedis;  
11 -import com.yoho.search.core.es.model.SearchParam;  
12 -import com.yoho.search.core.es.model.SearchResult;  
13 -import com.yoho.search.recall.scene.models.CacheRequestResponse;  
14 -import com.yoho.search.service.base.SearchCommonService;  
15 -import com.yoho.search.service.base.index.ProductIndexBaseService;  
16 -import org.apache.commons.collections.MapUtils;  
17 -import org.apache.commons.lang.StringUtils;  
18 -import org.elasticsearch.index.query.QueryBuilders;  
19 -import org.slf4j.Logger;  
20 -import org.slf4j.LoggerFactory;  
21 -import org.springframework.beans.factory.annotation.Autowired;  
22 -import org.springframework.stereotype.Component;  
23 -  
24 -import java.util.*;  
25 -  
26 -@Component  
27 -public class BacthQueryBySknComponent {  
28 -  
29 - private static final Logger logger = LoggerFactory.getLogger(BacthQueryBySknComponent.class);  
30 -  
31 - @Autowired  
32 - private SearchRedis searchRedis;  
33 - @Autowired  
34 - private SearchCommonService searchCommonService;  
35 - @Autowired  
36 - private ProductIndexBaseService productIndexBaseService;  
37 -  
38 - /**  
39 - * 按skn查询并按顺序返回  
40 - *  
41 - * @param productSkns  
42 - * @return  
43 - */  
44 - public List<Map<String, Object>> queryProductListBySkn(List<Integer> productSkns,int size){  
45 - //1、构建请求  
46 - List<CacheRequestResponse<Integer, Map<String, Object>>> sknInfoRequestResponses = new ArrayList<>();  
47 - for (Integer productSkn : productSkns) {  
48 - sknInfoRequestResponses.add(new CacheRequestResponse(productSkn,redisKeyBuilderTransfer(),toValueTransfer(),fromValueTransfer()));  
49 - }  
50 -  
51 - //2、批量从缓存中获取  
52 - sknInfoRequestResponses = this.batchQueryFromCache(sknInfoRequestResponses);  
53 -  
54 - //3、获取未命中缓存的skn  
55 - List<Integer> notCacheSkns = new ArrayList<>();  
56 - for (CacheRequestResponse<Integer, Map<String, Object>> cacheRequestResponse : sknInfoRequestResponses) {  
57 - if(cacheRequestResponse.getValueObject()==null){  
58 - notCacheSkns.add(cacheRequestResponse.getKeyObject());  
59 - }  
60 -  
61 - }  
62 -  
63 - //4、执行批量查询  
64 - Map<Integer,Map<String, Object>> queryResults = this.batchQuery(notCacheSkns);  
65 -  
66 - //5、填充查询结果  
67 - for (CacheRequestResponse<Integer, Map<String, Object>> cacheRequestResponse : sknInfoRequestResponses) {  
68 - if(cacheRequestResponse.getValueObject()==null){  
69 - cacheRequestResponse.setValueObject(queryResults.get(cacheRequestResponse.getKeyObject()));  
70 - cacheRequestResponse.setNeedRecache(true);  
71 - }  
72 - }  
73 -  
74 - //6、将CacheRequestResponse中需要缓存的key加入缓存  
75 - this.batchAddToCache(sknInfoRequestResponses,10*60);//缓存10分钟  
76 -  
77 - //7、构造返回结果  
78 - Map<Integer,Map<String, Object>> productInfoMap = new HashMap<>();  
79 - for (CacheRequestResponse<Integer, Map<String, Object>> sknInfoRequestResponse: sknInfoRequestResponses) {  
80 - productInfoMap.put(sknInfoRequestResponse.getKeyObject(),sknInfoRequestResponse.getValueObject());  
81 - }  
82 - List<Map<String, Object>> finalResults = new ArrayList<>();  
83 - for (Integer productSkn : productSkns) {  
84 - if(productInfoMap.get(productSkn)!=null){  
85 - finalResults.add(productInfoMap.get(productSkn));  
86 - }  
87 - if(finalResults.size()>=size){  
88 - break;  
89 - }  
90 - }  
91 - return finalResults;  
92 - }  
93 -  
94 -  
95 - private <K, V> List<CacheRequestResponse<K, V>> batchQueryFromCache(List<CacheRequestResponse<K, V>> cacheRequestResponses) {  
96 - try {  
97 - Collection<RedisKeyBuilder> keys = new ArrayList<>();  
98 - for (CacheRequestResponse<K, V> requestResponse : cacheRequestResponses) {  
99 - RedisKeyBuilder redisKeyBuilder = requestResponse.getRedisKeyBuilderTransfer().transfer(requestResponse.getKeyObject());  
100 - keys.add(redisKeyBuilder);  
101 - }  
102 - List<String> cachedValues = searchRedis.searchValueOperations.multiGet(keys);  
103 - for (int i = 0; i < cacheRequestResponses.size(); i++) {  
104 - CacheRequestResponse<K, V> requestResponse = cacheRequestResponses.get(i);  
105 - String cachedValue = cachedValues.get(i);  
106 - if (!StringUtils.isBlank(cachedValue)) {  
107 - requestResponse.setValueObject(requestResponse.getToValueTransfer().transfer(cachedValue));  
108 - requestResponse.setNeedRecache(false);  
109 - }  
110 - }  
111 - return cacheRequestResponses;  
112 - }catch (Exception e){  
113 - logger.error(e.getMessage(),e);  
114 - return cacheRequestResponses;  
115 - }  
116 - }  
117 -  
118 - private <K, V> void batchAddToCache(List<CacheRequestResponse<K, V>> cacheRequestResponses,long timeOutInSecond){  
119 - try {  
120 - Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>();  
121 - for (CacheRequestResponse<K, V> requestResponse : cacheRequestResponses) {  
122 - if(requestResponse.isNeedRecache() && requestResponse.getValueObject()!=null){  
123 - RedisKeyBuilder redisKeyBuilder = requestResponse.getRedisKeyBuilderTransfer().transfer(requestResponse.getKeyObject());  
124 - V value = requestResponse.getValueObject();  
125 - String cacheValue = requestResponse.getFromValueTransfer().transfer(value);  
126 - toCacheMap.put(redisKeyBuilder,cacheValue);  
127 - }  
128 - }  
129 - if(!toCacheMap.isEmpty()){  
130 - searchRedis.searchRedisTemplate.mset(toCacheMap,timeOutInSecond);  
131 - }  
132 - }catch (Exception e){  
133 - logger.error(e.getMessage(),e);  
134 - }  
135 - }  
136 -  
137 -  
138 - private Transfer<Integer, RedisKeyBuilder> redisKeyBuilderTransfer() {  
139 - return new Transfer<Integer, RedisKeyBuilder>() {  
140 - @Override  
141 - public RedisKeyBuilder transfer(Integer productSkn) {  
142 - return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN:").appendVar(productSkn);  
143 - }  
144 - };  
145 - }  
146 -  
147 - private Transfer<String, Map<String, Object>> toValueTransfer() {  
148 - return new Transfer<String, Map<String, Object>>() {  
149 - @Override  
150 - public Map<String, Object> transfer(String value) {  
151 - Map<String, Object> product = new HashMap<>();  
152 - product.putAll(JSONObject.parseObject(value));  
153 - return product;  
154 - }  
155 - };  
156 - }  
157 -  
158 - private Transfer<Map<String, Object>, String> fromValueTransfer() {  
159 - return new Transfer<Map<String, Object>, String>() {  
160 - @Override  
161 - public String transfer( Map<String, Object> product) {  
162 - return JSON.toJSONString(product);  
163 - }  
164 - };  
165 - }  
166 -  
167 -  
168 -  
169 - private Map<Integer,Map<String, Object>> batchQuery(List<Integer> productSkns) {  
170 - SearchParam searchParam = new SearchParam();  
171 - searchParam.setOffset(0);  
172 - searchParam.setSize(productSkns.size());  
173 - searchParam.setFiter(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkns));  
174 - searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields());  
175 - SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);  
176 - List<Map<String, Object>> productList = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());  
177 - Map<Integer,Map<String, Object>> results = new HashMap<>();  
178 - for (Map<String, Object> product: productList) {  
179 - results.put(MapUtils.getIntValue(product,"product_skn",0),product);  
180 - }  
181 - return results;  
182 - }  
183 -  
184 -} 1 +package com.yoho.search.recall.scene.component;
  2 +
  3 +
  4 +import com.alibaba.fastjson.JSON;
  5 +import com.alibaba.fastjson.JSONObject;
  6 +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
  7 +import com.yoho.search.base.utils.ISearchConstants;
  8 +import com.yoho.search.base.utils.ProductIndexEsField;
  9 +import com.yoho.search.base.utils.Transfer;
  10 +import com.yoho.search.common.cache.impls.SearchRedis;
  11 +import com.yoho.search.core.es.model.SearchParam;
  12 +import com.yoho.search.core.es.model.SearchResult;
  13 +import com.yoho.search.recall.scene.models.CacheRequestResponse;
  14 +import com.yoho.search.service.base.SearchCommonService;
  15 +import com.yoho.search.service.base.index.ProductIndexBaseService;
  16 +import org.apache.commons.collections.MapUtils;
  17 +import org.apache.commons.lang.StringUtils;
  18 +import org.elasticsearch.index.query.QueryBuilders;
  19 +import org.slf4j.Logger;
  20 +import org.slf4j.LoggerFactory;
  21 +import org.springframework.beans.factory.annotation.Autowired;
  22 +import org.springframework.stereotype.Component;
  23 +
  24 +import java.util.*;
  25 +
  26 +@Component
  27 +public class BacthQueryBySknComponent {
  28 +
  29 + private static final Logger logger = LoggerFactory.getLogger(BacthQueryBySknComponent.class);
  30 +
  31 + @Autowired
  32 + private SearchRedis searchRedis;
  33 + @Autowired
  34 + private SearchCommonService searchCommonService;
  35 + @Autowired
  36 + private ProductIndexBaseService productIndexBaseService;
  37 +
  38 + /**
  39 + * 按skn查询并按顺序返回
  40 + *
  41 + * @param productSkns
  42 + * @return
  43 + */
  44 + public List<Map<String, Object>> queryProductListBySkn(List<Integer> productSkns,int size){
  45 + //1、构建请求
  46 + List<CacheRequestResponse<Integer, Map<String, Object>>> sknInfoRequestResponses = new ArrayList<>();
  47 + for (Integer productSkn : productSkns) {
  48 + sknInfoRequestResponses.add(new CacheRequestResponse(productSkn,redisKeyBuilderTransfer(),toValueTransfer(),fromValueTransfer()));
  49 + }
  50 +
  51 + //2、批量从缓存中获取
  52 + sknInfoRequestResponses = this.batchQueryFromCache(sknInfoRequestResponses);
  53 +
  54 + //3、获取未命中缓存的skn
  55 + List<Integer> notCacheSkns = new ArrayList<>();
  56 + for (CacheRequestResponse<Integer, Map<String, Object>> cacheRequestResponse : sknInfoRequestResponses) {
  57 + if(cacheRequestResponse.getValueObject()==null){
  58 + notCacheSkns.add(cacheRequestResponse.getKeyObject());
  59 + }
  60 +
  61 + }
  62 +
  63 + //4、执行批量查询
  64 + Map<Integer,Map<String, Object>> queryResults = this.batchQuery(notCacheSkns);
  65 +
  66 + //5、填充查询结果
  67 + for (CacheRequestResponse<Integer, Map<String, Object>> cacheRequestResponse : sknInfoRequestResponses) {
  68 + if(cacheRequestResponse.getValueObject()==null){
  69 + cacheRequestResponse.setValueObject(queryResults.get(cacheRequestResponse.getKeyObject()));
  70 + cacheRequestResponse.setNeedRecache(true);
  71 + }
  72 + }
  73 +
  74 + //6、将CacheRequestResponse中需要缓存的key加入缓存
  75 + this.batchAddToCache(sknInfoRequestResponses,10*60);//缓存10分钟
  76 +
  77 + //7、构造返回结果
  78 + Map<Integer,Map<String, Object>> productInfoMap = new HashMap<>();
  79 + for (CacheRequestResponse<Integer, Map<String, Object>> sknInfoRequestResponse: sknInfoRequestResponses) {
  80 + productInfoMap.put(sknInfoRequestResponse.getKeyObject(),sknInfoRequestResponse.getValueObject());
  81 + }
  82 + List<Map<String, Object>> finalResults = new ArrayList<>();
  83 + for (Integer productSkn : productSkns) {
  84 + if(productInfoMap.get(productSkn)!=null){
  85 + finalResults.add(productInfoMap.get(productSkn));
  86 + }
  87 + if(finalResults.size()>=size){
  88 + break;
  89 + }
  90 + }
  91 + return finalResults;
  92 + }
  93 +
  94 +
  95 + private <K, V> List<CacheRequestResponse<K, V>> batchQueryFromCache(List<CacheRequestResponse<K, V>> cacheRequestResponses) {
  96 + try {
  97 + Collection<RedisKeyBuilder> keys = new ArrayList<>();
  98 + for (CacheRequestResponse<K, V> requestResponse : cacheRequestResponses) {
  99 + RedisKeyBuilder redisKeyBuilder = requestResponse.getRedisKeyBuilderTransfer().transfer(requestResponse.getKeyObject());
  100 + keys.add(redisKeyBuilder);
  101 + }
  102 + List<String> cachedValues = searchRedis.searchValueOperations.multiGet(keys);
  103 + for (int i = 0; i < cacheRequestResponses.size(); i++) {
  104 + CacheRequestResponse<K, V> requestResponse = cacheRequestResponses.get(i);
  105 + String cachedValue = cachedValues.get(i);
  106 + if (!StringUtils.isBlank(cachedValue)) {
  107 + requestResponse.setValueObject(requestResponse.getToValueTransfer().transfer(cachedValue));
  108 + requestResponse.setNeedRecache(false);
  109 + }
  110 + }
  111 + return cacheRequestResponses;
  112 + }catch (Exception e){
  113 + logger.error(e.getMessage(),e);
  114 + return cacheRequestResponses;
  115 + }
  116 + }
  117 +
  118 + private <K, V> void batchAddToCache(List<CacheRequestResponse<K, V>> cacheRequestResponses,long timeOutInSecond){
  119 + try {
  120 + Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>();
  121 + for (CacheRequestResponse<K, V> requestResponse : cacheRequestResponses) {
  122 + if(requestResponse.isNeedRecache() && requestResponse.getValueObject()!=null){
  123 + RedisKeyBuilder redisKeyBuilder = requestResponse.getRedisKeyBuilderTransfer().transfer(requestResponse.getKeyObject());
  124 + V value = requestResponse.getValueObject();
  125 + String cacheValue = requestResponse.getFromValueTransfer().transfer(value);
  126 + toCacheMap.put(redisKeyBuilder,cacheValue);
  127 + }
  128 + }
  129 + if(!toCacheMap.isEmpty()){
  130 + searchRedis.searchRedisTemplate.mset(toCacheMap,timeOutInSecond);
  131 + }
  132 + }catch (Exception e){
  133 + logger.error(e.getMessage(),e);
  134 + }
  135 + }
  136 +
  137 +
  138 + private Transfer<Integer, RedisKeyBuilder> redisKeyBuilderTransfer() {
  139 + return new Transfer<Integer, RedisKeyBuilder>() {
  140 + @Override
  141 + public RedisKeyBuilder transfer(Integer productSkn) {
  142 + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN:").appendVar(productSkn);
  143 + }
  144 + };
  145 + }
  146 +
  147 + private Transfer<String, Map<String, Object>> toValueTransfer() {
  148 + return new Transfer<String, Map<String, Object>>() {
  149 + @Override
  150 + public Map<String, Object> transfer(String value) {
  151 + Map<String, Object> product = new HashMap<>();
  152 + product.putAll(JSONObject.parseObject(value));
  153 + return product;
  154 + }
  155 + };
  156 + }
  157 +
  158 + private Transfer<Map<String, Object>, String> fromValueTransfer() {
  159 + return new Transfer<Map<String, Object>, String>() {
  160 + @Override
  161 + public String transfer( Map<String, Object> product) {
  162 + return JSON.toJSONString(product);
  163 + }
  164 + };
  165 + }
  166 +
  167 +
  168 +
  169 + private Map<Integer,Map<String, Object>> batchQuery(List<Integer> productSkns) {
  170 + SearchParam searchParam = new SearchParam();
  171 + searchParam.setOffset(0);
  172 + searchParam.setSize(productSkns.size());
  173 + searchParam.setFiter(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkns));
  174 + searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields());
  175 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
  176 + List<Map<String, Object>> productList = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
  177 + Map<Integer,Map<String, Object>> results = new HashMap<>();
  178 + for (Map<String, Object> product: productList) {
  179 + results.put(MapUtils.getIntValue(product,"product_skn",0),product);
  180 + }
  181 + return results;
  182 + }
  183 +
  184 +}
1 -package com.yoho.search.recall.scene.models;  
2 -  
3 -import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;  
4 -import com.yoho.search.base.utils.Transfer;  
5 -  
6 -public class CacheRequestResponse<K,V> {  
7 - private K keyObject;  
8 - private Transfer<K,RedisKeyBuilder> redisKeyBuilderTransfer;  
9 - private V valueObject;  
10 - private Transfer<String,V> toValueTransfer;  
11 - private Transfer<V,String> fromValueTransfer;  
12 - private boolean needRecache;  
13 -  
14 - public CacheRequestResponse(K keyObject,Transfer<K,RedisKeyBuilder> redisKeyBuilderTransfer,Transfer<String,V> toValueTransfer,Transfer<V,String> fromValueTransfer){  
15 - this.keyObject = keyObject;  
16 - this.redisKeyBuilderTransfer = redisKeyBuilderTransfer;  
17 - this.toValueTransfer = toValueTransfer;  
18 - this.fromValueTransfer = fromValueTransfer;  
19 - }  
20 -  
21 - public K getKeyObject() {  
22 - return keyObject;  
23 - }  
24 -  
25 - public V getValueObject() {  
26 - return valueObject;  
27 - }  
28 -  
29 - public Transfer<K, RedisKeyBuilder> getRedisKeyBuilderTransfer() {  
30 - return redisKeyBuilderTransfer;  
31 - }  
32 -  
33 - public Transfer<String, V> getToValueTransfer() {  
34 - return toValueTransfer;  
35 - }  
36 -  
37 - public Transfer<V, String> getFromValueTransfer() {  
38 - return fromValueTransfer;  
39 - }  
40 -  
41 - public void setValueObject(V valueObject) {  
42 - this.valueObject = valueObject;  
43 - }  
44 -  
45 - public boolean isNeedRecache() {  
46 - return needRecache;  
47 - }  
48 -  
49 - public void setNeedRecache(boolean needRecache) {  
50 - this.needRecache = needRecache;  
51 - }  
52 -} 1 +package com.yoho.search.recall.scene.models;
  2 +
  3 +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
  4 +import com.yoho.search.base.utils.Transfer;
  5 +
  6 +public class CacheRequestResponse<K,V> {
  7 + private K keyObject;
  8 + private Transfer<K,RedisKeyBuilder> redisKeyBuilderTransfer;
  9 + private V valueObject;
  10 + private Transfer<String,V> toValueTransfer;
  11 + private Transfer<V,String> fromValueTransfer;
  12 + private boolean needRecache;
  13 +
  14 + public CacheRequestResponse(K keyObject,Transfer<K,RedisKeyBuilder> redisKeyBuilderTransfer,Transfer<String,V> toValueTransfer,Transfer<V,String> fromValueTransfer){
  15 + this.keyObject = keyObject;
  16 + this.redisKeyBuilderTransfer = redisKeyBuilderTransfer;
  17 + this.toValueTransfer = toValueTransfer;
  18 + this.fromValueTransfer = fromValueTransfer;
  19 + }
  20 +
  21 + public K getKeyObject() {
  22 + return keyObject;
  23 + }
  24 +
  25 + public V getValueObject() {
  26 + return valueObject;
  27 + }
  28 +
  29 + public Transfer<K, RedisKeyBuilder> getRedisKeyBuilderTransfer() {
  30 + return redisKeyBuilderTransfer;
  31 + }
  32 +
  33 + public Transfer<String, V> getToValueTransfer() {
  34 + return toValueTransfer;
  35 + }
  36 +
  37 + public Transfer<V, String> getFromValueTransfer() {
  38 + return fromValueTransfer;
  39 + }
  40 +
  41 + public void setValueObject(V valueObject) {
  42 + this.valueObject = valueObject;
  43 + }
  44 +
  45 + public boolean isNeedRecache() {
  46 + return needRecache;
  47 + }
  48 +
  49 + public void setNeedRecache(boolean needRecache) {
  50 + this.needRecache = needRecache;
  51 + }
  52 +}