fwq
2022-05-11 4a044670a4b8a9bcc0a38b413ef803b9842a5052
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package com.hx.redis;
 
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
 
/**
 * 文件处理工具
 *
 * @author wangrenhuang
 * @Date 2021-10-19
 */
@Component
public class RedisUtil {
    /**
     * [redis]
     */
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
 
 
    /**
     * [判断key是否存在]
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            return false;
        }
    }
 
    /**
     * [普通缓存获取]
     *
     * @param key 键
     * @return 值
     */
    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }
 
    /**
     * [普通缓存删除]
     *
     * @param key 键
     * @return 值
     */
    public boolean delete(String key) {
        try {
            Boolean aBoolean = redisTemplate.hasKey(key);
            return aBoolean == false ? true : redisTemplate.delete(key);
        } catch (Exception e) {
            return false;
        }
    }
 
    /**
     * [普通缓存放入]
     *
     * @param key   键
     * @param value 值
     * @return true成功 false失败
     */
    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
 
    /**
     * 普通缓存放入并设置时间
     *
     * @param key   键
     * @param value 值
     * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
     * @return true成功 false 失败
     */
    public boolean set(String key, Object value, long time) {
        try {
            if (time > 0) {
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * 26
     * 指定缓存失效时间
     * 27
     *
     * @param key  键
     *             28
     * @param time 时间(秒)
     *             29
     * @return 30
     */
 
    public boolean expire(String key, long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
 
        }
 
    }
 
 
    /**
     * hash 删除操作
     *
     * @param key   键
     * @param valueKey 值
     * @return true成功 false 失败
     */
    public boolean hashDel(String key, Object... valueKey) {
        try {
            redisTemplate.opsForHash().delete(key,valueKey);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * hash 设置hashMapt操作
     *
     * @param key   键
     * @return true成功 false 失败
     */
    public boolean hashSetMap(String key, Map<String,Object> map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * hash 设置单个hashMap操作
     *
     * @param key   键
     * @return true成功 false 失败
     */
    public boolean hashSet(String hashKey, String key, Object value) {
        try {
            redisTemplate.opsForHash().put(hashKey,key,value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * hash 获取整个hashKey数据
     *
     * @param hashKey   键
     * @return true成功 false 失败
     */
    public Map<Object, Object> hashGetAll(String hashKey) {
        try {
            return redisTemplate.opsForHash().entries(hashKey);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * hash 获取单个hashKey
     *
     * @param hashKey   键
     * @return true成功 false 失败
     */
    public Object hashGet(String hashKey,Object key) {
        try {
            return redisTemplate.opsForHash().get(hashKey,key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * list 新增-左插入
     *
     * @param listKey   键
     * @return true成功 false 失败
     */
    public Object leftPush(String listKey,Object value) {
        try {
            return redisTemplate.opsForList().leftPush(listKey,value);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * list 新增-左插入(存在才插入)
     *
     * @param listKey   键
     * @return true成功 false 失败
     */
    public Object leftPushIfPresent(String listKey,Object value) {
        try {
            return redisTemplate.opsForList().leftPushIfPresent(listKey,value);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * list 新增-右插入(存在才插入)
     *
     * @param listKey   键
     * @return true成功 false 失败
     */
    public Object rightPushIfPresent(String listKey,Object value) {
        try {
            return redisTemplate.opsForList().rightPushIfPresent(listKey,value);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * list 新增-右插入
     *
     * @param listKey   键
     * @return true成功 false 失败
     */
    public Object rightPush(String listKey,Object value) {
        try {
            return redisTemplate.opsForList().rightPush(listKey,value);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * list 弹出-左
     *
     * @param listKey   键
     * @return true成功 false 失败
     */
    public Object leftPop(String listKey) {
        try {
            return redisTemplate.opsForList().leftPop(listKey);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * list 弹出-右
     *
     * @param listKey   键
     * @return true成功 false 失败
     */
    public Object rightPop(String listKey) {
        try {
            return redisTemplate.opsForList().rightPop(listKey);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * list 获取索引下的值
     *
     * @param key   键
     * @param index 索引
     * @return true成功 false 失败
     */
    public Object listGet(String key, long index) {
        try {
            return redisTemplate.opsForList().index(key,index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * list 获取索引下的值
     *
     * @param key   键
     * @param start 开始位置 0是开始位置
     * @param end 结束位置,-1返回所有
     * @return true成功 false 失败
     */
    public List<Object> listGetRange(String key, long start , long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * list 获取长度
     *
     * @param listKey   键
     * @return true成功 false 失败
     */
    public Object listSize(String listKey) {
        try {
            return redisTemplate.opsForList().size(listKey);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * list 删除
     *
     * @param listKey   键
     * @param index index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素; index<0, 从尾部开始删除第一个值等于value的元素
     * @param value 删除的值
     * @return true成功 false 失败
     */
    public Object listRemove(String listKey,long index,Object value) {
        try {
            return redisTemplate.opsForList().remove(listKey, index, value);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
}