提交 | 用户 | 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; |
fb3acc
|
7 |
import java.util.Map; |
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 |
|
fb3acc
|
131 |
/** |
Z |
132 |
* hash 删除操作 |
|
133 |
* |
|
134 |
* @param key 键 |
|
135 |
* @param valueKey 值 |
|
136 |
* @return true成功 false 失败 |
|
137 |
*/ |
|
138 |
public boolean hashDel(String key, Object... valueKey) { |
|
139 |
try { |
|
140 |
redisTemplate.opsForHash().delete(key,valueKey); |
|
141 |
return true; |
|
142 |
} catch (Exception e) { |
|
143 |
e.printStackTrace(); |
|
144 |
return false; |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
/** |
|
149 |
* hash 设置hashMapt操作 |
|
150 |
* |
|
151 |
* @param key 键 |
|
152 |
* @return true成功 false 失败 |
|
153 |
*/ |
|
154 |
public boolean hashSetMap(String key, Map<String,Object> map) { |
|
155 |
try { |
|
156 |
redisTemplate.opsForHash().putAll(key, map); |
|
157 |
return true; |
|
158 |
} catch (Exception e) { |
|
159 |
e.printStackTrace(); |
|
160 |
return false; |
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 |
/** |
|
165 |
* hash 设置单个hashMap操作 |
|
166 |
* |
|
167 |
* @param key 键 |
|
168 |
* @return true成功 false 失败 |
|
169 |
*/ |
|
170 |
public boolean hashSet(String hashKey, String key, Object value) { |
|
171 |
try { |
|
172 |
redisTemplate.opsForHash().put(hashKey,key,value); |
|
173 |
return true; |
|
174 |
} catch (Exception e) { |
|
175 |
e.printStackTrace(); |
|
176 |
return false; |
|
177 |
} |
|
178 |
} |
db287a
|
179 |
|
Z |
180 |
/** |
|
181 |
* hash 获取整个hashKey数据 |
|
182 |
* |
|
183 |
* @param hashKey 键 |
|
184 |
* @return true成功 false 失败 |
|
185 |
*/ |
|
186 |
public Map<Object, Object> hashGetAll(String hashKey) { |
|
187 |
try { |
|
188 |
return redisTemplate.opsForHash().entries(hashKey); |
|
189 |
} catch (Exception e) { |
|
190 |
e.printStackTrace(); |
|
191 |
return null; |
|
192 |
} |
|
193 |
} |
|
194 |
|
|
195 |
/** |
|
196 |
* hash 获取单个hashKey |
|
197 |
* |
|
198 |
* @param hashKey 键 |
|
199 |
* @return true成功 false 失败 |
|
200 |
*/ |
|
201 |
public Object hashGet(String hashKey,String key) { |
|
202 |
try { |
|
203 |
return redisTemplate.opsForHash().get(hashKey,key); |
|
204 |
} catch (Exception e) { |
|
205 |
e.printStackTrace(); |
|
206 |
return null; |
|
207 |
} |
|
208 |
} |
|
209 |
|
fb3acc
|
210 |
} |