fhx
2023-04-25 d780808bf8269c7872339cc34cfb13d74daa5e03
提交 | 用户 | age
f3ad4b 1 package com.hx.redis;
C 2
0cbabb 3 import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
f3ad4b 4 import org.springframework.data.redis.core.RedisTemplate;
C 5 import org.springframework.stereotype.Component;
6
7 import javax.annotation.Resource;
4a0446 8 import java.util.List;
fb3acc 9 import java.util.Map;
f3ad4b 10 import java.util.concurrent.TimeUnit;
C 11
12
041c40 13 /**
C 14  * 文件处理工具
15  *
f3ad4b 16  * @author wangrenhuang
C 17  * @Date 2021-10-19
18  */
19 @Component
20 public class RedisUtil {
21     /**
22      * [redis]
23      */
24     @Resource
25     private RedisTemplate<String, Object> redisTemplate;
26
0cbabb 27     /**
C 28      * 切换数据库
29      * @param dataNumber 数据库编号
30      */
31     public void setDataBase(int dataNumber) {
32         LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
33         if (connectionFactory != null && dataNumber != connectionFactory.getDatabase()) {
34             connectionFactory.setDatabase(dataNumber);
35             this.redisTemplate.setConnectionFactory(connectionFactory);
36             connectionFactory.resetConnection();
37         }
38     }
f3ad4b 39
C 40     /**
41      * [判断key是否存在]
041c40 42      *
f3ad4b 43      * @param key 键
C 44      * @return true 存在 false不存在
45      */
46     public boolean hasKey(String key) {
47         try {
48             return redisTemplate.hasKey(key);
49         } catch (Exception e) {
50             return false;
51         }
52     }
53
54     /**
55      * [普通缓存获取]
041c40 56      *
f3ad4b 57      * @param key 键
C 58      * @return 值
59      */
60     public Object get(String key) {
61         return key == null ? null : redisTemplate.opsForValue().get(key);
62     }
041c40 63
f3ad4b 64     /**
C 65      * [普通缓存删除]
041c40 66      *
f3ad4b 67      * @param key 键
C 68      * @return 值
69      */
70     public boolean delete(String key) {
71         try {
72             Boolean aBoolean = redisTemplate.hasKey(key);
73             return aBoolean == false ? true : redisTemplate.delete(key);
041c40 74         } catch (Exception e) {
f3ad4b 75             return false;
C 76         }
77     }
041c40 78
f3ad4b 79     /**
C 80      * [普通缓存放入]
041c40 81      *
C 82      * @param key   键
f3ad4b 83      * @param value 值
C 84      * @return true成功 false失败
85      */
86     public boolean set(String key, Object value) {
87         try {
88             redisTemplate.opsForValue().set(key, value);
89             return true;
90         } catch (Exception e) {
91             return false;
92         }
93     }
94
95
96     /**
97      * 普通缓存放入并设置时间
041c40 98      *
C 99      * @param key   键
f3ad4b 100      * @param value 值
041c40 101      * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
f3ad4b 102      * @return true成功 false 失败
C 103      */
104     public boolean set(String key, Object value, long time) {
105         try {
106             if (time > 0) {
107                 redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
108             } else {
109                 set(key, value);
110             }
111             return true;
112         } catch (Exception e) {
113             e.printStackTrace();
114             return false;
115         }
116     }
117
041c40 118     /**
C 119      * 26
120      * 指定缓存失效时间
121      * 27
122      *
123      * @param key  键
124      *             28
125      * @param time 时间(秒)
126      *             29
127      * @return 30
128      */
129
130     public boolean expire(String key, long time) {
131         try {
132             if (time > 0) {
133                 redisTemplate.expire(key, time, TimeUnit.SECONDS);
134             }
135             return true;
136         } catch (Exception e) {
137             e.printStackTrace();
138             return false;
139
140         }
141
142     }
143
2aad5c 144     /**
F 145      * 设置到期时间
146      * @param key       对应键
147      * @param time      时长
148      * @param timeUnit  时间单位
149      * @return
150      */
151     public boolean expire(String key, long time, TimeUnit timeUnit) {
152         try {
153             if (time > 0) {
d78080 154                 return redisTemplate.expire(key, time, timeUnit);
2aad5c 155             }
F 156         } catch (Exception e) {
157             e.printStackTrace();
158             return false;
159
160         }
d78080 161         return false;
2aad5c 162     }
F 163
f3ad4b 164
fb3acc 165     /**
Z 166      * hash 删除操作
167      *
168      * @param key   键
169      * @param valueKey 值
170      * @return true成功 false 失败
171      */
172     public boolean hashDel(String key, Object... valueKey) {
173         try {
174             redisTemplate.opsForHash().delete(key,valueKey);
175             return true;
176         } catch (Exception e) {
177             e.printStackTrace();
178             return false;
179         }
180     }
181
182     /**
183      * hash 设置hashMapt操作
184      *
185      * @param key   键
186      * @return true成功 false 失败
187      */
188     public boolean hashSetMap(String key, Map<String,Object> map) {
189         try {
190             redisTemplate.opsForHash().putAll(key, map);
191             return true;
192         } catch (Exception e) {
193             e.printStackTrace();
194             return false;
195         }
196     }
197
198     /**
199      * hash 设置单个hashMap操作
200      *
201      * @param key   键
202      * @return true成功 false 失败
203      */
204     public boolean hashSet(String hashKey, String key, Object value) {
205         try {
206             redisTemplate.opsForHash().put(hashKey,key,value);
207             return true;
208         } catch (Exception e) {
209             e.printStackTrace();
210             return false;
211         }
212     }
db287a 213
Z 214     /**
215      * hash 获取整个hashKey数据
216      *
217      * @param hashKey   键
218      * @return true成功 false 失败
219      */
220     public Map<Object, Object> hashGetAll(String hashKey) {
221         try {
222             return redisTemplate.opsForHash().entries(hashKey);
223         } catch (Exception e) {
224             e.printStackTrace();
225             return null;
226         }
227     }
228
229     /**
230      * hash 获取单个hashKey
231      *
232      * @param hashKey   键
233      * @return true成功 false 失败
234      */
8fcea5 235     public Object hashGet(String hashKey,Object key) {
db287a 236         try {
Z 237             return redisTemplate.opsForHash().get(hashKey,key);
238         } catch (Exception e) {
239             e.printStackTrace();
240             return null;
241         }
242     }
243
8fcea5 244     /**
F 245      * list 新增-左插入
246      *
247      * @param listKey   键
248      * @return true成功 false 失败
249      */
250     public Object leftPush(String listKey,Object value) {
251         try {
252             return redisTemplate.opsForList().leftPush(listKey,value);
253         } catch (Exception e) {
254             e.printStackTrace();
255             return null;
256         }
257     }
258
259     /**
260      * list 新增-左插入(存在才插入)
261      *
262      * @param listKey   键
263      * @return true成功 false 失败
264      */
265     public Object leftPushIfPresent(String listKey,Object value) {
266         try {
267             return redisTemplate.opsForList().leftPushIfPresent(listKey,value);
268         } catch (Exception e) {
269             e.printStackTrace();
270             return null;
271         }
272     }
273
274     /**
275      * list 新增-右插入(存在才插入)
276      *
277      * @param listKey   键
278      * @return true成功 false 失败
279      */
280     public Object rightPushIfPresent(String listKey,Object value) {
281         try {
282             return redisTemplate.opsForList().rightPushIfPresent(listKey,value);
283         } catch (Exception e) {
284             e.printStackTrace();
285             return null;
286         }
287     }
288
289     /**
290      * list 新增-右插入
291      *
292      * @param listKey   键
293      * @return true成功 false 失败
294      */
295     public Object rightPush(String listKey,Object value) {
296         try {
297             return redisTemplate.opsForList().rightPush(listKey,value);
298         } catch (Exception e) {
299             e.printStackTrace();
300             return null;
301         }
302     }
303
304     /**
305      * list 弹出-左
306      *
307      * @param listKey   键
308      * @return true成功 false 失败
309      */
310     public Object leftPop(String listKey) {
311         try {
312             return redisTemplate.opsForList().leftPop(listKey);
313         } catch (Exception e) {
314             e.printStackTrace();
315             return null;
316         }
317     }
318
319     /**
320      * list 弹出-右
321      *
322      * @param listKey   键
323      * @return true成功 false 失败
324      */
325     public Object rightPop(String listKey) {
326         try {
327             return redisTemplate.opsForList().rightPop(listKey);
328         } catch (Exception e) {
329             e.printStackTrace();
330             return null;
331         }
332     }
333
334     /**
a8e37f 335      * list 获取索引下的值
8fcea5 336      *
F 337      * @param key   键
338      * @param index 索引
339      * @return true成功 false 失败
340      */
341     public Object listGet(String key, long index) {
342         try {
343             return redisTemplate.opsForList().index(key,index);
344         } catch (Exception e) {
345             e.printStackTrace();
346             return null;
347         }
348     }
349
350     /**
a8e37f 351      * list 获取索引下的值
F 352      *
353      * @param key   键
354      * @param start 开始位置 0是开始位置
355      * @param end 结束位置,-1返回所有
356      * @return true成功 false 失败
357      */
4a0446 358     public List<Object> listGetRange(String key, long start , long end) {
a8e37f 359         try {
F 360             return redisTemplate.opsForList().range(key, start, end);
361         } catch (Exception e) {
362             e.printStackTrace();
363             return null;
364         }
365     }
366
367     /**
8fcea5 368      * list 获取长度
F 369      *
370      * @param listKey   键
371      * @return true成功 false 失败
372      */
373     public Object listSize(String listKey) {
374         try {
375             return redisTemplate.opsForList().size(listKey);
376         } catch (Exception e) {
377             e.printStackTrace();
378             return null;
379         }
380     }
381
382     /**
383      * list 删除
384      *
385      * @param listKey   键
386      * @param index index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素; index<0, 从尾部开始删除第一个值等于value的元素
387      * @param value 删除的值
388      * @return true成功 false 失败
389      */
390     public Object listRemove(String listKey,long index,Object value) {
391         try {
392             return redisTemplate.opsForList().remove(listKey, index, value);
393         } catch (Exception e) {
394             e.printStackTrace();
395             return null;
396         }
397     }
398
ff803d 399     /**
F 400      * @param key     键
401      * @param value   值
402      * @param timeOut 时间
403      * @param unit    时间单位
404      */
405     public Boolean setIfAbsent(String key, Object value, long timeOut, TimeUnit unit) {
406         try {
407             return redisTemplate.opsForValue().setIfAbsent(key, value, timeOut, unit);
408         } catch (Exception e) {
409             e.printStackTrace();
410             return null;
411         }
412     }
413
fb3acc 414 }