RecallConfigCommonService.java
4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.yoho.search.recall.config;
import com.yoho.search.dal.model.CsRecallConfigCommon;
import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
class RecallConfigCommonService {
private static final Logger RECALL_NEW_LOGGER = LoggerFactory.getLogger("RECALL");
@Autowired
private CsRecallConfigCommonIndexBaseService csRecallConfigCommonIndexBaseService;
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
private Map<String, Map<Integer, ConfigSizeInterval>> recallConfigCommonCache = new HashMap<>();
@PostConstruct
void init() {
schedule.scheduleAtFixedRate(() -> loadRecallConfigCommonCache(), 0, 1, TimeUnit.MINUTES);
}
private void loadRecallConfigCommonCache() {
try {
List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll();
Map<String, Map<Integer, ConfigSizeInterval>> tempCache = new HashMap<>();
for (CsRecallConfigCommon csRecallConfigCommon : configList) {
//1、类型判断
String configKey = csRecallConfigCommon.getConfigType();
if (!tempCache.containsKey(configKey)) {
tempCache.put(configKey, new HashMap<>());
}
//2、生成页面结果
int pageId = csRecallConfigCommon.getConfigPage();
int size = csRecallConfigCommon.getSize();
int interval = csRecallConfigCommon.getInterval();
ConfigSizeInterval configSizeInterval = new ConfigSizeInterval(size, interval);
tempCache.get(configKey).put(pageId, configSizeInterval);
}
recallConfigCommonCache = tempCache;
RECALL_NEW_LOGGER.info("loadRecallConfigCommonCache success,recallConfigCommonCache size is[{}]", recallConfigCommonCache.size());
} catch (Exception e) {
RECALL_NEW_LOGGER.error("loadRecallConfigCommonCache error,exception is:" + e.getMessage(), e);
}
}
private ConfigSizeInterval queryCommonConfig(String configKey, int configPage) {
Map<Integer, ConfigSizeInterval> pageConfigMap = recallConfigCommonCache.get(configKey);
if (pageConfigMap == null) {
return null;
}
ConfigSizeInterval pageConfig = pageConfigMap.get(configPage);
if (pageConfig == null) {
pageConfig = pageConfigMap.get(RecallConfigConstants.DEFAULT_PAGE_ID);
}
if (pageConfigMap == null) {
return null;
} else {
return pageConfig;
}
}
/**
* 获取配置的大小
*
* @param pageId
* @param configKey
* @param defaultSize
* @return
*/
public int queryConfigSize(int pageId, String configKey, int defaultSize) {
if (StringUtils.isBlank(configKey)) {
return defaultSize;
}
ConfigSizeInterval config = queryCommonConfig(configKey, pageId);
return config == null ? defaultSize : config.getSize();
}
/**
* 获取配置的间隔
*
* @param pageId
* @param configKey
* @param defaultInterval
* @return
*/
public int queryConfigInterval(int pageId, String configKey, int defaultInterval) {
if (StringUtils.isBlank(configKey)) {
return defaultInterval;
}
ConfigSizeInterval config = queryCommonConfig(configKey, pageId);
return config == null ? defaultInterval : config.getInterval();
}
}