1 package com.yw.redis.utils;
2
3 import com.alibaba.fastjson.JSON;
4 import redis.clients.jedis.Jedis;
5 import redis.clients.jedis.JedisPool;
6 import redis.clients.jedis.Transaction;
7
8 import java.util.*;
9
10 /**
11 * 功能描述: redis客户端
12 *
13 * @param:
14 * @return:
15 * @auther: YW
16 * @date: 2018/3/29 10:05
17 */
18 public class RedisClientUtils {
19 public JedisPool jedisPool;
20
21 public RedisClientUtils(JedisPool pool) {
22 this.jedisPool = pool;
23 }
24
25 public RedisClientUtils() {
26
27 }
28
29 public JedisPool getJedisPool() {
30 return jedisPool;
31 }
32
33 public void setJedisPool(JedisPool jedisPool) {
34 this.jedisPool = jedisPool;
35 }
36
37 /**
38 * 根据key来获取对应的value
39 */
40 public Object getByKey(String key) {
41 Jedis client = jedisPool.getResource();
42 Object o = null;
43 try {
44 o = client.get(key);
45 } finally {
46 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
47 }
48 return o;
49 }
50
51
52
53 /**
54 * 判断String类型key是否存在
55 */
56 public boolean isKeyExist(String key) {
57 Jedis client = jedisPool.getResource();
58 boolean o = false;
59 try {
60 o = client.exists(key);
61 } finally {
62 jedisPool.returnResourceObject(client);
63 ;// 向连接池“归还”资源
64 }
65 return o;
66 }
67
68 /**
69 * String类型的键值写入redis
70 *
71 * @param key
72 * @param value
73 * @return
74 */
75 public boolean set(String key, String value) {
76 Jedis client = jedisPool.getResource();
77 String issuccess = "";
78 try {
79 issuccess = client.set(key, value);
80 if ("OK" .equals(issuccess)) {
81 return true;
82 } else {
83 return false;
84 }
85 } finally {
86 jedisPool.returnResourceObject(client);
87 ;// 向连接池“归还”资源
88 }
89 }
90
91 public Long setnx(String key, String value) {
92 Jedis client = jedisPool.getResource();
93 try {
94
95 Long result = client.setnx(key, value);
96 System.out.println("setnx key=" + key + " value=" + value +
97 "result=" + result);
98 return result;
99 } finally {
100 jedisPool.returnResourceObject(client);
101 ;// 向连接池“归还”资源
102 }
103 }
104
105 /**
106 * String类型的键值写入redis,并设置失效时间
107 *
108 * @param key
109 * @param value
110 * @return
111 */
112 public boolean setKeyWithExpireTime(String key, String value, int time) {
113 if (time == 0) {
114
115 }
116 Jedis client = jedisPool.getResource();
117 String issuccess = "";
118 try {
119 issuccess = client.setex(key, time, value);
120 if ("OK" .equals(issuccess)) {
121 return true;
122 } else {
123 return false;
124 }
125 } finally {
126 jedisPool.returnResourceObject(client);
127 ;// 向连接池“归还”资源
128 }
129 }
130
131 /**
132 * list<String>结构的数据写入redis
133 *
134 * @param key
135 * @param value
136 * @return
137 */
138 public boolean lpush(String key, List<String> value) {
139 Jedis client = jedisPool.getResource();
140 try {
141 Transaction tx = client.multi();
142 for (String one : value) {
143 tx.lpush(key, one);
144 }
145 tx.exec();
146 return true;
147 } finally {
148 jedisPool.returnResourceObject(client);
149 ;// 向连接池“归还”资源
150 }
151 }
152
153 /**
154 * 根据key获取list类型
155 *
156 * @param key
157 * @return
158 */
159 public List<String> lrange(String key) {
160 Jedis client = jedisPool.getResource();
161 List<String> returnList = null;
162 try {
163 returnList = client.lrange(key, 0, -1);
164
165 } finally {
166 jedisPool.returnResourceObject(client);
167 ;// 向连接池“归还”资源
168 }
169 return returnList;
170 }
171
172 public List<String> lrange(String key, int start, int length) {
173 Jedis client = jedisPool.getResource();
174 try {
175 return client.lrange(key, start, length);
176 } finally {
177 jedisPool.returnResourceObject(client);
178 }
179 }
180
181 /**
182 * @param key
183 * @param o
184 * @return
185 */
186 public boolean setAnObject(String key, Object o) {
187 Jedis client = jedisPool.getResource();
188 try {
189 String afterSerialize = JSON.toJSONString(o);
190 o = client.set(key, afterSerialize);
191 return true;
192 } finally {
193 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
194 }
195 }
196
197 @SuppressWarnings("unchecked")
198 public <T> T getSetT(String key, T newValue) {
199 Jedis client = jedisPool.getResource();
200 T t;
201 try {
202 String afterSerialize = Util.beanToJson(newValue);
203 String value = client.getSet(key, afterSerialize);
204 t = (T) Util.jsonToBean(value, newValue.getClass());
205 return t;
206 } finally {
207 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
208 }
209 }
210
211 public <T> T getAnObject(String key, Class<T> zz) {
212 Jedis client = jedisPool.getResource();
213 T t;
214 try {
215 String s = client.get(key);
216 if (s == null || s.length() == 0) {
217 return null;
218 }
219 t = Util.jsonToBean(s, zz);
220 } finally {
221 jedisPool.returnResourceObject(client);
222 }
223 return t;
224
225 }
226
227 public List<String> getKeys(String pattern) {
228 Jedis client = jedisPool.getResource();
229 List<String> result = new ArrayList<String>();
230 try {
231 Set<String> set = client.keys(pattern);
232 result.addAll(set);
233 } finally {
234 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
235 }
236 return result;
237 }
238
239 /*
240 * 减
241 * */
242 public boolean delKey(String key) {
243 Jedis client = jedisPool.getResource();
244 try {
245 System.out.println("del key=" + key);
246 client.del(key);
247 return true;
248 } finally {
249 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
250 }
251 }
252
253 /* hsah 类型复制*/
254 public <T> boolean hset(String key, String field, T t) {
255 Jedis client = jedisPool.getResource();
256 try {
257 String afterSerialize = Util.beanToJson(t);
258 client.hset(key, field, afterSerialize);
259 return true;
260 } finally {
261 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
262 }
263
264 }
265
266 /**
267 * 存入的时hash结构的数据
268 *
269 * @param key key
270 * @param map map的key实质为field。
271 * @return
272 */
273 public <T, S> boolean hmset(String key, Map<T, S> map) {
274 Jedis client = jedisPool.getResource();
275 try {
276 Iterator<Map.Entry<T, S>> iterator = map.entrySet().iterator();
277 Map<String, String> stringMap = new HashMap<String, String>();
278 String filed;
279 String value;
280 while (iterator.hasNext()) {
281 Map.Entry<T, S> entry = iterator.next();
282 filed = String.valueOf(entry.getKey());
283 value = Util.beanToJson(entry.getValue());
284 stringMap.put(filed, value);
285 }
286 client.hmset(key, stringMap);
287 return true;
288 } finally {
289 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
290 }
291
292 }
293
294 public <T> T hgetObject(String key, String field, Class<T> cls) {
295 Jedis client = jedisPool.getResource();
296 try {
297 String value = client.hget(key, field);
298 return (T) Util.jsonToBean(value, cls);
299 } finally {
300 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
301 }
302
303 }
304
305 public String hgetString(String key, String field) {
306 Jedis client = jedisPool.getResource();
307 try {
308 String value = client.hget(key, field);
309 return value;
310 } finally {
311 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
312 }
313
314 }
315
316 public Map<String, String> hGetAll(String key) {
317 Jedis client = jedisPool.getResource();
318 try {
319 return client.hgetAll(key);
320 } finally {
321 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
322 }
323
324 }
325
326 public List<String> hkeys(String key) {
327 Jedis client = jedisPool.getResource();
328 try {
329 List<String> fields = new ArrayList<String>();
330 Set<String> set = client.hkeys(key);
331 fields.addAll(set);
332 return fields;
333 } finally {
334 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
335 }
336
337 }
338
339 public List<String> hvals(String key) {
340 Jedis client = jedisPool.getResource();
341 try {
342 List<String> values = client.hvals(key);
343 return values;
344 } finally {
345 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
346 }
347
348 }
349
350 public boolean hexists(String key, String field) {
351 Jedis client = jedisPool.getResource();
352 try {
353 return client.hexists(key, field);
354 } finally {
355 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
356 }
357 }
358
359 /*
360 * 增
361 * */
362 public long incr(String key) {
363 Jedis client = jedisPool.getResource();
364 try {
365 return client.incr(key);
366 } finally {
367 jedisPool.returnResourceObject(client);
368 }
369 }
370
371 public void hdel(String key, String... fields) {
372 Jedis client = jedisPool.getResource();
373 try {
374 client.hdel(key, fields);
375 } finally {
376 jedisPool.returnResourceObject(client);
377 }
378 }
379
380 /**
381 * @param key
382 * @param field
383 */
384 public void lpush(String key, String field) {
385 Jedis client = jedisPool.getResource();
386 try {
387 client.lpush(key, field);
388 } finally {
389 jedisPool.returnResourceObject(client);
390 }
391 }
392
393 public void lpush(String key, Object obj) {
394 Jedis client = jedisPool.getResource();
395 try {
396 String field = Util.beanToJson(obj);
397 client.lpush(key, field);
398 } finally {
399 jedisPool.returnResourceObject(client);
400 }
401 }
402
403 /**
404 * 该方法不适用于普通的调用,该方法只针对于错误日志记录
405 *
406 * @param key
407 * @param field
408 */
409 public void lpushForErrorMsg(String key, String field) {
410 Jedis client = jedisPool.getResource();
411 try {
412 if (client.llen(key) > 1000) {
413 return;
414 }
415 client.lpush(key, field);
416 } finally {
417 jedisPool.returnResourceObject(client);
418 }
419 }
420
421 public long llen(String key) {
422 Jedis client = jedisPool.getResource();
423 try {
424 return client.llen(key);
425 } finally {
426 jedisPool.returnResourceObject(client);
427 }
428 }
429
430 public List<String> blPop(String key, int timeoutSeconds) {
431 Jedis client = jedisPool.getResource();
432 try {
433 return client.blpop(timeoutSeconds, key);
434 } finally {
435 jedisPool.returnResourceObject(client);
436 }
437 }
438
439 public <T> long sadd(String key, String... values) {
440 Jedis client = jedisPool.getResource();
441 try {
442 return client.sadd(key, values);
443 } finally {
444 jedisPool.returnResourceObject(client);
445 }
446 }
447
448 public <T> long sadd(String key, List<T> ts) {
449 Jedis client = jedisPool.getResource();
450 try {
451 if (ts == null || ts.size() == 0) {
452 return 0l;
453 }
454 String[] values = new String[ts.size()];
455 for (int i = 0; i < ts.size(); i++) {
456 values[i] = ts.get(i).toString();
457 }
458 return client.sadd(key, values);
459 } finally {
460 jedisPool.returnResourceObject(client);
461 }
462 }
463
464 public long srem(String key, String... values) {
465 Jedis client = jedisPool.getResource();
466 try {
467 return client.srem(key, values);
468 } finally {
469 jedisPool.returnResourceObject(client);
470 }
471 }
472
473 public <T> long srem(String key, List<T> ts) {
474 Jedis client = jedisPool.getResource();
475 try {
476 if (ts == null || ts.size() == 0) {
477 return 0l;
478 }
479 String[] values = new String[ts.size()];
480 for (int i = 0; i < ts.size(); i++) {
481 values[i] = ts.get(i).toString();
482 }
483 return client.srem(key, values);
484 } finally {
485 jedisPool.returnResourceObject(client);
486 }
487 }
488
489 public Set<String> getByRange(String key, double min, double max) {
490 Jedis client = jedisPool.getResource();
491 try {
492 return client.zrangeByScore(key, min, max);
493 } finally {
494 jedisPool.returnResourceObject(client);
495 }
496 }
497
498 public Long decr(String key) {
499 Jedis client = jedisPool.getResource();
500 try {
501 return client.decr(key);
502 } finally {
503 jedisPool.returnResourceObject(client);
504 }
505 }
506
507 public Long hlen(String key) {
508 Jedis client = jedisPool.getResource();
509 try {
510 return client.hlen(key);
511 } finally {
512 jedisPool.returnResourceObject(client);
513 }
514 }
515
516 public List<String> hmget(String key, String... fields) {
517 Jedis client = jedisPool.getResource();
518 try {
519 return client.hmget(key, fields);
520 } finally {
521 jedisPool.returnResourceObject(client);
522 }
523 }
524
525 /**
526 * 从redis里面得到以 某字符串开头的所有key
527 *
528 * @param str
529 */
530 public Set<String> getKeyByStr(String str) {
531 Jedis client = jedisPool.getResource();
532
533 Set<String> keys = null;
534 try {
535 keys = client.keys(str);
536 } finally {
537 jedisPool.returnResourceObject(client);
538 }
539 return keys;
540 }
541
542 public void ltrim(String key, int start, int stop) {
543 Jedis client = jedisPool.getResource();
544 try {
545 client.ltrim(key, start, stop);
546 } finally {
547 jedisPool.returnResourceObject(client);
548 }
549 }
550
551 /**
552 * @param key
553 * @param seconds
554 * @return
555 */
556 public Long expire(String key, Integer seconds) {
557 Jedis client = jedisPool.getResource();
558 Long success = 1l;
559 try {
560 success = client.expire(key, seconds);
561 } finally {
562 jedisPool.returnResourceObject(client);
563 ;
564 }
565 return success;
566 }
567
568 /**
569 * 存入的时hash结构的数据,并且去掉value中的引号
570 *
571 * @param key key
572 * @param map map的key实质为field。
573 * @return
574 */
575 public <T, S> boolean hmsetWithoutQuotationMarks(String key, Map<T, S> map) {
576 Jedis client = jedisPool.getResource();
577 try {
578 Iterator<Map.Entry<T, S>> iterator = map.entrySet().iterator();
579 Map<String, String> stringMap = new HashMap<String, String>();
580 String filed;
581 String value;
582 while (iterator.hasNext()) {
583 Map.Entry<T, S> entry = iterator.next();
584 filed = String.valueOf(entry.getKey());
585 value = JSON.toJSONString(entry.getValue()).replace(""", "");
586 stringMap.put(filed, value);
587 }
588 client.hmset(key, stringMap);
589 return true;
590 } finally {
591 jedisPool.returnResourceObject(client);// 向连接池“归还”资源
592 }
593 }
594 }