RedisGwCacheClient.java
6.71 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.yohoufo.common.cache;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.yoho.core.redis.cluster.annotation.Redis;
import com.yoho.core.redis.cluster.operations.nosync.YHHashOperations;
import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate;
import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
public class RedisGwCacheClient implements CacheClient {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisGwCacheClient.class);
@Redis("gwNoSyncRedis")
private YHRedisTemplate redis;
@Redis("gwNoSyncRedis")
private YHValueOperations valueOperations;
@Redis("gwNoSyncRedis")
private YHHashOperations hashOperations;
@Override
public void set(String key, int expireInSeconds, Object o) {
if (expireInSeconds > 120) {
expireInSeconds = expireInSeconds + getRandomNum(-20, 60);
}
try {
String compressStr= SnappyZipUtils.compress(JSON.toJSONString(o));
this.valueOperations.set(RedisKeyBuilder.newInstance().appendFixed(key), compressStr,
expireInSeconds, TimeUnit.SECONDS);
} catch (Exception e) {
LOGGER.warn("set into redis failed!! key is :{} ,e is :{}", key,e);
// do nothings
}
}
@Override
public void setAndSync(String key, int expireInSeconds, Object o) {
if (expireInSeconds > 120) {
expireInSeconds = expireInSeconds + getRandomNum(-20, 60);
}
try {
String compressStr= SnappyZipUtils.compress(JSON.toJSONString(o));
this.valueOperations.setAndAsyncClean(RedisKeyBuilder.newInstance().appendFixed(key), compressStr,
expireInSeconds, TimeUnit.SECONDS);
} catch (Exception e) {
LOGGER.warn("set into redis failed!! key is :{} ,e is :{}", key,e);
// do nothing
}
}
@Override
public <T> T get(String key, Class<T> clazz) {
try {
String value = this.valueOperations.get(RedisKeyBuilder.newInstance().appendFixed(key));
if (StringUtils.isNotEmpty(value)) {
String unCompressStr= SnappyZipUtils.uncompress(value);
return JSON.parseObject(unCompressStr, clazz);
} else {
LOGGER.debug("can not get data from redis by key {}.", key);
return null;
}
} catch (Exception e) {
LOGGER.warn("get from redis failed!! key is:{},e is :{}", key,e);
return null;
}
}
@Override
public <T> Map<String, T> getBulk(Collection<String> keys, Class<T> clazz) {
List<RedisKeyBuilder> redisKeyBuilders=convert(keys);
try {
List<String> multiGet =valueOperations.multiGet(redisKeyBuilders);
int size = multiGet.size();
Map<String, T> result = new HashMap<String, T>(size);
String curItem;
for (int i = 0; i < size; i++) {
curItem = multiGet.get(i);
if (null == curItem) {
result.put(redisKeyBuilders.get(i).getKey(), null);
} else {
result.put(redisKeyBuilders.get(i).getKey(), JSON.parseObject(curItem, clazz));
}
}
return result;
} catch (Exception e) {
LOGGER.warn("getBulk from redis failed!!", e);
return Maps.newHashMap();
}
}
@Override
public void delete(String key) {
try {
redis.delete(RedisKeyBuilder.newInstance().appendFixed(key));
} catch (Exception e) {
LOGGER.warn("delete from redis failed!! key is:{},e is :{}", key,e);
}
}
@Override
public void mset(Map<String, ? extends Object> map, long timeout) {
if (MapUtils.isEmpty(map)) {
return;
}
Map<RedisKeyBuilder, String> cacheMap = new HashMap<RedisKeyBuilder, String>(map.size());
try{
for (String key : map.keySet()) {
Object value = map.get(key);
RedisKeyBuilder keyBuilder = RedisKeyBuilder.newInstance().appendFixed(key);
if (value instanceof String) {
cacheMap.put(keyBuilder, (String) value);
} else if (value instanceof Integer) {
cacheMap.put(keyBuilder, value + "");
} else {
cacheMap.put(keyBuilder, JSON.toJSONString(value));
}
}
redis.mset(cacheMap, timeout);
}catch (Exception e){
LOGGER.warn("mset cache failed!!! e is: {}", e);
}
}
@Override
public <T> void hashPut(String key, String field, T value, long timeout) {
if(null == value){
return;
}
RedisKeyBuilder keyBuilder = RedisKeyBuilder.newInstance().appendFixed(key);
try{
String hashValue;
if (value instanceof String){
hashValue = (String)value;
}else{
hashValue = JSON.toJSONString(value);
}
hashOperations.put(keyBuilder, field, hashValue);
redis.longExpire(keyBuilder, timeout, TimeUnit.SECONDS);
}catch (Exception e){
LOGGER.warn("hashPut redis value operation failed. e is: {}", e);
}
}
@Override
public <T> T hashGet(String key, String field, Class<T> clazz){
String value = null;
try {
RedisKeyBuilder keyBuilder = RedisKeyBuilder.newInstance().appendFixed(key);
value = hashOperations.get(keyBuilder, field);
return getValue(value, clazz);
}catch (Exception e){
LOGGER.warn("hashGet redis value operation failed. e is: {}", e);
}
return null;
}
@Override
public boolean setNx(String key, int expireInSeconds, Object o) {
try {
RedisKeyBuilder keyB = RedisKeyBuilder.newInstance().appendFixed(key);
String compressStr = SnappyZipUtils.compress(JSON.toJSONString(o));
Boolean resu = this.valueOperations.setIfAbsent(keyB, compressStr);
if (resu != null && resu) {
redis.expire(keyB, expireInSeconds, TimeUnit.SECONDS);
return true;
}
return false;
} catch (Exception e) {
LOGGER.warn("setNx into redis failed!! key is :{} ,e is :{}", key, e);
return false;
}
}
/**
* 获取正负区间的一个随机数
*
* @param smallistNum
* @param biggestNum
* @return
*/
private int getRandomNum(int smallistNum, int biggestNum) {
Random random = new Random();
return (Math.abs(random.nextInt()) % (biggestNum - smallistNum + 1)) + smallistNum;
}
private List<RedisKeyBuilder> convert(Collection<String> keys) {
return keys.stream().map(key -> RedisKeyBuilder.newInstance().appendFixed(key)).collect(Collectors.toList());
}
@SuppressWarnings("unchecked")
private <T> T getValue(String value, Class<T> clazz) {
T t = null;
if (value == null){
return (T) null;
}
if (clazz.equals(String.class)) {
t = (T) value;
} else if (clazz.equals(Integer.class)) {
t = (T) Integer.valueOf(value);
} else {
try{
t = JSON.parseObject(value, clazz);
}catch (Exception e) {
LOGGER.warn("get value cache failed!!! value is: "+ value,e);
}
}
return t;
}
}