RedisUtil.java
8.9 KB
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
package com.it.tool;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Resource
private RedisTemplate<String, Object> redisTemplate;
public RedisUtil() {
}
public boolean expire(String key, long time) {
try {
if (time > 0L) {
this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception var5) {
var5.printStackTrace();
return false;
}
}
public long getExpire(String key) {
return this.redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
public boolean hasKey(String key) {
try {
return this.redisTemplate.hasKey(key);
} catch (Exception var3) {
var3.printStackTrace();
return false;
}
}
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
this.redisTemplate.delete(key[0]);
} else {
this.redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
public Object get(String key) {
return key == null ? null : this.redisTemplate.opsForValue().get(key);
}
public boolean set(String key, Object value) {
try {
this.redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public boolean set(String key, Object value, long time) {
try {
if (time > 0L) {
this.redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
this.set(key, value);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
public long incr(String key, long delta) {
if (delta < 0L) {
throw new RuntimeException("递增因子必须大于0");
} else {
return this.redisTemplate.opsForValue().increment(key, delta);
}
}
public long decr(String key, long delta) {
if (delta < 0L) {
throw new RuntimeException("递减因子必须大于0");
} else {
return this.redisTemplate.opsForValue().increment(key, -delta);
}
}
public Object hget(String key, String item) {
return this.redisTemplate.opsForHash().get(key, item);
}
public Map<Object, Object> hmget(String key) {
return this.redisTemplate.opsForHash().entries(key);
}
public boolean hmset(String key, Map<String, Object> map) {
try {
this.redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
this.redisTemplate.opsForHash().putAll(key, map);
if (time > 0L) {
this.expire(key, time);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
public boolean hset(String key, String item, Object value) {
try {
this.redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception var5) {
var5.printStackTrace();
return false;
}
}
public boolean hset(String key, String item, Object value, long time) {
try {
this.redisTemplate.opsForHash().put(key, item, value);
if (time > 0L) {
this.expire(key, time);
}
return true;
} catch (Exception var7) {
var7.printStackTrace();
return false;
}
}
public void hdel(String key, Object... item) {
this.redisTemplate.opsForHash().delete(key, item);
}
public boolean hHasKey(String key, String item) {
return this.redisTemplate.opsForHash().hasKey(key, item);
}
public double hincr(String key, String item, double by) {
return this.redisTemplate.opsForHash().increment(key, item, by);
}
public double hdecr(String key, String item, double by) {
return this.redisTemplate.opsForHash().increment(key, item, -by);
}
public Set<Object> sGet(String key) {
try {
return this.redisTemplate.opsForSet().members(key);
} catch (Exception var3) {
var3.printStackTrace();
return null;
}
}
public boolean sHasKey(String key, Object value) {
try {
return this.redisTemplate.opsForSet().isMember(key, value);
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public long sSet(String key, Object... values) {
try {
return this.redisTemplate.opsForSet().add(key, values);
} catch (Exception var4) {
var4.printStackTrace();
return 0L;
}
}
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = this.redisTemplate.opsForSet().add(key, values);
if (time > 0L) {
this.expire(key, time);
}
return count;
} catch (Exception var6) {
var6.printStackTrace();
return 0L;
}
}
public long sGetSetSize(String key) {
try {
return this.redisTemplate.opsForSet().size(key);
} catch (Exception var3) {
var3.printStackTrace();
return 0L;
}
}
public long setRemove(String key, Object... values) {
try {
Long count = this.redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception var4) {
var4.printStackTrace();
return 0L;
}
}
public List<Object> lGet(String key, long start, long end) {
try {
return this.redisTemplate.opsForList().range(key, start, end);
} catch (Exception var7) {
var7.printStackTrace();
return null;
}
}
public long lGetListSize(String key) {
try {
return this.redisTemplate.opsForList().size(key);
} catch (Exception var3) {
var3.printStackTrace();
return 0L;
}
}
public Object lGetIndex(String key, long index) {
try {
return this.redisTemplate.opsForList().index(key, index);
} catch (Exception var5) {
var5.printStackTrace();
return null;
}
}
public boolean lSet(String key, Object value) {
try {
this.redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public boolean lSet(String key, Object value, long time) {
try {
this.redisTemplate.opsForList().rightPush(key, value);
if (time > 0L) {
this.expire(key, time);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
public boolean lSet(String key, List<Object> value) {
try {
this.redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public boolean lSet(String key, List<Object> value, long time) {
try {
this.redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0L) {
this.expire(key, time);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
public boolean lUpdateIndex(String key, long index, Object value) {
try {
this.redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
public long lRemove(String key, long count, Object value) {
try {
Long remove = this.redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception var6) {
var6.printStackTrace();
return 0L;
}
}
}