轉載:http://blog.csdn.net/liuxiao723846/article/details/50401406
1、使用了jedis客戶端,對redis進行了封裝,包括:
1)使用了redispool獲取連接;以及連接的回收;
2)常用五種數據結構的常用操作封裝;
1 package redis.utils; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Set; 6 7 //import org.apache.log4j.Logger; 8 9 10 import redis.clients.jedis.Jedis; 11 import redis.clients.jedis.JedisPool; 12 import redis.clients.jedis.JedisPoolConfig; 13 import redis.clients.jedis.SortingParams; 14 import redis.clients.jedis.BinaryClient.LIST_POSITION; 15 import redis.clients.util.SafeEncoder; 16 17 /** 18 * @author Mr.hu 19 * @version crateTime:2013-10-30 下午5:41:30 20 * Class Explain:JedisUtil 21 */ 22 public class JedisUtil { 23 24 //private Logger log = Logger.getLogger(this.getClass()); 25 /**緩存生存時間 */ 26 private final int expire = 60000; 27 /** 操作Key的方法 */ 28 public Keys KEYS; 29 /** 對存儲結構為String類型的操作 */ 30 public Strings STRINGS; 31 /** 對存儲結構為List類型的操作 */ 32 public Lists LISTS; 33 /** 對存儲結構為Set類型的操作 */ 34 public Sets SETS; 35 /** 對存儲結構為HashMap類型的操作 */ 36 public Hash HASH; 37 /** 對存儲結構為Set(排序的)類型的操作 */ 38 public SortSet SORTSET; 39 private static JedisPool jedisPool = null; 40 41 private JedisUtil() { 42 43 } 44 static { 45 JedisPoolConfig config = new JedisPoolConfig(); 46 //控制一個pool可分配多少個jedis實例,通過pool.getResource()來獲取; 47 //如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。 48 config.setMaxTotal(500); 49 //控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例。 50 config.setMaxIdle(5); 51 //表示當borrow(引入)一個jedis實例時,最大的等待時間,如果超過等待時間,則直接拋出JedisConnectionException; 52 config.setMaxWaitMillis(1000 * 100); 53 //在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的; 54 config.setTestOnBorrow(true); 55 56 //redis如果設置了密碼: 57 /*jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP, 58 JRedisPoolConfig.REDIS_PORT, 59 10000,JRedisPoolConfig.REDIS_PASSWORD); */ 60 61 //redis未設置了密碼: 62 jedisPool = new JedisPool(config, "172.30.37.73",6379); 63 } 64 65 public JedisPool getPool() { 66 return jedisPool; 67 } 68 69 /** 70 * 從jedis連接池中獲取獲取jedis對象 71 * @return 72 */ 73 public Jedis getJedis() { 74 return jedisPool.getResource(); 75 } 76 77 78 private static final JedisUtil jedisUtil = new JedisUtil(); 79 80 81 /** 82 * 獲取JedisUtil實例 83 * @return 84 */ 85 public static JedisUtil getInstance() { 86 return jedisUtil; 87 } 88 89 /** 90 * 回收jedis(放到finally中) 91 * @param jedis 92 */ 93 public void returnJedis(Jedis jedis) { 94 if (null != jedis && null != jedisPool) { 95 jedisPool.returnResource(jedis); 96 } 97 } 98 99 /** 100 * 銷毀連接(放到catch中) 101 * @param pool 102 * @param jedis 103 */ 104 public static void returnBrokenResource(Jedis jedis) { 105 if (null != jedis && null != jedisPool) { 106 jedisPool.returnResource(jedis); 107 } 108 } 109 110 111 /** 112 * 設置過期時間 113 * @author ruan 2013-4-11 114 * @param key 115 * @param seconds 116 */ 117 public void expire(String key, int seconds) { 118 if (seconds <= 0) { 119 return; 120 } 121 Jedis jedis = getJedis(); 122 jedis.expire(key, seconds); 123 returnJedis(jedis); 124 } 125 126 /** 127 * 設置默認過期時間 128 * @author ruan 2013-4-11 129 * @param key 130 */ 131 public void expire(String key) { 132 expire(key, expire); 133 } 134 135 136 //*******************************************Keys*******************************************// 137 public class Keys { 138 139 /** 140 * 清空所有key 141 */ 142 public String flushAll() { 143 Jedis jedis = getJedis(); 144 String stata = jedis.flushAll(); 145 returnJedis(jedis); 146 return stata; 147 } 148 149 /** 150 * 更改key 151 * @param String oldkey 152 * @param String newkey 153 * @return 狀態碼 154 * */ 155 public String rename(String oldkey, String newkey) { 156 return rename(SafeEncoder.encode(oldkey), 157 SafeEncoder.encode(newkey)); 158 } 159 160 /** 161 * 更改key,僅當新key不存在時才執行 162 * @param String oldkey 163 * @param String newkey 164 * @return 狀態碼 165 * */ 166 public long renamenx(String oldkey, String newkey) { 167 Jedis jedis = getJedis(); 168 long status = jedis.renamenx(oldkey, newkey); 169 returnJedis(jedis); 170 return status; 171 } 172 173 /** 174 * 更改key 175 * @param String oldkey 176 * @param String newkey 177 * @return 狀態碼 178 * */ 179 public String rename(byte[] oldkey, byte[] newkey) { 180 Jedis jedis = getJedis(); 181 String status = jedis.rename(oldkey, newkey); 182 returnJedis(jedis); 183 return status; 184 } 185 186 /** 187 * 設置key的過期時間,以秒為單位 188 * @param String key 189 * @param 時間,已秒為單位 190 * @return 影響的記錄數 191 * */ 192 public long expired(String key, int seconds) { 193 Jedis jedis = getJedis(); 194 long count = jedis.expire(key, seconds); 195 returnJedis(jedis); 196 return count; 197 } 198 199 /** 200 * 設置key的過期時間,它是距歷元(即格林威治標准時間 1970 年 1 月 1 日的 00:00:00,格里高利歷)的偏移量。 201 * @param String key 202 * @param 時間,已秒為單位 203 * @return 影響的記錄數 204 * */ 205 public long expireAt(String key, long timestamp) { 206 Jedis jedis = getJedis(); 207 long count = jedis.expireAt(key, timestamp); 208 returnJedis(jedis); 209 return count; 210 } 211 212 /** 213 * 查詢key的過期時間 214 * @param String key 215 * @return 以秒為單位的時間表示 216 * */ 217 public long ttl(String key) { 218 //ShardedJedis sjedis = getShardedJedis(); 219 Jedis sjedis=getJedis(); 220 long len = sjedis.ttl(key); 221 returnJedis(sjedis); 222 return len; 223 } 224 225 /** 226 * 取消對key過期時間的設置 227 * @param key 228 * @return 影響的記錄數 229 * */ 230 public long persist(String key) { 231 Jedis jedis = getJedis(); 232 long count = jedis.persist(key); 233 returnJedis(jedis); 234 return count; 235 } 236 237 /** 238 * 刪除keys對應的記錄,可以是多個key 239 * @param String ... keys 240 * @return 刪除的記錄數 241 * */ 242 public long del(String... keys) { 243 Jedis jedis = getJedis(); 244 long count = jedis.del(keys); 245 returnJedis(jedis); 246 return count; 247 } 248 249 /** 250 * 刪除keys對應的記錄,可以是多個key 251 * @param String .. keys 252 * @return 刪除的記錄數 253 * */ 254 public long del(byte[]... keys) { 255 Jedis jedis = getJedis(); 256 long count = jedis.del(keys); 257 returnJedis(jedis); 258 return count; 259 } 260 261 /** 262 * 判斷key是否存在 263 * @param String key 264 * @return boolean 265 * */ 266 public boolean exists(String key) { 267 //ShardedJedis sjedis = getShardedJedis(); 268 Jedis sjedis=getJedis(); 269 boolean exis = sjedis.exists(key); 270 returnJedis(sjedis); 271 return exis; 272 } 273 274 /** 275 * 對List,Set,SortSet進行排序,如果集合數據較大應避免使用這個方法 276 * @param String key 277 * @return List<String> 集合的全部記錄 278 * **/ 279 public List<String> sort(String key) { 280 //ShardedJedis sjedis = getShardedJedis(); 281 Jedis sjedis=getJedis(); 282 List<String> list = sjedis.sort(key); 283 returnJedis(sjedis); 284 return list; 285 } 286 287 /** 288 * 對List,Set,SortSet進行排序或limit 289 * @param String key 290 * @param SortingParams parame 定義排序類型或limit的起止位置. 291 * @return List<String> 全部或部分記錄 292 * **/ 293 public List<String> sort(String key, SortingParams parame) { 294 //ShardedJedis sjedis = getShardedJedis(); 295 Jedis sjedis=getJedis(); 296 List<String> list = sjedis.sort(key, parame); 297 returnJedis(sjedis); 298 return list; 299 } 300 301 /** 302 * 返回指定key存儲的類型 303 * @param String key 304 * @return String string|list|set|zset|hash 305 * **/ 306 public String type(String key) { 307 //ShardedJedis sjedis = getShardedJedis(); 308 Jedis sjedis=getJedis(); 309 String type = sjedis.type(key); 310 returnJedis(sjedis); 311 return type; 312 } 313 314 /** 315 * 查找所有匹配給定的模式的鍵 316 * @param String key的表達式,*表示多個,?表示一個 317 * */ 318 public Set<String> keys(String pattern) { 319 Jedis jedis = getJedis(); 320 Set<String> set = jedis.keys(pattern); 321 returnJedis(jedis); 322 return set; 323 } 324 } 325 326 //*******************************************Sets*******************************************// 327 public class Sets { 328 329 /** 330 * 向Set添加一條記錄,如果member已存在返回0,否則返回1 331 * @param String key 332 * @param String member 333 * @return 操作碼,0或1 334 * */ 335 public long sadd(String key, String member) { 336 Jedis jedis = getJedis(); 337 long s = jedis.sadd(key, member); 338 returnJedis(jedis); 339 return s; 340 } 341 342 public long sadd(byte[] key, byte[] member) { 343 Jedis jedis = getJedis(); 344 long s = jedis.sadd(key, member); 345 returnJedis(jedis); 346 return s; 347 } 348 349 /** 350 * 獲取給定key中元素個數 351 * @param String key 352 * @return 元素個數 353 * */ 354 public long scard(String key) { 355 //ShardedJedis sjedis = getShardedJedis(); 356 Jedis sjedis = getJedis(); 357 long len = sjedis.scard(key); 358 returnJedis(sjedis); 359 return len; 360 } 361 362 /** 363 * 返回從第一組和所有的給定集合之間的差異的成員 364 * @param String ... keys 365 * @return 差異的成員集合 366 * */ 367 public Set<String> sdiff(String... keys) { 368 Jedis jedis = getJedis(); 369 Set<String> set = jedis.sdiff(keys); 370 returnJedis(jedis); 371 return set; 372 } 373 374 /** 375 * 這個命令等於sdiff,但返回的不是結果集,而是將結果集存儲在新的集合中,如果目標已存在,則覆蓋。 376 * @param String newkey 新結果集的key 377 * @param String ... keys 比較的集合 378 * @return 新集合中的記錄數 379 * **/ 380 public long sdiffstore(String newkey, String... keys) { 381 Jedis jedis = getJedis(); 382 long s = jedis.sdiffstore(newkey, keys); 383 returnJedis(jedis); 384 return s; 385 } 386 387 /** 388 * 返回給定集合交集的成員,如果其中一個集合為不存在或為空,則返回空Set 389 * @param String ... keys 390 * @return 交集成員的集合 391 * **/ 392 public Set<String> sinter(String... keys) { 393 Jedis jedis = getJedis(); 394 Set<String> set = jedis.sinter(keys); 395 returnJedis(jedis); 396 return set; 397 } 398 399 /** 400 * 這個命令等於sinter,但返回的不是結果集,而是將結果集存儲在新的集合中,如果目標已存在,則覆蓋。 401 * @param String newkey 新結果集的key 402 * @param String ... keys 比較的集合 403 * @return 新集合中的記錄數 404 * **/ 405 public long sinterstore(String newkey, String... keys) { 406 Jedis jedis = getJedis(); 407 long s = jedis.sinterstore(newkey, keys); 408 returnJedis(jedis); 409 return s; 410 } 411 412 /** 413 * 確定一個給定的值是否存在 414 * @param String key 415 * @param String member 要判斷的值 416 * @return 存在返回1,不存在返回0 417 * **/ 418 public boolean sismember(String key, String member) { 419 //ShardedJedis sjedis = getShardedJedis(); 420 Jedis sjedis = getJedis(); 421 boolean s = sjedis.sismember(key, member); 422 returnJedis(sjedis); 423 return s; 424 } 425 426 /** 427 * 返回集合中的所有成員 428 * @param String key 429 * @return 成員集合 430 * */ 431 public Set<String> smembers(String key) { 432 //ShardedJedis sjedis = getShardedJedis(); 433 Jedis sjedis = getJedis(); 434 Set<String> set = sjedis.smembers(key); 435 returnJedis(sjedis); 436 return set; 437 } 438 439 public Set<byte[]> smembers(byte[] key) { 440 //ShardedJedis sjedis = getShardedJedis(); 441 Jedis sjedis = getJedis(); 442 Set<byte[]> set = sjedis.smembers(key); 443 returnJedis(sjedis); 444 return set; 445 } 446 447 /** 448 * 將成員從源集合移出放入目標集合 <br/> 449 * 如果源集合不存在或不包哈指定成員,不進行任何操作,返回0<br/> 450 * 否則該成員從源集合上刪除,並添加到目標集合,如果目標集合中成員已存在,則只在源集合進行刪除 451 * @param String srckey 源集合 452 * @param String dstkey 目標集合 453 * @param String member 源集合中的成員 454 * @return 狀態碼,1成功,0失敗 455 * */ 456 public long smove(String srckey, String dstkey, String member) { 457 Jedis jedis = getJedis(); 458 long s = jedis.smove(srckey, dstkey, member); 459 returnJedis(jedis); 460 return s; 461 } 462 463 /** 464 * 從集合中刪除成員 465 * @param String key 466 * @return 被刪除的成員 467 * */ 468 public String spop(String key) { 469 Jedis jedis = getJedis(); 470 String s = jedis.spop(key); 471 returnJedis(jedis); 472 return s; 473 } 474 475 /** 476 * 從集合中刪除指定成員 477 * @param String key 478 * @param String member 要刪除的成員 479 * @return 狀態碼,成功返回1,成員不存在返回0 480 * */ 481 public long srem(String key, String member) { 482 Jedis jedis = getJedis(); 483 long s = jedis.srem(key, member); 484 returnJedis(jedis); 485 return s; 486 } 487 488 /** 489 * 合並多個集合並返回合並后的結果,合並后的結果集合並不保存<br/> 490 * @param String ... keys 491 * @return 合並后的結果集合 492 * @see sunionstore 493 * */ 494 public Set<String> sunion(String... keys) { 495 Jedis jedis = getJedis(); 496 Set<String> set = jedis.sunion(keys); 497 returnJedis(jedis); 498 return set; 499 } 500 501 /** 502 * 合並多個集合並將合並后的結果集保存在指定的新集合中,如果新集合已經存在則覆蓋 503 * @param String newkey 新集合的key 504 * @param String ... keys 要合並的集合 505 * **/ 506 public long sunionstore(String newkey, String... keys) { 507 Jedis jedis = getJedis(); 508 long s = jedis.sunionstore(newkey, keys); 509 returnJedis(jedis); 510 return s; 511 } 512 } 513 514 //*******************************************SortSet*******************************************// 515 public class SortSet { 516 517 /** 518 * 向集合中增加一條記錄,如果這個值已存在,這個值對應的權重將被置為新的權重 519 * @param String key 520 * @param double score 權重 521 * @param String member 要加入的值, 522 * @return 狀態碼 1成功,0已存在member的值 523 * */ 524 public long zadd(String key, double score, String member) { 525 Jedis jedis = getJedis(); 526 long s = jedis.zadd(key, score, member); 527 returnJedis(jedis); 528 return s; 529 } 530 531 /*public long zadd(String key, Map<Double, String> scoreMembers) { 532 Jedis jedis = getJedis(); 533 long s = jedis.zadd(key, scoreMembers); 534 returnJedis(jedis); 535 return s; 536 }*/ 537 538 /** 539 * 獲取集合中元素的數量 540 * @param String key 541 * @return 如果返回0則集合不存在 542 * */ 543 public long zcard(String key) { 544 //ShardedJedis sjedis = getShardedJedis(); 545 Jedis sjedis = getJedis(); 546 long len = sjedis.zcard(key); 547 returnJedis(sjedis); 548 return len; 549 } 550 551 /** 552 * 獲取指定權重區間內集合的數量 553 * @param String key 554 * @param double min 最小排序位置 555 * @param double max 最大排序位置 556 * */ 557 public long zcount(String key, double min, double max) { 558 //ShardedJedis sjedis = getShardedJedis(); 559 Jedis sjedis = getJedis(); 560 long len = sjedis.zcount(key, min, max); 561 returnJedis(sjedis); 562 return len; 563 } 564 565 /** 566 * 獲得set的長度 567 * 568 * @param key 569 * @return 570 */ 571 public long zlength(String key) { 572 long len = 0; 573 Set<String> set = zrange(key, 0, -1); 574 len = set.size(); 575 return len; 576 } 577 578 /** 579 * 權重增加給定值,如果給定的member已存在 580 * @param String key 581 * @param double score 要增的權重 582 * @param String member 要插入的值 583 * @return 增后的權重 584 * */ 585 public double zincrby(String key, double score, String member) { 586 Jedis jedis = getJedis(); 587 double s = jedis.zincrby(key, score, member); 588 returnJedis(jedis); 589 return s; 590 } 591 592 /** 593 * 返回指定位置的集合元素,0為第一個元素,-1為最后一個元素 594 * @param String key 595 * @param int start 開始位置(包含) 596 * @param int end 結束位置(包含) 597 * @return Set<String> 598 * */ 599 public Set<String> zrange(String key, int start, int end) { 600 //ShardedJedis sjedis = getShardedJedis(); 601 Jedis sjedis = getJedis(); 602 Set<String> set = sjedis.zrange(key, start, end); 603 returnJedis(sjedis); 604 return set; 605 } 606 607 /** 608 * 返回指定權重區間的元素集合 609 * @param String key 610 * @param double min 上限權重 611 * @param double max 下限權重 612 * @return Set<String> 613 * */ 614 public Set<String> zrangeByScore(String key, double min, double max) { 615 //ShardedJedis sjedis = getShardedJedis(); 616 Jedis sjedis = getJedis(); 617 Set<String> set = sjedis.zrangeByScore(key, min, max); 618 returnJedis(sjedis); 619 return set; 620 } 621 622 /** 623 * 獲取指定值在集合中的位置,集合排序從低到高 624 * @see zrevrank 625 * @param String key 626 * @param String member 627 * @return long 位置 628 * */ 629 public long zrank(String key, String member) { 630 //ShardedJedis sjedis = getShardedJedis(); 631 Jedis sjedis = getJedis(); 632 long index = sjedis.zrank(key, member); 633 returnJedis(sjedis); 634 return index; 635 } 636 637 /** 638 * 獲取指定值在集合中的位置,集合排序從高到低 639 * @see zrank 640 * @param String key 641 * @param String member 642 * @return long 位置 643 * */ 644 public long zrevrank(String key, String member) { 645 //ShardedJedis sjedis = getShardedJedis(); 646 Jedis sjedis = getJedis(); 647 long index = sjedis.zrevrank(key, member); 648 returnJedis(sjedis); 649 return index; 650 } 651 652 /** 653 * 從集合中刪除成員 654 * @param String key 655 * @param String member 656 * @return 返回1成功 657 * */ 658 public long zrem(String key, String member) { 659 Jedis jedis = getJedis(); 660 long s = jedis.zrem(key, member); 661 returnJedis(jedis); 662 return s; 663 } 664 665 /** 666 * 刪除 667 * @param key 668 * @return 669 */ 670 public long zrem(String key) { 671 Jedis jedis = getJedis(); 672 long s = jedis.del(key); 673 returnJedis(jedis); 674 return s; 675 } 676 677 /** 678 * 刪除給定位置區間的元素 679 * @param String key 680 * @param int start 開始區間,從0開始(包含) 681 * @param int end 結束區間,-1為最后一個元素(包含) 682 * @return 刪除的數量 683 * */ 684 public long zremrangeByRank(String key, int start, int end) { 685 Jedis jedis = getJedis(); 686 long s = jedis.zremrangeByRank(key, start, end); 687 returnJedis(jedis); 688 return s; 689 } 690 691 /** 692 * 刪除給定權重區間的元素 693 * @param String key 694 * @param double min 下限權重(包含) 695 * @param double max 上限權重(包含) 696 * @return 刪除的數量 697 * */ 698 public long zremrangeByScore(String key, double min, double max) { 699 Jedis jedis = getJedis(); 700 long s = jedis.zremrangeByScore(key, min, max); 701 returnJedis(jedis); 702 return s; 703 } 704 705 /** 706 * 獲取給定區間的元素,原始按照權重由高到低排序 707 * @param String key 708 * @param int start 709 * @param int end 710 * @return Set<String> 711 * */ 712 public Set<String> zrevrange(String key, int start, int end) { 713 //ShardedJedis sjedis = getShardedJedis(); 714 Jedis sjedis = getJedis(); 715 Set<String> set = sjedis.zrevrange(key, start, end); 716 returnJedis(sjedis); 717 return set; 718 } 719 720 /** 721 * 獲取給定值在集合中的權重 722 * @param String key 723 * @param memeber 724 * @return double 權重 725 * */ 726 public double zscore(String key, String memebr) { 727 //ShardedJedis sjedis = getShardedJedis(); 728 Jedis sjedis = getJedis(); 729 Double score = sjedis.zscore(key, memebr); 730 returnJedis(sjedis); 731 if (score != null) 732 return score; 733 return 0; 734 } 735 } 736 737 //*******************************************Hash*******************************************// 738 public class Hash { 739 740 /** 741 * 從hash中刪除指定的存儲 742 * @param String key 743 * @param String fieid 存儲的名字 744 * @return 狀態碼,1成功,0失敗 745 * */ 746 public long hdel(String key, String fieid) { 747 Jedis jedis = getJedis(); 748 long s = jedis.hdel(key, fieid); 749 returnJedis(jedis); 750 return s; 751 } 752 753 public long hdel(String key) { 754 Jedis jedis = getJedis(); 755 long s = jedis.del(key); 756 returnJedis(jedis); 757 return s; 758 } 759 760 /** 761 * 測試hash中指定的存儲是否存在 762 * @param String key 763 * @param String fieid 存儲的名字 764 * @return 1存在,0不存在 765 * */ 766 public boolean hexists(String key, String fieid) { 767 //ShardedJedis sjedis = getShardedJedis(); 768 Jedis sjedis = getJedis(); 769 boolean s = sjedis.hexists(key, fieid); 770 returnJedis(sjedis); 771 return s; 772 } 773 774 /** 775 * 返回hash中指定存儲位置的值 776 * 777 * @param String key 778 * @param String fieid 存儲的名字 779 * @return 存儲對應的值 780 * */ 781 public String hget(String key, String fieid) { 782 //ShardedJedis sjedis = getShardedJedis(); 783 Jedis sjedis = getJedis(); 784 String s = sjedis.hget(key, fieid); 785 returnJedis(sjedis); 786 return s; 787 } 788 789 public byte[] hget(byte[] key, byte[] fieid) { 790 //ShardedJedis sjedis = getShardedJedis(); 791 Jedis sjedis = getJedis(); 792 byte[] s = sjedis.hget(key, fieid); 793 returnJedis(sjedis); 794 return s; 795 } 796 797 /** 798 * 以Map的形式返回hash中的存儲和值 799 * @param String key 800 * @return Map<Strinig,String> 801 * */ 802 public Map<String, String> hgetAll(String key) { 803 //ShardedJedis sjedis = getShardedJedis(); 804 Jedis sjedis = getJedis(); 805 Map<String, String> map = sjedis.hgetAll(key); 806 returnJedis(sjedis); 807 return map; 808 } 809 810 /** 811 * 添加一個對應關系 812 * @param String key 813 * @param String fieid 814 * @param String value 815 * @return 狀態碼 1成功,0失敗,fieid已存在將更新,也返回0 816 * **/ 817 public long hset(String key, String fieid, String value) { 818 Jedis jedis = getJedis(); 819 long s = jedis.hset(key, fieid, value); 820 returnJedis(jedis); 821 return s; 822 } 823 824 public long hset(String key, String fieid, byte[] value) { 825 Jedis jedis = getJedis(); 826 long s = jedis.hset(key.getBytes(), fieid.getBytes(), value); 827 returnJedis(jedis); 828 return s; 829 } 830 831 /** 832 * 添加對應關系,只有在fieid不存在時才執行 833 * @param String key 834 * @param String fieid 835 * @param String value 836 * @return 狀態碼 1成功,0失敗fieid已存 837 * **/ 838 public long hsetnx(String key, String fieid, String value) { 839 Jedis jedis = getJedis(); 840 long s = jedis.hsetnx(key, fieid, value); 841 returnJedis(jedis); 842 return s; 843 } 844 845 /** 846 * 獲取hash中value的集合 847 * 848 * @param String 849 * key 850 * @return List<String> 851 * */ 852 public List<String> hvals(String key) { 853 //ShardedJedis sjedis = getShardedJedis(); 854 Jedis sjedis = getJedis(); 855 List<String> list = sjedis.hvals(key); 856 returnJedis(sjedis); 857 return list; 858 } 859 860 /** 861 * 在指定的存儲位置加上指定的數字,存儲位置的值必須可轉為數字類型 862 * @param String key 863 * @param String fieid 存儲位置 864 * @param String long value 要增加的值,可以是負數 865 * @return 增加指定數字后,存儲位置的值 866 * */ 867 public long hincrby(String key, String fieid, long value) { 868 Jedis jedis = getJedis(); 869 long s = jedis.hincrBy(key, fieid, value); 870 returnJedis(jedis); 871 return s; 872 } 873 874 /** 875 * 返回指定hash中的所有存儲名字,類似Map中的keySet方法 876 * @param String key 877 * @return Set<String> 存儲名稱的集合 878 * */ 879 public Set<String> hkeys(String key) { 880 //ShardedJedis sjedis = getShardedJedis(); 881 Jedis sjedis = getJedis(); 882 Set<String> set = sjedis.hkeys(key); 883 returnJedis(sjedis); 884 return set; 885 } 886 887 /** 888 * 獲取hash中存儲的個數,類似Map中size方法 889 * @param String key 890 * @return long 存儲的個數 891 * */ 892 public long hlen(String key) { 893 //ShardedJedis sjedis = getShardedJedis(); 894 Jedis sjedis = getJedis(); 895 long len = sjedis.hlen(key); 896 returnJedis(sjedis); 897 return len; 898 } 899 900 /** 901 * 根據多個key,獲取對應的value,返回List,如果指定的key不存在,List對應位置為null 902 * @param String key 903 * @param String ... fieids 存儲位置 904 * @return List<String> 905 * */ 906 public List<String> hmget(String key, String... fieids) { 907 //ShardedJedis sjedis = getShardedJedis(); 908 Jedis sjedis = getJedis(); 909 List<String> list = sjedis.hmget(key, fieids); 910 returnJedis(sjedis); 911 return list; 912 } 913 914 public List<byte[]> hmget(byte[] key, byte[]... fieids) { 915 //ShardedJedis sjedis = getShardedJedis(); 916 Jedis sjedis = getJedis(); 917 List<byte[]> list = sjedis.hmget(key, fieids); 918 returnJedis(sjedis); 919 return list; 920 } 921 922 /** 923 * 添加對應關系,如果對應關系已存在,則覆蓋 924 * @param Strin key 925 * @param Map <String,String> 對應關系 926 * @return 狀態,成功返回OK 927 * */ 928 public String hmset(String key, Map<String, String> map) { 929 Jedis jedis = getJedis(); 930 String s = jedis.hmset(key, map); 931 returnJedis(jedis); 932 return s; 933 } 934 935 /** 936 * 添加對應關系,如果對應關系已存在,則覆蓋 937 * @param Strin key 938 * @param Map <String,String> 對應關系 939 * @return 狀態,成功返回OK 940 * */ 941 public String hmset(byte[] key, Map<byte[], byte[]> map) { 942 Jedis jedis = getJedis(); 943 String s = jedis.hmset(key, map); 944 returnJedis(jedis); 945 return s; 946 } 947 948 } 949 950 951 //*******************************************Strings*******************************************// 952 public class Strings { 953 /** 954 * 根據key獲取記錄 955 * @param String key 956 * @return 值 957 * */ 958 public String get(String key) { 959 //ShardedJedis sjedis = getShardedJedis(); 960 Jedis sjedis = getJedis(); 961 String value = sjedis.get(key); 962 returnJedis(sjedis); 963 return value; 964 } 965 966 /** 967 * 根據key獲取記錄 968 * @param byte[] key 969 * @return 值 970 * */ 971 public byte[] get(byte[] key) { 972 //ShardedJedis sjedis = getShardedJedis(); 973 Jedis sjedis = getJedis(); 974 byte[] value = sjedis.get(key); 975 returnJedis(sjedis); 976 return value; 977 } 978 979 /** 980 * 添加有過期時間的記錄 981 * 982 * @param String key 983 * @param int seconds 過期時間,以秒為單位 984 * @param String value 985 * @return String 操作狀態 986 * */ 987 public String setEx(String key, int seconds, String value) { 988 Jedis jedis = getJedis(); 989 String str = jedis.setex(key, seconds, value); 990 returnJedis(jedis); 991 return str; 992 } 993 994 /** 995 * 添加有過期時間的記錄 996 * 997 * @param String key 998 * @param int seconds 過期時間,以秒為單位 999 * @param String value 1000 * @return String 操作狀態 1001 * */ 1002 public String setEx(byte[] key, int seconds, byte[] value) { 1003 Jedis jedis = getJedis(); 1004 String str = jedis.setex(key, seconds, value); 1005 returnJedis(jedis); 1006 return str; 1007 } 1008 1009 /** 1010 * 添加一條記錄,僅當給定的key不存在時才插入 1011 * @param String key 1012 * @param String value 1013 * @return long 狀態碼,1插入成功且key不存在,0未插入,key存在 1014 * */ 1015 public long setnx(String key, String value) { 1016 Jedis jedis = getJedis(); 1017 long str = jedis.setnx(key, value); 1018 returnJedis(jedis); 1019 return str; 1020 } 1021 1022 /** 1023 * 添加記錄,如果記錄已存在將覆蓋原有的value 1024 * @param String key 1025 * @param String value 1026 * @return 狀態碼 1027 * */ 1028 public String set(String key, String value) { 1029 return set(SafeEncoder.encode(key), SafeEncoder.encode(value)); 1030 } 1031 1032 /** 1033 * 添加記錄,如果記錄已存在將覆蓋原有的value 1034 * @param String key 1035 * @param String value 1036 * @return 狀態碼 1037 * */ 1038 public String set(String key, byte[] value) { 1039 return set(SafeEncoder.encode(key), value); 1040 } 1041 1042 /** 1043 * 添加記錄,如果記錄已存在將覆蓋原有的value 1044 * @param byte[] key 1045 * @param byte[] value 1046 * @return 狀態碼 1047 * */ 1048 public String set(byte[] key, byte[] value) { 1049 Jedis jedis = getJedis(); 1050 String status = jedis.set(key, value); 1051 returnJedis(jedis); 1052 return status; 1053 } 1054 1055 /** 1056 * 從指定位置開始插入數據,插入的數據會覆蓋指定位置以后的數據<br/> 1057 * 例:String str1="123456789";<br/> 1058 * 對str1操作后setRange(key,4,0000),str1="123400009"; 1059 * @param String key 1060 * @param long offset 1061 * @param String value 1062 * @return long value的長度 1063 * */ 1064 public long setRange(String key, long offset, String value) { 1065 Jedis jedis = getJedis(); 1066 long len = jedis.setrange(key, offset, value); 1067 returnJedis(jedis); 1068 return len; 1069 } 1070 1071 /** 1072 * 在指定的key中追加value 1073 * @param String key 1074 * @param String value 1075 * @return long 追加后value的長度 1076 * **/ 1077 public long append(String key, String value) { 1078 Jedis jedis = getJedis(); 1079 long len = jedis.append(key, value); 1080 returnJedis(jedis); 1081 return len; 1082 } 1083 1084 /** 1085 * 將key對應的value減去指定的值,只有value可以轉為數字時該方法才可用 1086 * @param String key 1087 * @param long number 要減去的值 1088 * @return long 減指定值后的值 1089 * */ 1090 public long decrBy(String key, long number) { 1091 Jedis jedis = getJedis(); 1092 long len = jedis.decrBy(key, number); 1093 returnJedis(jedis); 1094 return len; 1095 } 1096 1097 /** 1098 * <b>可以作為獲取唯一id的方法</b><br/> 1099 * 將key對應的value加上指定的值,只有value可以轉為數字時該方法才可用 1100 * @param String key 1101 * @param long number 要減去的值 1102 * @return long 相加后的值 1103 * */ 1104 public long incrBy(String key, long number) { 1105 Jedis jedis = getJedis(); 1106 long len = jedis.incrBy(key, number); 1107 returnJedis(jedis); 1108 return len; 1109 } 1110 1111 /** 1112 * 對指定key對應的value進行截取 1113 * @param String key 1114 * @param long startOffset 開始位置(包含) 1115 * @param long endOffset 結束位置(包含) 1116 * @return String 截取的值 1117 * */ 1118 public String getrange(String key, long startOffset, long endOffset) { 1119 //ShardedJedis sjedis = getShardedJedis(); 1120 Jedis sjedis = getJedis(); 1121 String value = sjedis.getrange(key, startOffset, endOffset); 1122 returnJedis(sjedis); 1123 return value; 1124 } 1125 1126 /** 1127 * 獲取並設置指定key對應的value<br/> 1128 * 如果key存在返回之前的value,否則返回null 1129 * @param String key 1130 * @param String value 1131 * @return String 原始value或null 1132 * */ 1133 public String getSet(String key, String value) { 1134 Jedis jedis = getJedis(); 1135 String str = jedis.getSet(key, value); 1136 returnJedis(jedis); 1137 return str; 1138 } 1139 1140 /** 1141 * 批量獲取記錄,如果指定的key不存在返回List的對應位置將是null 1142 * @param String keys 1143 * @return List<String> 值得集合 1144 * */ 1145 public List<String> mget(String... keys) { 1146 Jedis jedis = getJedis(); 1147 List<String> str = jedis.mget(keys); 1148 returnJedis(jedis); 1149 return str; 1150 } 1151 1152 /** 1153 * 批量存儲記錄 1154 * @param String keysvalues 例:keysvalues="key1","value1","key2","value2"; 1155 * @return String 狀態碼 1156 * */ 1157 public String mset(String... keysvalues) { 1158 Jedis jedis = getJedis(); 1159 String str = jedis.mset(keysvalues); 1160 returnJedis(jedis); 1161 return str; 1162 } 1163 1164 /** 1165 * 獲取key對應的值的長度 1166 * @param String key 1167 * @return value值得長度 1168 * */ 1169 public long strlen(String key) { 1170 Jedis jedis = getJedis(); 1171 long len = jedis.strlen(key); 1172 returnJedis(jedis); 1173 return len; 1174 } 1175 } 1176 1177 1178 //*******************************************Lists*******************************************// 1179 public class Lists { 1180 /** 1181 * List長度 1182 * @param String key 1183 * @return 長度 1184 * */ 1185 public long llen(String key) { 1186 return llen(SafeEncoder.encode(key)); 1187 } 1188 1189 /** 1190 * List長度 1191 * @param byte[] key 1192 * @return 長度 1193 * */ 1194 public long llen(byte[] key) { 1195 //ShardedJedis sjedis = getShardedJedis(); 1196 Jedis sjedis = getJedis(); 1197 long count = sjedis.llen(key); 1198 returnJedis(sjedis); 1199 return count; 1200 } 1201 1202 /** 1203 * 覆蓋操作,將覆蓋List中指定位置的值 1204 * @param byte[] key 1205 * @param int index 位置 1206 * @param byte[] value 值 1207 * @return 狀態碼 1208 * */ 1209 public String lset(byte[] key, int index, byte[] value) { 1210 Jedis jedis = getJedis(); 1211 String status = jedis.lset(key, index, value); 1212 returnJedis(jedis); 1213 return status; 1214 } 1215 1216 /** 1217 * 覆蓋操作,將覆蓋List中指定位置的值 1218 * @param key 1219 * @param int index 位置 1220 * @param String value 值 1221 * @return 狀態碼 1222 * */ 1223 public String lset(String key, int index, String value) { 1224 return lset(SafeEncoder.encode(key), index, 1225 SafeEncoder.encode(value)); 1226 } 1227 1228 /** 1229 * 在value的相對位置插入記錄 1230 * @param key 1231 * @param LIST_POSITION 前面插入或后面插入 1232 * @param String pivot 相對位置的內容 1233 * @param String value 插入的內容 1234 * @return 記錄總數 1235 * */ 1236 public long linsert(String key, LIST_POSITION where, String pivot, 1237 String value) { 1238 return linsert(SafeEncoder.encode(key), where, 1239 SafeEncoder.encode(pivot), SafeEncoder.encode(value)); 1240 } 1241 1242 /** 1243 * 在指定位置插入記錄 1244 * @param String key 1245 * @param LIST_POSITION 前面插入或后面插入 1246 * @param byte[] pivot 相對位置的內容 1247 * @param byte[] value 插入的內容 1248 * @return 記錄總數 1249 * */ 1250 public long linsert(byte[] key, LIST_POSITION where, byte[] pivot, 1251 byte[] value) { 1252 Jedis jedis = getJedis(); 1253 long count = jedis.linsert(key, where, pivot, value); 1254 returnJedis(jedis); 1255 return count; 1256 } 1257 1258 /** 1259 * 獲取List中指定位置的值 1260 * @param String key 1261 * @param int index 位置 1262 * @return 值 1263 * **/ 1264 public String lindex(String key, int index) { 1265 return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index)); 1266 } 1267 1268 /** 1269 * 獲取List中指定位置的值 1270 * @param byte[] key 1271 * @param int index 位置 1272 * @return 值 1273 * **/ 1274 public byte[] lindex(byte[] key, int index) { 1275 //ShardedJedis sjedis = getShardedJedis(); 1276 Jedis sjedis = getJedis(); 1277 byte[] value = sjedis.lindex(key, index); 1278 returnJedis(sjedis); 1279 return value; 1280 } 1281 1282 /** 1283 * 將List中的第一條記錄移出List 1284 * @param String key 1285 * @return 移出的記錄 1286 * */ 1287 public String lpop(String key) { 1288 return SafeEncoder.encode(lpop(SafeEncoder.encode(key))); 1289 } 1290 1291 /** 1292 * 將List中的第一條記錄移出List 1293 * @param byte[] key 1294 * @return 移出的記錄 1295 * */ 1296 public byte[] lpop(byte[] key) { 1297 Jedis jedis = getJedis(); 1298 byte[] value = jedis.lpop(key); 1299 returnJedis(jedis); 1300 return value; 1301 } 1302 1303 /** 1304 * 將List中最后第一條記錄移出List 1305 * 1306 * @param byte[] key 1307 * @return 移出的記錄 1308 * */ 1309 public String rpop(String key) { 1310 Jedis jedis = getJedis(); 1311 String value = jedis.rpop(key); 1312 returnJedis(jedis); 1313 return value; 1314 } 1315 1316 /** 1317 * 向List尾部追加記錄 1318 * @param String key 1319 * @param String value 1320 * @return 記錄總數 1321 * */ 1322 public long lpush(String key, String value) { 1323 return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value)); 1324 } 1325 1326 /** 1327 * 向List頭部追加記錄 1328 * @param String key 1329 * @param String value 1330 * @return 記錄總數 1331 * */ 1332 public long rpush(String key, String value) { 1333 Jedis jedis = getJedis(); 1334 long count = jedis.rpush(key, value); 1335 returnJedis(jedis); 1336 return count; 1337 } 1338 1339 /** 1340 * 向List頭部追加記錄 1341 * @param String key 1342 * @param String value 1343 * @return 記錄總數 1344 * */ 1345 public long rpush(byte[] key, byte[] value) { 1346 Jedis jedis = getJedis(); 1347 long count = jedis.rpush(key, value); 1348 returnJedis(jedis); 1349 return count; 1350 } 1351 1352 /** 1353 * 向List中追加記錄 1354 * @param byte[] key 1355 * @param byte[] value 1356 * @return 記錄總數 1357 * */ 1358 public long lpush(byte[] key, byte[] value) { 1359 Jedis jedis = getJedis(); 1360 long count = jedis.lpush(key, value); 1361 returnJedis(jedis); 1362 return count; 1363 } 1364 1365 /** 1366 * 獲取指定范圍的記錄,可以做為分頁使用 1367 * @param String key 1368 * @param long start 1369 * @param long end 1370 * @return List 1371 * */ 1372 public List<String> lrange(String key, long start, long end) { 1373 //ShardedJedis sjedis = getShardedJedis(); 1374 Jedis sjedis = getJedis(); 1375 List<String> list = sjedis.lrange(key, start, end); 1376 returnJedis(sjedis); 1377 return list; 1378 } 1379 1380 /** 1381 * 獲取指定范圍的記錄,可以做為分頁使用 1382 * @param byte[] key 1383 * @param int start 1384 * @param int end 如果為負數,則尾部開始計算 1385 * @return List 1386 * */ 1387 public List<byte[]> lrange(byte[] key, int start, int end) { 1388 //ShardedJedis sjedis = getShardedJedis(); 1389 Jedis sjedis = getJedis(); 1390 List<byte[]> list = sjedis.lrange(key, start, end); 1391 returnJedis(sjedis); 1392 return list; 1393 } 1394 1395 /** 1396 * 刪除List中c條記錄,被刪除的記錄值為value 1397 * @param byte[] key 1398 * @param int c 要刪除的數量,如果為負數則從List的尾部檢查並刪除符合的記錄 1399 * @param byte[] value 要匹配的值 1400 * @return 刪除后的List中的記錄數 1401 * */ 1402 public long lrem(byte[] key, int c, byte[] value) { 1403 Jedis jedis = getJedis(); 1404 long count = jedis.lrem(key, c, value); 1405 returnJedis(jedis); 1406 return count; 1407 } 1408 1409 /** 1410 * 刪除List中c條記錄,被刪除的記錄值為value 1411 * @param String key 1412 * @param int c 要刪除的數量,如果為負數則從List的尾部檢查並刪除符合的記錄 1413 * @param String value 要匹配的值 1414 * @return 刪除后的List中的記錄數 1415 * */ 1416 public long lrem(String key, int c, String value) { 1417 return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value)); 1418 } 1419 1420 /** 1421 * 算是刪除吧,只保留start與end之間的記錄 1422 * @param byte[] key 1423 * @param int start 記錄的開始位置(0表示第一條記錄) 1424 * @param int end 記錄的結束位置(如果為-1則表示最后一個,-2,-3以此類推) 1425 * @return 執行狀態碼 1426 * */ 1427 public String ltrim(byte[] key, int start, int end) { 1428 Jedis jedis = getJedis(); 1429 String str = jedis.ltrim(key, start, end); 1430 returnJedis(jedis); 1431 return str; 1432 } 1433 1434 /** 1435 * 算是刪除吧,只保留start與end之間的記錄 1436 * @param String key 1437 * @param int start 記錄的開始位置(0表示第一條記錄) 1438 * @param int end 記錄的結束位置(如果為-1則表示最后一個,-2,-3以此類推) 1439 * @return 執行狀態碼 1440 * */ 1441 public String ltrim(String key, int start, int end) { 1442 return ltrim(SafeEncoder.encode(key), start, end); 1443 } 1444 } 1445 1446 public static void main(String[] args) { 1447 JedisUtil jedisUtil= JedisUtil.getInstance(); 1448 JedisUtil.Strings strings=jedisUtil.new Strings(); 1449 strings.set("nnn", "nnnn"); 1450 System.out.println("-----"+strings.get("nnn")); 1451 1452 Jedis jedis=JedisUtil.getInstance().getJedis(); 1453 for (int i = 0; i < 10; i++) { 1454 jedis.set("test", "test"); 1455 System.out.println(i+"=="+jedis.get("test")); 1456 1457 } 1458 JedisUtil.getInstance().returnJedis(jedis); 1459 } 1460 1461 }
補充:
1 /** 2 * @Title: 3 * getList @Description:(根據key以及類得到redis服務器中集合字符串對應的集合) @param @param 4 * key @param @param clazz @param @return 設定文件 @return List<T> 返回類型 @throws 5 */ 6 public <T> List<T> getList(String key, Class<T> clazz) { 7 String valueStr = this.get(key); 8 if (valueStr == null || "".equals(valueStr)) 9 return null; 10 if (!isGoodJson(valueStr)) { 11 valueStr = JSON.toJSONString(valueStr); 12 } 13 return JSONArray.parseArray(valueStr, clazz); 14 } 15 16 /** 17 * 18 * @Title: getObject @Description: 19 * (根據key以及類得到redis服務器中對象字符串對應的對象) @param @param key @param @param 20 * clazz @param @return 設定文件 @return T 返回類型 @throws 21 */ 22 public <T> T getObject(String key, Class<T> clazz) { 23 String valueStr = this.get(key); 24 if (valueStr == null || "".equals(valueStr)) 25 return null; 26 if (!isGoodJson(valueStr)) { 27 valueStr = JSON.toJSONString(valueStr); 28 } 29 return (T) JSON.parseObject(valueStr, clazz); 30 } 31 32 /** 33 * 34 * @Title: isGoodJson @Description: (判斷是否是json格式字符串) @param @param 35 * json @param @return 設定文件 @return boolean 返回類型 @throws 36 */ 37 public static boolean isGoodJson(String json) { 38 if (StringUtils.isBlank(json)) { 39 return false; 40 } 41 try { 42 JSON.parse(json); 43 return true; 44 } catch (Exception e) { 45 return false; 46 } 47 }
2、序列化、反序列化:
redis服務器本身支持二進制安全的類型,所以可以把一個java對象序列化后存儲到redis中。下面封裝了一個序列化、反序列化的工具類:
1 package redis.utils; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.ObjectInputStream; 6 import java.io.ObjectOutputStream; 7 8 public class SerializeUtil { 9 /** 10 * 序列化 11 * 12 * @param object 13 * @return 14 */ 15 public static byte[] serialize(Object object) { 16 ObjectOutputStream oos = null; 17 ByteArrayOutputStream baos = null; 18 try { 19 // 序列化 20 baos = new ByteArrayOutputStream(); 21 oos = new ObjectOutputStream(baos); 22 oos.writeObject(object); 23 byte[] bytes = baos.toByteArray(); 24 return bytes; 25 } catch (Exception e) { 26 27 } 28 return null; 29 } 30 31 /** 32 * 反序列化 33 * 34 * @param bytes 35 * @return 36 */ 37 public static Object unserialize(byte[] bytes) { 38 ByteArrayInputStream bais = null; 39 try { 40 // 反序列化 41 bais = new ByteArrayInputStream(bytes); 42 ObjectInputStream ois = new ObjectInputStream(bais); 43 return ois.readObject(); 44 } catch (Exception e) { 45 46 } 47 return null; 48 } 49 }
3、測試:
1)直接使用RedisUtils實例進行五大數據類型的操作:(這樣,使用完后會自動歸還到池子中)
1 JedisUtil jedisUtil= JedisUtil.getInstance(); 2 JedisUtil.Strings strings=jedisUtil.new Strings(); 3 strings.set("nnn", "nnnn"); 4 System.out.println("-----"+strings.get("nnn"));
2)通過RedisUtil實例獲取Jedis連接對象;這樣就可以用原生的方式使用;最后使用完后需要手動將其歸還到池子中:
1 Jedis jedis=JedisUtil.getInstance().getJedis(); 2 for (int i = 0; i < 10; i++) { 3 jedis.set("test", "test"); 4 System.out.println(i+"=="+jedis.get("test")); 5 6 } 7 JedisUtil.getInstance().returnJedis(jedis);
3)將java對象存到redis中:
1 Person p = new Person(); 2 p.setId(3); 3 p.setName("測試"); 4 5 JedisUtil.Strings strings=jedisUtil.new Strings(); 6 strings.set("object3", SerializeUtil.serialize(p)); 7 8 //jedis.set(SafeEncoder.encode("object1"),SerializeUtil.serialize(p)); 9 byte[] personBy = jedis.get(SafeEncoder.encode("object3")); 10 Person p1 = (Person) SerializeUtil.unserialize(personBy); 11 System.out.println(p1.getName());