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