Authored by hugufei

召回数量代码优化

@@ -40,7 +40,7 @@ public class SortBrandRecallRequestBuilder { @@ -40,7 +40,7 @@ public class SortBrandRecallRequestBuilder {
40 40
41 // 3)、参数构造 41 // 3)、参数构造
42 for (SortBrand sortBrand : sortBrands) { 42 for (SortBrand sortBrand : sortBrands) {
43 - RecallSknCount recallSknCount = recallConfigService.queryRecallSknCount(pageId, sortBrand); 43 + RecallSknCount recallSknCount = recallConfigService.queryRecallSknCount(pageId, sortBrandType,sortBrand);
44 if (recallSknCount == null) { 44 if (recallSknCount == null) {
45 continue; 45 continue;
46 } 46 }
1 package com.yoho.search.recall.beans.strategy; 1 package com.yoho.search.recall.beans.strategy;
2 2
3 -public enum SortBrandType { 3 +/**
  4 + * 品牌品类类型,字段不能该,取召回数量的时后要用到
  5 + */
  6 +public enum SortBrandType {
4 REC_SORT_BRAND, 7 REC_SORT_BRAND,
5 VEC_RNN_SORT_BRAND, 8 VEC_RNN_SORT_BRAND,
6 VEC_W2V_SORT_BRAND 9 VEC_W2V_SORT_BRAND
@@ -6,10 +6,6 @@ public class RecallConfigConstants { @@ -6,10 +6,6 @@ public class RecallConfigConstants {
6 6
7 public static final int DEFAULT_PAGE_ID = 0; 7 public static final int DEFAULT_PAGE_ID = 0;
8 8
9 - public static final int SKN_COUNT_CONFIG_PAGE = 1;  
10 - public static final int SKN_COUNT_CONFIG_SORT = 2;  
11 - public static final int SKN_COUNT_CONFIG_BRAND = 3;  
12 -  
13 9
14 public static final String DIRECT_TRAIN = "DIRECT_TRAIN"; 10 public static final String DIRECT_TRAIN = "DIRECT_TRAIN";
15 public static final String REC_SKN = "REC_SKN"; 11 public static final String REC_SKN = "REC_SKN";
@@ -20,11 +16,10 @@ public class RecallConfigConstants { @@ -20,11 +16,10 @@ public class RecallConfigConstants {
20 public static final String VEC_RNN_S_B_COUNT = "VEC_RNN_S_B_COUNT"; 16 public static final String VEC_RNN_S_B_COUNT = "VEC_RNN_S_B_COUNT";
21 public static final String VEC_W2V_S_B_COUNT = "VEC_W2V_S_B_COUNT"; 17 public static final String VEC_W2V_S_B_COUNT = "VEC_W2V_S_B_COUNT";
22 18
23 -  
24 - public static final String REC_SORT_BRAND = "REC_SORT_BRAND";  
25 - public static final String RNN_SORT_BRAND = "RNN_SORT_BRAND";  
26 - public static final String W2V_SORT_BRAND = "W2V_SORT_BRAND";  
27 - public static final String SORT = "SORT";  
28 - public static final String BRAND = "SORT"; 19 + public static final String RECALL_SKN_COUNT_REC_SORT_BRAND = "REC_SORT_BRAND";
  20 + public static final String RECALL_SKN_COUNT_RNN_SORT_BRAND = "VEC_RNN_SORT_BRAND";
  21 + public static final String RECALL_SKN_COUNT_W2V_SORT_BRAND = "VEC_W2V_SORT_BRAND";
  22 + public static final String RECALL_SKN_COUNT_SORT = "SORT";
  23 + public static final String RECALL_SKN_COUNT_BRAND = "BRAND";
29 24
30 } 25 }
1 package com.yoho.search.recall.config; 1 package com.yoho.search.recall.config;
2 2
3 import com.yoho.search.core.personalized.models.SortBrand; 3 import com.yoho.search.core.personalized.models.SortBrand;
  4 +import com.yoho.search.recall.beans.strategy.SortBrandType;
4 import com.yoho.search.recall.beans.strategy.StrategyEnum; 5 import com.yoho.search.recall.beans.strategy.StrategyEnum;
5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Service; 7 import org.springframework.stereotype.Service;
@@ -19,8 +20,8 @@ public class RecallConfigService { @@ -19,8 +20,8 @@ public class RecallConfigService {
19 * @param sortBrand 20 * @param sortBrand
20 * @return 21 * @return
21 */ 22 */
22 - public RecallSknCount queryRecallSknCount(int pageId, SortBrand sortBrand) {  
23 - return recallConfigServiceProduct.queryRecallSknCount(pageId, sortBrand); 23 + public RecallSknCount queryRecallSknCount(int pageId, SortBrandType sortBrandType,SortBrand sortBrand) {
  24 + return recallConfigServiceProduct.queryRecallSknCount(pageId,sortBrandType,sortBrand);
24 } 25 }
25 26
26 /** 27 /**
@@ -2,6 +2,7 @@ package com.yoho.search.recall.config; @@ -2,6 +2,7 @@ package com.yoho.search.recall.config;
2 2
3 import com.yoho.search.core.personalized.models.SortBrand; 3 import com.yoho.search.core.personalized.models.SortBrand;
4 import com.yoho.search.dal.model.CsRecallConfigProduct; 4 import com.yoho.search.dal.model.CsRecallConfigProduct;
  5 +import com.yoho.search.recall.beans.strategy.SortBrandType;
5 import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService; 6 import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService;
6 import org.slf4j.Logger; 7 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory; 8 import org.slf4j.LoggerFactory;
@@ -24,7 +25,7 @@ class RecallConfigServiceProduct { @@ -24,7 +25,7 @@ class RecallConfigServiceProduct {
24 @Autowired 25 @Autowired
25 private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService; 26 private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService;
26 27
27 - private Map<Integer, Map<Integer, RecallSknCount>> configCache = new HashMap<>(); 28 + private Map<String, Map<Integer, RecallSknCount>> typePageConfigCache = new HashMap<>();
28 29
29 private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor(); 30 private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
30 31
@@ -35,24 +36,24 @@ class RecallConfigServiceProduct { @@ -35,24 +36,24 @@ class RecallConfigServiceProduct {
35 36
36 private void loadRecallSknCountConfig() { 37 private void loadRecallSknCountConfig() {
37 try { 38 try {
38 - Map<Integer, Map<Integer, RecallSknCount>> tempConfig = new HashMap<>(); 39 + Map<String, Map<Integer, RecallSknCount>> tempTypePageConfigCache = new HashMap<>();
39 List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll(); 40 List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll();
40 for (CsRecallConfigProduct productConfig : configList) { 41 for (CsRecallConfigProduct productConfig : configList) {
41 - //1、按配置类型获取配置  
42 - int configType = productConfig.getConfigType();  
43 - Map<Integer, RecallSknCount> typeConfig = tempConfig.get(configType);  
44 - if (typeConfig == null) {  
45 - typeConfig = new HashMap<>();  
46 - tempConfig.put(configType, typeConfig);  
47 - }  
48 - //2、页面类型的配置,不判断状态,其他类型的需要判断状态  
49 - int configTypeId = productConfig.getConfigTypeId();  
50 - int configStatus = productConfig.getConfigStatus();  
51 - if (configType == RecallConfigConstants.SKN_COUNT_CONFIG_PAGE || configStatus == 1) {  
52 - typeConfig.put(configTypeId, this.genProductCountConfig(productConfig));  
53 - } 42 +// //1、按配置类型获取配置
  43 +// int configType = productConfig.getConfigType();
  44 +// Map<Integer, RecallSknCount> typeConfig = tempConfig.get(configType);
  45 +// if (typeConfig == null) {
  46 +// typeConfig = new HashMap<>();
  47 +// tempConfig.put(configType, typeConfig);
  48 +// }
  49 +// //2、页面类型的配置,不判断状态,其他类型的需要判断状态
  50 +// int configTypeId = productConfig.getConfigTypeId();
  51 +// int configStatus = productConfig.getConfigStatus();
  52 +// if (configType == RecallConfigConstants.SKN_COUNT_CONFIG_PAGE || configStatus == 1) {
  53 +// typeConfig.put(configTypeId, this.genProductCountConfig(productConfig));
  54 +// }
54 } 55 }
55 - configCache = tempConfig; 56 + typePageConfigCache = tempTypePageConfigCache;
56 } catch (Exception e) { 57 } catch (Exception e) {
57 logger.error(e.getMessage(), e); 58 logger.error(e.getMessage(), e);
58 } 59 }
@@ -89,33 +90,44 @@ class RecallConfigServiceProduct { @@ -89,33 +90,44 @@ class RecallConfigServiceProduct {
89 return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random); 90 return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);
90 } 91 }
91 92
92 - private RecallSknCount queryConfigBySort(int middleSortId) {  
93 - Map<Integer, RecallSknCount> sortConfig = configCache.get(RecallConfigConstants.SKN_COUNT_CONFIG_SORT);  
94 - if (sortConfig == null) {  
95 - return null;  
96 - }  
97 - return sortConfig.get(middleSortId); 93 + private String buildSortCacheKey(int middleSortId) {
  94 + return RecallConfigConstants.RECALL_SKN_COUNT_SORT + "_" + middleSortId;
98 } 95 }
99 96
100 - private RecallSknCount queryConfigByBrand(int brandId) {  
101 - Map<Integer, RecallSknCount> brandConfig = configCache.get(RecallConfigConstants.SKN_COUNT_CONFIG_BRAND);  
102 - if (brandConfig == null) {  
103 - return null;  
104 - }  
105 - return brandConfig.get(brandId); 97 + private String buildBrandCacheKey(int brandId) {
  98 + return RecallConfigConstants.RECALL_SKN_COUNT_BRAND + "_" + brandId;
106 } 99 }
107 100
108 - private RecallSknCount queryConfigByPage(int pageId) {  
109 - Map<Integer, RecallSknCount> pageConfig = configCache.get(RecallConfigConstants.SKN_COUNT_CONFIG_PAGE); 101 + private String buildSortBrandCacheKey(SortBrandType sortBrandType, SortBrand sortBrand) {
  102 + return sortBrandType.name() + "_" + sortBrand.key();
  103 + }
  104 +
  105 +
  106 + private RecallSknCount getRecallSknCount(String key, int pageId) {
  107 + Map<Integer, RecallSknCount> pageConfig = typePageConfigCache.get(key);
110 if (pageConfig == null) { 108 if (pageConfig == null) {
111 return null; 109 return null;
112 } 110 }
113 - RecallSknCount config = pageConfig.get(pageId);  
114 - if (config == null) {  
115 - return pageConfig.get(RecallConfigConstants.DEFAULT_PAGE_ID);  
116 - } else {  
117 - return config; 111 + RecallSknCount sknCount = pageConfig.get(pageId);
  112 + if (sknCount == null) {
  113 + sknCount = pageConfig.get(RecallConfigConstants.DEFAULT_PAGE_ID);
118 } 114 }
  115 + return sknCount;
  116 + }
  117 +
  118 + private RecallSknCount queryConfigBySort(int pageId, int middleSortId) {
  119 + String sortKey = this.buildSortCacheKey(middleSortId);
  120 + return this.getRecallSknCount(sortKey, pageId);
  121 + }
  122 +
  123 + private RecallSknCount queryConfigByBrand(int pageId, int brandId) {
  124 + String brandKey = this.buildBrandCacheKey(brandId);
  125 + return this.getRecallSknCount(brandKey, pageId);
  126 + }
  127 +
  128 + private RecallSknCount queryConfigByPage(int pageId, SortBrandType sortBrandType, SortBrand sortBrand) {
  129 + String sortBrandKey = this.buildSortBrandCacheKey(sortBrandType, sortBrand);
  130 + return this.getRecallSknCount(sortBrandKey, pageId);
119 } 131 }
120 132
121 /** 133 /**
@@ -125,9 +137,9 @@ class RecallConfigServiceProduct { @@ -125,9 +137,9 @@ class RecallConfigServiceProduct {
125 * @param sortBrand 137 * @param sortBrand
126 * @return 138 * @return
127 */ 139 */
128 - public RecallSknCount queryRecallSknCount(int pageId, SortBrand sortBrand) {  
129 - RecallSknCount sortConfig = this.queryConfigBySort(sortBrand.getMisort());  
130 - RecallSknCount brandConfig = this.queryConfigByBrand(sortBrand.getBrandId()); 140 + public RecallSknCount queryRecallSknCount(int pageId, SortBrandType sortBrandType, SortBrand sortBrand) {
  141 + RecallSknCount sortConfig = this.queryConfigBySort(pageId, sortBrand.getMisort());
  142 + RecallSknCount brandConfig = this.queryConfigByBrand(pageId, sortBrand.getBrandId());
131 if (sortConfig != null && sortConfig != null) { 143 if (sortConfig != null && sortConfig != null) {
132 return genProductCountConfigByCompare(sortConfig, brandConfig); 144 return genProductCountConfigByCompare(sortConfig, brandConfig);
133 } 145 }
@@ -137,7 +149,11 @@ class RecallConfigServiceProduct { @@ -137,7 +149,11 @@ class RecallConfigServiceProduct {
137 if (brandConfig != null) { 149 if (brandConfig != null) {
138 return brandConfig; 150 return brandConfig;
139 } 151 }
140 - RecallSknCount pageConfig = this.queryConfigByPage(pageId); 152 + RecallSknCount pageConfig = this.queryConfigByPage(pageId, sortBrandType, sortBrand);
  153 + if (pageConfig != null) {
  154 + return pageConfig;
  155 + }
  156 + pageConfig = this.queryConfigByPage(pageId, sortBrandType, new SortBrand());//使用默认的值替代
141 if (pageConfig != null) { 157 if (pageConfig != null) {
142 return pageConfig; 158 return pageConfig;
143 } 159 }