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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package cn.wisenergy.common.utils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* redis工具 类
*/
@Component
@EnableCaching
public class RedisUtils {
static Logger log = LoggerFactory.getLogger(RedisUtils.class);
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 存储key-value
* @param key
* @param value
* @throws Exception
*/
public void putValue(String key, String value) {
try {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
valueOps.set(key, value);
log.info("[REDIS] put k-v key=%s, value=%s", key, value);
} catch (Exception e) {
log.error("[REDIS-ERROR] put k-v key=%s, value=%s", key, value);
}
}
/**
* 存储key-value
*
* @param key
* @param value
* @param timeout 单位为毫秒
* @throws Exception
*/
public void putValue(String key, String value, long timeout) {
try {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
valueOps.set(key, value, timeout, TimeUnit.SECONDS);
log.info("[REDIS] put k-v key=%s, value=%s, timeout=%s", key, value, timeout);
} catch (Exception e) {
log.info("[REDIS--ERROR] put k-v key=%s, value=%s, timeout=%s", key, value, timeout);
}
}
/**
* 批量存储key-value
*
* @param values
* @throws Exception
*/
public void putValue(Map<String, String> values) {
try {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
valueOps.multiSet(values);
log.info("[REDIS] put map Map=%s", values);
} catch (Exception e) {
log.error("[REDIS--ERROR] put map Map=%s", values);
}
}
/**
* 获取value
*
* @param key
* @return 不存在时,返回null
* @throws Exception
*/
public String getValue(String key) {
String result = null;
try {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
result = valueOps.get(key);
log.info("[REDIS] Get k-v key={}, value={}", key, result);
} catch (Exception e) {
log.error("[REDIS--ERROR]-->getValue(String key)错误:{}", e);
}
return result;
}
/**
* 批量获取值
*
* @param keys
* @return
* @throws Exception
*/
public Map<String, String> multiGetValue(List<String> keys) {
Map<String, String> results = null;
try {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
List<String> values = valueOps.multiGet(keys);
results = new HashMap<>();
if (values != null && !keys.isEmpty()) {
for (int i = 0; i < keys.size(); i++) {
String fieldName = keys.get(i);
String fieldValue = values.get(i);
// 如果对应的字段为null,则不需要放入
if (!StringUtil.isBlank(fieldValue)) {
results.put(fieldName, fieldValue);
}
}
}
log.info("[REDIS] Get Map map=%s", results);
} catch (Exception e) {
log.error("[REDIS--ERROR] Get Map map=%s", results);
}
return results;
}
/**
* 批量删除key
*
* @param keys
* @throws Exception
*/
public void delete(Collection<String> keys) {
try {
redisTemplate.delete(keys);
log.info("[REDIS] Delete keys=%s", keys);
} catch (Exception e) {
log.error("[REDIS] Delete keys=%s", keys);
}
}
/**
* 删除key
*
* @param key
* @throws Exception
*/
public void delete(String key) {
try {
redisTemplate.delete(key);
log.info("[REDIS] Delete key=%s", key);
} catch (Exception e) {
log.error("[REDIS] Delete key=%s", key);
}
}
/**
* 更新hash里面的属性字段
*
* @param key 键
* @param hashKey 属性名称
* @param value 属性值
* @throws Exception
*/
public void putHash(String key, String hashKey, String value) {
try {
HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
hashOps.put(key, hashKey, value);
log.info("[REDIS] Put hash key=%s,hashKey=%s,value=%s", key, hashKey, value);
} catch (Exception e) {
log.error("[REDIS] Put hash key=%s,hashKey=%s,value=%s", key, hashKey, value);
}
}
/**
* 批量更新hash里面的属性字段
*
* @param key
* @param hashProperties 属性集合
* @throws Exception
*/
public void putHash(String key, Map<String, String> hashProperties) {
try {
HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
hashOps.putAll(key, hashProperties);
log.info("[REDIS] Put hash key=%s,hashKey-value=%s", key, hashProperties);
} catch (Exception e) {
log.error("[REDIS] Put hash key=%s,hashKey-value=%s", key, hashProperties);
}
}
/**
* 获取hash的字段值
*
* @param key
* @param hashKey
* @throws Exception
*/
public String getHash(String key, String hashKey) {
String result = null;
try {
HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
result = hashOps.get(key, hashKey);
log.info("[REDIS] Get hash key=%s,hashKey=%s,value=%s", key, hashKey, result);
return StringUtil.isBlank(result) ? null : result;
} catch (Exception e) {
log.error("[REDIS] Get hash key=%s,hashKey=%s,value=%s", key, hashKey, result);
}
return result;
}
/**
* 批量获取hash的字段值
*
* @param key
* @param hashKeys
* @return filed的Map集合, filedName-filedValue
* @throws Exception
*/
public Map<String, String> multiGetHash(String key, List<String> hashKeys) {
HashMap<String, String> results = null;
try {
HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
List<String> values = hashOps.multiGet(key, hashKeys);
results = new HashMap<String, String>();
if (values != null && !values.isEmpty()) {
for (int i = 0; i < hashKeys.size(); i++) {
String fieldName = hashKeys.get(i);
String fieldValue = values.get(i);
// 如果对应的字段为null,则不需要放入
if (!StringUtil.isBlank(fieldValue)) {
results.put(fieldName, fieldValue);
}
}
}
log.info("[REDIS] Get hash key=%s,hashKey-value=%s", key, results);
} catch (Exception e) {
log.error("[REDIS] Get hash key=%s,hashKey-value=%s", key, results);
}
return results;
}
/**
* 获取结果
*
* @param key
* @return
* @throws Exception
*/
public Map<String, String> getHashValues(String key) {
Map<String, String> results = null;
try {
HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
results = hashOps.entries(key);
log.info("[REDIS] Get hash key=%s,hashKey-value=%s", key, results);
} catch (Exception e) {
log.error("[REDIS] Get hash key=%s,hashKey-value=%s", key, results);
}
return results;
}
/**
* 从hash表里面删除对应的值
*
* @param key
* @param hashKey
* @throws Exception
*/
public void deleteHash(String key, String hashKey) {
try {
HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
hashOps.delete(key, hashKey);
log.info("[REDIS] Delete hash key=%s, hashKey=%s", key, hashKey);
} catch (Exception e) {
log.error("[REDIS] Delete hash key=%s, hashKey=%s", key, hashKey);
}
}
/**
* 为key设置超时时间
*
* @param key
* @param timeout
*/
public void expire(String key, long timeout) {
try {
boolean expore = redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
if (expore) {
log.info("[REDIS] Expire success key=%s, timeout=%s", key, timeout);
} else {
log.info("[REDIS] Expire fail key=%s, timeout=%s", key, timeout);
}
} catch (Exception e) {
log.error("[REDIS] Expire fail key=%s, timeout=%s", key, timeout);
}
}
/**
* 进行递增
*
* @param key
* @param delta
* @param timeout
* @return
* @throws Exception
*/
public long incrementDelta(String key, long delta, long timeout) {
long result = -1L;
try {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
result = valueOps.increment(key, delta);
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
log.info("[REDIS] increment key=%s, result=%s, timeout=%s", key, result, timeout);
} catch (Exception e) {
log.error("[REDIS] increment key=%s, result=%s, timeout=%s", key, result, timeout);
}
return result;
}
/**
* 进行递增
*
* @param key
* @param delta
* @return
* @throws Exception
*/
public long incrementDelta(String key, long delta) {
long result = -1L;
try {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
result = valueOps.increment(key, delta);
log.info("[REDIS] increment key=%s, result=%s", key, result);
} catch (Exception e) {
log.error("[REDIS] increment key=%s, result=%s", key, result);
}
return result;
}
/**
* 从左推送到list中
*/
public void leftPush(String key, String value) {
try {
BoundListOperations<String, String> stringStringBoundListOperations = redisTemplate.boundListOps(key);
stringStringBoundListOperations.leftPush(value);
log.info("[REDIS] leftPush list key=%s,value=%s", key, value);
} catch (Exception e) {
log.error("[REDIS] leftPush list key=%s,value=%s", key, value);
}
}
/**
* 从list右边弹出
*/
public String rightPop(String key) {
String result = null;
try {
BoundListOperations<String, String> stringStringBoundListOperations = redisTemplate.boundListOps(key);
result = stringStringBoundListOperations.rightPop();
log.info("[REDIS] rightPop list key=%s,result=%s", key, result);
} catch (Exception e) {
log.error("[REDIS] rightPop list key=%s,result=%s", key, result);
}
return result;
}
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value.toString());
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value.toString(), time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public <T> T getJson(String key, Class<T> entityClass) {
try {
String value = getValue(key);
if (StringUtil.isBlank(value)) {
return null;
}
return JSON.parseObject(value, entityClass);
} catch (Exception e) {
log.error("从redis获取json对象失败:%s,异常信息为: %s", key, ExceptionUtils.getStackTrace(e));
}
return null;
}
public boolean setJson(String key, Object value, long time) {
try {
if (time > 0) {
putValue(key, JSON.toJSONString(value), time);
} else {
putValue(key, JSON.toJSONString(value));
}
return true;
} catch (Exception e) {
log.error("往redis放入json对象失败:%s,异常信息为: %s", key, ExceptionUtils.getStackTrace(e));
return false;
}
}
public <T> List<T> getJsonList(String key, Class<T> entityClass) {
try {
String value = getValue(key);
if (StringUtil.isBlank(value)) {
return null;
}
return JSON.parseArray(value, entityClass);
} catch (Exception e) {
log.error("从redis获取json对象失败:%s", key, ExceptionUtils.getStackTrace(e));
}
return null;
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
}