AbstractCacheComponent.java
3.65 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
package com.yoho.search.cache.beans;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
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.service.recall.models.common.ParamQueryFilter;
import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
public abstract class AbstractCacheComponent<T> {
@Autowired
private EhCache ehCache;
@Autowired
private SearchRedis searchRedis;
public T queryWithCache(ParamQueryFilter paramQueryFilter) {
//1、生成RedisKeyBuilder
RedisKeyBuilder redisKeyBuilder = this.genRedisKeyBuilder(paramQueryFilter);
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 && useEhcache()) {
this.addValueToEhcache(redisKeyBuilder, result);
return result;
}
return this.doInnerQuery(redisKeyBuilder, paramQueryFilter);
}
private T doInnerQuery(RedisKeyBuilder redisKeyBuilder, ParamQueryFilter paramQueryFilter) {
T result = this.doRealQuery(paramQueryFilter);
if (result == null) {
return result;
}
if (useEhcache()) {
this.addValueToEhcache(redisKeyBuilder, result);
}
if (useRedis()) {
this.addValueToRedis(redisKeyBuilder, result);
}
return result;
}
private T getValueFromEhcache(RedisKeyBuilder redisKeyBuilder) {
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) {
ehCache.addOrUpdate(redisKeyBuilder, new CacheObject(result), this.cacheTimeInMinute());
}
protected T getValueFromRedis(RedisKeyBuilder redisKeyBuilder) {
if(!useRedis()){
return null;
}
CacheObject cacheObject = searchRedis.get(redisKeyBuilder);
if (cacheObject == null) {
return null;
}
String redisValue = (String)cacheObject.toObject();
Type superClass = getClass().getGenericSuperclass();
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
return JSON.parseObject(redisValue, type);
}
protected void addValueToRedis(RedisKeyBuilder redisKeyBuilder, T result) {
String jsonString = JSON.toJSONString(result);
CacheObject toCacheResult = new CacheObject(jsonString);
searchRedis.addOrUpdate(redisKeyBuilder, toCacheResult, this.cacheTimeInMinute());
}
protected abstract RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter);
protected abstract int cacheTimeInMinute();
protected abstract T doRealQuery(ParamQueryFilter paramQueryFilter);
protected boolean useEhcache() {
return false;
}
protected boolean useRedis() {
return true;
}
}