1
|
package com.yoho.search.cache;
|
1
|
package com.yoho.search.cache;
|
2
|
|
2
|
|
|
|
3
|
+import java.util.List;
|
3
|
import java.util.Map;
|
4
|
import java.util.Map;
|
|
|
5
|
+import java.util.Set;
|
4
|
import java.util.concurrent.ConcurrentHashMap;
|
6
|
import java.util.concurrent.ConcurrentHashMap;
|
5
|
import java.util.concurrent.Executors;
|
7
|
import java.util.concurrent.Executors;
|
6
|
import java.util.concurrent.ScheduledExecutorService;
|
8
|
import java.util.concurrent.ScheduledExecutorService;
|
|
@@ -14,6 +16,7 @@ import org.slf4j.LoggerFactory; |
|
@@ -14,6 +16,7 @@ import org.slf4j.LoggerFactory; |
14
|
import org.springframework.stereotype.Service;
|
16
|
import org.springframework.stereotype.Service;
|
15
|
|
17
|
|
16
|
import com.yoho.search.dal.model.SearchParam;
|
18
|
import com.yoho.search.dal.model.SearchParam;
|
|
|
19
|
+import com.yoho.search.dal.model.SearchResult;
|
17
|
import com.yoho.search.utils.MD5Util;
|
20
|
import com.yoho.search.utils.MD5Util;
|
18
|
import com.yoho.search.utils.SearchParamUtils;
|
21
|
import com.yoho.search.utils.SearchParamUtils;
|
19
|
|
22
|
|
|
@@ -26,9 +29,9 @@ public class CacheService { |
|
@@ -26,9 +29,9 @@ public class CacheService { |
26
|
private CacheCount cacheCount = new CacheCount();
|
29
|
private CacheCount cacheCount = new CacheCount();
|
27
|
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
|
30
|
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
|
28
|
|
31
|
|
29
|
- private static final int cacheTime = 3;//结果缓存的时间
|
|
|
30
|
- private static final int logPeriod = 5;//记录缓存命中的时间
|
|
|
31
|
- private static final int clearCachePeriod = 15;//清除整个缓存对象的时间
|
32
|
+ private static final int cacheTimeInMinute = 3;//结果缓存的时间
|
|
|
33
|
+ private static final int logPeriodInMinute = 3;//记录缓存命中的时间
|
|
|
34
|
+ private static final int clearCachePeriodInMinute = 15;//清除整个缓存对象的时间
|
32
|
|
35
|
|
33
|
@PostConstruct
|
36
|
@PostConstruct
|
34
|
void init(){
|
37
|
void init(){
|
|
@@ -36,10 +39,10 @@ public class CacheService { |
|
@@ -36,10 +39,10 @@ public class CacheService { |
36
|
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
|
39
|
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
|
37
|
@Override
|
40
|
@Override
|
38
|
public void run() {
|
41
|
public void run() {
|
39
|
- logger.info("do log cache matchPercent,logPeriod is [{}],[{}]",logPeriod,cacheCount);
|
42
|
+ logger.info("do log cache matchPercent,logPeriodInMinute is [{}],[{}]",logPeriodInMinute,cacheCount);
|
40
|
cacheCount.clear();
|
43
|
cacheCount.clear();
|
41
|
}
|
44
|
}
|
42
|
- }, logPeriod, logPeriod, TimeUnit.MINUTES);
|
45
|
+ }, logPeriodInMinute, logPeriodInMinute, TimeUnit.MINUTES);
|
43
|
|
46
|
|
44
|
//每15分钟清除缓存对象,防止内存爆掉
|
47
|
//每15分钟清除缓存对象,防止内存爆掉
|
45
|
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
|
48
|
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
|
|
@@ -48,7 +51,7 @@ public class CacheService { |
|
@@ -48,7 +51,7 @@ public class CacheService { |
48
|
logger.info("do clear cacheObject...");
|
51
|
logger.info("do clear cacheObject...");
|
49
|
cache = new ConcurrentHashMap<String,CacheObject>();
|
52
|
cache = new ConcurrentHashMap<String,CacheObject>();
|
50
|
}
|
53
|
}
|
51
|
- }, clearCachePeriod, clearCachePeriod, TimeUnit.MINUTES);
|
54
|
+ }, clearCachePeriodInMinute, clearCachePeriodInMinute, TimeUnit.MINUTES);
|
52
|
}
|
55
|
}
|
53
|
|
56
|
|
54
|
private Object getObjectFromCache(String key){
|
57
|
private Object getObjectFromCache(String key){
|
|
@@ -65,38 +68,59 @@ public class CacheService { |
|
@@ -65,38 +68,59 @@ public class CacheService { |
65
|
return cacheObject.getObject();
|
68
|
return cacheObject.getObject();
|
66
|
}
|
69
|
}
|
67
|
|
70
|
|
68
|
- private void addObjectToCache(String key,Object object,long expireInSecond){
|
|
|
69
|
- CacheObject cacheObject = new CacheObject(object, expireInSecond);
|
71
|
+ private void addObjectToCache(String key,Object object){
|
|
|
72
|
+ CacheObject cacheObject = new CacheObject(object, cacheTimeInMinute * 60);
|
70
|
cache.put(key, cacheObject);
|
73
|
cache.put(key, cacheObject);
|
71
|
}
|
74
|
}
|
72
|
-
|
|
|
73
|
- /**
|
|
|
74
|
- * 根据SearchParam生成的ES报文
|
|
|
75
|
- * @param searchParam
|
|
|
76
|
- * @return
|
|
|
77
|
- */
|
|
|
78
|
- private String genSearchParamString(String indexName,SearchParam searchParam){
|
75
|
+
|
|
|
76
|
+ /***********************************for SearchParam and SearchResult*****************************************/
|
|
|
77
|
+ private String genSearchParamString(String indexName, SearchParam searchParam) {
|
79
|
StringBuilder sb = new StringBuilder();
|
78
|
StringBuilder sb = new StringBuilder();
|
80
|
- //拼装索引名称
|
79
|
+ // 拼装索引名称
|
81
|
sb.append("indexName:").append(indexName).append(";");
|
80
|
sb.append("indexName:").append(indexName).append(";");
|
82
|
- //拼装搜索类型
|
|
|
83
|
- sb.append("searchType:").append(searchParam.getSearchType()==null?"":searchParam.getSearchType().name()).append(";");
|
|
|
84
|
- //拼装报文
|
81
|
+ // 拼装搜索类型
|
|
|
82
|
+ sb.append("searchType:").append(searchParam.getSearchType() == null ? "" : searchParam.getSearchType().name()).append(";");
|
|
|
83
|
+ // 拼装报文
|
85
|
SearchSourceBuilder searchSourceBuilder = SearchParamUtils.genSearchSourceBuilderFromSearchParam(searchParam);
|
84
|
SearchSourceBuilder searchSourceBuilder = SearchParamUtils.genSearchSourceBuilderFromSearchParam(searchParam);
|
86
|
sb.append("searchSource:").append(searchSourceBuilder.toString()).append(";");
|
85
|
sb.append("searchSource:").append(searchSourceBuilder.toString()).append(";");
|
87
|
- //打印拼装结果
|
|
|
88
|
- //logger.info("cacheKey is [{}]",sb.toString());
|
86
|
+ // 打印拼装结果
|
|
|
87
|
+ // logger.info("cacheKey is [{}]",sb.toString());
|
|
|
88
|
+ return MD5Util.string2MD5(sb.toString());
|
|
|
89
|
+ }
|
|
|
90
|
+
|
|
|
91
|
+ public SearchResult getSearchResultFromCache(String indexName, SearchParam searchParam) {
|
|
|
92
|
+ String key = this.genSearchParamString(indexName, searchParam);
|
|
|
93
|
+ return (SearchResult) this.getObjectFromCache(key);
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ public void addSearchResultToCache(String indexName, SearchParam searchParam, SearchResult result) {
|
|
|
97
|
+ String key = this.genSearchParamString(indexName, searchParam);
|
|
|
98
|
+ this.addObjectToCache(key, result);
|
|
|
99
|
+ }
|
|
|
100
|
+
|
|
|
101
|
+ /***********************************for multiGet*****************************************/
|
|
|
102
|
+ private String genMultiGetParamString(String indexName, Set<String> idList, List<String> fields) {
|
|
|
103
|
+ StringBuilder sb = new StringBuilder();
|
|
|
104
|
+ // 拼装索引名称
|
|
|
105
|
+ sb.append("indexName:").append(indexName).append(";");
|
|
|
106
|
+ // 拼装搜索类型
|
|
|
107
|
+ sb.append("idList:").append(idList.toString()).append(";");
|
|
|
108
|
+ // 拼装报文
|
|
|
109
|
+ sb.append("fields:").append(fields==null?"":fields.toString()).append(";");
|
|
|
110
|
+ // 打印拼装结果
|
|
|
111
|
+ // logger.info("cacheKey is [{}]",sb.toString());
|
89
|
return MD5Util.string2MD5(sb.toString());
|
112
|
return MD5Util.string2MD5(sb.toString());
|
90
|
}
|
113
|
}
|
91
|
|
114
|
|
92
|
- public Object getObjectFromCache(String indexName,SearchParam searchParam){
|
|
|
93
|
- String key = this.genSearchParamString(indexName,searchParam);
|
|
|
94
|
- return this.getObjectFromCache(key);
|
115
|
+ @SuppressWarnings("unchecked")
|
|
|
116
|
+ public List<Map<String, Object>> getMultiGetResultFromCache(String indexName, Set<String> idList, List<String> fields) {
|
|
|
117
|
+ String key = this.genMultiGetParamString(indexName, idList, fields);
|
|
|
118
|
+ return (List<Map<String, Object>>) this.getObjectFromCache(key);
|
95
|
}
|
119
|
}
|
96
|
|
120
|
|
97
|
- public void addObjectToCache(String indexName,SearchParam searchParam,Object object){
|
|
|
98
|
- String key = this.genSearchParamString(indexName,searchParam);
|
|
|
99
|
- this.addObjectToCache(key, object, cacheTime);
|
121
|
+ public void addMultiGetResultResultToCache(String indexName, Set<String> idList, List<String> fields, List<Map<String, Object>> result) {
|
|
|
122
|
+ String key = this.genMultiGetParamString(indexName, idList, fields);
|
|
|
123
|
+ this.addObjectToCache(key, result);
|
100
|
}
|
124
|
}
|
101
|
|
125
|
|
102
|
} |
126
|
} |