RedisUtil工具类

Redis工具组件

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
//redis工具类
@UtilityClass
@Slf4j
public class RedisUtils {
private RedisService redisService;

private RedisTemplate<String, String> stringRedisTemplate;

public RedisService getRedisService() {
if(redisService==null){
redisService=SpringContextHolder.getBean(RedisService.class);
}
return redisService;
}

public RedisTemplate<String, Object> getRedisTemplate() {
return SpringContextHolder.getBean("redisTemplate");
}

public RedisTemplate<String, String> getStringRedisTemplate() {
if(stringRedisTemplate==null){
stringRedisTemplate=SpringContextHolder.getBean("stringRedisTemplate");
}
return stringRedisTemplate;
}

/**
* HashGet
* @param key 键 不能为null
* @param item 项 不能为null
* @return
*/
public Object hGet(String key, String item) {
HashOperations<String, Object, Object> ops = getRedisTemplate().opsForHash();
return ops.get(key, item);
}

/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public boolean hSet(String key, String item, Object value, long time) {
try {
HashOperations<String, Object, Object> ops = getRedisTemplate().opsForHash();
ops.put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error("hSet err{}",e.getMessage());
return false;
}
}

/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
getRedisTemplate().expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
log.error("expire err{}",e.getMessage());
return false;
}
}

/**
* redis string类型 存储方法
* kc:1:appName:key
* @param key 自定义key
* @param value 存储的字符串
* @param timeout 超时时间
*/
public void setStringValue(String key, String value, long timeout, TimeUnit unit) {
ValueOperations<String, String> vo = getStringRedisTemplate().opsForValue();
vo.set(getRedisService().getFormatKey(key), value, timeout, unit);
vo.set(getRedisService().getFormatKey(key), value);
}

/**
* redis string类型 存储方法
*
* @param key 自定义key
* @param value 存储的字符串
*/
public void setStringValue(String key, String value) {
ValueOperations<String, String> vo = getStringRedisTemplate().opsForValue();
vo.set(getRedisService().getFormatKey(key), value);
}
/**
* redis string类型 存储方法
*
* @param key 自定义key
* @param value 存储的字符串
*/
public void setStringVal(String key, String value) {
ValueOperations<String, String> vo = getStringRedisTemplate().opsForValue();
vo.set(key, value);
}

/**
* redis string类型 存储方法
* @param key 自定义key
*/
public String getStringValue(String key) {
ValueOperations<String, String> vo = getStringRedisTemplate().opsForValue();
String s = vo.get(getRedisService().getFormatKey(key));
log.info("获取{}={}",getFormatKey(key),s);
return s;
}

/**
* 设置key并设置过期时间
* @param key
* @param value
* @param time
* @return
*/
public Boolean setIfAbsent(String key, String value, long time) {
return getStringRedisTemplate().opsForValue().setIfAbsent(key, value, time, TimeUnit.SECONDS);
}

/**
* redis string类型 存储方法
* @param key 自定义key
*/
public String getStringVal(String key) {
ValueOperations<String, String> vo = getStringRedisTemplate().opsForValue();
String s = vo.get(key);
if(log.isDebugEnabled()){
log.debug("获取{}={}",key,s);
}
return s;
}

/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public void hDel(String key, Object... item) {
HashOperations<String, Object, Object> ops = getStringRedisTemplate().opsForHash();
ops.delete(key, item);
}

/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
HashOperations<String, Object, Object> ops = getStringRedisTemplate().opsForHash();
return ops.hasKey(key, item);
}

/**
* HashSet 并设置时间
*
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
*/
public boolean hMSet(String key, Map<String, Object> map, long time) {
try {
HashOperations<String, Object, Object> ops = getStringRedisTemplate().opsForHash();
ops.putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error("hMSet err{}",e.getMessage());
return false;
}
}

/**
* hMGetStr 批量获取redis hash得值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return
*/
public List<Object> hMGetStr(String key, List<Object> item) {
HashOperations<String, Object, Object> ops = getStringRedisTemplate().opsForHash();
List<Object> objects = ops.multiGet(key, item);
return objects;
}

/**
* 获取hashKey对应的所有键值
* @param key 键
* @return 对应的多个键值
*/
public Map<Object, Object> hMGet(String key) {
HashOperations<String, Object, Object> ops = getStringRedisTemplate().opsForHash();
return ops.entries(key);
}

/**
* hGetStr
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return
*/
public Object hGetStr(String key, String item) {
HashOperations<String, Object, Object> ops = getStringRedisTemplate().opsForHash();
return ops.get(key, item);
}
}

[up主专用,视频内嵌代码贴在这]