AbsBaseRpcService.java
3.41 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
package cn.fw.hestia.rpc;
import cn.fw.hestia.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
/**
* @author : kurisu
* @className : AbsBaseRpcService
* @description : 公共方法
* @date: 2020-12-17 14:13
*/
@Slf4j
public abstract class AbsBaseRpcService {
/**
* Redis工具
*/
@Autowired
protected StringRedisTemplate redisTemplate;
/**
* 缓存KEY前缀
*
* @return
*/
protected abstract String getKeyPrefix();
/**
* 从缓存获取对象
*
* @param key
* @param clazz
* @param <E> 获取的对象
* @return
*/
@Nullable
protected <E> E getFromCache(@NonNull final String key, Class<E> clazz) {
String cache = getFromCache(key);
if (StringUtils.isEmpty(cache)) {
return null;
}
return JSON.parseObject(cache, clazz);
}
protected String getFromCache(@NonNull final String key) {
String json = null;
try {
BoundValueOperations<String, String> ops = redisTemplate.boundValueOps(key);
json = ops.get();
} catch (Exception e) {
log.error("从缓存获信息失败[{}]", key, e);
}
return json;
}
protected void setToCache(@NonNull final String key, @NonNull final String value) {
setToCache(key, value, 30);
}
/**
* 缓存信息
* @param key
* @param value
* @param timeout 缓存时间(秒)
*/
protected void setToCache(@NonNull final String key, @NonNull final String value, long timeout) {
try {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("缓存信息失败[{}][{}]", key, value, e);
}
}
protected <E> List<E> getListFromCache(final String key, Class<E> clazz) {
try {
BoundListOperations<String, String> queue = getQueue(key);
final long size = Optional.ofNullable(queue.size()).orElse(0L);
if (size > 0) {
final List<E> dtos = new ArrayList<>();
for (long i = 0; i < size; i++) {
final String json = Objects.requireNonNull(queue.index(i));
dtos.add(JSONObject.parseObject(json, clazz));
}
return dtos;
}
} catch (Exception e) {
log.error("从缓存获取信息失败[{}]", key, e);
}
return null;
}
protected void setListToCache(@NonNull final String key, @NonNull final String value) {
try {
getQueue(key).rightPush(value);
} catch (Exception e) {
log.error("缓存息失败[{}][{}]", key, value, e);
}
}
protected BoundListOperations<String, String> getQueue(@NonNull final String key) {
return redisTemplate.boundListOps(key);
}
}