fhx
2023-04-25 2aad5c6ca552423d1b8e6e8c869a698a15822759
提交 | 用户 | 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) {
154                 redisTemplate.expire(key, time, timeUnit);
155             }
156             return true;
157         } catch (Exception e) {
158             e.printStackTrace();
159             return false;
160
161         }
162
163     }
164
f3ad4b 165
fb3acc 166     /**
Z 167      * hash 删除操作
168      *
169      * @param key   键
170      * @param valueKey 值
171      * @return true成功 false 失败
172      */
173     public boolean hashDel(String key, Object... valueKey) {
174         try {
175             redisTemplate.opsForHash().delete(key,valueKey);
176             return true;
177         } catch (Exception e) {
178             e.printStackTrace();
179             return false;
180         }
181     }
182
183     /**
184      * hash 设置hashMapt操作
185      *
186      * @param key   键
187      * @return true成功 false 失败
188      */
189     public boolean hashSetMap(String key, Map<String,Object> map) {
190         try {
191             redisTemplate.opsForHash().putAll(key, map);
192             return true;
193         } catch (Exception e) {
194             e.printStackTrace();
195             return false;
196         }
197     }
198
199     /**
200      * hash 设置单个hashMap操作
201      *
202      * @param key   键
203      * @return true成功 false 失败
204      */
205     public boolean hashSet(String hashKey, String key, Object value) {
206         try {
207             redisTemplate.opsForHash().put(hashKey,key,value);
208             return true;
209         } catch (Exception e) {
210             e.printStackTrace();
211             return false;
212         }
213     }
db287a 214
Z 215     /**
216      * hash 获取整个hashKey数据
217      *
218      * @param hashKey   键
219      * @return true成功 false 失败
220      */
221     public Map<Object, Object> hashGetAll(String hashKey) {
222         try {
223             return redisTemplate.opsForHash().entries(hashKey);
224         } catch (Exception e) {
225             e.printStackTrace();
226             return null;
227         }
228     }
229
230     /**
231      * hash 获取单个hashKey
232      *
233      * @param hashKey   键
234      * @return true成功 false 失败
235      */
8fcea5 236     public Object hashGet(String hashKey,Object key) {
db287a 237         try {
Z 238             return redisTemplate.opsForHash().get(hashKey,key);
239         } catch (Exception e) {
240             e.printStackTrace();
241             return null;
242         }
243     }
244
8fcea5 245     /**
F 246      * list 新增-左插入
247      *
248      * @param listKey   键
249      * @return true成功 false 失败
250      */
251     public Object leftPush(String listKey,Object value) {
252         try {
253             return redisTemplate.opsForList().leftPush(listKey,value);
254         } catch (Exception e) {
255             e.printStackTrace();
256             return null;
257         }
258     }
259
260     /**
261      * list 新增-左插入(存在才插入)
262      *
263      * @param listKey   键
264      * @return true成功 false 失败
265      */
266     public Object leftPushIfPresent(String listKey,Object value) {
267         try {
268             return redisTemplate.opsForList().leftPushIfPresent(listKey,value);
269         } catch (Exception e) {
270             e.printStackTrace();
271             return null;
272         }
273     }
274
275     /**
276      * list 新增-右插入(存在才插入)
277      *
278      * @param listKey   键
279      * @return true成功 false 失败
280      */
281     public Object rightPushIfPresent(String listKey,Object value) {
282         try {
283             return redisTemplate.opsForList().rightPushIfPresent(listKey,value);
284         } catch (Exception e) {
285             e.printStackTrace();
286             return null;
287         }
288     }
289
290     /**
291      * list 新增-右插入
292      *
293      * @param listKey   键
294      * @return true成功 false 失败
295      */
296     public Object rightPush(String listKey,Object value) {
297         try {
298             return redisTemplate.opsForList().rightPush(listKey,value);
299         } catch (Exception e) {
300             e.printStackTrace();
301             return null;
302         }
303     }
304
305     /**
306      * list 弹出-左
307      *
308      * @param listKey   键
309      * @return true成功 false 失败
310      */
311     public Object leftPop(String listKey) {
312         try {
313             return redisTemplate.opsForList().leftPop(listKey);
314         } catch (Exception e) {
315             e.printStackTrace();
316             return null;
317         }
318     }
319
320     /**
321      * list 弹出-右
322      *
323      * @param listKey   键
324      * @return true成功 false 失败
325      */
326     public Object rightPop(String listKey) {
327         try {
328             return redisTemplate.opsForList().rightPop(listKey);
329         } catch (Exception e) {
330             e.printStackTrace();
331             return null;
332         }
333     }
334
335     /**
a8e37f 336      * list 获取索引下的值
8fcea5 337      *
F 338      * @param key   键
339      * @param index 索引
340      * @return true成功 false 失败
341      */
342     public Object listGet(String key, long index) {
343         try {
344             return redisTemplate.opsForList().index(key,index);
345         } catch (Exception e) {
346             e.printStackTrace();
347             return null;
348         }
349     }
350
351     /**
a8e37f 352      * list 获取索引下的值
F 353      *
354      * @param key   键
355      * @param start 开始位置 0是开始位置
356      * @param end 结束位置,-1返回所有
357      * @return true成功 false 失败
358      */
4a0446 359     public List<Object> listGetRange(String key, long start , long end) {
a8e37f 360         try {
F 361             return redisTemplate.opsForList().range(key, start, end);
362         } catch (Exception e) {
363             e.printStackTrace();
364             return null;
365         }
366     }
367
368     /**
8fcea5 369      * list 获取长度
F 370      *
371      * @param listKey   键
372      * @return true成功 false 失败
373      */
374     public Object listSize(String listKey) {
375         try {
376             return redisTemplate.opsForList().size(listKey);
377         } catch (Exception e) {
378             e.printStackTrace();
379             return null;
380         }
381     }
382
383     /**
384      * list 删除
385      *
386      * @param listKey   键
387      * @param index index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素; index<0, 从尾部开始删除第一个值等于value的元素
388      * @param value 删除的值
389      * @return true成功 false 失败
390      */
391     public Object listRemove(String listKey,long index,Object value) {
392         try {
393             return redisTemplate.opsForList().remove(listKey, index, value);
394         } catch (Exception e) {
395             e.printStackTrace();
396             return null;
397         }
398     }
399
ff803d 400     /**
F 401      * @param key     键
402      * @param value   值
403      * @param timeOut 时间
404      * @param unit    时间单位
405      */
406     public Boolean setIfAbsent(String key, Object value, long timeOut, TimeUnit unit) {
407         try {
408             return redisTemplate.opsForValue().setIfAbsent(key, value, timeOut, unit);
409         } catch (Exception e) {
410             e.printStackTrace();
411             return null;
412         }
413     }
414
fb3acc 415 }