redis + 主从 + 持久化 + 分片 + 集群 + spring集成(2)
10.5 RedisClient.java
1 package com.commons.redis; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Set; 6 7 import org.apache.commons.lang3.StringUtils; 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import redis.clients.jedis.BinaryClient; 12 import redis.clients.jedis.Jedis; 13 import redis.clients.jedis.JedisPool; 14 15 public class RedisClient { 16 private static final Log LOG = LogFactory.getLog(RedisClient.class); 17 private RedisClientConfig redisClientConfig; 18 private JedisPool jedisPool; 19 20 public RedisClient(RedisClientConfig redisClientConfig) { 21 this.redisClientConfig = redisClientConfig; 22 init(); 23 } 24 25 /** 26 * 27 * @描述 : 初始化,配置ip和端口 28 * @创建时间: 2015年7月20日下午1:43:29 29 * 30 */ 31 private void init() { 32 LOG.info('redis client init start~'); 33 String ip = ''; 34 int port; 35 if (StringUtils.isNotBlank(this.redisClientConfig.getIpConfString())) { 36 String[] ipPortArray = this.redisClientConfig.getIpConfString().split(':'); 37 if (ipPortArray.length == 1) { 38 throw new RedisInitializerException(ipPortArray + ' is not include host:port or host:port after split ':''); 39 } 40 ip = ipPortArray[0]; 41 port = Integer.valueOf(ipPortArray[1]).intValue(); 42 } else { 43 throw new RuntimeException('init throw exception'); 44 } 45 LOG.info('write redis client connect ip:' + ip + ',port:' + port); 46 this.jedisPool = new JedisPool(this.redisClientConfig.getJedisPoolConfig(), ip, port); 47 } 48 49 /** 50 * 51 * @描述 : 设置键值对 52 * @创建时间: 2015年7月20日下午1:44:10 53 * 54 * @param key 55 * @param value 56 * @return 57 * @throws RedisAccessException 58 */ 59 public String set(String key, String value) throws RedisAccessException { 60 Jedis client = null; 61 try { 62 client = jedisPool.getResource(); 63 return client.set(key, value); 64 } catch (Exception e) { 65 LOG.error(e.getMessage(), e); 66 throw new RedisAccessException(e); 67 } finally { 68 if (null != client) { 69 jedisPool.returnResourceObject(client); 70 } 71 } 72 } 73 74 /** 75 * 76 * @描述 : 将值 value 关联到 key ,并将 key 的生存时间设为 seconds (以秒为单位)。如果 key 已经存在, SETEX 77 * 命令将覆写旧值。 78 * @创建时间: 2015年7月20日下午1:44:36 79 * 80 * @param key 81 * @param seconds 82 * @param value 83 * @return 84 * @throws RedisAccessException 85 */ 86 public String set(String key, int seconds, String value) throws RedisAccessException { 87 Jedis client = null; 88 try { 89 client = jedisPool.getResource(); 90 return client.setex(key, seconds, value); 91 } catch (Exception e) { 92 LOG.error(e.getMessage(), e); 93 throw new RedisAccessException(e); 94 } finally { 95 if (null != client) { 96 jedisPool.returnResourceObject(client); 97 } 98 } 99 } 100 101 /** 102 * 103 * @描述 : 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 如果 key 不存在, 104 * APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 105 * @创建时间: 2015年7月20日下午1:45:55 106 * 107 * @param key 108 * @param value 109 * @return 110 * @throws RedisAccessException 111 */ 112 public Long append(String key, String value) throws RedisAccessException { 113 Jedis client = null; 114 try { 115 client = jedisPool.getResource(); 116 return client.append(key, value); 117 } catch (Exception e) { 118 LOG.error(e.getMessage(), e); 119 throw new RedisAccessException(e); 120 } finally { 121 if (null != client) { 122 jedisPool.returnResourceObject(client); 123 } 124 } 125 } 126 127 /** 128 * 129 * @描述 : 返回 key 所关联的字符串值。如果 key 不存在那么返回特殊值 null 。 假如 key 130 * 储存的值不是字符串类型,返回一个错误,因为 GET 只能用于处理字符串值。 131 * @创建时间: 2015年7月20日下午1:47:10 132 * 133 * @param key 134 * @return 135 * @throws RedisAccessException 136 */ 137 public String get(String key) throws RedisAccessException { 138 Jedis client = null; 139 try { 140 client = jedisPool.getResource(); 141 return client.get(key); 142 } catch (Exception e) { 143 LOG.error(e.getMessage(), e); 144 throw new RedisAccessException(e); 145 } finally { 146 if (null != client) { 147 jedisPool.returnResourceObject(client); 148 } 149 } 150 } 151 152 /** 153 * 154 * @描述 : 删除给定的一个或多个 key 。 不存在的 key 会被忽略。 155 * @创建时间: 2015年7月20日下午1:48:37 156 * 157 * @param key 158 * @return 159 * @throws RedisAccessException 160 */ 161 public Long del(String key) throws RedisAccessException { 162 Jedis client = null; 163 try { 164 client = jedisPool.getResource(); 165 return client.del(key); 166 } catch (Exception e) { 167 LOG.error(e.getMessage(), e); 168 throw new RedisAccessException(e); 169 } finally { 170 if (null != client) { 171 jedisPool.returnResourceObject(client); 172 } 173 } 174 } 175 176 /** 177 * 178 * @描述 : 检查给定 key 是否存在。 179 * @创建时间: 2015年7月20日下午1:48:48 180 * 181 * @param key 182 * @return 183 * @throws RedisAccessException 184 */ 185 public Boolean exists(String key) throws RedisAccessException { 186 Jedis client = null; 187 try { 188 client = jedisPool.getResource(); 189 return client.exists(key); 190 } catch (Exception e) { 191 LOG.error(e.getMessage(), e); 192 throw new RedisAccessException(e); 193 } finally { 194 if (null != client) { 195 jedisPool.returnResourceObject(client); 196 } 197 } 198 } 199 200 /** 201 * @描述 : 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。 202 * @创建时间: 2015年7月20日下午1:49:29 203 * 204 * @param key 205 * @param seconds 206 * @return 207 * @throws RedisAccessException 208 */ 209 public Long expire(String key, int seconds) throws RedisAccessException { 210 Jedis client = null; 211 try { 212 client = jedisPool.getResource(); 213 return client.expire(key, seconds); 214 } catch (Exception e) { 215 LOG.error(e.getMessage(), e); 216 throw new RedisAccessException(e); 217 } finally { 218 if (null != client) { 219 jedisPool.returnResourceObject(client); 220 } 221 } 222 } 223 224 225 226 /** 227 * @描述 : 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。 228 * @创建时间: 2015年7月20日下午1:49:29 229 * 230 * @param key 231 * @param seconds 232 * @return 233 * @throws RedisAccessException 234 */ 235 public Long expireAt(String key, long unixTime) throws RedisAccessException { 236 Jedis client = null; 237 try { 238 client = jedisPool.getResource(); 239 return client.expireAt(key,unixTime); 240 } catch (Exception e) { 241 LOG.error(e.getMessage(), e); 242 throw new RedisAccessException(e); 243 } finally { 244 if (null != client) { 245 jedisPool.returnResourceObject(client); 246 } 247 } 248 } 249 250 /** 251 * 252 * @描述 : 将给定 key 的值设为 value ,并返回 key 的旧值(old value)。当 key 253 * 存在但不是字符串类型时,返回一个错误。 254 * @创建时间: 2015年7月20日下午1:50:28 255 * 256 * @param key 257 * @param value 258 * @return 259 * @throws RedisAccessException 260 */ 261 public String getSet(String key, String value) throws RedisAccessException { 262 Jedis client = null; 263 try { 264 client = jedisPool.getResource(); 265 return client.getSet(key, value); 266 } catch (Exception e) { 267 LOG.error(e.getMessage(), e); 268 throw new RedisAccessException(e); 269 } finally { 270 if (null != client) { 271 jedisPool.returnResourceObject(client); 272 } 273 } 274 } 275 276 /** 277 * 278 * @描述 : 将 key 的值设为 value ,当且仅当 key 不存在。若给定的 key 已经存在,则 SETNX 不做任何动作。 279 * @创建时间: 2015年7月20日下午1:52:07 280 * 281 * @param key 282 * @param value 283 * @return 284 * @throws RedisAccessException 285 */ 286 public Long setnx(String key, String value) throws RedisAccessException { 287 Jedis client = null; 288 try { 289 client = jedisPool.getResource(); 290 return client.setnx(key, value); 291 } catch (Exception e) { 292 LOG.error(e.getMessage(), e); 293 throw new RedisAccessException(e); 294 } finally { 295 if (null != client) { 296 jedisPool.returnResourceObject(client); 297 } 298 } 299 } 300 301 /** 302 * 303 * @描述 : 将哈希表 key 中的域 field 的值设为 value 。 如果 key 不存在,一个新的哈希表被创建并进行 HSET操作。 304 * 如果域 field 已经存在于哈希表中,旧值将被覆盖。 305 * @创建时间: 2015年7月20日下午1:53:23 306 * 307 * @param key 308 * @param field 309 * @param value 310 * @return 311 * @throws RedisAccessException 312 */ 313 public Long hset(String key, String field, String value) throws RedisAccessException { 314 Jedis client = null; 315 try { 316 client = jedisPool.getResource(); 317 return client.hset(key, field, value); 318 } catch (Exception e) { 319 LOG.error(e.getMessage(), e); 320 throw new RedisAccessException(e); 321 } finally { 322 if (null != client) { 323 jedisPool.returnResourceObject(client); 324 } 325 } 326 } 327 328 /** 329 * 330 * @描述 :返回哈希表 key 中给定域 field 的值。 331 * @创建时间: 2015年7月20日下午1:54:24 332 * 333 * @param key 334 * @param field 335 * @return 336 * @throws RedisAccessException 337 */ 338 public String hget(String key, String field) throws RedisAccessException { 339 Jedis client = null; 340 try { 341 client = jedisPool.getResource(); 342 return client.hget(key, field); 343 } catch (Exception e) { 344 LOG.error(e.getMessage(), e); 345 throw new RedisAccessException(e); 346 } finally { 347 if (null != client) { 348 jedisPool.returnResourceObject(client); 349 } 350 } 351 } 352 353 /** 354 * 355 * @描述 : 将哈希表 key 中的域 field 的值设置为 value ,当且仅当域 field 不存在。若域 field 356 * 已经存在,该操作无效。 如果 key 不存在,一个新哈希表被创建并执行 HSETNX 命令。 357 * @创建时间: 2015年7月20日下午1:54:44 358 * 359 * @param key 360 * @param field 361 * @param value 362 * @return 363 * @throws RedisAccessException 364 */ 365 public Long hsetnx(String key, String field, String value) throws RedisAccessException { 366 Jedis client = null; 367 try { 368 client = jedisPool.getResource(); 369 return client.hsetnx(key, field, value); 370 } catch (Exception e) { 371 LOG.error(e.getMessage(), e); 372 throw new RedisAccessException(e); 373 } finally { 374 if (null != client) { 375 jedisPool.returnResourceObject(client); 376 } 377 } 378 } 379 380 /** 381 * 382 * @描述 :同时将多个 field-value (域-值)对设置到哈希表 key 中。此命令会覆盖哈希表中已存在的域。 如果 key 383 * 不存在,一个空哈希表被创建并执行 HMSET 操作。 384 * @创建时间: 2015年7月20日下午1:58:26 385 * 386 * @param key 387 * @param hash 388 * @return 389 * @throws RedisAccessException 390 */ 391 public String hmset(String key, Map<String, String> hash) throws RedisAccessException { 392 Jedis client = null; 393 try { 394 client = jedisPool.getResource(); 395 return client.hmset(key, hash); 396 } catch (Exception e) { 397 LOG.error(e.getMessage(), e); 398 throw new RedisAccessException(e); 399 } finally { 400 if (null != client) { 401 jedisPool.returnResourceObject(client); 402 } 403 } 404 } 405 406 /** 407 * 408 * @描述 : 返回哈希表 key 中,一个或多个给定域的值。如果给定的域不存在于哈希表,那么返回一个 null 值。 因为不存在的 key 409 * 被当作一个空哈希表来处理,所以对一个不存在的 key 进行 HMGET 操作将返回一个只带有 null 值的表。 410 * @创建时间: 2015年7月20日下午1:57:32 411 * 412 * @param key 413 * @param fields 414 * @return 415 * @throws RedisAccessException 416 */ 417 public List<String> hmget(String key, String[] fields) throws RedisAccessException { 418 Jedis client = null; 419 try { 420 client = jedisPool.getResource(); 421 return client.hmget(key, fields); 422 } catch (Exception e) { 423 LOG.error(e.getMessage(), e); 424 throw new RedisAccessException(e); 425 } finally { 426 if (null != client) { 427 jedisPool.returnResourceObject(client); 428 } 429 } 430 } 431 432 /** 433 * 434 * @描述 :删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。 435 * @创建时间: 2015年7月20日下午1:59:32 436 * 437 * @param key 438 * @param fields 439 * @return 440 * @throws RedisAccessException 441 */ 442 public Long hdel(String key, String[] fields) throws RedisAccessException { 443 Jedis client = null; 444 try { 445 client = jedisPool.getResource(); 446 return client.hdel(key, fields); 447 } catch (Exception e) { 448 LOG.error(e.getMessage(), e); 449 throw new RedisAccessException(e); 450 } finally { 451 if (null != client) { 452 jedisPool.returnResourceObject(client); 453 } 454 } 455 } 456 457 /** 458 * 459 * @描述 : 返回哈希表 key 中域的数量。 460 * @创建时间: 2015年7月20日下午2:00:13 461 * 462 * @param key 463 * @return 464 * @throws RedisAccessException 465 */ 466 public Long hlen(String key) throws RedisAccessException { 467 Jedis client = null; 468 try { 469 client = jedisPool.getResource(); 470 return client.hlen(key); 471 } catch (Exception e) { 472 LOG.error(e.getMessage(), e); 473 throw new RedisAccessException(e); 474 } finally { 475 if (null != client) { 476 jedisPool.returnResourceObject(client); 477 } 478 } 479 } 480 481 /** 482 * 483 * @描述 :返回哈希表 key 中的所有域。 484 * @创建时间: 2015年7月20日下午2:00:59 485 * 486 * @param key 487 * @return 488 * @throws RedisAccessException 489 */ 490 public Set<String> hkeys(String key) throws RedisAccessException { 491 Jedis client = null; 492 try { 493 client = jedisPool.getResource(); 494 return client.hkeys(key); 495 } catch (Exception e) { 496 LOG.error(e.getMessage(), e); 497 throw new RedisAccessException(e); 498 } finally { 499 if (null != client) { 500 jedisPool.returnResourceObject(client); 501 } 502 } 503 } 504 505 /** 506 * 507 * @描述 : 返回哈希表 key 中所有域的值。 508 * @创建时间: 2015年7月20日下午2:01:06 509 * 510 * @param key 511 * @return 512 * @throws RedisAccessException 513 */ 514 public List<String> hvals(String key) throws RedisAccessException { 515 Jedis client = null; 516 try { 517 client = jedisPool.getResource(); 518 return client.hvals(key); 519 } catch (Exception e) { 520 LOG.error(e.getMessage(), e); 521 throw new RedisAccessException(e); 522 } finally { 523 if (null != client) { 524 jedisPool.returnResourceObject(client); 525 } 526 } 527 } 528 529 /** 530 * 531 * @描述 : 返回哈希表 key 中,所有的域和值。在返回值里,紧跟每个域名(field 532 * name)之后是域的值(value),所以返回值的长度是哈希表大小的两倍。 533 * @创建时间: 2015年7月20日下午2:01:36 534 * 535 * @param key 536 * @return 537 * @throws RedisAccessException 538 */ 539 public Map<String, String> hgetAll(String key) throws RedisAccessException { 540 Jedis client = null; 541 try { 542 client = jedisPool.getResource(); 543 return client.hgetAll(key); 544 } catch (Exception e) { 545 LOG.error(e.getMessage(), e); 546 throw new RedisAccessException(e); 547 } finally { 548 if (null != client) { 549 jedisPool.returnResourceObject(client); 550 } 551 } 552 } 553 554 /** 555 * 556 * @描述 :查看哈希表 key 中,给定域 field 是否存在。 557 * @创建时间: 2015年7月20日下午2:02:38 558 * 559 * @param key 560 * @param field 561 * @return 562 * @throws RedisAccessException 563 */ 564 public Boolean hexists(String key, String field) throws RedisAccessException { 565 Jedis client = null; 566 try { 567 client = jedisPool.getResource(); 568 return client.hexists(key, field); 569 } catch (Exception e) { 570 LOG.error(e.getMessage(), e); 571 throw new RedisAccessException(e); 572 } finally { 573 if (null != client) { 574 jedisPool.returnResourceObject(client); 575 } 576 } 577 } 578 579 /** 580 * 581 * @描述 : 将一个或多个值 value 插入到列表 key 的表尾(最右边)。 582 * @创建时间: 2015年7月20日下午2:02:44 583 * 584 * @param key 585 * @param values 586 * @return 587 * @throws RedisAccessException 588 */ 589 public Long rpush(String key, String[] values) throws RedisAccessException { 590 Jedis client = null; 591 try { 592 client = jedisPool.getResource(); 593 return client.rpush(key, values); 594 } catch (Exception e) { 595 LOG.error(e.getMessage(), e); 596 throw new RedisAccessException(e); 597 } finally { 598 if (null != client) { 599 jedisPool.returnResourceObject(client); 600 } 601 } 602 } 603 604 /** 605 * 606 * @描述 : 将值 value 插入到列表 key 的表尾,当且仅当 key 存在并且是一个列表。 607 * @创建时间: 2015年7月20日下午2:03:20 608 * 609 * @param key 610 * @param value 611 * @return 612 * @throws RedisAccessException 613 */ 614 public Long rpushx(String key, String value) throws RedisAccessException { 615 Jedis client = null; 616 try { 617 client = jedisPool.getResource(); 618 return client.rpushx(key, new String[] { value }); 619 } catch (Exception e) { 620 LOG.error(e.getMessage(), e); 621 throw new RedisAccessException(e); 622 } finally { 623 if (null != client) { 624 jedisPool.returnResourceObject(client); 625 } 626 } 627 } 628 629 /** 630 * 631 * @描述 : 将一个或多个值 value 插入到列表 key 的表头 632 * @创建时间: 2015年7月20日下午2:04:13 633 * 634 * @param key 635 * @param values 636 * @return 637 * @throws RedisAccessException 638 */ 639 public Long lpush(String key, String[] values) throws RedisAccessException { 640 Jedis client = null; 641 try { 642 client = jedisPool.getResource(); 643 return client.lpush(key, values); 644 } catch (Exception e) { 645 LOG.error(e.getMessage(), e); 646 throw new RedisAccessException(e); 647 } finally { 648 if (null != client) { 649 jedisPool.returnResourceObject(client); 650 } 651 } 652 } 653 654 /** 655 * 656 * @描述 : 将值 value 插入到列表 key 的表头,当且仅当 key 存在并且是一个列表。 和 LPUSH 命令相反,当 key 不存在时, 657 * LPUSHX 命令什么也不做。 658 * @创建时间: 2015年7月20日下午2:05:09 659 * 660 * @param key 661 * @param value 662 * @return 663 * @throws RedisAccessException 664 */ 665 public Long lpushx(String key, String value) throws RedisAccessException { 666 Jedis client = null; 667 try { 668 client = jedisPool.getResource(); 669 return client.lpushx(key, new String[] { value }); 670 } catch (Exception e) { 671 LOG.error(e.getMessage(), e); 672 throw new RedisAccessException(e); 673 } finally { 674 if (null != client) { 675 jedisPool.returnResourceObject(client); 676 } 677 } 678 } 679 680 /** 681 * 682 * @描述 : 返回列表 key 的长度。如果 key 不存在,则 key 被解释为一个空列表,返回 0 . 如果 key 683 * 不是列表类型,返回一个错误。 684 * @创建时间: 2015年7月20日下午2:05:25 685 * 686 * @param key 687 * @return 688 * @throws RedisAccessException 689 */ 690 public Long llen(String key) throws RedisAccessException { 691 Jedis client = null; 692 try { 693 client = jedisPool.getResource(); 694 return client.llen(key); 695 } catch (Exception e) { 696 LOG.error(e.getMessage(), e); 697 throw new RedisAccessException(e); 698 } finally { 699 if (null != client) { 700 jedisPool.returnResourceObject(client); 701 } 702 } 703 } 704 705 /** 706 * 707 * @描述 : 返回列表 key 中,下标为 index 的元素。 708 * @创建时间: 2015年7月20日下午2:05:37 709 * 710 * @param key 711 * @param index 712 * @return 713 * @throws RedisAccessException 714 */ 715 public String lindex(String key, long index) throws RedisAccessException { 716 Jedis client = null; 717 try { 718 client = jedisPool.getResource(); 719 return client.lindex(key, index); 720 } catch (Exception e) { 721 LOG.error(e.getMessage(), e); 722 throw new RedisAccessException(e); 723 } finally { 724 if (null != client) { 725 jedisPool.returnResourceObject(client); 726 } 727 } 728 } 729 730 /** 731 * 732 * @描述 : 将列表 key 下标为 index 的元素的值设置为 value 。当 index 参数超出范围,或对一个空列表( key 733 * 不存在)进行 LSET 时,返回一个错误。 734 * @创建时间: 2015年7月20日下午2:07:06 735 * 736 * @param key 737 * @param index 738 * @param value 739 * @return 740 * @throws RedisAccessException 741 */ 742 public String lset(String key, long index, String value) throws RedisAccessException { 743 Jedis client = null; 744 try { 745 client = jedisPool.getResource(); 746 return client.lset(key, index, value); 747 } catch (Exception e) { 748 LOG.error(e.getMessage(), e); 749 throw new RedisAccessException(e); 750 } finally { 751 if (null != client) { 752 jedisPool.returnResourceObject(client); 753 } 754 } 755 } 756 757 /** 758 * 759 * @描述 : 移除并返回列表 key 的头元素。 760 * @创建时间: 2015年7月20日下午2:07:10 761 * 762 * @param key 763 * @return 764 * @throws RedisAccessException 765 */ 766 public String lpop(String key) throws RedisAccessException { 767 Jedis client = null; 768 try { 769 client = jedisPool.getResource(); 770 return client.lpop(key); 771 } catch (Exception e) { 772 LOG.error(e.getMessage(), e); 773 throw new RedisAccessException(e); 774 } finally { 775 if (null != client) { 776 jedisPool.returnResourceObject(client); 777 } 778 } 779 } 780 781 /** 782 * 783 * @描述 : 移除并返回列表 key 的尾元素。 784 * @创建时间: 2015年7月20日下午2:07:13 785 * 786 * @param key 787 * @return 788 * @throws RedisAccessException 789 */ 790 public String rpop(String key) throws RedisAccessException { 791 Jedis client = null; 792 try { 793 client = jedisPool.getResource(); 794 return client.rpop(key); 795 } catch (Exception e) { 796 LOG.error(e.getMessage(), e); 797 throw new RedisAccessException(e); 798 } finally { 799 if (null != client) { 800 jedisPool.returnResourceObject(client); 801 } 802 } 803 } 804 805 /** 806 * 807 * @描述 : 将值 value 插入到列表 key 当中,位于值 pivot 之前或之后。当 pivot 不存在于列表 key 时,不执行任何操作。 808 * key 不存在时, key 被视为空列表,不执行任何操作。 如果 key 不是列表类型,返回一个错误。 809 * @创建时间: 2015年7月20日下午2:07:37 810 * 811 * @param key 812 * @param where 813 * @param pivot 814 * @param value 815 * @return 816 * @throws RedisAccessException 817 */ 818 public Long linsert(String key, BinaryClient.LIST_POSITION where, String pivot, String value) throws RedisAccessException { 819 Jedis client = null; 820 try { 821 client = jedisPool.getResource(); 822 return client.linsert(key, where, pivot, value); 823 } catch (Exception e) { 824 LOG.error(e.getMessage(), e); 825 throw new RedisAccessException(e); 826 } finally { 827 if (null != client) { 828 jedisPool.returnResourceObject(client); 829 } 830 } 831 } 832 833 /** 834 * @描述 : 将 key 中储存的数字值增一。如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。 835 * @创建时间: 2014-6-16下午2:31:58 836 * 837 * @param key 838 * @return 839 * @throws RedisAccessException 840 */ 841 public Long incr(String key) throws RedisAccessException { 842 Jedis client = null; 843 try { 844 client = jedisPool.getResource(); 845 return client.incr(key); 846 } catch (Exception e) { 847 LOG.error(e.getMessage(), e); 848 throw new RedisAccessException(e); 849 } finally { 850 if (null != client) { 851 jedisPool.returnResourceObject(client); 852 } 853 } 854 } 855 856 857 /** 858 * @描述 : 将 key 所储存的值加上增量 increment 。如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCRBY 命令。 859 * @创建时间: 2014-6-16下午2:31:58 860 * 861 * @param key 862 * @return 863 * @throws RedisAccessException 864 */ 865 public double incrBy(String key,long value) throws RedisAccessException { 866 Jedis client = null; 867 try { 868 client = jedisPool.getResource(); 869 return client.incrBy(key, value); 870 } catch (Exception e) { 871 LOG.error(e.getMessage(), e); 872 throw new RedisAccessException(e); 873 } finally { 874 if (null != client) { 875 jedisPool.returnResourceObject(client); 876 } 877 } 878 } 879 880 }OK,这个关于redis的相关内容更新完毕!
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/server/equal/11917.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
ZooKeeper集群安装
时间:2021-01-10
-
KeepAlive详解
时间:2021-01-10
-
Spark教程 构建Spark集群(
时间:2021-01-10
-
高效搭建Spark完全分布式集
时间:2021-01-10
-
负载均衡与缓存
时间:2021-01-10
-
Hadoop2.2.0NNHA详细配置+Cli
时间:2021-01-10
-
Mongodb集群搭建过程及常见
时间:2021-01-09
-
DRBD+HeartBeat架构实验
时间:2021-01-09
热门文章
-
Nagios监控生产环境redis集群服务实战
时间:2021-01-08
-
Spark教程 构建Spark集群(1)
时间:2021-01-10
-
SqlServer横向扩展负载均衡终极利器SqlSer
时间:2021-01-08
-
Kafka集群安装
时间:2021-01-09
-
WAS集群系列(13):举例WAS集群下ear包部
时间:2021-01-08
-
Memcached基础知识
时间:2021-01-08
-
KeepAlive详解
时间:2021-01-10
-
WAS集群系列(12):集群搭建:步骤10:通
时间:2021-01-08
-
Cloudera Manager 4.6 安装部署hadoop CDH集群
时间:2021-01-09
-
DRBD+HeartBeat架构实验
时间:2021-01-09
