Showing
8 changed files
with
65 additions
and
75 deletions
1 | package com.yoho.search.recall.scene.cache; | 1 | package com.yoho.search.recall.scene.cache; |
2 | 2 | ||
3 | -import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | ||
4 | import com.yoho.search.base.utils.Transfer; | 3 | import com.yoho.search.base.utils.Transfer; |
4 | +import com.yoho.search.recall.scene.models.ICacheRequest; | ||
5 | 5 | ||
6 | -public class CacheRequestResponse<K,V> { | 6 | +public class CacheRequestResponse<K extends ICacheRequest,V> { |
7 | private K request; | 7 | private K request; |
8 | - private RedisKeyBuilder redisKeyBuilder; | ||
9 | - private int cacheTimeInSecond; | ||
10 | - | ||
11 | private V response; | 8 | private V response; |
12 | private Transfer<String,V> toResponseTransfer; | 9 | private Transfer<String,V> toResponseTransfer; |
13 | private Transfer<V,String> fromResponseTransfer; | 10 | private Transfer<V,String> fromResponseTransfer; |
14 | private boolean needRecache = false; | 11 | private boolean needRecache = false; |
15 | 12 | ||
16 | - | ||
17 | - public CacheRequestResponse(K request,Transfer<K,RedisKeyBuilder> requestRedisKeyBuilder,int cacheTimeInSecond,Transfer<String,V> toResponseTransfer,Transfer<V,String> fromResponseTransfer){ | 13 | + public CacheRequestResponse(K request ,Transfer<String,V> toResponseTransfer,Transfer<V,String> fromResponseTransfer){ |
18 | this.request = request; | 14 | this.request = request; |
19 | - this.redisKeyBuilder = requestRedisKeyBuilder.transfer(request); | ||
20 | - this.cacheTimeInSecond = cacheTimeInSecond; | ||
21 | - | ||
22 | this.toResponseTransfer = toResponseTransfer; | 15 | this.toResponseTransfer = toResponseTransfer; |
23 | this.fromResponseTransfer = fromResponseTransfer; | 16 | this.fromResponseTransfer = fromResponseTransfer; |
24 | } | 17 | } |
@@ -31,10 +24,6 @@ public class CacheRequestResponse<K,V> { | @@ -31,10 +24,6 @@ public class CacheRequestResponse<K,V> { | ||
31 | return response; | 24 | return response; |
32 | } | 25 | } |
33 | 26 | ||
34 | - public RedisKeyBuilder getRequestRedisKeyBuilder() { | ||
35 | - return redisKeyBuilder; | ||
36 | - } | ||
37 | - | ||
38 | public Transfer<String, V> getToResponseTransfer() { | 27 | public Transfer<String, V> getToResponseTransfer() { |
39 | return toResponseTransfer; | 28 | return toResponseTransfer; |
40 | } | 29 | } |
@@ -52,9 +41,4 @@ public class CacheRequestResponse<K,V> { | @@ -52,9 +41,4 @@ public class CacheRequestResponse<K,V> { | ||
52 | return needRecache; | 41 | return needRecache; |
53 | } | 42 | } |
54 | 43 | ||
55 | - public int getCacheTimeInSecond() { | ||
56 | - return cacheTimeInSecond; | ||
57 | - } | ||
58 | - | ||
59 | - | ||
60 | } | 44 | } |
@@ -5,6 +5,7 @@ import com.yoho.search.base.utils.CollectionUtils; | @@ -5,6 +5,7 @@ import com.yoho.search.base.utils.CollectionUtils; | ||
5 | import com.yoho.search.base.utils.Transfer; | 5 | import com.yoho.search.base.utils.Transfer; |
6 | import com.yoho.search.common.cache.impls.EhCache; | 6 | import com.yoho.search.common.cache.impls.EhCache; |
7 | import com.yoho.search.core.redis.components.YohoSearchRedisComponent; | 7 | import com.yoho.search.core.redis.components.YohoSearchRedisComponent; |
8 | +import com.yoho.search.recall.scene.models.ICacheRequest; | ||
8 | import org.apache.commons.lang.StringUtils; | 9 | import org.apache.commons.lang.StringUtils; |
9 | import org.slf4j.Logger; | 10 | import org.slf4j.Logger; |
10 | import org.slf4j.LoggerFactory; | 11 | import org.slf4j.LoggerFactory; |
@@ -31,11 +32,11 @@ public class CacheRequestResponseComponent { | @@ -31,11 +32,11 @@ public class CacheRequestResponseComponent { | ||
31 | * @param <V> | 32 | * @param <V> |
32 | * @param <T> | 33 | * @param <T> |
33 | */ | 34 | */ |
34 | - public <K, V, T extends CacheRequestResponse<K, V>> void batchFillResponseFromCache(final List<T> cacheRequestResponses, boolean useEhCache) { | 35 | + public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchFillResponseFromCache(final List<T> cacheRequestResponses, boolean useEhCache) { |
35 | try { | 36 | try { |
36 | Collection<RedisKeyBuilder> keys = new ArrayList<>(); | 37 | Collection<RedisKeyBuilder> keys = new ArrayList<>(); |
37 | for (T requestResponse : cacheRequestResponses) { | 38 | for (T requestResponse : cacheRequestResponses) { |
38 | - RedisKeyBuilder redisKeyBuilder = requestResponse.getRequestRedisKeyBuilder(); | 39 | + RedisKeyBuilder redisKeyBuilder = requestResponse.getRequest().redisKeyBuilder(); |
39 | keys.add(redisKeyBuilder); | 40 | keys.add(redisKeyBuilder); |
40 | } | 41 | } |
41 | List<String> cachedValues = this.mutiGetFromCache(keys, useEhCache); | 42 | List<String> cachedValues = this.mutiGetFromCache(keys, useEhCache); |
@@ -60,7 +61,7 @@ public class CacheRequestResponseComponent { | @@ -60,7 +61,7 @@ public class CacheRequestResponseComponent { | ||
60 | * @param <T> | 61 | * @param <T> |
61 | * @return | 62 | * @return |
62 | */ | 63 | */ |
63 | - public <K, V, T extends CacheRequestResponse<K, V>> List<T> filterMissCacheRequests(List<T> cacheRequestResponses){ | 64 | + public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> List<T> filterMissCacheRequests(List<T> cacheRequestResponses){ |
64 | List<T> notCachedRequests = new ArrayList<>(); | 65 | List<T> notCachedRequests = new ArrayList<>(); |
65 | for (T requestResponse : cacheRequestResponses) { | 66 | for (T requestResponse : cacheRequestResponses) { |
66 | if (requestResponse != null && requestResponse.getResponse()==null) { | 67 | if (requestResponse != null && requestResponse.getResponse()==null) { |
@@ -78,12 +79,12 @@ public class CacheRequestResponseComponent { | @@ -78,12 +79,12 @@ public class CacheRequestResponseComponent { | ||
78 | * @param <V> | 79 | * @param <V> |
79 | * @param <T> | 80 | * @param <T> |
80 | */ | 81 | */ |
81 | - public <K, V, T extends CacheRequestResponse<K, V>> void batchFillResponseWithQueryResults(List<T> cacheRequestResponses,Map<RedisKeyBuilder,V> queryResults){ | 82 | + public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchFillResponseWithQueryResults(List<T> cacheRequestResponses,Map<RedisKeyBuilder,V> queryResults){ |
82 | for (T sknInfoRequestResponse : cacheRequestResponses) { | 83 | for (T sknInfoRequestResponse : cacheRequestResponses) { |
83 | if(sknInfoRequestResponse.getResponse()!=null){ | 84 | if(sknInfoRequestResponse.getResponse()!=null){ |
84 | continue; | 85 | continue; |
85 | } | 86 | } |
86 | - RedisKeyBuilder redisKeyBuilder = sknInfoRequestResponse.getRequestRedisKeyBuilder(); | 87 | + RedisKeyBuilder redisKeyBuilder = sknInfoRequestResponse.getRequest().redisKeyBuilder(); |
87 | V response = queryResults.get(redisKeyBuilder); | 88 | V response = queryResults.get(redisKeyBuilder); |
88 | if(response!=null){ | 89 | if(response!=null){ |
89 | sknInfoRequestResponse.setResponse(response,true); | 90 | sknInfoRequestResponse.setResponse(response,true); |
@@ -99,12 +100,12 @@ public class CacheRequestResponseComponent { | @@ -99,12 +100,12 @@ public class CacheRequestResponseComponent { | ||
99 | * @param <V> | 100 | * @param <V> |
100 | * @param <T> | 101 | * @param <T> |
101 | */ | 102 | */ |
102 | - public <K, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache) { | 103 | + public <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache) { |
103 | //1、按缓存时间分组 | 104 | //1、按缓存时间分组 |
104 | Map<Integer, List<T>> groupMap = CollectionUtils.toListMap(cacheRequestResponses, new Transfer<T, Integer>() { | 105 | Map<Integer, List<T>> groupMap = CollectionUtils.toListMap(cacheRequestResponses, new Transfer<T, Integer>() { |
105 | @Override | 106 | @Override |
106 | public Integer transfer(T t) { | 107 | public Integer transfer(T t) { |
107 | - return t.getCacheTimeInSecond(); | 108 | + return t.getRequest().cacheTimeInSecond(); |
108 | } | 109 | } |
109 | }); | 110 | }); |
110 | //2、按缓存时间大小直接加入缓存 | 111 | //2、按缓存时间大小直接加入缓存 |
@@ -113,14 +114,14 @@ public class CacheRequestResponseComponent { | @@ -113,14 +114,14 @@ public class CacheRequestResponseComponent { | ||
113 | } | 114 | } |
114 | } | 115 | } |
115 | 116 | ||
116 | - private <K, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache, int timeOutInSecond) { | 117 | + private <K extends ICacheRequest, V, T extends CacheRequestResponse<K, V>> void batchAddResponseToCache(List<T> cacheRequestResponses, boolean useEhCache, int timeOutInSecond) { |
117 | try { | 118 | try { |
118 | Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>(); | 119 | Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>(); |
119 | for (T requestResponse : cacheRequestResponses) { | 120 | for (T requestResponse : cacheRequestResponses) { |
120 | if (!requestResponse.isNeedRecache()){ | 121 | if (!requestResponse.isNeedRecache()){ |
121 | continue; | 122 | continue; |
122 | } | 123 | } |
123 | - RedisKeyBuilder redisKeyBuilder = requestResponse.getRequestRedisKeyBuilder(); | 124 | + RedisKeyBuilder redisKeyBuilder = requestResponse.getRequest().redisKeyBuilder(); |
124 | V response = requestResponse.getResponse(); | 125 | V response = requestResponse.getResponse(); |
125 | if(redisKeyBuilder==null || response==null){ | 126 | if(redisKeyBuilder==null || response==null){ |
126 | continue; | 127 | continue; |
@@ -9,6 +9,7 @@ import com.yoho.search.core.es.model.SearchResult; | @@ -9,6 +9,7 @@ import com.yoho.search.core.es.model.SearchResult; | ||
9 | import com.yoho.search.recall.scene.cache.CacheRequestResponseComponent; | 9 | import com.yoho.search.recall.scene.cache.CacheRequestResponseComponent; |
10 | import com.yoho.search.recall.scene.constants.CacheTimeConstants; | 10 | import com.yoho.search.recall.scene.constants.CacheTimeConstants; |
11 | import com.yoho.search.recall.scene.models.CacheSknInfoRequestResponse; | 11 | import com.yoho.search.recall.scene.models.CacheSknInfoRequestResponse; |
12 | +import com.yoho.search.recall.scene.models.SknInfoRequest; | ||
12 | import com.yoho.search.service.base.SearchCommonService; | 13 | import com.yoho.search.service.base.SearchCommonService; |
13 | import com.yoho.search.service.base.index.ProductIndexBaseService; | 14 | import com.yoho.search.service.base.index.ProductIndexBaseService; |
14 | import org.apache.commons.collections.MapUtils; | 15 | import org.apache.commons.collections.MapUtils; |
@@ -26,8 +27,6 @@ import java.util.Map; | @@ -26,8 +27,6 @@ import java.util.Map; | ||
26 | @Component | 27 | @Component |
27 | public class BacthSknInfoComponent { | 28 | public class BacthSknInfoComponent { |
28 | 29 | ||
29 | - private static final Logger logger = LoggerFactory.getLogger(BacthSknInfoComponent.class); | ||
30 | - | ||
31 | @Autowired | 30 | @Autowired |
32 | private SearchCommonService searchCommonService; | 31 | private SearchCommonService searchCommonService; |
33 | @Autowired | 32 | @Autowired |
@@ -37,7 +36,6 @@ public class BacthSknInfoComponent { | @@ -37,7 +36,6 @@ public class BacthSknInfoComponent { | ||
37 | 36 | ||
38 | private static final boolean useEhCache = true; | 37 | private static final boolean useEhCache = true; |
39 | 38 | ||
40 | - | ||
41 | /** | 39 | /** |
42 | * 按skn查询并按顺序返回 | 40 | * 按skn查询并按顺序返回 |
43 | * | 41 | * |
@@ -64,7 +62,7 @@ public class BacthSknInfoComponent { | @@ -64,7 +62,7 @@ public class BacthSknInfoComponent { | ||
64 | //1、构建请求与返回结果 | 62 | //1、构建请求与返回结果 |
65 | final List<CacheSknInfoRequestResponse> sknInfoCacheRequestRespons = new ArrayList<>(); | 63 | final List<CacheSknInfoRequestResponse> sknInfoCacheRequestRespons = new ArrayList<>(); |
66 | for (Integer productSkn : productSkns) { | 64 | for (Integer productSkn : productSkns) { |
67 | - sknInfoCacheRequestRespons.add(new CacheSknInfoRequestResponse(productSkn, CacheTimeConstants.SKN_INFO)); | 65 | + sknInfoCacheRequestRespons.add(new CacheSknInfoRequestResponse(new SknInfoRequest(productSkn))); |
68 | } | 66 | } |
69 | //2、批量从缓存中获取 | 67 | //2、批量从缓存中获取 |
70 | cacheRequestResponseComponent.batchFillResponseFromCache(sknInfoCacheRequestRespons,useEhCache); | 68 | cacheRequestResponseComponent.batchFillResponseFromCache(sknInfoCacheRequestRespons,useEhCache); |
@@ -88,7 +86,7 @@ public class BacthSknInfoComponent { | @@ -88,7 +86,7 @@ public class BacthSknInfoComponent { | ||
88 | //2、获取skn | 86 | //2、获取skn |
89 | List<Integer> productSkns = new ArrayList<>(); | 87 | List<Integer> productSkns = new ArrayList<>(); |
90 | for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse : notCachedRequestResponse) { | 88 | for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse : notCachedRequestResponse) { |
91 | - productSkns.add(sknInfoCacheRequestResponse.getRequest()); | 89 | + productSkns.add(sknInfoCacheRequestResponse.getRequest().getProductSkn()); |
92 | } | 90 | } |
93 | //3、构建SearchParam | 91 | //3、构建SearchParam |
94 | SearchParam searchParam = new SearchParam(); | 92 | SearchParam searchParam = new SearchParam(); |
@@ -104,8 +102,8 @@ public class BacthSknInfoComponent { | @@ -104,8 +102,8 @@ public class BacthSknInfoComponent { | ||
104 | productTempMap.put(MapUtils.getIntValue(product,"product_skn",0),product); | 102 | productTempMap.put(MapUtils.getIntValue(product,"product_skn",0),product); |
105 | } | 103 | } |
106 | //5、构造最终结果 | 104 | //5、构造最终结果 |
107 | - for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse :notCachedRequestResponse ) { | ||
108 | - results.put(sknInfoCacheRequestResponse.getRequestRedisKeyBuilder(),productTempMap.get(sknInfoCacheRequestResponse.getRequest())); | 105 | + for (CacheSknInfoRequestResponse requestResponse :notCachedRequestResponse ) { |
106 | + results.put(requestResponse.getRequest().redisKeyBuilder(),productTempMap.get(requestResponse.getRequest().getProductSkn())); | ||
109 | } | 107 | } |
110 | return results; | 108 | return results; |
111 | } | 109 | } |
@@ -7,29 +7,22 @@ import com.yoho.search.recall.scene.cache.CacheRequestResponse; | @@ -7,29 +7,22 @@ import com.yoho.search.recall.scene.cache.CacheRequestResponse; | ||
7 | 7 | ||
8 | public class CacheRecallRequestResponse extends CacheRequestResponse<RecallRequest,RecallResponse> { | 8 | public class CacheRecallRequestResponse extends CacheRequestResponse<RecallRequest,RecallResponse> { |
9 | 9 | ||
10 | - static Transfer<RecallRequest,RedisKeyBuilder> requestRedisKeyBuilderTransfer = new Transfer<RecallRequest, RedisKeyBuilder>() { | ||
11 | - @Override | ||
12 | - public RedisKeyBuilder transfer(RecallRequest recallRequest) { | ||
13 | - return recallRequest.redisKeyBuilder(); | ||
14 | - } | ||
15 | - }; | 10 | + public CacheRecallRequestResponse(RecallRequest request) { |
11 | + super(request, toResponseTransfer,fromResponseTransfer); | ||
12 | + } | ||
16 | 13 | ||
17 | - static Transfer<String,RecallResponse> toResponseTransfer = new Transfer<String, RecallResponse>() { | 14 | + private static Transfer<String,RecallResponse> toResponseTransfer = new Transfer<String, RecallResponse>() { |
18 | @Override | 15 | @Override |
19 | public RecallResponse transfer(String jsonValue) { | 16 | public RecallResponse transfer(String jsonValue) { |
20 | return JSON.parseObject(jsonValue, RecallResponse.class); | 17 | return JSON.parseObject(jsonValue, RecallResponse.class); |
21 | } | 18 | } |
22 | }; | 19 | }; |
23 | 20 | ||
24 | - static Transfer<RecallResponse,String> fromResponseTransfer = new Transfer<RecallResponse, String>() { | 21 | + private static Transfer<RecallResponse,String> fromResponseTransfer = new Transfer<RecallResponse, String>() { |
25 | @Override | 22 | @Override |
26 | public String transfer(RecallResponse recallResponse) { | 23 | public String transfer(RecallResponse recallResponse) { |
27 | return JSON.toJSONString(recallResponse); | 24 | return JSON.toJSONString(recallResponse); |
28 | } | 25 | } |
29 | }; | 26 | }; |
30 | 27 | ||
31 | - public CacheRecallRequestResponse(RecallRequest request) { | ||
32 | - super(request,requestRedisKeyBuilderTransfer,request.cacheTimeInSecond(), toResponseTransfer,fromResponseTransfer); | ||
33 | - } | ||
34 | - | ||
35 | } | 28 | } |
@@ -9,14 +9,11 @@ import com.yoho.search.recall.scene.cache.CacheRequestResponse; | @@ -9,14 +9,11 @@ import com.yoho.search.recall.scene.cache.CacheRequestResponse; | ||
9 | import java.util.HashMap; | 9 | import java.util.HashMap; |
10 | import java.util.Map; | 10 | import java.util.Map; |
11 | 11 | ||
12 | -public class CacheSknInfoRequestResponse extends CacheRequestResponse<Integer, Map<String, Object>> { | 12 | +public class CacheSknInfoRequestResponse extends CacheRequestResponse<SknInfoRequest, Map<String, Object>> { |
13 | 13 | ||
14 | - static Transfer<Integer, RedisKeyBuilder> requestRedisKeyBuilder = new Transfer<Integer, RedisKeyBuilder>() { | ||
15 | - @Override | ||
16 | - public RedisKeyBuilder transfer(Integer productSkn) { | ||
17 | - return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN:").appendVar(productSkn); | ||
18 | - } | ||
19 | - }; | 14 | + public CacheSknInfoRequestResponse(SknInfoRequest sknInfoRequest) { |
15 | + super(sknInfoRequest, toResponseTransfer, fromResponseTransfer); | ||
16 | + } | ||
20 | 17 | ||
21 | static Transfer<String, Map<String, Object>> toResponseTransfer = new Transfer<String, Map<String, Object>>() { | 18 | static Transfer<String, Map<String, Object>> toResponseTransfer = new Transfer<String, Map<String, Object>>() { |
22 | @Override | 19 | @Override |
@@ -34,8 +31,4 @@ public class CacheSknInfoRequestResponse extends CacheRequestResponse<Integer, M | @@ -34,8 +31,4 @@ public class CacheSknInfoRequestResponse extends CacheRequestResponse<Integer, M | ||
34 | } | 31 | } |
35 | }; | 32 | }; |
36 | 33 | ||
37 | - public CacheSknInfoRequestResponse(Integer productSkn, int cacheTimeInSecond) { | ||
38 | - super(productSkn, requestRedisKeyBuilder, cacheTimeInSecond , toResponseTransfer,fromResponseTransfer); | ||
39 | - } | ||
40 | - | ||
41 | } | 34 | } |
1 | +package com.yoho.search.recall.scene.models; | ||
2 | + | ||
3 | +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | ||
4 | +import com.yoho.search.recall.scene.constants.CacheTimeConstants; | ||
5 | + | ||
6 | +public class SknInfoRequest implements ICacheRequest{ | ||
7 | + | ||
8 | + private Integer productSkn; | ||
9 | + | ||
10 | + public SknInfoRequest(Integer productSkn){ | ||
11 | + this.productSkn = productSkn; | ||
12 | + } | ||
13 | + | ||
14 | + @Override | ||
15 | + public RedisKeyBuilder redisKeyBuilder() { | ||
16 | + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN:").appendVar(productSkn); | ||
17 | + } | ||
18 | + | ||
19 | + @Override | ||
20 | + public int cacheTimeInSecond() { | ||
21 | + return CacheTimeConstants.SKN_INFO; | ||
22 | + } | ||
23 | + | ||
24 | + public Integer getProductSkn() { | ||
25 | + return productSkn; | ||
26 | + } | ||
27 | +} |
service/src/main/java/com/yoho/search/recall/scene/models/interfaces/ICacheRequest.java
deleted
100644 → 0
1 | -package com.yoho.search.recall.scene.models.interfaces; | ||
2 | - | ||
3 | -import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | ||
4 | - | ||
5 | -public interface ICacheRequest { | ||
6 | - /** | ||
7 | - * 缓存key | ||
8 | - * @return | ||
9 | - */ | ||
10 | - RedisKeyBuilder redisKeyBuilder(); | ||
11 | - | ||
12 | - /** | ||
13 | - * 缓存时间 | ||
14 | - * @return | ||
15 | - */ | ||
16 | - int cacheTimeInSecond(); | ||
17 | -} |
-
Please register or login to post a comment