RedisUtils.java
3.47 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
package org.nafmii.common.utils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class RedisUtils {
@SuppressWarnings("unchecked")
private static RedisTemplate<String, Object> redisTemplate = SpringUtils.getBean("redisTemplate", RedisTemplate.class);
public RedisUtils(RedisTemplate<String, Object> redisTemplate) {
RedisUtils.redisTemplate = redisTemplate;
}
/**
* 普通缓存获取
* @param key 键
* @return 值
*/
public static Object get(String key){
return key == null ? null : redisTemplate.opsForValue().get(key);
}
public static <T> T hGetObject(String key, Class<T> clazz){
Object o = redisTemplate.opsForValue().get(key);
return JSONObject.parseObject((String) o, clazz);
}
public static <T> T hGetObject(String key, String item, Class<T> clazz){
Object o = redisTemplate.opsForHash().get(key, item);
return JSONObject.parseObject((String) o, clazz);
}
public static <T> List<T> hGetList(String key, String item, Class<T> clazz){
Object o = redisTemplate.opsForHash().get(key, item);
return JSONObject.parseArray((String) o, clazz);
}
/**
* 向一张hash表中放入数据,如果不存在将创建
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
*/
public static boolean hSet(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean hSet(String key, Object value,Long time,TimeUnit unit) {
try {
redisTemplate.opsForValue().set(key, value);
if (time > 0) {
expire(key, time,unit);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean hSet(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static boolean expire(String key, long time,TimeUnit unit){
try {
if(time>0){
redisTemplate.expire(key, time, unit);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除hash表中的值
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public static void hDel(String key, Object... item){
redisTemplate.opsForHash().delete(key,item);
}
/**
* 删除hash表中的值
* @param key 键 不能为null
*/
public static void hDel(String key){
redisTemplate.delete(key);
}
/**
* redis自增命令
* @param key 如果key有值加1,否则新增key且初始化为0
*/
public static void increment(String key, Long time, TimeUnit unit){
try {
redisTemplate.opsForValue().increment(key);
if (time > 0) {
expire(key, time, unit);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}