AbstractCacheComponent.java
5.05 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
package com.yoho.search.cache.beans;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.cache.impls.EhCache;
import com.yoho.search.cache.impls.SearchRedis;
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.common.Nullable;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public abstract class AbstractCacheComponent<T> {
@Autowired
private EhCache ehCache;
@Autowired
private SearchRedis searchRedis;
public T queryWithCache(SearchParam searchParam, @Nullable Object... params) throws Exception {
//1、生成RedisKeyBuilder
RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(searchParam, params);
if (redisKeyBuilder == null) {
return null;
}
//2、取本地缓存,缓存命中,则直接返回
T result = this.getValueFromEhcache(redisKeyBuilder);
if (result != null) {
return result;
}
//3、取redis缓存,缓存命中,则回写ehcahce
result = this.getValueFromRedis(redisKeyBuilder);
if (result != null) {
this.addValueToEhcache(redisKeyBuilder, result);
return result;
}
return this.doInnerQuery(redisKeyBuilder, searchParam, params);
}
private T doInnerQuery(RedisKeyBuilder redisKeyBuilder, SearchParam searchParam, @Nullable Object... params) throws Exception {
T result = this.doRealQuery(searchParam, params);
if (result == null) {
return result;
}
this.addValueToEhcache(redisKeyBuilder, result);
this.addValueToRedis(redisKeyBuilder, result);
return result;
}
protected abstract T doRealQuery(SearchParam searchParam, @Nullable Object... params) throws Exception;
private T getValueFromEhcache(RedisKeyBuilder redisKeyBuilder) throws Exception {
if (!useEhcache()) {
return null;
}
CacheObject cacheObject = ehCache.get(redisKeyBuilder);
if (cacheObject != null) {
Object object = cacheObject.toObject();
return (T) object;
}
return null;
}
private void addValueToEhcache(RedisKeyBuilder redisKeyBuilder, T result) {
if (!useEhcache()) {
return;
}
ehCache.addOrUpdate(redisKeyBuilder, new CacheObject(result), this.cacheTimeInMinute());
}
private void addValueToRedis(RedisKeyBuilder redisKeyBuilder, T result) {
if (!useRedis()) {
return;
}
String jsonString = JSON.toJSONString(result, SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect);
CacheObject toCacheResult = new CacheObject(jsonString);
searchRedis.addOrUpdate(redisKeyBuilder, toCacheResult, this.cacheTimeInMinute());
}
private T getValueFromRedis(RedisKeyBuilder redisKeyBuilder) {
if (!useRedis()) {
return null;
}
CacheObject cacheObject = searchRedis.get(redisKeyBuilder);
if (cacheObject == null || cacheObject.getValue() == null) {
return null;
}
if (!(cacheObject.getValue() instanceof String)) {
return null;
}
String redisValue = (String) cacheObject.toObject();
Type superClass = getClass().getGenericSuperclass();
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
return JSON.parseObject(redisValue, type);
}
protected abstract int cacheTimeInMinute();
protected abstract String cacheSceneKey();
protected RedisKeyBuilder defaultRedisKeyBuilder() {
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
redisKeyBuilder.appendFixed("YOHOSEARCH");//前缀
redisKeyBuilder.appendFixed(":").appendFixed(cacheSceneKey());//场景
redisKeyBuilder.appendFixed(":").appendFixed(cacheTimeInMinute());//缓存时间
return redisKeyBuilder;
}
protected RedisKeyBuilder genRedisKeyBuilder(SearchParam searchParam, @Nullable Object... params) {
RedisKeyBuilder redisKeyBuilder = defaultRedisKeyBuilder();
if (searchParam != null) {
SearchSourceBuilder searchSourceBuilder = SearchParamUtils.genSearchSourceBuilderFromSearchParam(searchParam);
redisKeyBuilder.appendFixed(":").appendVar(MD5Util.string2MD5(searchSourceBuilder.toString()));//报文
}
return redisKeyBuilder;
}
protected boolean useEhcache() {
return false;
}
protected boolean useRedis() {
return true;
}
}