使用AbstractCacheComponent重构PromotionProductListService缓存
Showing
2 changed files
with
29 additions
and
99 deletions
@@ -49,38 +49,6 @@ public class SearchCacheFactory { | @@ -49,38 +49,6 @@ public class SearchCacheFactory { | ||
49 | return this.getOrCreateSearchCache("AGGREGATION", cacheType, cacheInMinute); | 49 | return this.getOrCreateSearchCache("AGGREGATION", cacheType, cacheInMinute); |
50 | } | 50 | } |
51 | 51 | ||
52 | - /** | ||
53 | - * 获取默认的搜索缓存 | ||
54 | - * | ||
55 | - * @return | ||
56 | - */ | ||
57 | - public SearchCache getProductCountSearchCache() { | ||
58 | - CacheType cacheType = CacheType.SEARCH_REDIS; | ||
59 | - int cacheInMinute = 15; | ||
60 | - return this.getOrCreateSearchCache("PRODUCT_COUNT", cacheType, cacheInMinute); | ||
61 | - } | ||
62 | - | ||
63 | - /** | ||
64 | - * 获取聚合相关的缓存 | ||
65 | - * | ||
66 | - * @return | ||
67 | - */ | ||
68 | - public SearchCache getSelectionsForAppCache() { | ||
69 | - CacheType cacheType = CacheType.SEARCH_REDIS; | ||
70 | - int cacheInMinute = 15; | ||
71 | - return this.getOrCreateSearchCache("SELECTIONS_APP", cacheType, cacheInMinute); | ||
72 | - } | ||
73 | - | ||
74 | - /** | ||
75 | - * 获取聚合相关的缓存 | ||
76 | - * | ||
77 | - * @return | ||
78 | - */ | ||
79 | - public SearchCache getSelectionsForPcCache() { | ||
80 | - CacheType cacheType = CacheType.SEARCH_REDIS; | ||
81 | - int cacheInMinute = 15; | ||
82 | - return this.getOrCreateSearchCache("SELECTIONS_PC", cacheType, cacheInMinute); | ||
83 | - } | ||
84 | 52 | ||
85 | /** | 53 | /** |
86 | * 个性化聚合推荐相关的缓存 | 54 | * 个性化聚合推荐相关的缓存 |
@@ -94,17 +62,6 @@ public class SearchCacheFactory { | @@ -94,17 +62,6 @@ public class SearchCacheFactory { | ||
94 | } | 62 | } |
95 | 63 | ||
96 | /** | 64 | /** |
97 | - * 品牌相关的缓存 | ||
98 | - * | ||
99 | - * @return | ||
100 | - */ | ||
101 | - public SearchCache getBrandRelatedCache() { | ||
102 | - CacheType cacheType = CacheType.SEARCH_REDIS; | ||
103 | - int cacheInMinute = 15; | ||
104 | - return this.getOrCreateSearchCache("BRAND_RELATED", cacheType, cacheInMinute); | ||
105 | - } | ||
106 | - | ||
107 | - /** | ||
108 | * aop的缓存 | 65 | * aop的缓存 |
109 | * | 66 | * |
110 | * @return | 67 | * @return |
@@ -135,27 +92,4 @@ public class SearchCacheFactory { | @@ -135,27 +92,4 @@ public class SearchCacheFactory { | ||
135 | int cacheInMinute = 15; | 92 | int cacheInMinute = 15; |
136 | return this.getOrCreateSearchCache("PROMOTION", cacheType, cacheInMinute); | 93 | return this.getOrCreateSearchCache("PROMOTION", cacheType, cacheInMinute); |
137 | } | 94 | } |
138 | - | ||
139 | - /** | ||
140 | - * PC列表的缓存 | ||
141 | - * | ||
142 | - * @return | ||
143 | - */ | ||
144 | - public SearchCache getWebProductListSearchCache() { | ||
145 | - CacheType cacheType = CacheType.SEARCH_REDIS; | ||
146 | - int cacheInMinute = 15; | ||
147 | - return this.getOrCreateSearchCache("WEB_PRODUCT_LIST", cacheType, cacheInMinute); | ||
148 | - } | ||
149 | - | ||
150 | - /** | ||
151 | - * 有好货的缓存 | ||
152 | - * | ||
153 | - * @return | ||
154 | - */ | ||
155 | - public SearchCache goodProductListSearchCache() { | ||
156 | - CacheType cacheType = CacheType.SEARCH_REDIS; | ||
157 | - int cacheInMinute = 15; | ||
158 | - return this.getOrCreateSearchCache("GOOD_PRODUCT", cacheType, cacheInMinute); | ||
159 | - } | ||
160 | - | ||
161 | } | 95 | } |
service/src/main/java/com/yoho/search/service/scene/pages/promotion/PromotionProductListService.java
@@ -2,25 +2,23 @@ package com.yoho.search.service.scene.pages.promotion; | @@ -2,25 +2,23 @@ package com.yoho.search.service.scene.pages.promotion; | ||
2 | 2 | ||
3 | import com.alibaba.fastjson.JSONObject; | 3 | import com.alibaba.fastjson.JSONObject; |
4 | import com.yoho.search.base.utils.ISearchConstants; | 4 | import com.yoho.search.base.utils.ISearchConstants; |
5 | -import com.yoho.search.cache.log.SearchCacheMatchLogger; | ||
6 | -import com.yoho.search.cache.model.SearchCache; | 5 | +import com.yoho.search.cache.CacheTimeConstants; |
6 | +import com.yoho.search.cache.beans.AbstractCacheComponent; | ||
7 | +import com.yoho.search.common.SearchCommonService; | ||
8 | +import com.yoho.search.common.SearchDynamicConfigService; | ||
7 | import com.yoho.search.core.es.model.SearchParam; | 9 | import com.yoho.search.core.es.model.SearchParam; |
8 | import com.yoho.search.core.es.model.SearchResult; | 10 | import com.yoho.search.core.es.model.SearchResult; |
9 | import com.yoho.search.models.PromotionConditions; | 11 | import com.yoho.search.models.PromotionConditions; |
10 | import com.yoho.search.models.SearchApiResult; | 12 | import com.yoho.search.models.SearchApiResult; |
11 | -import com.yoho.search.service.recall.SceneRecallProductListService; | ||
12 | -import com.yoho.search.service.recall.beans.builder.UserRecallRequestBuilder; | ||
13 | -import com.yoho.search.service.recall.beans.strategy.NotRecallTypeEnum; | ||
14 | -import com.yoho.search.service.recall.models.req.UserRecallRequest; | ||
15 | -import com.yoho.search.cache.beans.SearchCacheService; | ||
16 | -import com.yoho.search.common.SearchCommonService; | ||
17 | -import com.yoho.search.common.SearchDynamicConfigService; | ||
18 | -import com.yoho.search.service.index.ProductIndexBaseService; | ||
19 | import com.yoho.search.service.helper.ProductListHelper; | 13 | import com.yoho.search.service.helper.ProductListHelper; |
20 | import com.yoho.search.service.helper.SearchCommonHelper; | 14 | import com.yoho.search.service.helper.SearchCommonHelper; |
21 | import com.yoho.search.service.helper.SearchParamHelper; | 15 | import com.yoho.search.service.helper.SearchParamHelper; |
22 | import com.yoho.search.service.helper.SearchSortHelper; | 16 | import com.yoho.search.service.helper.SearchSortHelper; |
23 | -import com.yoho.search.cache.beans.AbstractCacheAbleService; | 17 | +import com.yoho.search.service.index.ProductIndexBaseService; |
18 | +import com.yoho.search.service.recall.SceneRecallProductListService; | ||
19 | +import com.yoho.search.service.recall.beans.builder.UserRecallRequestBuilder; | ||
20 | +import com.yoho.search.service.recall.beans.strategy.NotRecallTypeEnum; | ||
21 | +import com.yoho.search.service.recall.models.req.UserRecallRequest; | ||
24 | import org.apache.commons.lang.StringUtils; | 22 | import org.apache.commons.lang.StringUtils; |
25 | import org.elasticsearch.index.query.BoolQueryBuilder; | 23 | import org.elasticsearch.index.query.BoolQueryBuilder; |
26 | import org.springframework.beans.factory.annotation.Autowired; | 24 | import org.springframework.beans.factory.annotation.Autowired; |
@@ -29,7 +27,7 @@ import org.springframework.stereotype.Component; | @@ -29,7 +27,7 @@ import org.springframework.stereotype.Component; | ||
29 | import java.util.Map; | 27 | import java.util.Map; |
30 | 28 | ||
31 | @Component | 29 | @Component |
32 | -public class PromotionProductListService extends AbstractCacheAbleService { | 30 | +public class PromotionProductListService extends AbstractCacheComponent<JSONObject> { |
33 | 31 | ||
34 | @Autowired | 32 | @Autowired |
35 | private PromotionSceneHelper promotionSceneHelper; | 33 | private PromotionSceneHelper promotionSceneHelper; |
@@ -44,8 +42,6 @@ public class PromotionProductListService extends AbstractCacheAbleService { | @@ -44,8 +42,6 @@ public class PromotionProductListService extends AbstractCacheAbleService { | ||
44 | @Autowired | 42 | @Autowired |
45 | private SearchCommonHelper searchCommonHelper; | 43 | private SearchCommonHelper searchCommonHelper; |
46 | @Autowired | 44 | @Autowired |
47 | - private SearchCacheService searchCacheService; | ||
48 | - @Autowired | ||
49 | private UserRecallRequestBuilder userRecallRequestBuilder; | 45 | private UserRecallRequestBuilder userRecallRequestBuilder; |
50 | @Autowired | 46 | @Autowired |
51 | private SceneRecallProductListService sceneRecallProductListService; | 47 | private SceneRecallProductListService sceneRecallProductListService; |
@@ -54,11 +50,6 @@ public class PromotionProductListService extends AbstractCacheAbleService { | @@ -54,11 +50,6 @@ public class PromotionProductListService extends AbstractCacheAbleService { | ||
54 | @Autowired | 50 | @Autowired |
55 | private ProductIndexBaseService productIndexBaseService; | 51 | private ProductIndexBaseService productIndexBaseService; |
56 | 52 | ||
57 | - @Override | ||
58 | - public SearchCache getSearchCache() { | ||
59 | - return searchCacheFactory.getPromotionSearchCache(); | ||
60 | - } | ||
61 | - | ||
62 | /** | 53 | /** |
63 | * 促销列表入口 | 54 | * 促销列表入口 |
64 | * | 55 | * |
@@ -100,7 +91,6 @@ public class PromotionProductListService extends AbstractCacheAbleService { | @@ -100,7 +91,6 @@ public class PromotionProductListService extends AbstractCacheAbleService { | ||
100 | return true; | 91 | return true; |
101 | } | 92 | } |
102 | 93 | ||
103 | - | ||
104 | /** | 94 | /** |
105 | * 默认的促销列表的返回方式 | 95 | * 默认的促销列表的返回方式 |
106 | * | 96 | * |
@@ -127,26 +117,32 @@ public class PromotionProductListService extends AbstractCacheAbleService { | @@ -127,26 +117,32 @@ public class PromotionProductListService extends AbstractCacheAbleService { | ||
127 | searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields()); | 117 | searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields()); |
128 | 118 | ||
129 | // 5)从缓存中获取数据 | 119 | // 5)从缓存中获取数据 |
130 | - final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX; | ||
131 | - JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, productIndexName, searchParam); | ||
132 | - if (cacheObject != null) { | ||
133 | - SearchCacheMatchLogger.doSearchCacheMatchLog("/promotion/list.json", paramMap); | ||
134 | - return new SearchApiResult().setData(cacheObject); | ||
135 | - } | 120 | + JSONObject productListResult = super.queryWithCache(searchParam,paramMap); |
121 | + return new SearchApiResult().setData(productListResult); | ||
122 | + } | ||
136 | 123 | ||
137 | - // 6)查询ES | 124 | + @Override |
125 | + protected JSONObject doRealQuery(SearchParam searchParam, Map<String, String> paramMap) throws Exception { | ||
126 | + // 1、查询ES | ||
127 | + final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX; | ||
138 | SearchResult searchResult = searchCommonService.doSearch(productIndexName, searchParam); | 128 | SearchResult searchResult = searchCommonService.doSearch(productIndexName, searchParam); |
139 | if (searchResult == null) { | 129 | if (searchResult == null) { |
140 | - return new SearchApiResult().setCode(500).setMessage("execption"); | 130 | + throw new RuntimeException("searchResult is Empty"); |
141 | } | 131 | } |
142 | - // 7)构造返回结果 | 132 | + // 2、构造返回结果 |
143 | boolean needResort = searchCommonHelper.isOrderEmpty(paramMap); | 133 | boolean needResort = searchCommonHelper.isOrderEmpty(paramMap); |
134 | + int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); | ||
144 | JSONObject productListResult = productListHelper.buildProductListResult(searchResult, pageSize, needResort,needResort? NotRecallTypeEnum.W2V_PERSIONAL:NotRecallTypeEnum.NOT_PERSIONAL); | 135 | JSONObject productListResult = productListHelper.buildProductListResult(searchResult, pageSize, needResort,needResort? NotRecallTypeEnum.W2V_PERSIONAL:NotRecallTypeEnum.NOT_PERSIONAL); |
145 | - | ||
146 | - // 10)将结果存进缓存 | ||
147 | - searchCacheService.addJSONObjectToCache(this.searchCache, productIndexName, searchParam, productListResult); | ||
148 | - return new SearchApiResult().setData(productListResult); | 136 | + return productListResult; |
149 | } | 137 | } |
150 | 138 | ||
139 | + @Override | ||
140 | + protected int cacheTimeInMinute() { | ||
141 | + return CacheTimeConstants.CACHE_15_MINUTE; | ||
142 | + } | ||
151 | 143 | ||
144 | + @Override | ||
145 | + protected String cacheSceneKey() { | ||
146 | + return "PROMOTION_PRODUCT_LIST"; | ||
147 | + } | ||
152 | } | 148 | } |
-
Please register or login to post a comment