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