提交 | 用户 | 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; |
041c40
|
7 |
import java.util.Date; |
f3ad4b
|
8 |
import java.util.concurrent.TimeUnit; |
C |
9 |
|
|
10 |
|
041c40
|
11 |
/** |
C |
12 |
* 文件处理工具 |
|
13 |
* |
f3ad4b
|
14 |
* @author wangrenhuang |
C |
15 |
* @Date 2021-10-19 |
|
16 |
*/ |
|
17 |
@Component |
|
18 |
public class RedisUtil { |
|
19 |
/** |
|
20 |
* [redis] |
|
21 |
*/ |
|
22 |
@Resource |
|
23 |
private RedisTemplate<String, Object> redisTemplate; |
|
24 |
|
|
25 |
|
|
26 |
/** |
|
27 |
* [判断key是否存在] |
041c40
|
28 |
* |
f3ad4b
|
29 |
* @param key 键 |
C |
30 |
* @return true 存在 false不存在 |
|
31 |
*/ |
|
32 |
public boolean hasKey(String key) { |
|
33 |
try { |
|
34 |
return redisTemplate.hasKey(key); |
|
35 |
} catch (Exception e) { |
|
36 |
return false; |
|
37 |
} |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* [普通缓存获取] |
041c40
|
42 |
* |
f3ad4b
|
43 |
* @param key 键 |
C |
44 |
* @return 值 |
|
45 |
*/ |
|
46 |
public Object get(String key) { |
|
47 |
return key == null ? null : redisTemplate.opsForValue().get(key); |
|
48 |
} |
041c40
|
49 |
|
f3ad4b
|
50 |
/** |
C |
51 |
* [普通缓存删除] |
041c40
|
52 |
* |
f3ad4b
|
53 |
* @param key 键 |
C |
54 |
* @return 值 |
|
55 |
*/ |
|
56 |
public boolean delete(String key) { |
|
57 |
try { |
|
58 |
Boolean aBoolean = redisTemplate.hasKey(key); |
|
59 |
return aBoolean == false ? true : redisTemplate.delete(key); |
041c40
|
60 |
} catch (Exception e) { |
f3ad4b
|
61 |
return false; |
C |
62 |
} |
|
63 |
} |
041c40
|
64 |
|
f3ad4b
|
65 |
/** |
C |
66 |
* [普通缓存放入] |
041c40
|
67 |
* |
C |
68 |
* @param key 键 |
f3ad4b
|
69 |
* @param value 值 |
C |
70 |
* @return true成功 false失败 |
|
71 |
*/ |
|
72 |
public boolean set(String key, Object value) { |
|
73 |
try { |
|
74 |
redisTemplate.opsForValue().set(key, value); |
|
75 |
return true; |
|
76 |
} catch (Exception e) { |
|
77 |
return false; |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
|
|
82 |
/** |
|
83 |
* 普通缓存放入并设置时间 |
041c40
|
84 |
* |
C |
85 |
* @param key 键 |
f3ad4b
|
86 |
* @param value 值 |
041c40
|
87 |
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 |
f3ad4b
|
88 |
* @return true成功 false 失败 |
C |
89 |
*/ |
|
90 |
public boolean set(String key, Object value, long time) { |
|
91 |
try { |
|
92 |
if (time > 0) { |
|
93 |
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); |
|
94 |
} else { |
|
95 |
set(key, value); |
|
96 |
} |
|
97 |
return true; |
|
98 |
} catch (Exception e) { |
|
99 |
e.printStackTrace(); |
|
100 |
return false; |
|
101 |
} |
|
102 |
} |
|
103 |
|
041c40
|
104 |
/** |
C |
105 |
* 26 |
|
106 |
* 指定缓存失效时间 |
|
107 |
* 27 |
|
108 |
* |
|
109 |
* @param key 键 |
|
110 |
* 28 |
|
111 |
* @param time 时间(秒) |
|
112 |
* 29 |
|
113 |
* @return 30 |
|
114 |
*/ |
|
115 |
|
|
116 |
public boolean expire(String key, long time) { |
|
117 |
try { |
|
118 |
if (time > 0) { |
|
119 |
redisTemplate.expire(key, time, TimeUnit.SECONDS); |
|
120 |
} |
|
121 |
return true; |
|
122 |
} catch (Exception e) { |
|
123 |
e.printStackTrace(); |
|
124 |
return false; |
|
125 |
|
|
126 |
} |
|
127 |
|
|
128 |
} |
|
129 |
|
f3ad4b
|
130 |
} |
C |
131 |
|