SearchCacheService.java
7.95 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.yoho.search.cache.beans;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.cache.model.SearchCache;
import com.yoho.search.common.SearchServiceConfiger;
import com.yoho.search.cache.model.CacheObject;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.utils.SearchParamUtils;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.List;
@Service
public class SearchCacheService {
@Autowired
private SearchServiceConfiger searchServiceConfiger;
private void addObjectToCache(RedisKeyBuilder redisKeyBuilder, Object object, SearchCache searchCache) {
// 1、如果不适用缓存,则直接返回
if (!searchServiceConfiger.useCache()) {
return;
}
// 2、如果缓存不存在,则直接返回
if (searchCache == null || searchCache.getCache() == null) {
return;
}
// 3、加入缓存
CacheObject cacheObject = null;
if (object instanceof JSONObject) {
cacheObject = new CacheObject((JSONObject) object);
} else if (object instanceof JSONArray) {
cacheObject = new CacheObject((JSONArray) object);
} else {
cacheObject = new CacheObject(object);
}
searchCache.getCache().addOrUpdate(redisKeyBuilder, cacheObject, searchCache.getCacheInMinute());
}
/**
* 从缓存中取数据
*
* @param redisKeyBuilder
* @return
*/
private CacheObject getCacheObjectFromCache(RedisKeyBuilder redisKeyBuilder, SearchCache searchCache) {
// 1、如果不适用缓存,则直接返回
if (!searchServiceConfiger.useCache()) {
return null;
}
// 2、如果缓存不存在,则直接返回
if (searchCache == null || searchCache.getCache() == null) {
return null;
}
// 3、增加缓存访问次数
searchCache.incTotalCount();
// 4、从缓存中获取cacheObject
CacheObject cacheObject = searchCache.getCache().get(redisKeyBuilder);
// 5、如果从缓存中拿不到数据,则直接返回
if (cacheObject == null) {
return null;
}
// 6、增加缓存命中次数,并返回缓存对象
searchCache.incMatchCount();
return cacheObject;
}
/*********************************** JSONObject *****************************************/
public RedisKeyBuilder genSearchParamString(String indexName, SearchParam searchParam) {
//1、拼装内容
StringBuilder redisKeyValue = new StringBuilder();
redisKeyValue.append("indexName:").append(indexName).append(';');
redisKeyValue.append("searchType:").append(searchParam.getSearchType() == null ? "" : searchParam.getSearchType().name()).append(';');
SearchSourceBuilder searchSourceBuilder = SearchParamUtils.genSearchSourceBuilderFromSearchParam(searchParam);
redisKeyValue.append("searchSource:").append(searchSourceBuilder).append(';');
//2、构建RedisKeyBuilder
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
redisKeyBuilder.appendFixed("YOHOSEARCH:");
redisKeyBuilder.appendFixed("DEFAULT1:");
redisKeyBuilder.appendVar(MD5Util.string2MD5(redisKeyValue.toString()));
return redisKeyBuilder;
}
private RedisKeyBuilder genSearchParamString(String indexName, List<SearchParam> searchParams) {
//1、拼装内容
StringBuilder redisKeyValue = new StringBuilder();
// 拼装索引名称
redisKeyValue.append("indexName:").append(indexName).append(';');
for (SearchParam searchParam : searchParams) {
// 拼装搜索类型
redisKeyValue.append("searchType:").append(searchParam.getSearchType() == null ? "" : searchParam.getSearchType().name()).append(';');
// 拼装报文
SearchSourceBuilder searchSourceBuilder = SearchParamUtils.genSearchSourceBuilderFromSearchParam(searchParam);
redisKeyValue.append("searchSource:").append(searchSourceBuilder).append(';');
}
//2、构建RedisKeyBuilder
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
redisKeyBuilder.appendFixed("YOHOSEARCH:");
redisKeyBuilder.appendFixed("DEFAULT2:");
redisKeyBuilder.appendVar(MD5Util.string2MD5(redisKeyValue.toString()));
return redisKeyBuilder;
}
public void addJSONObjectToCache(SearchCache searchCache, RedisKeyBuilder redisKeyBuilder, JSONObject jsonObject) {
this.addObjectToCache(redisKeyBuilder, jsonObject, searchCache);
}
public JSONObject getJSONObjectFromCache(SearchCache searchCache, RedisKeyBuilder redisKeyBuilder) {
CacheObject cacheObject = this.getCacheObjectFromCache(redisKeyBuilder, searchCache);
if (cacheObject == null) {
return null;
}
return cacheObject.toJSONObject();
}
public void addJSONObjectToCache(SearchCache searchCache, String indexName, SearchParam searchParam, JSONObject jsonObject) {
RedisKeyBuilder key = this.genSearchParamString(indexName, searchParam);
this.addObjectToCache(key, jsonObject, searchCache);
}
public JSONObject getJSONObjectFromCache(SearchCache searchCache, String indexName, SearchParam searchParam) {
RedisKeyBuilder key = this.genSearchParamString(indexName, searchParam);
CacheObject cacheObject = this.getCacheObjectFromCache(key, searchCache);
if (cacheObject == null) {
return null;
}
return cacheObject.toJSONObject();
}
public void addJSONObjectToCache(SearchCache searchCache, String indexName, List<SearchParam> searchParams, JSONObject jsonObject) {
RedisKeyBuilder key = this.genSearchParamString(indexName, searchParams);
this.addObjectToCache(key, jsonObject, searchCache);
}
public JSONObject getJSONObjectFromCache(SearchCache searchCache, String indexName, List<SearchParam> searchParams) {
RedisKeyBuilder key = this.genSearchParamString(indexName, searchParams);
CacheObject cacheObject = this.getCacheObjectFromCache(key, searchCache);
if (cacheObject == null) {
return null;
}
return cacheObject.toJSONObject();
}
/*********************************** JSONArray *****************************************/
public void addJSONArrayToCache(SearchCache searchCache, String indexName, SearchParam searchParam, JSONArray jsonObject) {
RedisKeyBuilder key = this.genSearchParamString(indexName, searchParam);
this.addObjectToCache(key, jsonObject, searchCache);
}
public JSONArray getJSONArrayFromCache(SearchCache searchCache, String indexName, SearchParam searchParam) {
RedisKeyBuilder key = this.genSearchParamString(indexName, searchParam);
CacheObject cacheObject = this.getCacheObjectFromCache(key, searchCache);
if (cacheObject == null) {
return null;
}
return cacheObject.toJSONArray();
}
/*********************************** SearchApiResult *****************************************/
@SuppressWarnings("unchecked")
public <T extends Serializable> T getSerializableObjectFromCache(SearchCache searchCache, RedisKeyBuilder redisKeyBuilder, Class<T> clazz, boolean useJsonSerializable) {
try {
CacheObject cacheObject = this.getCacheObjectFromCache(redisKeyBuilder, searchCache);
if (cacheObject == null) {
return null;
}
if (useJsonSerializable) {
JSONObject searchApiResultJSONObject = cacheObject.toJSONObject();
return JSON.toJavaObject(searchApiResultJSONObject, clazz);
} else {
return (T) cacheObject.getValue();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public <T extends Serializable> void addSerializableObjectToCache(SearchCache searchCache, RedisKeyBuilder redisKeyBuilder, T object, boolean useJsonSerializable) {
if (useJsonSerializable) {
JSONObject jsonObject = (JSONObject) JSON.toJSON(object);
this.addObjectToCache(redisKeyBuilder, jsonObject, searchCache);
} else {
this.addObjectToCache(redisKeyBuilder, object, searchCache);
}
}
}