Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server。
pring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端。
Jedis
Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接
Lettuce
Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
lettuce主要利用netty实现与redis的同步和异步通信。
转载链接:https://blog.csdn.net/weixin_37264997/article/details/90026602
SpringBoot集成Jedis:
1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-data-redis</artifactId>
4 </dependency>
5
6 <dependency>
7 <groupId>redis.clients</groupId>
8 <artifactId>jedis</artifactId>
9 <version>2.9.0</version>
10 </dependency>
JedisConfig配置类
1 import lombok.extern.slf4j.Slf4j;
2 import org.springframework.beans.factory.annotation.Value;
3 import org.springframework.context.annotation.Bean;
4 import org.springframework.context.annotation.Configuration;
5 import redis.clients.jedis.JedisPool;
6 import redis.clients.jedis.JedisPoolConfig;
7
8 @Configuration
9 @Slf4j
10 public class JedisConfig {
11
12 @Value("${spring.redis.host}")
13 private String host;
14
15 @Value("${spring.redis.port}")
16 private int port;
17
18 @Value("${spring.redis.timeout}")
19 private int timeout;
20
21 @Value("${spring.redis.jedis.pool.max-idle}")
22 private int maxIdle;
23
24 @Value("${spring.redis.jedis.pool.max-wait}")
25 private long maxWaitMillis;
26
27 @Value("${spring.redis.password}")
28 private String password;
29
30 @Value("${spring.redis.block-when-exhausted}")
31 private boolean blockWhenExhausted;
32
33 @Bean
34 public JedisPool redisPoolFactory() throws Exception{
35 log.info("JedisPool注入成功!!");
36 log.info("redis地址:" + host + ":" + port);
37 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
38 jedisPoolConfig.setMaxIdle(maxIdle);
39 jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
40 // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
41 jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
42 // 是否启用pool的jmx管理功能, 默认true
43 jedisPoolConfig.setJmxEnabled(true);
44 /**
45 * 這裏password參數填null,不然會報錯(jedis連接數據庫沒設置密碼填null,填其他回報這個錯):
46 * Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
47 */
48 JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, null);
49 return jedisPool;
50 }
51
52 }
SerializeUtil
1 import lombok.extern.slf4j.Slf4j;
2 import org.springframework.stereotype.Component;
3 import java.io.*;
4
5 @Component
6 @Slf4j
7 public class SerializeUtil {
8
9 // 序列化
10 public static byte[] serialize(Object object) {
11
12 ObjectOutputStream oos = null;
13 ByteArrayOutputStream baos = null;
14 byte[] bytes = null;
15 try {
16 baos = new ByteArrayOutputStream();
17 oos = new ObjectOutputStream(baos);
18 oos.writeObject(object);
19 bytes = baos.toByteArray();
20 } catch (Exception e) {
21 System.err.println("序列化失败" + e.getMessage());
22 }
23 return bytes;
24 }
25
26 // 反序列化
27 public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
28
29 ByteArrayInputStream bais = null;
30 ObjectInputStream ois = null;
31 try {
32 bais = new ByteArrayInputStream(bytes);
33 ois = new ObjectInputStream(bais);
34 } catch (Exception e) {
35 System.err.println("反序列化失败" + e.getMessage());
36 }
37 return ois.readObject();
38 }
39
40 }
JedisUtil
1 package com.example.redis.util;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.springframework.stereotype.Component;
5 import java.io.ByteArrayInputStream;
6 import java.io.ByteArrayOutputStream;
7 import java.io.ObjectInputStream;
8 import java.io.ObjectOutputStream;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import redis.clients.jedis.BinaryClient.LIST_POSITION;
14 import redis.clients.jedis.Jedis;
15 import redis.clients.jedis.JedisPool;
16 import redis.clients.jedis.SortingParams;
17
18 @Component
19 @Slf4j
20 public class JedisUtil {
21
22 @Autowired
23 private JedisPool jedisPool;
24
25 /**
26 * 通过key获取储存在redis中的value
27 * 并释放连接
28 * @param key
29 * @param indexdb 选择redis库 0-15
30 * @return 成功返回value 失败返回null
31 */
32 public String get(String key,int indexdb) {
33 Jedis jedis = null;
34 String value = null;
35 try {
36 jedis = jedisPool.getResource();
37 jedis.select(indexdb);
38 value = jedis.get(key);
39 log.info(value);
40 } catch (Exception e) {
41
42 log.error(e.getMessage());
43 } finally {
44 returnResource(jedisPool, jedis);
45 }
46 return value;
47 }
48
49 /**
50 * 通过key获取储存在redis中的value
51 * 并释放连接
52 * @param key
53 * @param indexdb 选择redis库 0-15
54 * @return 成功返回value 失败返回null
55 */
56 public byte[] get(byte[] key,int indexdb) {
57 Jedis jedis = null;
58 byte[] value = null;
59 try {
60 jedis = jedisPool.getResource();
61 jedis.select(indexdb);
62 value = jedis.get(key);
63 } catch (Exception e) {
64
65 log.error(e.getMessage());
66 } finally {
67 returnResource(jedisPool, jedis);
68 }
69 return value;
70 }
71 /**
72 * 向redis存入key和value,并释放连接资源
73 * 如果key已经存在 则覆盖
74 * @param key
75 * @param value
76 * @param indexdb 选择redis库 0-15
77 * @return 成功 返回OK 失败返回 0
78 */
79 public String set(String key, String value,int indexdb) {
80 Jedis jedis = null;
81 try {
82 jedis = jedisPool.getResource();
83 jedis.select(indexdb);
84 return jedis.set(key, value);
85 } catch (Exception e) {
86
87 log.error(e.getMessage());
88 return "0";
89 } finally {
90 returnResource(jedisPool, jedis);
91 }
92 }
93 /**
94 * 向redis存入key和value,并释放连接资源
95 * 如果key已经存在 则覆盖
96 * @param key
97 * @param value
98 * @param indexdb 选择redis库 0-15
99 * @return 成功 返回OK 失败返回 0
100 */
101 public String set(byte[] key, byte[] value,int indexdb) {
102 Jedis jedis = null;
103 try {
104 jedis = jedisPool.getResource();
105 jedis.select(indexdb);
106 return jedis.set(key, value);
107 } catch (Exception e) {
108
109 log.error(e.getMessage());
110 return "0";
111 } finally {
112 returnResource(jedisPool, jedis);
113 }
114 }
115 /**
116 * 删除指定的key,也可以传入一个包含key的数组
117 * @param keys 一个key 也可以使 string 数组
118 * @return 返回删除成功的个数
119 */
120 public Long del(String... keys) {
121 Jedis jedis = null;
122 try {
123 jedis = jedisPool.getResource();
124 return jedis.del(keys);
125 } catch (Exception e) {
126
127 log.error(e.getMessage());
128 return 0L;
129 } finally {
130 returnResource(jedisPool, jedis);
131 }
132 }
133 /**
134 * 删除指定的key,也可以传入一个包含key的数组
135 * @param indexdb 选择redis库 0-15
136 * @param keys 一个key 也可以使 string 数组
137 * @return 返回删除成功的个数
138 */
139 public Long del(int indexdb,String... keys) {
140 Jedis jedis = null;
141 try {
142 jedis = jedisPool.getResource();
143 jedis.select(indexdb);
144 return jedis.del(keys);
145 } catch (Exception e) {
146
147 log.error(e.getMessage());
148 return 0L;
149 } finally {
150 returnResource(jedisPool, jedis);
151 }
152 }
153 /**
154 * 删除指定的key,也可以传入一个包含key的数组
155 * @param indexdb 选择redis库 0-15
156 * @param keys 一个key 也可以使 string 数组
157 * @return 返回删除成功的个数
158 */
159 public Long del(int indexdb,byte[]... keys) {
160 Jedis jedis = null;
161 try {
162 jedis = jedisPool.getResource();
163 jedis.select(indexdb);
164 return jedis.del(keys);
165 } catch (Exception e) {
166
167 log.error(e.getMessage());
168 return 0L;
169 } finally {
170 returnResource(jedisPool, jedis);
171 }
172 }
173 /**
174 * 通过key向指定的value值追加值
175 * @param key
176 * @param str
177 * @return 成功返回 添加后value的长度 失败 返回 添加的 value 的长度 异常返回0L
178 */
179 public Long append(String key, String str) {
180 Jedis jedis = null;
181 Long res = null;
182 try {
183 jedis = jedisPool.getResource();
184 res = jedis.append(key, str);
185 } catch (Exception e) {
186
187 log.error(e.getMessage());
188 return 0L;
189 } finally {
190 returnResource(jedisPool, jedis);
191 }
192 return res;
193 }
194
195 /**
196 * 判断key是否存在
197 * @param key
198 * @return true OR false
199 */
200 public Boolean exists(String key) {
201 Jedis jedis = null;
202 try {
203 jedis = jedisPool.getResource();
204 return jedis.exists(key);
205 } catch (Exception e) {
206
207 log.error(e.getMessage());
208 return false;
209 } finally {
210 returnResource(jedisPool, jedis);
211 }
212 }
213
214 /**
215 * 清空当前数据库中的所有 key,此命令从不失败。
216 * @return 总是返回 OK
217 */
218 public String flushDB() {
219 Jedis jedis = null;
220 try {
221 jedis = jedisPool.getResource();
222 return jedis.flushDB();
223 } catch (Exception e) {
224 log.error(e.getMessage());
225 } finally {
226 returnResource(jedisPool, jedis);
227 }
228 return null;
229 }
230
231 /**
232 * 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。
233 * @param key
234 * @param value
235 * 过期时间,单位:秒
236 * @return 成功返回1 如果存在 和 发生异常 返回 0
237 */
238 public Long expire(String key, int value, int indexdb) {
239 Jedis jedis = null;
240 try {
241 jedis = jedisPool.getResource();
242 jedis.select(indexdb);
243 return jedis.expire(key, value);
244 } catch (Exception e) {
245 log.error(e.getMessage());
246 return 0L;
247 } finally {
248 returnResource(jedisPool, jedis);
249 }
250 }
251
252 /**
253 * <p>
254 * 以秒为单位,返回给定 key 的剩余生存时间
255 * </p>
256 *
257 * @param key
258 * @return 当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以秒为单位,返回 key
259 * 的剩余生存时间。 发生异常 返回 0
260 */
261 public Long ttl(String key,int indexdb) {
262 Jedis jedis = null;
263 try {
264 jedis = jedisPool.getResource();
265 jedis.select(indexdb);
266 return jedis.ttl(key);
267 } catch (Exception e) {
268
269 log.error(e.getMessage());
270 return 0L;
271 } finally {
272 returnResource(jedisPool, jedis);
273 }
274 }
275
276 /**
277 * <p>
278 * 移除给定 key 的生存时间,将这个 key 从『易失的』(带生存时间 key )转换成『持久的』(一个不带生存时间、永不过期的 key )
279 * </p>
280 *
281 * @param key
282 * @return 当生存时间移除成功时,返回 1 .如果 key 不存在或 key 没有设置生存时间,返回 0 , 发生异常 返回 -1
283 */
284 public Long persist(String key) {
285 Jedis jedis = null;
286 try {
287 jedis = jedisPool.getResource();
288 return jedis.persist(key);
289 } catch (Exception e) {
290
291 log.error(e.getMessage());
292 return -1L;
293 } finally {
294 returnResource(jedisPool, jedis);
295 }
296 }
297
298 /**
299 * <p>
300 * 新增key,并将 key 的生存时间 (以秒为单位)
301 * </p>
302 *
303 * @param key
304 * @param seconds
305 * 生存时间 单位:秒
306 * @param value
307 * @return 设置成功时返回 OK 。当 seconds 参数不合法时,返回一个错误。
308 */
309 public String setex(String key, int seconds, String value) {
310 Jedis jedis = null;
311 try {
312 jedis = jedisPool.getResource();
313 return jedis.setex(key, seconds, value);
314 } catch (Exception e) {
315
316 log.error(e.getMessage());
317 } finally {
318 returnResource(jedisPool, jedis);
319 }
320 return null;
321 }
322
323 /**
324 * <p>
325 * 设置key value,如果key已经存在则返回0,nx==> not exist
326 * </p>
327 *
328 * @param key
329 * @param value
330 * @return 成功返回1 如果存在 和 发生异常 返回 0
331 */
332 public Long setnx(String key, String value) {
333 Jedis jedis = null;
334 try {
335 jedis = jedisPool.getResource();
336 return jedis.setnx(key, value);
337 } catch (Exception e) {
338
339 log.error(e.getMessage());
340 return 0L;
341 } finally {
342 returnResource(jedisPool, jedis);
343 }
344 }
345
346 /**
347 * <p>
348 * 将给定 key 的值设为 value ,并返回 key 的旧值(old value)。
349 * </p>
350 * <p>
351 * 当 key 存在但不是字符串类型时,返回一个错误。
352 * </p>
353 *
354 * @param key
355 * @param value
356 * @return 返回给定 key 的旧值。当 key 没有旧值时,也即是, key 不存在时,返回 nil
357 */
358 public String getSet(String key, String value) {
359 Jedis jedis = null;
360 try {
361 jedis = jedisPool.getResource();
362 return jedis.getSet(key, value);
363 } catch (Exception e) {
364
365 log.error(e.getMessage());
366 } finally {
367 returnResource(jedisPool, jedis);
368 }
369 return null;
370 }
371
372 /**
373 * <p>
374 * 设置key value并制定这个键值的有效期
375 * </p>
376 *
377 * @param key
378 * @param value
379 * @param seconds
380 * 单位:秒
381 * @return 成功返回OK 失败和异常返回null
382 */
383 public String setex(String key, String value, int seconds) {
384 Jedis jedis = null;
385 String res = null;
386 try {
387 jedis = jedisPool.getResource();
388 res = jedis.setex(key, seconds, value);
389 } catch (Exception e) {
390
391 log.error(e.getMessage());
392 } finally {
393 returnResource(jedisPool, jedis);
394 }
395 return res;
396 }
397
398 /**
399 * <p>
400 * 通过key 和offset 从指定的位置开始将原先value替换
401 * </p>
402 * <p>
403 * 下标从0开始,offset表示从offset下标开始替换
404 * </p>
405 * <p>
406 * 如果替换的字符串长度过小则会这样
407 * </p>
408 * <p>
409 * example:
410 * </p>
411 * <p>
412 * value : bigsea@zto.cn
413 * </p>
414 * <p>
415 * str : abc
416 * </p>
417 * <P>
418 * 从下标7开始替换 则结果为
419 * </p>
420 * <p>
421 * RES : bigsea.abc.cn
422 * </p>
423 *
424 * @param key
425 * @param str
426 * @param offset
427 * 下标位置
428 * @return 返回替换后 value 的长度
429 */
430 public Long setrange(String key, String str, int offset) {
431 Jedis jedis = null;
432 try {
433 jedis = jedisPool.getResource();
434 return jedis.setrange(key, offset, str);
435 } catch (Exception e) {
436
437 log.error(e.getMessage());
438 return 0L;
439 } finally {
440 returnResource(jedisPool, jedis);
441 }
442 }
443
444 /**
445 * <p>
446 * 通过批量的key获取批量的value
447 * </p>
448 *
449 * @param keys
450 * string数组 也可以是一个key
451 * @return 成功返回value的集合, 失败返回null的集合 ,异常返回空
452 */
453 public List<String> mget(String... keys) {
454 Jedis jedis = null;
455 List<String> values = null;
456 try {
457 jedis = jedisPool.getResource();
458 values = jedis.mget(keys);
459 } catch (Exception e) {
460
461 log.error(e.getMessage());
462 } finally {
463 returnResource(jedisPool, jedis);
464 }
465 return values;
466 }
467
468 /**
469 * <p>
470 * 批量的设置key:value,可以一个
471 * </p>
472 * <p>
473 * example:
474 * </p>
475 * <p>
476 * obj.mset(new String[]{"key2","value1","key2","value2"})
477 * </p>
478 *
479 * @param keysvalues
480 * @return 成功返回OK 失败 异常 返回 null
481 *
482 */
483 public String mset(String... keysvalues) {
484 Jedis jedis = null;
485 String res = null;
486 try {
487 jedis = jedisPool.getResource();
488 res = jedis.mset(keysvalues);
489 } catch (Exception e) {
490
491 log.error(e.getMessage());
492 } finally {
493 returnResource(jedisPool, jedis);
494 }
495 return res;
496 }
497
498 /**
499 * <p>
500 * 批量的设置key:value,可以一个,如果key已经存在则会失败,操作会回滚
501 * </p>
502 * <p>
503 * example:
504 * </p>
505 * <p>
506 * obj.msetnx(new String[]{"key2","value1","key2","value2"})
507 * </p>
508 *
509 * @param keysvalues
510 * @return 成功返回1 失败返回0
511 */
512 public Long msetnx(String... keysvalues) {
513 Jedis jedis = null;
514 Long res = 0L;
515 try {
516 jedis = jedisPool.getResource();
517 res = jedis.msetnx(keysvalues);
518 } catch (Exception e) {
519
520 log.error(e.getMessage());
521 } finally {
522 returnResource(jedisPool, jedis);
523 }
524 return res;
525 }
526
527 /**
528 * <p>
529 * 设置key的值,并返回一个旧值
530 * </p>
531 *
532 * @param key
533 * @param value
534 * @return 旧值 如果key不存在 则返回null
535 */
536 public String getset(String key, String value) {
537 Jedis jedis = null;
538 String res = null;
539 try {
540 jedis = jedisPool.getResource();
541 res = jedis.getSet(key, value);
542 } catch (Exception e) {
543
544 log.error(e.getMessage());
545 } finally {
546 returnResource(jedisPool, jedis);
547 }
548 return res;
549 }
550
551 /**
552 * <p>
553 * 通过下标 和key 获取指定下标位置的 value
554 * </p>
555 *
556 * @param key
557 * @param startOffset
558 * 开始位置 从0 开始 负数表示从右边开始截取
559 * @param endOffset
560 * @return 如果没有返回null
561 */
562 public String getrange(String key, int startOffset, int endOffset) {
563 Jedis jedis = null;
564 String res = null;
565 try {
566 jedis = jedisPool.getResource();
567 res = jedis.getrange(key, startOffset, endOffset);
568 } catch (Exception e) {
569
570 log.error(e.getMessage());
571 } finally {
572 returnResource(jedisPool, jedis);
573 }
574 return res;
575 }
576
577 /**
578 * <p>
579 * 通过key 对value进行加值+1操作,当value不是int类型时会返回错误,当key不存在是则value为1
580 * </p>
581 *
582 * @param key
583 * @return 加值后的结果
584 */
585 public Long incr(String key) {
586 Jedis jedis = null;
587 Long res = null;
588 try {
589 jedis = jedisPool.getResource();
590 res = jedis.incr(key);
591 } catch (Exception e) {
592
593 log.error(e.getMessage());
594 } finally {
595 returnResource(jedisPool, jedis);
596 }
597 return res;
598 }
599
600 /**
601 * <p>
602 * 通过key给指定的value加值,如果key不存在,则这是value为该值
603 * </p>
604 *
605 * @param key
606 * @param integer
607 * @return
608 */
609 public Long incrBy(String key, Long integer) {
610 Jedis jedis = null;
611 Long res = null;
612 try {
613 jedis = jedisPool.getResource();
614 res = jedis.incrBy(key, integer);
615 } catch (Exception e) {
616
617 log.error(e.getMessage());
618 } finally {
619 returnResource(jedisPool, jedis);
620 }
621 return res;
622 }
623
624 /**
625 * <p>
626 * 对key的值做减减操作,如果key不存在,则设置key为-1
627 * </p>
628 *
629 * @param key
630 * @return
631 */
632 public Long decr(String key) {
633 Jedis jedis = null;
634 Long res = null;
635 try {
636 jedis = jedisPool.getResource();
637 res = jedis.decr(key);
638 } catch (Exception e) {
639
640 log.error(e.getMessage());
641 } finally {
642 returnResource(jedisPool, jedis);
643 }
644 return res;
645 }
646
647 /**
648 * <p>
649 * 减去指定的值
650 * </p>
651 *
652 * @param key
653 * @param integer
654 * @return
655 */
656 public Long decrBy(String key, Long integer) {
657 Jedis jedis = null;
658 Long res = null;
659 try {
660 jedis = jedisPool.getResource();
661 res = jedis.decrBy(key, integer);
662 } catch (Exception e) {
663
664 log.error(e.getMessage());
665 } finally {
666 returnResource(jedisPool, jedis);
667 }
668 return res;
669 }
670
671 /**
672 * <p>
673 * 通过key获取value值的长度
674 * </p>
675 *
676 * @param key
677 * @return 失败返回null
678 */
679 public Long serlen(String key) {
680 Jedis jedis = null;
681 Long res = null;
682 try {
683 jedis = jedisPool.getResource();
684 res = jedis.strlen(key);
685 } catch (Exception e) {
686
687 log.error(e.getMessage());
688 } finally {
689 returnResource(jedisPool, jedis);
690 }
691 return res;
692 }
693
694 /**
695 * <p>
696 * 通过key给field设置指定的值,如果key不存在,则先创建
697 * </p>
698 *
699 * @param key
700 * @param field
701 * 字段
702 * @param value
703 * @return 如果存在返回0 异常返回null
704 */
705 public Long hset(String key, String field, String value) {
706 Jedis jedis = null;
707 Long res = null;
708 try {
709 jedis = jedisPool.getResource();
710 res = jedis.hset(key, field, value);
711 } catch (Exception e) {
712
713 log.error(e.getMessage());
714 } finally {
715 returnResource(jedisPool, jedis);
716 }
717 return res;
718 }
719
720 /**
721 * <p>
722 * 通过key给field设置指定的值,如果key不存在则先创建,如果field已经存在,返回0
723 * </p>
724 *
725 * @param key
726 * @param field
727 * @param value
728 * @return
729 */
730 public Long hsetnx(String key, String field, String value) {
731 Jedis jedis = null;
732 Long res = null;
733 try {
734 jedis = jedisPool.getResource();
735 res = jedis.hsetnx(key, field, value);
736 } catch (Exception e) {
737
738 log.error(e.getMessage());
739 } finally {
740 returnResource(jedisPool, jedis);
741 }
742 return res;
743 }
744
745 /**
746 * <p>
747 * 通过key同时设置 hash的多个field
748 * </p>
749 *
750 * @param key
751 * @param hash
752 * @return 返回OK 异常返回null
753 */
754 public String hmset(String key, Map<String, String> hash, int indexdb) {
755 Jedis jedis = null;
756 String res = null;
757 try {
758 jedis = jedisPool.getResource();
759 jedis.select(indexdb);
760 res = jedis.hmset(key, hash);
761 } catch (Exception e) {
762
763 log.error(e.getMessage());
764 } finally {
765 returnResource(jedisPool, jedis);
766 }
767 return res;
768 }
769
770 /**
771 * <p>
772 * 通过key 和 field 获取指定的 value
773 * </p>
774 *
775 * @param key
776 * @param field
777 * @return 没有返回null
778 */
779 public String hget(String key, String field) {
780 Jedis jedis = null;
781 String res = null;
782 try {
783 jedis = jedisPool.getResource();
784 res = jedis.hget(key, field);
785 } catch (Exception e) {
786
787 log.error(e.getMessage());
788 } finally {
789 returnResource(jedisPool, jedis);
790 }
791 return res;
792 }
793
794 /**
795 * <p>
796 * 通过key 和 fields 获取指定的value 如果没有对应的value则返回null
797 * </p>
798 *
799 * @param key
800 * @param fields
801 * 可以使 一个String 也可以是 String数组
802 * @return
803 */
804 public List<String> hmget(String key, int indexdb, String... fields) {
805 Jedis jedis = null;
806 List<String> res = null;
807 try {
808 jedis = jedisPool.getResource();
809 jedis.select(indexdb);
810 res = jedis.hmget(key, fields);
811 } catch (Exception e) {
812
813 log.error(e.getMessage());
814 } finally {
815 returnResource(jedisPool, jedis);
816 }
817 return res;
818 }
819
820 /**
821 * <p>
822 * 通过key给指定的field的value加上给定的值
823 * </p>
824 *
825 * @param key
826 * @param field
827 * @param value
828 * @return
829 */
830 public Long hincrby(String key, String field, Long value) {
831 Jedis jedis = null;
832 Long res = null;
833 try {
834 jedis = jedisPool.getResource();
835 res = jedis.hincrBy(key, field, value);
836 } catch (Exception e) {
837
838 log.error(e.getMessage());
839 } finally {
840 returnResource(jedisPool, jedis);
841 }
842 return res;
843 }
844
845 /**
846 * <p>
847 * 通过key和field判断是否有指定的value存在
848 * </p>
849 *
850 * @param key
851 * @param field
852 * @return
853 */
854 public Boolean hexists(String key, String field) {
855 Jedis jedis = null;
856 Boolean res = false;
857 try {
858 jedis = jedisPool.getResource();
859 res = jedis.hexists(key, field);
860 } catch (Exception e) {
861
862 log.error(e.getMessage());
863 } finally {
864 returnResource(jedisPool, jedis);
865 }
866 return res;
867 }
868
869 /**
870 * <p>
871 * 通过key返回field的数量
872 * </p>
873 *
874 * @param key
875 * @return
876 */
877 public Long hlen(String key) {
878 Jedis jedis = null;
879 Long res = null;
880 try {
881 jedis = jedisPool.getResource();
882 res = jedis.hlen(key);
883 } catch (Exception e) {
884
885 log.error(e.getMessage());
886 } finally {
887 returnResource(jedisPool, jedis);
888 }
889 return res;
890
891 }
892
893 /**
894 * <p>
895 * 通过key 删除指定的 field
896 * </p>
897 *
898 * @param key
899 * @param fields
900 * 可以是 一个 field 也可以是 一个数组
901 * @return
902 */
903 public Long hdel(String key, String... fields) {
904 Jedis jedis = null;
905 Long res = null;
906 try {
907 jedis = jedisPool.getResource();
908 res = jedis.hdel(key, fields);
909 } catch (Exception e) {
910
911 log.error(e.getMessage());
912 } finally {
913 returnResource(jedisPool, jedis);
914 }
915 return res;
916 }
917
918 /**
919 * <p>
920 * 通过key返回所有的field
921 * </p>
922 *
923 * @param key
924 * @return
925 */
926 public Set<String> hkeys(String key) {
927 Jedis jedis = null;
928 Set<String> res = null;
929 try {
930 jedis = jedisPool.getResource();
931 res = jedis.hkeys(key);
932 } catch (Exception e) {
933
934 log.error(e.getMessage());
935 } finally {
936 returnResource(jedisPool, jedis);
937 }
938 return res;
939 }
940
941 /**
942 * <p>
943 * 通过key返回所有和key有关的value
944 * </p>
945 *
946 * @param key
947 * @return
948 */
949 public List<String> hvals(String key) {
950 Jedis jedis = null;
951 List<String> res = null;
952 try {
953 jedis = jedisPool.getResource();
954 res = jedis.hvals(key);
955 } catch (Exception e) {
956
957 log.error(e.getMessage());
958 } finally {
959 returnResource(jedisPool, jedis);
960 }
961 return res;
962 }
963
964 /**
965 * <p>
966 * 通过key获取所有的field和value
967 * </p>
968 *
969 * @param key
970 * @return
971 */
972 public Map<String, String> hgetall(String key, int indexdb) {
973 Jedis jedis = null;
974 Map<String, String> res = null;
975 try {
976 jedis = jedisPool.getResource();
977 jedis.select(indexdb);
978 res = jedis.hgetAll(key);
979 } catch (Exception e) {
980 log.error(e.getMessage());
981 } finally {
982 returnResource(jedisPool, jedis);
983 }
984 return res;
985 }
986
987 /**
988 * <p>
989 * 通过key向list头部添加字符串
990 * </p>
991 *
992 * @param key
993 * @param strs
994 * 可以使一个string 也可以使string数组
995 * @return 返回list的value个数
996 */
997 public Long lpush(int indexdb, String key, String... strs) {
998 Jedis jedis = null;
999 Long res = null;
1000 try {
1001 jedis = jedisPool.getResource();
1002 jedis.select(indexdb);
1003 res = jedis.lpush(key, strs);
1004 } catch (Exception e) {
1005
1006 log.error(e.getMessage());
1007 } finally {
1008 returnResource(jedisPool, jedis);
1009 }
1010 return res;
1011 }
1012
1013 /**
1014 * <p>
1015 * 通过key向list尾部添加字符串
1016 * </p>
1017 *
1018 * @param key
1019 * @param strs
1020 * 可以使一个string 也可以使string数组
1021 * @return 返回list的value个数
1022 */
1023 public Long rpush(String key, String... strs) {
1024 Jedis jedis = null;
1025 Long res = null;
1026 try {
1027 jedis = jedisPool.getResource();
1028 res = jedis.rpush(key, strs);
1029 } catch (Exception e) {
1030
1031 log.error(e.getMessage());
1032 } finally {
1033 returnResource(jedisPool, jedis);
1034 }
1035 return res;
1036 }
1037
1038 /**
1039 * <p>
1040 * 通过key在list指定的位置之前或者之后 添加字符串元素
1041 * </p>
1042 *
1043 * @param key
1044 * @param where
1045 * LIST_POSITION枚举类型
1046 * @param pivot
1047 * list里面的value
1048 * @param value
1049 * 添加的value
1050 * @return
1051 */
1052 public Long linsert(String key, LIST_POSITION where, String pivot,
1053 String value) {
1054 Jedis jedis = null;
1055 Long res = null;
1056 try {
1057 jedis = jedisPool.getResource();
1058 res = jedis.linsert(key, where, pivot, value);
1059 } catch (Exception e) {
1060
1061 log.error(e.getMessage());
1062 } finally {
1063 returnResource(jedisPool, jedis);
1064 }
1065 return res;
1066 }
1067
1068 /**
1069 * <p>
1070 * 通过key设置list指定下标位置的value
1071 * </p>
1072 * <p>
1073 * 如果下标超过list里面value的个数则报错
1074 * </p>
1075 *
1076 * @param key
1077 * @param index
1078 * 从0开始
1079 * @param value
1080 * @return 成功返回OK
1081 */
1082 public String lset(String key, Long index, String value) {
1083 Jedis jedis = null;
1084 String res = null;
1085 try {
1086 jedis = jedisPool.getResource();
1087 res = jedis.lset(key, index, value);
1088 } catch (Exception e) {
1089
1090 log.error(e.getMessage());
1091 } finally {
1092 returnResource(jedisPool, jedis);
1093 }
1094 return res;
1095 }
1096
1097 /**
1098 * <p>
1099 * 通过key从对应的list中删除指定的count个 和 value相同的元素
1100 * </p>
1101 *
1102 * @param key
1103 * @param count
1104 * 当count为0时删除全部
1105 * @param value
1106 * @return 返回被删除的个数
1107 */
1108 public Long lrem(String key, long count, String value) {
1109 Jedis jedis = null;
1110 Long res = null;
1111 try {
1112 jedis = jedisPool.getResource();
1113 res = jedis.lrem(key, count, value);
1114 } catch (Exception e) {
1115
1116 log.error(e.getMessage());
1117 } finally {
1118 returnResource(jedisPool, jedis);
1119 }
1120 return res;
1121 }
1122
1123 /**
1124 * <p>
1125 * 通过key保留list中从strat下标开始到end下标结束的value值
1126 * </p>
1127 *
1128 * @param key
1129 * @param start
1130 * @param end
1131 * @return 成功返回OK
1132 */
1133 public String ltrim(String key, long start, long end) {
1134 Jedis jedis = null;
1135 String res = null;
1136 try {
1137 jedis = jedisPool.getResource();
1138 res = jedis.ltrim(key, start, end);
1139 } catch (Exception e) {
1140
1141 log.error(e.getMessage());
1142 } finally {
1143 returnResource(jedisPool, jedis);
1144 }
1145 return res;
1146 }
1147
1148 /**
1149 * <p>
1150 * 通过key从list的头部删除一个value,并返回该value
1151 * </p>
1152 *
1153 * @param key
1154 * @return
1155 */
1156 synchronized public String lpop(String key) {
1157 Jedis jedis = null;
1158 String res = null;
1159 try {
1160 jedis = jedisPool.getResource();
1161 res = jedis.lpop(key);
1162 } catch (Exception e) {
1163
1164 log.error(e.getMessage());
1165 } finally {
1166 returnResource(jedisPool, jedis);
1167 }
1168 return res;
1169 }
1170
1171 /**
1172 * <p>
1173 * 通过key从list尾部删除一个value,并返回该元素
1174 * </p>
1175 *
1176 * @param key
1177 * @return
1178 */
1179 synchronized public String rpop(String key, int indexdb) {
1180 Jedis jedis = null;
1181 String res = null;
1182 try {
1183 jedis = jedisPool.getResource();
1184 jedis.select(indexdb);
1185 res = jedis.rpop(key);
1186 } catch (Exception e) {
1187
1188 log.error(e.getMessage());
1189 } finally {
1190 returnResource(jedisPool, jedis);
1191 }
1192 return res;
1193 }
1194
1195 /**
1196 * <p>
1197 * 通过key从一个list的尾部删除一个value并添加到另一个list的头部,并返回该value
1198 * </p>
1199 * <p>
1200 * 如果第一个list为空或者不存在则返回null
1201 * </p>
1202 *
1203 * @param srckey
1204 * @param dstkey
1205 * @return
1206 */
1207 public String rpoplpush(String srckey, String dstkey, int indexdb) {
1208 Jedis jedis = null;
1209 String res = null;
1210 try {
1211 jedis = jedisPool.getResource();
1212 jedis.select(indexdb);
1213 res = jedis.rpoplpush(srckey, dstkey);
1214 } catch (Exception e) {
1215
1216 log.error(e.getMessage());
1217 } finally {
1218 returnResource(jedisPool, jedis);
1219 }
1220 return res;
1221 }
1222
1223 /**
1224 * <p>
1225 * 通过key获取list中指定下标位置的value
1226 * </p>
1227 *
1228 * @param key
1229 * @param index
1230 * @return 如果没有返回null
1231 */
1232 public String lindex(String key, long index) {
1233 Jedis jedis = null;
1234 String res = null;
1235 try {
1236 jedis = jedisPool.getResource();
1237 res = jedis.lindex(key, index);
1238 } catch (Exception e) {
1239
1240 log.error(e.getMessage());
1241 } finally {
1242 returnResource(jedisPool, jedis);
1243 }
1244 return res;
1245 }
1246
1247 /**
1248 * <p>
1249 * 通过key返回list的长度
1250 * </p>
1251 *
1252 * @param key
1253 * @return
1254 */
1255 public Long llen(String key) {
1256 Jedis jedis = null;
1257 Long res = null;
1258 try {
1259 jedis = jedisPool.getResource();
1260 res = jedis.llen(key);
1261 } catch (Exception e) {
1262
1263 log.error(e.getMessage());
1264 } finally {
1265 returnResource(jedisPool, jedis);
1266 }
1267 return res;
1268 }
1269
1270 /**
1271 * <p>
1272 * 通过key获取list指定下标位置的value
1273 * </p>
1274 * <p>
1275 * 如果start 为 0 end 为 -1 则返回全部的list中的value
1276 * </p>
1277 *
1278 * @param key
1279 * @param start
1280 * @param end
1281 * @return
1282 */
1283 public List<String> lrange(String key, long start, long end, int indexdb) {
1284 Jedis jedis = null;
1285 List<String> res = null;
1286 try {
1287 jedis = jedisPool.getResource();
1288 jedis.select(indexdb);
1289 res = jedis.lrange(key, start, end);
1290 } catch (Exception e) {
1291
1292 log.error(e.getMessage());
1293 } finally {
1294 returnResource(jedisPool, jedis);
1295 }
1296 return res;
1297 }
1298
1299 /**
1300 * <p>
1301 * 将列表 key 下标为 index 的元素的值设置为 value
1302 * </p>
1303 *
1304 * @param key
1305 * @param index
1306 * @param value
1307 * @return 操作成功返回 ok ,否则返回错误信息
1308 */
1309 public String lset(String key, long index, String value) {
1310 Jedis jedis = null;
1311 try {
1312 jedis = jedisPool.getResource();
1313 return jedis.lset(key, index, value);
1314 } catch (Exception e) {
1315
1316 log.error(e.getMessage());
1317 } finally {
1318 returnResource(jedisPool, jedis);
1319 }
1320 return null;
1321 }
1322
1323 /**
1324 * <p>
1325 * 返回给定排序后的结果
1326 * </p>
1327 *
1328 * @param key
1329 * @param sortingParameters
1330 * @return 返回列表形式的排序结果
1331 */
1332 public List<String> sort(String key, SortingParams sortingParameters) {
1333 Jedis jedis = null;
1334 try {
1335 jedis = jedisPool.getResource();
1336 return jedis.sort(key, sortingParameters);
1337 } catch (Exception e) {
1338
1339 log.error(e.getMessage());
1340 } finally {
1341 returnResource(jedisPool, jedis);
1342 }
1343 return null;
1344 }
1345
1346 /**
1347 * <p>
1348 * 返回排序后的结果,排序默认以数字作为对象,值被解释为双精度浮点数,然后进行比较。
1349 * </p>
1350 *
1351 * @param key
1352 * @return 返回列表形式的排序结果
1353 */
1354 public List<String> sort(String key) {
1355 Jedis jedis = null;
1356 try {
1357 jedis = jedisPool.getResource();
1358 return jedis.sort(key);
1359 } catch (Exception e) {
1360
1361 log.error(e.getMessage());
1362 } finally {
1363 returnResource(jedisPool, jedis);
1364 }
1365 return null;
1366 }
1367
1368 /**
1369 * <p>
1370 * 通过key向指定的set中添加value
1371 * </p>
1372 *
1373 * @param key
1374 * @param members
1375 * 可以是一个String 也可以是一个String数组
1376 * @return 添加成功的个数
1377 */
1378 public Long sadd(String key, String... members) {
1379 Jedis jedis = null;
1380 Long res = null;
1381 try {
1382 jedis = jedisPool.getResource();
1383 res = jedis.sadd(key, members);
1384 } catch (Exception e) {
1385
1386 log.error(e.getMessage());
1387 } finally {
1388 returnResource(jedisPool, jedis);
1389 }
1390 return res;
1391 }
1392
1393 /**
1394 * <p>
1395 * 通过key删除set中对应的value值
1396 * </p>
1397 *
1398 * @param key
1399 * @param members
1400 * 可以是一个String 也可以是一个String数组
1401 * @return 删除的个数
1402 */
1403 public Long srem(String key, String... members) {
1404 Jedis jedis = null;
1405 Long res = null;
1406 try {
1407 jedis = jedisPool.getResource();
1408 res = jedis.srem(key, members);
1409 } catch (Exception e) {
1410
1411 log.error(e.getMessage());
1412 } finally {
1413 returnResource(jedisPool, jedis);
1414 }
1415 return res;
1416 }
1417
1418 /**
1419 * <p>
1420 * 通过key随机删除一个set中的value并返回该值
1421 * </p>
1422 *
1423 * @param key
1424 * @return
1425 */
1426 public String spop(String key) {
1427 Jedis jedis = null;
1428 String res = null;
1429 try {
1430 jedis = jedisPool.getResource();
1431 res = jedis.spop(key);
1432 } catch (Exception e) {
1433
1434 log.error(e.getMessage());
1435 } finally {
1436 returnResource(jedisPool, jedis);
1437 }
1438 return res;
1439 }
1440
1441 /**
1442 * <p>
1443 * 通过key获取set中的差集
1444 * </p>
1445 * <p>
1446 * 以第一个set为标准
1447 * </p>
1448 *
1449 * @param keys
1450 * 可以使一个string 则返回set中所有的value 也可以是string数组
1451 * @return
1452 */
1453 public Set<String> sdiff(String... keys) {
1454 Jedis jedis = null;
1455 Set<String> res = null;
1456 try {
1457 jedis = jedisPool.getResource();
1458 res = jedis.sdiff(keys);
1459 } catch (Exception e) {
1460
1461 log.error(e.getMessage());
1462 } finally {
1463 returnResource(jedisPool, jedis);
1464 }
1465 return res;
1466 }
1467
1468 /**
1469 * <p>
1470 * 通过key获取set中的差集并存入到另一个key中
1471 * </p>
1472 * <p>
1473 * 以第一个set为标准
1474 * </p>
1475 *
1476 * @param dstkey
1477 * 差集存入的key
1478 * @param keys
1479 * 可以使一个string 则返回set中所有的value 也可以是string数组
1480 * @return
1481 */
1482 public Long sdiffstore(String dstkey, String... keys) {
1483 Jedis jedis = null;
1484 Long res = null;
1485 try {
1486 jedis = jedisPool.getResource();
1487 res = jedis.sdiffstore(dstkey, keys);
1488 } catch (Exception e) {
1489
1490 log.error(e.getMessage());
1491 } finally {
1492 returnResource(jedisPool, jedis);
1493 }
1494 return res;
1495 }
1496
1497 /**
1498 * <p>
1499 * 通过key获取指定set中的交集
1500 * </p>
1501 *
1502 * @param keys
1503 * 可以使一个string 也可以是一个string数组
1504 * @return
1505 */
1506 public Set<String> sinter(String... keys) {
1507 Jedis jedis = null;
1508 Set<String> res = null;
1509 try {
1510 jedis = jedisPool.getResource();
1511 res = jedis.sinter(keys);
1512 } catch (Exception e) {
1513
1514 log.error(e.getMessage());
1515 } finally {
1516 returnResource(jedisPool, jedis);
1517 }
1518 return res;
1519 }
1520
1521 /**
1522 * <p>
1523 * 通过key获取指定set中的交集 并将结果存入新的set中
1524 * </p>
1525 *
1526 * @param dstkey
1527 * @param keys
1528 * 可以使一个string 也可以是一个string数组
1529 * @return
1530 */
1531 public Long sinterstore(String dstkey, String... keys) {
1532 Jedis jedis = null;
1533 Long res = null;
1534 try {
1535 jedis = jedisPool.getResource();
1536 res = jedis.sinterstore(dstkey, keys);
1537 } catch (Exception e) {
1538
1539 log.error(e.getMessage());
1540 } finally {
1541 returnResource(jedisPool, jedis);
1542 }
1543 return res;
1544 }
1545
1546 /**
1547 * <p>
1548 * 通过key返回所有set的并集
1549 * </p>
1550 *
1551 * @param keys
1552 * 可以使一个string 也可以是一个string数组
1553 * @return
1554 */
1555 public Set<String> sunion(String... keys) {
1556 Jedis jedis = null;
1557 Set<String> res = null;
1558 try {
1559 jedis = jedisPool.getResource();
1560 res = jedis.sunion(keys);
1561 } catch (Exception e) {
1562
1563 log.error(e.getMessage());
1564 } finally {
1565 returnResource(jedisPool, jedis);
1566 }
1567 return res;
1568 }
1569
1570 /**
1571 * <p>
1572 * 通过key返回所有set的并集,并存入到新的set中
1573 * </p>
1574 *
1575 * @param dstkey
1576 * @param keys
1577 * 可以使一个string 也可以是一个string数组
1578 * @return
1579 */
1580 public Long sunionstore(String dstkey, String... keys) {
1581 Jedis jedis = null;
1582 Long res = null;
1583 try {
1584 jedis = jedisPool.getResource();
1585 res = jedis.sunionstore(dstkey, keys);
1586 } catch (Exception e) {
1587
1588 log.error(e.getMessage());
1589 } finally {
1590 returnResource(jedisPool, jedis);
1591 }
1592 return res;
1593 }
1594
1595 /**
1596 * <p>
1597 * 通过key将set中的value移除并添加到第二个set中
1598 * </p>
1599 *
1600 * @param srckey
1601 * 需要移除的
1602 * @param dstkey
1603 * 添加的
1604 * @param member
1605 * set中的value
1606 * @return
1607 */
1608 public Long smove(String srckey, String dstkey, String member) {
1609 Jedis jedis = null;
1610 Long res = null;
1611 try {
1612 jedis = jedisPool.getResource();
1613 res = jedis.smove(srckey, dstkey, member);
1614 } catch (Exception e) {
1615
1616 log.error(e.getMessage());
1617 } finally {
1618 returnResource(jedisPool, jedis);
1619 }
1620 return res;
1621 }
1622
1623 /**
1624 * <p>
1625 * 通过key获取set中value的个数
1626 * </p>
1627 *
1628 * @param key
1629 * @return
1630 */
1631 public Long scard(String key) {
1632 Jedis jedis = null;
1633 Long res = null;
1634 try {
1635 jedis = jedisPool.getResource();
1636 res = jedis.scard(key);
1637 } catch (Exception e) {
1638
1639 log.error(e.getMessage());
1640 } finally {
1641 returnResource(jedisPool, jedis);
1642 }
1643 return res;
1644 }
1645
1646 /**
1647 * <p>
1648 * 通过key判断value是否是set中的元素
1649 * </p>
1650 *
1651 * @param key
1652 * @param member
1653 * @return
1654 */
1655 public Boolean sismember(String key, String member) {
1656 Jedis jedis = null;
1657 Boolean res = null;
1658 try {
1659 jedis = jedisPool.getResource();
1660 res = jedis.sismember(key, member);
1661 } catch (Exception e) {
1662
1663 log.error(e.getMessage());
1664 } finally {
1665 returnResource(jedisPool, jedis);
1666 }
1667 return res;
1668 }
1669
1670 /**
1671 * <p>
1672 * 通过key获取set中随机的value,不删除元素
1673 * </p>
1674 *
1675 * @param key
1676 * @return
1677 */
1678 public String srandmember(String key) {
1679 Jedis jedis = null;
1680 String res = null;
1681 try {
1682 jedis = jedisPool.getResource();
1683 res = jedis.srandmember(key);
1684 } catch (Exception e) {
1685
1686 log.error(e.getMessage());
1687 } finally {
1688 returnResource(jedisPool, jedis);
1689 }
1690 return res;
1691 }
1692
1693 /**
1694 * <p>
1695 * 通过key获取set中所有的value
1696 * </p>
1697 *
1698 * @param key
1699 * @return
1700 */
1701 public Set<String> smembers(String key) {
1702 Jedis jedis = null;
1703 Set<String> res = null;
1704 try {
1705 jedis = jedisPool.getResource();
1706 res = jedis.smembers(key);
1707 } catch (Exception e) {
1708
1709 log.error(e.getMessage());
1710 } finally {
1711 returnResource(jedisPool, jedis);
1712 }
1713 return res;
1714 }
1715
1716 /**
1717 * <p>
1718 * 通过key向zset中添加value,score,其中score就是用来排序的
1719 * </p>
1720 * <p>
1721 * 如果该value已经存在则根据score更新元素
1722 * </p>
1723 *
1724 * @param key
1725 * @param score
1726 * @param member
1727 * @return
1728 */
1729 public Long zadd(String key, double score, String member) {
1730 Jedis jedis = null;
1731 Long res = null;
1732 try {
1733 jedis = jedisPool.getResource();
1734 res = jedis.zadd(key, score, member);
1735 } catch (Exception e) {
1736
1737 log.error(e.getMessage());
1738 } finally {
1739 returnResource(jedisPool, jedis);
1740 }
1741 return res;
1742 }
1743
1744 /**
1745 * <p>
1746 * 返回有序集 key 中,指定区间内的成员。min=0,max=-1代表所有元素
1747 * </p>
1748 *
1749 * @param key
1750 * @param min
1751 * @param max
1752 * @return 指定区间内的有序集成员的列表。
1753 */
1754 public Set<String> zrange(String key, long min, long max) {
1755 Jedis jedis = null;
1756 try {
1757 jedis = jedisPool.getResource();
1758 return jedis.zrange(key, min, max);
1759 } catch (Exception e) {
1760
1761 log.error(e.getMessage());
1762 } finally {
1763 returnResource(jedisPool, jedis);
1764 }
1765 return null;
1766 }
1767
1768 /**
1769 * <p>
1770 * 统计有序集 key 中,值在 min 和 max 之间的成员的数量
1771 * </p>
1772 *
1773 * @param key
1774 * @param min
1775 * @param max
1776 * @return 值在 min 和 max 之间的成员的数量。异常返回0
1777 */
1778 public Long zcount(String key, double min, double max) {
1779 Jedis jedis = null;
1780 try {
1781 jedis = jedisPool.getResource();
1782 return jedis.zcount(key, min, max);
1783 } catch (Exception e) {
1784
1785 log.error(e.getMessage());
1786 return 0L;
1787 } finally {
1788 returnResource(jedisPool, jedis);
1789 }
1790
1791 }
1792
1793 /**
1794 * <p>
1795 * 为哈希表 key 中的域 field 的值加上增量 increment 。增量也可以为负数,相当于对给定域进行减法操作。 如果 key
1796 * 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。如果域 field 不存在,那么在执行命令前,域的值被初始化为 0 。
1797 * 对一个储存字符串值的域 field 执行 HINCRBY 命令将造成一个错误。本操作的值被限制在 64 位(bit)有符号数字表示之内。
1798 * </p>
1799 * <p>
1800 * 将名称为key的hash中field的value增加integer
1801 * </p>
1802 *
1803 * @param key
1804 * @param value
1805 * @param increment
1806 * @return 执行 HINCRBY 命令之后,哈希表 key 中域 field的值。异常返回0
1807 */
1808 public Long hincrBy(String key, String value, long increment) {
1809 Jedis jedis = null;
1810 try {
1811 jedis = jedisPool.getResource();
1812 return jedis.hincrBy(key, value, increment);
1813 } catch (Exception e) {
1814 log.error(e.getMessage());
1815 return 0L;
1816 } finally {
1817 returnResource(jedisPool, jedis);
1818 }
1819
1820 }
1821
1822 /**
1823 * <p>
1824 * 通过key删除在zset中指定的value
1825 * </p>
1826 *
1827 * @param key
1828 * @param members
1829 * 可以使一个string 也可以是一个string数组
1830 * @return
1831 */
1832 public Long zrem(String key, String... members) {
1833 Jedis jedis = null;
1834 Long res = null;
1835 try {
1836 jedis = jedisPool.getResource();
1837 res = jedis.zrem(key, members);
1838 } catch (Exception e) {
1839
1840 log.error(e.getMessage());
1841 } finally {
1842 returnResource(jedisPool, jedis);
1843 }
1844 return res;
1845 }
1846
1847 /**
1848 * <p>
1849 * 通过key增加该zset中value的score的值
1850 * </p>
1851 *
1852 * @param key
1853 * @param score
1854 * @param member
1855 * @return
1856 */
1857 public Double zincrby(String key, double score, String member) {
1858 Jedis jedis = null;
1859 Double res = null;
1860 try {
1861 jedis = jedisPool.getResource();
1862 res = jedis.zincrby(key, score, member);
1863 } catch (Exception e) {
1864
1865 log.error(e.getMessage());
1866 } finally {
1867 returnResource(jedisPool, jedis);
1868 }
1869 return res;
1870 }
1871
1872 /**
1873 * <p>
1874 * 通过key返回zset中value的排名
1875 * </p>
1876 * <p>
1877 * 下标从小到大排序
1878 * </p>
1879 *
1880 * @param key
1881 * @param member
1882 * @return
1883 */
1884 public Long zrank(String key, String member) {
1885 Jedis jedis = null;
1886 Long res = null;
1887 try {
1888 jedis = jedisPool.getResource();
1889 res = jedis.zrank(key, member);
1890 } catch (Exception e) {
1891
1892 log.error(e.getMessage());
1893 } finally {
1894 returnResource(jedisPool, jedis);
1895 }
1896 return res;
1897 }
1898
1899 /**
1900 * <p>
1901 * 通过key返回zset中value的排名
1902 * </p>
1903 * <p>
1904 * 下标从大到小排序
1905 * </p>
1906 *
1907 * @param key
1908 * @param member
1909 * @return
1910 */
1911 public Long zrevrank(String key, String member) {
1912 Jedis jedis = null;
1913 Long res = null;
1914 try {
1915 jedis = jedisPool.getResource();
1916 res = jedis.zrevrank(key, member);
1917 } catch (Exception e) {
1918
1919 log.error(e.getMessage());
1920 } finally {
1921 returnResource(jedisPool, jedis);
1922 }
1923 return res;
1924 }
1925
1926 /**
1927 * <p>
1928 * 通过key将获取score从start到end中zset的value
1929 * </p>
1930 * <p>
1931 * socre从大到小排序
1932 * </p>
1933 * <p>
1934 * 当start为0 end为-1时返回全部
1935 * </p>
1936 *
1937 * @param key
1938 * @param start
1939 * @param end
1940 * @return
1941 */
1942 public Set<String> zrevrange(String key, long start, long end) {
1943 Jedis jedis = null;
1944 Set<String> res = null;
1945 try {
1946 jedis = jedisPool.getResource();
1947 res = jedis.zrevrange(key, start, end);
1948 } catch (Exception e) {
1949
1950 log.error(e.getMessage());
1951 } finally {
1952 returnResource(jedisPool, jedis);
1953 }
1954 return res;
1955 }
1956
1957 /**
1958 * <p>
1959 * 通过key返回指定score内zset中的value
1960 * </p>
1961 *
1962 * @param key
1963 * @param max
1964 * @param min
1965 * @return
1966 */
1967 public Set<String> zrangebyscore(String key, String max, String min) {
1968 Jedis jedis = null;
1969 Set<String> res = null;
1970 try {
1971 jedis = jedisPool.getResource();
1972 res = jedis.zrevrangeByScore(key, max, min);
1973 } catch (Exception e) {
1974
1975 log.error(e.getMessage());
1976 } finally {
1977 returnResource(jedisPool, jedis);
1978 }
1979 return res;
1980 }
1981
1982 /**
1983 * <p>
1984 * 通过key返回指定score内zset中的value
1985 * </p>
1986 *
1987 * @param key
1988 * @param max
1989 * @param min
1990 * @return
1991 */
1992 public Set<String> zrangeByScore(String key, double max, double min) {
1993 Jedis jedis = null;
1994 Set<String> res = null;
1995 try {
1996 jedis = jedisPool.getResource();
1997 res = jedis.zrevrangeByScore(key, max, min);
1998 } catch (Exception e) {
1999
2000 log.error(e.getMessage());
2001 } finally {
2002 returnResource(jedisPool, jedis);
2003 }
2004 return res;
2005 }
2006
2007 /**
2008 * <p>
2009 * 返回指定区间内zset中value的数量
2010 * </p>
2011 *
2012 * @param key
2013 * @param min
2014 * @param max
2015 * @return
2016 */
2017 public Long zcount(String key, String min, String max) {
2018 Jedis jedis = null;
2019 Long res = null;
2020 try {
2021 jedis = jedisPool.getResource();
2022 res = jedis.zcount(key, min, max);
2023 } catch (Exception e) {
2024
2025 log.error(e.getMessage());
2026 } finally {
2027 returnResource(jedisPool, jedis);
2028 }
2029 return res;
2030 }
2031
2032 /**
2033 * <p>
2034 * 通过key返回zset中的value个数
2035 * </p>
2036 *
2037 * @param key
2038 * @return
2039 */
2040 public Long zcard(String key) {
2041 Jedis jedis = null;
2042 Long res = null;
2043 try {
2044 jedis = jedisPool.getResource();
2045 res = jedis.zcard(key);
2046 } catch (Exception e) {
2047
2048 log.error(e.getMessage());
2049 } finally {
2050 returnResource(jedisPool, jedis);
2051 }
2052 return res;
2053 }
2054
2055 /**
2056 * <p>
2057 * 通过key获取zset中value的score值
2058 * </p>
2059 *
2060 * @param key
2061 * @param member
2062 * @return
2063 */
2064 public Double zscore(String key, String member) {
2065 Jedis jedis = null;
2066 Double res = null;
2067 try {
2068 jedis = jedisPool.getResource();
2069 res = jedis.zscore(key, member);
2070 } catch (Exception e) {
2071
2072 log.error(e.getMessage());
2073 } finally {
2074 returnResource(jedisPool, jedis);
2075 }
2076 return res;
2077 }
2078
2079 /**
2080 * <p>
2081 * 通过key删除给定区间内的元素
2082 * </p>
2083 *
2084 * @param key
2085 * @param start
2086 * @param end
2087 * @return
2088 */
2089 public Long zremrangeByRank(String key, long start, long end) {
2090 Jedis jedis = null;
2091 Long res = null;
2092 try {
2093 jedis = jedisPool.getResource();
2094 res = jedis.zremrangeByRank(key, start, end);
2095 } catch (Exception e) {
2096
2097 log.error(e.getMessage());
2098 } finally {
2099 returnResource(jedisPool, jedis);
2100 }
2101 return res;
2102 }
2103
2104 /**
2105 * <p>
2106 * 通过key删除指定score内的元素
2107 * </p>
2108 *
2109 * @param key
2110 * @param start
2111 * @param end
2112 * @return
2113 */
2114 public Long zremrangeByScore(String key, double start, double end) {
2115 Jedis jedis = null;
2116 Long res = null;
2117 try {
2118 jedis = jedisPool.getResource();
2119 res = jedis.zremrangeByScore(key, start, end);
2120 } catch (Exception e) {
2121
2122 log.error(e.getMessage());
2123 } finally {
2124 returnResource(jedisPool, jedis);
2125 }
2126 return res;
2127 }
2128
2129 /**
2130 * <p>
2131 * 返回满足pattern表达式的所有key
2132 * </p>
2133 * <p>
2134 * keys(*)
2135 * </p>
2136 * <p>
2137 * 返回所有的key
2138 * </p>
2139 *
2140 * @param pattern
2141 * @return
2142 */
2143 public Set<String> keys(String pattern) {
2144 Jedis jedis = null;
2145 Set<String> res = null;
2146 try {
2147 jedis = jedisPool.getResource();
2148 res = jedis.keys(pattern);
2149 } catch (Exception e) {
2150
2151 log.error(e.getMessage());
2152 } finally {
2153 returnResource(jedisPool, jedis);
2154 }
2155 return res;
2156 }
2157
2158 public Set<String> keysBySelect(String pattern,int database) {
2159 Jedis jedis = null;
2160 Set<String> res = null;
2161 try {
2162 jedis = jedisPool.getResource();
2163 jedis.select(database);
2164 res = jedis.keys(pattern);
2165 } catch (Exception e) {
2166
2167 log.error(e.getMessage());
2168 } finally {
2169 returnResource(jedisPool, jedis);
2170 }
2171 return res;
2172 }
2173
2174
2175 /**
2176 * 通过key判断值得类型
2177 * @param key
2178 * @return
2179 */
2180 public String type(String key) {
2181 Jedis jedis = null;
2182 String res = null;
2183 try {
2184 jedis = jedisPool.getResource();
2185 res = jedis.type(key);
2186 } catch (Exception e) {
2187
2188 log.error(e.getMessage());
2189 } finally {
2190 returnResource(jedisPool, jedis);
2191 }
2192 return res;
2193 }
2194
2195 /**
2196 * 序列化对象
2197 * @param obj
2198 * @return
2199 * 对象需实现Serializable接口
2200 */
2201 public static byte[] ObjTOSerialize(Object obj) {
2202 ObjectOutputStream oos = null;
2203 ByteArrayOutputStream byteOut = null;
2204 try {
2205 byteOut = new ByteArrayOutputStream();
2206 oos = new ObjectOutputStream(byteOut);
2207 oos.writeObject(obj);
2208 byte[] bytes = byteOut.toByteArray();
2209 return bytes;
2210 } catch (Exception e) {
2211 }
2212 return null;
2213 }
2214
2215 /**
2216 * 反序列化对象
2217 * @param bytes
2218 * @return
2219 * 对象需实现Serializable接口
2220 */
2221 public static Object unserialize(byte[] bytes) {
2222 ByteArrayInputStream bais = null;
2223 try {
2224 //反序列化
2225 bais = new ByteArrayInputStream(bytes);
2226 ObjectInputStream ois = new ObjectInputStream(bais);
2227 return ois.readObject();
2228 } catch (Exception e) {
2229 }
2230 return null;
2231 }
2232
2233 /**
2234 * 返还到连接池
2235 *
2236 * @param jedisPool
2237 * @param jedis
2238 */
2239 public static void returnResource(JedisPool jedisPool, Jedis jedis) {
2240 if (jedis != null) {
2241 jedisPool.returnResource(jedis);
2242 }
2243 }
2244 }
application.properties
1 # Redis数据库索引(默认为0)
2 spring.redis.database=0
3 # Redis服务器地址
4 spring.redis.host=127.0.0.1
5 # Redis服务器连接端口
6 spring.redis.port=6379
7 # Redis服务器连接密码(默认为空)
8 spring.redis.password=
9 # 连接超时时间(毫秒)
10 spring.redis.timeout=1000
11
12 # 连接池最大连接数(使用负值表示没有限制)
13 spring.redis.jedis.pool.max-active=200
14 # 连接池最大阻塞等待时间(使用负值表示没有限制)
15 spring.redis.jedis.pool.max-wait=1000
16 # 连接池中的最大空闲连接
17 spring.redis.jedis.pool.max-idle=8
18 # 连接池中的最小空闲连接
19 spring.redis.jedis.pool.min-idle=0
20 # 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
21 spring.redis.block-when-exhausted=true
Jedis单元测试
1 import com.example.redis.pojo.User;
2 import com.example.redis.util.JedisUtil;
3 import com.example.redis.util.SerializeUtil;
4 import org.junit.Test;
5 import org.junit.runner.RunWith;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.test.context.SpringBootTest;
8 import org.springframework.data.redis.core.RedisTemplate;
9 import org.springframework.data.redis.core.ValueOperations;
10 import org.springframework.test.context.junit4.SpringRunner;
11 import redis.clients.jedis.JedisPool;
12 import java.io.IOException;
13
14 @RunWith(SpringRunner.class)
15 @SpringBootTest
16 public class RedisApplicationTests {
17
18 @Autowired
19 private JedisUtil redisUtil;
20 @Autowired
21 private SerializeUtil serializeUtil;
22 @Autowired
23 private JedisPool jedisPool;
24
25 /**
26 * 測試jedis連接池
27 */
28 @Test
29 public void testJedis() {
30 User user = new User(1L, "問女何所思", "000000002", "123456");
31 User user1 = new User(1L, "問女何所憶", "000000003", "123456");
32 String set = redisUtil.set("qq".getBytes(), serializeUtil.serialize(user), 0);
33 //jedisPool.getResource().set("qq".getBytes(), serializeUtil.serialize(user));
34 String set1 = redisUtil.set("weixin".getBytes(), serializeUtil.serialize(user1), 0);
35 //jedisPool.getResource().set("weixin".getBytes(), serializeUtil.serialize(user1));
36
37 byte[] bytes = redisUtil.get("qq".getBytes(), 0);
38 System.out.println("redis存放結果: " + bytes);
39 byte[] bytes1 = redisUtil.get("weixin".getBytes(), 0);
40 System.out.println("redis存放結果: " + bytes1);
41 try {
42 User user2 = (User)serializeUtil.deserialize(bytes);
43 System.out.println(user2);
44
45 User user3 = (User)serializeUtil.deserialize(bytes1);
46 System.out.println(user3);
47 } catch (IOException e) {
48 e.printStackTrace();
49 } catch (ClassNotFoundException e) {
50 e.printStackTrace();
51 }
52 }
53
54 }
SpringBoot集成Lettuce连接池:
1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-data-redis</artifactId>
4 </dependency>
5
6 <!--lettuce pool連接池-->
7 <dependency>
8 <groupId>org.apache.commons</groupId>
9 <artifactId>commons-pool2</artifactId>
10 </dependency>
RedisConfig
1 import org.springframework.context.annotation.Bean;
2 import org.springframework.context.annotation.Configuration;
3 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
4 import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
5 import org.springframework.data.redis.core.RedisTemplate;
6 import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
7 import org.springframework.data.redis.serializer.StringRedisSerializer;
8 import java.io.Serializable;
9
10 @Configuration
11 public class RedisConfig {
12
13 @Bean
14 public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
15 RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
16 redisTemplate.setKeySerializer(new StringRedisSerializer());
17 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
18 redisTemplate.setConnectionFactory(connectionFactory);
19 //redisTemplate.setConnectionFactory(new JedisConnectionFactory());
20 return redisTemplate;
21 }
22
23 }
application.properties
1 # Redis数据库索引(默认为0)
2 spring.redis.database=0
3 # Redis服务器地址
4 spring.redis.host=127.0.0.1
5 # Redis服务器连接端口
6 spring.redis.port=6379
7 # Redis服务器连接密码(默认为空)
8 spring.redis.password=
9 # 连接池最大连接数(使用负值表示没有限制)
10 spring.redis.lettuce.pool.max-active=200
11 # 连接池最大阻塞等待时间(使用负值表示没有限制)
12 spring.redis.lettuce.pool.max-wait=-1
13 # 连接池中的最大空闲连接
14 spring.redis.lettuce.pool.max-idle=10
15 # 连接池中的最小空闲连接
16 spring.redis.lettuce.pool.min-idle=0
17 # 连接超时时间(毫秒)
18 spring.redis.timeout=1000
Lettuce单元测试
1 import com.example.redis.pojo.User;
2 import com.example.redis.util.JedisUtil;
3 import com.example.redis.util.SerializeUtil;
4 import org.junit.Test;
5 import org.junit.runner.RunWith;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.test.context.SpringBootTest;
8 import org.springframework.data.redis.core.RedisTemplate;
9 import org.springframework.data.redis.core.ValueOperations;
10 import org.springframework.test.context.junit4.SpringRunner;
11 import redis.clients.jedis.JedisPool;
12 import java.io.IOException;
13
14 @RunWith(SpringRunner.class)
15 @SpringBootTest
16 public class RedisApplicationTests {
17
18 @Autowired
19 private RedisTemplate redisTemplate;
20
21 /**
22 * 測試redisTemplate
23 */
24 @Test
25 public void contextLoads() {
26 User user = new User(1L, "666先生的救贖", "000000001", "123456");
27 ValueOperations<String, User> operations = redisTemplate.opsForValue();
28 operations.set("maxinhai", user);
29 Boolean exists = redisTemplate.hasKey("maxinhai");
30 System.out.println("redis是否在存在相應的key: " + exists);
31 User user1 = (User)redisTemplate.opsForValue().get("maxinhai");
32 System.out.println(user1);
33 }
34
35 }
redis存储的对象要实现Serializable接口。