RedisUtil工具类

   1 import org.springframework.beans.factory.annotation.Autowired;
   2 import org.springframework.data.redis.core.RedisTemplate;
   3 import org.springframework.data.redis.core.ZSetOperations;
   4 import org.springframework.stereotype.Component;
   5 import org.springframework.util.CollectionUtils;
   6 
   7 import java.util.Collection;
   8 import java.util.List;
   9 import java.util.Map;
  10 import java.util.Set;
  11 import java.util.concurrent.TimeUnit;
  12 
  13 /**
  14  * ClassName:RedisUtil                    //类名
  15  * Packge:utils                //包名
  16  * Description:                         //描述
  17  *
  18  * @date:2020/6/3 10:51                //日期
  19  * @author:xiayang13627161402@163.com //联系方式
  20  */
  21 @Component
  22 public class RedisUtil {
  23 
  24     @Autowired
  25     private RedisTemplate<String,Object> redisTemplate;
  26 
  27     // =============================common============================
  28     /**
  29      * 指定缓存失效时间
  30      * @param key  键
  31      * @param time 时间(秒)
  32      */
  33     public boolean expire(String key, long time) {
  34         try {
  35             if (time > 0) {
  36                 redisTemplate.expire(key, time, TimeUnit.SECONDS);
  37             }
  38             return true;
  39         } catch (Exception e) {
  40             e.printStackTrace();
  41             return false;
  42         }
  43     }
  44 
  45     /**
  46      * 根据key 获取过期时间
  47      * @param key 键 不能为null
  48      * @return 时间(秒) 返回0代表为永久有效
  49      */
  50     public long getExpire(String key) {
  51         return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  52     }
  53 
  54 
  55     /**
  56      * 移除指定key 的过期时间
  57      * @param key
  58      * @return
  59      */
  60     public boolean persist(String key){
  61         return redisTemplate.boundValueOps(key).persist();
  62     }
  63 
  64     /**
  65      * 判断key是否存在
  66      * @param key 键
  67      * @return true 存在 false不存在
  68      */
  69     public boolean hasKey(String key) {
  70         try {
  71             return redisTemplate.hasKey(key);
  72         } catch (Exception e) {
  73             e.printStackTrace();
  74             return false;
  75         }
  76     }
  77 
  78     /**
  79      * 修改 key
  80      * @param key
  81      * @return
  82      */
  83     public void rename(String key,String newKey){
  84         redisTemplate.boundValueOps(key).rename(newKey);
  85     }
  86 
  87     /**
  88      * 删除缓存
  89      * @param key 可以传一个值 或多个
  90      */
  91     @SuppressWarnings("unchecked")
  92     public void del(String... key) {
  93         if (key != null && key.length > 0) {
  94             if (key.length == 1) {
  95                 redisTemplate.delete(key[0]);
  96             } else {
  97                 redisTemplate.delete(CollectionUtils.arrayToList(key));
  98             }
  99         }
 100     }
 101 
 102 
 103     // ============================String=============================
 104 
 105     /**
 106      * 普通缓存获取
 107      * @param key 键
 108      * @return 109      */
 110     public Object get(String key) {
 111         return key == null ? null : redisTemplate.opsForValue().get(key);
 112     }
 113 
 114     /**
 115      * 普通缓存放入
 116      * @param key   键
 117      * @param value 值
 118      * @return true成功 false失败
 119      */
 120 
 121     public boolean set(String key, Object value) {
 122         try {
 123             redisTemplate.opsForValue().set(key, value);
 124             return true;
 125         } catch (Exception e) {
 126             e.printStackTrace();
 127             return false;
 128         }
 129     }
 130 
 131 
 132     /**
 133      * 普通缓存放入并设置时间
 134      * @param key   键
 135      * @param value 值
 136      * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
 137      * @return true成功 false 失败
 138      */
 139     public boolean set(String key, Object value, long time) {
 140         try {
 141             if (time > 0) {
 142                 redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
 143             } else {
 144                 set(key, value);
 145             }
 146             return true;
 147         } catch (Exception e) {
 148             e.printStackTrace();
 149             return false;
 150         }
 151     }
 152 
 153     /**
 154      * 如果 key 存在则覆盖,并返回旧值.
 155      * 如果不存在,返回null 并添加
 156      * @param key
 157      * @param value
 158      * @return
 159      */
 160     public Object getAndSet(String key,Object value){
 161         return redisTemplate.opsForValue().getAndSet(key, value);
 162     }
 163 
 164     /**
 165      * 批量添加 key-value (重复的键会覆盖)
 166      * @param keyAndValue
 167      */
 168     public void batchSet(Map<String,Object> keyAndValue){
 169         redisTemplate.opsForValue().multiSet(keyAndValue);
 170     }
 171 
 172     /**
 173      * 批量添加 key-value 只有在键不存在时,才添加
 174      * map 中只要有一个key存在,则全部不添加
 175      * @param keyAndValue
 176      */
 177     public void batchSetIfAbsent(Map<String,Object> keyAndValue){
 178         redisTemplate.opsForValue().multiSetIfAbsent(keyAndValue);
 179     }
 180 
 181 
 182     /**
 183      * 递增
 184      * @param key   键
 185      * @param delta 要增加几(大于0)
 186      */
 187     public long incr(String key, long delta) {
 188         if (delta < 0) {
 189             throw new RuntimeException("递增因子必须大于0");
 190         }
 191         return redisTemplate.opsForValue().increment(key, delta);
 192     }
 193     /**
 194      * 递增
 195      * @param key   键
 196      * @param delta 要增加几(大于0)
 197      */
 198     public double incr(String key, double delta) {
 199         if (delta < 0) {
 200             throw new RuntimeException("递增因子必须大于0");
 201         }
 202         return redisTemplate.opsForValue().increment(key, delta);
 203     }
 204 
 205 
 206     /**
 207      * 递减
 208      * @param key   键
 209      * @param delta 要减少几(小于0)
 210      */
 211     public long decr(String key, long delta) {
 212         if (delta < 0) {
 213             throw new RuntimeException("递减因子必须大于0");
 214         }
 215         return redisTemplate.opsForValue().increment(key, -delta);
 216     }
 217 
 218     /**
 219      * 递减
 220      * @param key   键
 221      * @param delta 要减少几(小于0)
 222      */
 223     public double decr(String key, double delta) {
 224         if (delta < 0) {
 225             throw new RuntimeException("递减因子必须大于0");
 226         }
 227         return redisTemplate.opsForValue().increment(key, -delta);
 228     }
 229 
 230 
 231     // ================================Map=================================
 232 
 233     /**
 234      * HashGet
 235      * @param key  键 不能为null
 236      * @param item 项 不能为null
 237      */
 238     public Object hget(String key, String item) {
 239         return redisTemplate.opsForHash().get(key, item);
 240     }
 241 
 242     /**
 243      * 获取hashKey对应的所有键值
 244      * @param key 键
 245      * @return 对应的多个键值
 246      */
 247     public Map<Object, Object> hmget(String key) {
 248         return redisTemplate.opsForHash().entries(key);
 249     }
 250 
 251     /**
 252      * HashSet
 253      * @param key 键
 254      * @param map 对应多个键值
 255      */
 256     public boolean hmset(String key, Map<String, Object> map) {
 257         try {
 258             redisTemplate.opsForHash().putAll(key, map);
 259             return true;
 260         } catch (Exception e) {
 261             e.printStackTrace();
 262             return false;
 263         }
 264     }
 265 
 266 
 267     /**
 268      * HashSet 并设置时间
 269      * @param key  键
 270      * @param map  对应多个键值
 271      * @param time 时间(秒)
 272      * @return true成功 false失败
 273      */
 274     public boolean hmset(String key, Map<String, Object> map, long time) {
 275         try {
 276             redisTemplate.opsForHash().putAll(key, map);
 277             if (time > 0) {
 278                 expire(key, time);
 279             }
 280             return true;
 281         } catch (Exception e) {
 282             e.printStackTrace();
 283             return false;
 284         }
 285     }
 286 
 287 
 288     /**
 289      * 向一张hash表中放入数据,如果不存在将创建
 290      *
 291      * @param key   键
 292      * @param item  项
 293      * @param value 值
 294      * @return true 成功 false失败
 295      */
 296     public boolean hset(String key, String item, Object value) {
 297         try {
 298             redisTemplate.opsForHash().put(key, item, value);
 299             return true;
 300         } catch (Exception e) {
 301             e.printStackTrace();
 302             return false;
 303         }
 304     }
 305 
 306     /**
 307      * 向一张hash表中放入数据,如果不存在将创建
 308      *
 309      * @param key   键
 310      * @param item  项
 311      * @param value 值
 312      * @param time  时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
 313      * @return true 成功 false失败
 314      */
 315     public boolean hset(String key, String item, Object value, long time) {
 316         try {
 317             redisTemplate.opsForHash().put(key, item, value);
 318             if (time > 0) {
 319                 expire(key, time);
 320             }
 321             return true;
 322         } catch (Exception e) {
 323             e.printStackTrace();
 324             return false;
 325         }
 326     }
 327 
 328 
 329     /**
 330      * 删除hash表中的值
 331      *
 332      * @param key  键 不能为null
 333      * @param item 项 可以使多个 不能为null
 334      */
 335     public void hdel(String key, Object... item) {
 336         redisTemplate.opsForHash().delete(key, item);
 337     }
 338 
 339 
 340     /**
 341      * 判断hash表中是否有该项的值
 342      *
 343      * @param key  键 不能为null
 344      * @param item 项 不能为null
 345      * @return true 存在 false不存在
 346      */
 347     public boolean hHasKey(String key, String item) {
 348         return redisTemplate.opsForHash().hasKey(key, item);
 349     }
 350 
 351     /**
 352      * 获取 key 下的 所有 hashkey 字段名
 353      * @param key
 354      * @return
 355      */
 356     public Set<Object> hashKeys(String key){
 357         return redisTemplate.opsForHash().keys(key);
 358     }
 359 
 360     /**
 361      * 获取指定 hash 下面的 键值对 数量
 362      * @param key
 363      * @return
 364      */
 365     public Long hashSize(String key){
 366         return redisTemplate.opsForHash().size(key);
 367     }
 368 
 369     /**
 370      * hash递增 如果不存在,就会创建一个 并把新增后的值返回
 371      *
 372      * @param key  键
 373      * @param item 项
 374      * @param by   要增加几(大于0)
 375      */
 376     public double hincrDouble(String key, String item, double by) {
 377         return redisTemplate.opsForHash().increment(key, item, by);
 378     }
 379 
 380     /**
 381      * hash递增 如果不存在,就会创建一个 并把新增后的值返回
 382      *
 383      * @param key  键
 384      * @param item 项
 385      * @param by   要增加几(大于0)
 386      */
 387     public long hincrLong(String key, String item, long by) {
 388         return redisTemplate.opsForHash().increment(key, item, by);
 389     }
 390 
 391 
 392     /**
 393      * hash递减
 394      *
 395      * @param key  键
 396      * @param item 项
 397      * @param by   要减少记(小于0)
 398      */
 399     public double hdecrDouble(String key, String item, double by) {
 400         return redisTemplate.opsForHash().increment(key, item, -by);
 401     }
 402 
 403     /**
 404      * hash递减
 405      *
 406      * @param key  键
 407      * @param item 项
 408      * @param by   要减少记(小于0)
 409      */
 410     public long hdecrLong(String key, String item, long by) {
 411         return redisTemplate.opsForHash().increment(key, item, -by);
 412     }
 413 
 414 
 415     // ============================set=============================
 416 
 417     /**
 418      * 根据key获取Set中的所有值
 419      * @param key 键
 420      */
 421     public Set<Object> sGet(String key) {
 422         try {
 423             return redisTemplate.opsForSet().members(key);
 424         } catch (Exception e) {
 425             e.printStackTrace();
 426             return null;
 427         }
 428     }
 429 
 430 
 431     /**
 432      * 根据value从一个set中查询,是否存在
 433      *
 434      * @param key   键
 435      * @param value 值
 436      * @return true 存在 false不存在
 437      */
 438     public boolean sHasKey(String key, Object value) {
 439         try {
 440             return redisTemplate.opsForSet().isMember(key, value);
 441         } catch (Exception e) {
 442             e.printStackTrace();
 443             return false;
 444         }
 445     }
 446 
 447 
 448     /**
 449      * 将数据放入set缓存
 450      *
 451      * @param key    键
 452      * @param values 值 可以是多个
 453      * @return 成功个数
 454      */
 455     public long sSet(String key, Object... values) {
 456         try {
 457             return redisTemplate.opsForSet().add(key, values);
 458         } catch (Exception e) {
 459             e.printStackTrace();
 460             return 0;
 461         }
 462     }
 463 
 464 
 465     /**
 466      * 将set数据放入缓存
 467      *
 468      * @param key    键
 469      * @param time   时间(秒)
 470      * @param values 值 可以是多个
 471      * @return 成功个数
 472      */
 473     public long sSetAndTime(String key, long time, Object... values) {
 474         try {
 475             Long count = redisTemplate.opsForSet().add(key, values);
 476             if (time > 0)
 477                 expire(key, time);
 478             return count;
 479         } catch (Exception e) {
 480             e.printStackTrace();
 481             return 0;
 482         }
 483     }
 484 
 485 
 486     /**
 487      * 获取set缓存的长度
 488      *
 489      * @param key 键
 490      */
 491     public long sGetSetSize(String key) {
 492         try {
 493             return redisTemplate.opsForSet().size(key);
 494         } catch (Exception e) {
 495             e.printStackTrace();
 496             return 0;
 497         }
 498     }
 499 
 500     /**
 501      * 获取两个集合的差集
 502      * @param key
 503      * @param otherkey
 504      * @return
 505      */
 506     public Set<Object> difference(String key ,String otherkey){
 507         return redisTemplate.opsForSet().difference(key, otherkey);
 508     }
 509 
 510     /**
 511      * 获取 key 和 集合  collections 中的 key 集合的差集
 512      * @param key
 513      * @param otherKeys
 514      * @return
 515      */
 516     public Set<Object> difference(String key ,Collection<String> otherKeys){
 517         return redisTemplate.opsForSet().difference(key, otherKeys);
 518     }
 519 
 520     /**
 521      * 将  key 与 otherkey 的差集 ,添加到新的 newKey 集合中
 522      * @param key
 523      * @param otherkey
 524      * @param newKey
 525      * @return 返回差集的数量
 526      */
 527     public Long differenceAndStore(String key ,String otherkey,String newKey){
 528         return redisTemplate.opsForSet().differenceAndStore(key, otherkey, newKey);
 529     }
 530 
 531     /**
 532      * 移除值为value的
 533      *
 534      * @param key    键
 535      * @param values 值 可以是多个
 536      * @return 移除的个数
 537      */
 538     public long setRemove(String key, Object... values) {
 539         try {
 540             Long count = redisTemplate.opsForSet().remove(key, values);
 541             return count;
 542         } catch (Exception e) {
 543             e.printStackTrace();
 544             return 0;
 545         }
 546     }
 547 
 548     /**
 549      * 随机移除一个元素,并返回出来
 550      * @param key
 551      * @return
 552      */
 553     public Object randomSetPop(String key){
 554         return redisTemplate.opsForSet().pop(key);
 555     }
 556 
 557     /**
 558      * 随机获取一个元素
 559      * @param key
 560      * @return
 561      */
 562     public Object randomSet(String key){
 563         return redisTemplate.opsForSet().randomMember(key);
 564     }
 565 
 566     /**
 567      * 随机获取指定数量的元素,同一个元素可能会选中两次
 568      * @param key
 569      * @param count
 570      * @return
 571      */
 572     public List<Object> randomSet(String key,long count){
 573         return redisTemplate.opsForSet().randomMembers(key, count);
 574     }
 575 
 576     /**
 577      * 随机获取指定数量的元素,去重(同一个元素只能选择一次)
 578      * @param key
 579      * @param count
 580      * @return
 581      */
 582     public Set<Object> randomSetDistinct(String key,long count){
 583         return redisTemplate.opsForSet().distinctRandomMembers(key, count);
 584     }
 585 
 586     /**
 587      * 将 key 中的 value 转入到 destKey 中
 588      * @param key
 589      * @param value
 590      * @param destKey
 591      * @return 返回成功与否
 592      */
 593     public boolean moveSet(String key,Object value,String destKey){
 594         return redisTemplate.opsForSet().move(key, value, destKey);
 595     }
 596 
 597     /**
 598      * 返回 key 和 othere 的并集
 599      * @param key
 600      * @param otherKey
 601      * @return
 602      */
 603     public Set<Object> unionSet(String key,String otherKey){
 604         return redisTemplate.opsForSet().union(key, otherKey);
 605     }
 606 
 607     /**
 608      * 返回 key 和 otherKeys 的并集
 609      * @param key
 610      * @param otherKeys key 的集合
 611      * @return
 612      */
 613     public Set<Object> unionSet(String key, Collection<String> otherKeys){
 614         return redisTemplate.opsForSet().union(key, otherKeys);
 615     }
 616 
 617     /**
 618      * 将 key 与 otherKey 的并集,保存到 destKey 中
 619      * @param key
 620      * @param otherKey
 621      * @param destKey
 622      * @return destKey 数量
 623      */
 624     public Long unionAndStoreSet(String key, String otherKey,String destKey){
 625         return redisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);
 626     }
 627 
 628     /**
 629      * 将 key 与 otherKey 的并集,保存到 destKey 中
 630      * @param key
 631      * @param otherKeys
 632      * @param destKey
 633      * @return destKey 数量
 634      */
 635     public Long unionAndStoreSet(String key, Collection<String> otherKeys,String destKey){
 636         return redisTemplate.opsForSet().unionAndStore(key, otherKeys, destKey);
 637     }
 638     // ===============================list=================================
 639 
 640     /**
 641      * 指定 list 从左入栈
 642      * @param key
 643      * @return 当前队列的长度
 644      */
 645     public Long leftPush(String key,Object value){
 646         return redisTemplate.opsForList().leftPush(key, value);
 647     }
 648     /**
 649      * 从左边依次入栈
 650      * 导入顺序按照 Collection 顺序
 651      * 如: a b c => c b a
 652      * @param key
 653      * @param values
 654      * @return
 655      */
 656     public Long leftPushAll(String key, List<Object> values){
 657         return redisTemplate.opsForList().leftPushAll(key, values);
 658     }
 659 
 660     /**
 661      * 指定 list 从左出栈
 662      *
 663      * @param key
 664      * @return 出栈的值
 665      */
 666     public Object leftPop(String key){
 667         try {
 668             return redisTemplate.opsForList().leftPop(key);
 669         } catch (Exception e) {
 670             e.printStackTrace();
 671             return null;
 672         }
 673     }
 674 
 675     /**
 676      * 获取list缓存的内容
 677      *
 678      * @param key   键
 679      * @param start 开始
 680      * @param end   结束 0 到 -1代表所有值
 681      */
 682     public List<Object> lGet(String key, long start, long end) {
 683         try {
 684             return redisTemplate.opsForList().range(key, start, end);
 685         } catch (Exception e) {
 686             e.printStackTrace();
 687             return null;
 688         }
 689     }
 690 
 691 
 692     /**
 693      * 获取list缓存的长度
 694      *
 695      * @param key 键
 696      */
 697     public long lGetListSize(String key) {
 698         try {
 699             return redisTemplate.opsForList().size(key);
 700         } catch (Exception e) {
 701             e.printStackTrace();
 702             return 0;
 703         }
 704     }
 705 
 706 
 707     /**
 708      * 通过索引 获取list中的值
 709      *
 710      * @param key   键
 711      * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
 712      */
 713     public Object lGetIndex(String key, long index) {
 714         try {
 715             return redisTemplate.opsForList().index(key, index);
 716         } catch (Exception e) {
 717             e.printStackTrace();
 718             return null;
 719         }
 720     }
 721 
 722 
 723     /**
 724      * 将list放入缓存
 725      *
 726      * @param key   键
 727      * @param value 值
 728      */
 729     public boolean lSet(String key, Object value) {
 730         try {
 731             redisTemplate.opsForList().rightPush(key, value);
 732             return true;
 733         } catch (Exception e) {
 734             e.printStackTrace();
 735             return false;
 736         }
 737     }
 738 
 739 
 740     /**
 741      * 将list放入缓存
 742      * @param key   键
 743      * @param value 值
 744      * @param time  时间(秒)
 745      */
 746     public boolean lSet(String key, Object value, long time) {
 747         try {
 748             redisTemplate.opsForList().rightPush(key, value);
 749             if (time > 0)
 750                 expire(key, time);
 751             return true;
 752         } catch (Exception e) {
 753             e.printStackTrace();
 754             return false;
 755         }
 756 
 757     }
 758 
 759 
 760     /**
 761      * 将list放入缓存
 762      *
 763      * @param key   键
 764      * @param value 值
 765      * @return
 766      */
 767     public boolean lSet(String key, List<Object> value) {
 768         try {
 769             redisTemplate.opsForList().rightPushAll(key, value);
 770             return true;
 771         } catch (Exception e) {
 772             e.printStackTrace();
 773             return false;
 774         }
 775 
 776     }
 777 
 778     /**
 779      * 指定 list 从左出栈
 780      *
 781      * @param key
 782      * @return 出栈的值
 783      */
 784     public Object rightPop(String key){
 785         try {
 786             return redisTemplate.opsForList().rightPop(key);
 787         } catch (Exception e) {
 788             e.printStackTrace();
 789             return null;
 790         }
 791     }
 792 
 793     /**
 794      * 将list放入缓存
 795      *
 796      * @param key   键
 797      * @param value 值
 798      * @param time  时间(秒)
 799      * @return
 800      */
 801     public boolean lSet(String key, List<Object> value, long time) {
 802         try {
 803             redisTemplate.opsForList().rightPushAll(key, value);
 804             if (time > 0)
 805                 expire(key, time);
 806             return true;
 807         } catch (Exception e) {
 808             e.printStackTrace();
 809             return false;
 810         }
 811     }
 812 
 813     /**
 814      * 根据索引修改list中的某条数据
 815      *
 816      * @param key   键
 817      * @param index 索引
 818      * @param value 值
 819      * @return
 820      */
 821 
 822     public boolean lUpdateIndex(String key, long index, Object value) {
 823         try {
 824             redisTemplate.opsForList().set(key, index, value);
 825             return true;
 826         } catch (Exception e) {
 827             e.printStackTrace();
 828             return false;
 829         }
 830     }
 831 
 832     /**
 833      * 移除N个值为value
 834      *
 835      * @param key   键
 836      * @param count 移除多少个
 837      * @param value 值
 838      * @return 移除的个数
 839      */
 840 
 841     public long lRemove(String key, long count, Object value) {
 842         try {
 843             Long remove = redisTemplate.opsForList().remove(key, count, value);
 844             return remove;
 845         } catch (Exception e) {
 846             e.printStackTrace();
 847             return 0;
 848         }
 849 
 850     }
 851     /**
 852      * 删除 列表 [start,end] 以外的所有元素
 853      * @param key
 854      * @param start
 855      * @param end
 856      */
 857     public void listTrim(String key,long start,long end){
 858         redisTemplate.opsForList().trim(key, start, end);
 859 
 860     }
 861 
 862     /**
 863      * 将 key 右出栈,并左入栈到 key2
 864      *
 865      * @param key 右出栈的列表
 866      * @param key2 左入栈的列表
 867      * @return 操作的值
 868      */
 869     public Object rightPopAndLeftPush(String key,String key2){
 870         return redisTemplate.opsForList().rightPopAndLeftPush(key, key2);
 871 
 872     }
 873 
 874 //=====================zSet======================================
 875     /**
 876      * 添加 ZSet 元素
 877      * @param key
 878      * @param value
 879      * @param score
 880      */
 881     public boolean add(String key,Object value,double score){
 882         return redisTemplate.opsForZSet().add(key, value, score);
 883     }
 884 
 885     /**
 886      * 批量添加 Zset <br>
 887      *         Set<TypedTuple<V>> tuples = new HashSet<>();<br>
 888      *         TypedTuple<V> objectTypedTuple1 = new DefaultTypedTuple<V>("zset-5",9.6);<br>
 889      *         tuples.add(objectTypedTuple1);
 890      * @param key
 891      * @param tuples
 892      * @return
 893      */
 894     public Long batchAddZset(String key,Set<ZSetOperations.TypedTuple<Object>> tuples){
 895         return redisTemplate.opsForZSet().add(key, tuples);
 896     }
 897 
 898     /**
 899      * Zset 删除一个或多个元素
 900      * @param key
 901      * @param values
 902      * @return
 903      */
 904     public Long removeZset(String key,Object ...values){
 905         return redisTemplate.opsForZSet().remove(key, values);
 906     }
 907 
 908     /**
 909      * 对指定的 zset 的 value 值 , socre 属性做增减操作
 910      * @param key
 911      * @param value
 912      * @param score
 913      * @return
 914      */
 915     public Double incrementScore(String key,Object value,double score){
 916         return redisTemplate.opsForZSet().incrementScore(key, value, score);
 917     }
 918 
 919     /**
 920      * 获取 key 中指定 value 的排名(从0开始,从小到大排序)
 921      * @param key
 922      * @param value
 923      * @return
 924      */
 925     public Long rank(String key,Object value){
 926         return redisTemplate.opsForZSet().rank(key, value);
 927     }
 928 
 929     /**
 930      * 获取 key 中指定 value 的排名(从0开始,从大到小排序)
 931      * @param key
 932      * @param value
 933      * @return
 934      */
 935     public Long reverseRank(String key,Object value){
 936         return redisTemplate.opsForZSet().reverseRank(key, value);
 937     }
 938 
 939     /**
 940      * 获取索引区间内的排序结果集合(从0开始,从小到大,带上分数)
 941      * @param key
 942      * @param start
 943      * @param end
 944      * @return
 945      */
 946     public Set<ZSetOperations.TypedTuple<Object>> rangeWithScores(String key, long start, long end){
 947         return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
 948     }
 949 
 950     /**
 951      * 获取索引区间内的排序结果集合(从0开始,从小到大,只有列名)
 952      * @param key
 953      * @param start
 954      * @param end
 955      * @return
 956      */
 957     public Set<Object> range(String key, long start, long end){
 958         return redisTemplate.opsForZSet().range(key, start, end);
 959     }
 960 
 961     /**
 962      * 获取分数范围内的 [min,max] 的排序结果集合 (从小到大,只有列名)
 963      * @param key
 964      * @param min
 965      * @param max
 966      * @return
 967      */
 968     public Set<Object> rangeByScore(String key, double min, double max){
 969         return redisTemplate.opsForZSet().rangeByScore(key, min, max);
 970     }
 971 
 972     /**
 973      * 获取分数范围内的 [min,max] 的排序结果集合 (从小到大,集合带分数)
 974      * @param key
 975      * @param min
 976      * @param max
 977      * @return
 978      */
 979     public Set<ZSetOperations.TypedTuple<Object>> rangeByScoreWithScores(String key, double min, double max){
 980         return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
 981     }
 982 
 983     /**
 984      * 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从小到大,不带分数的集合)
 985      * @param key
 986      * @param min
 987      * @param max
 988      * @param offset 从指定下标开始
 989      * @param count 输出指定元素数量
 990      * @return
 991      */
 992     public Set<Object> rangeByScore(String key, double min, double max,long offset,long count){
 993         return redisTemplate.opsForZSet().rangeByScore(key, min, max, offset, count);
 994     }
 995 
 996     /**
 997      * 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从小到大,带分数的集合)
 998      * @param key
 999      * @param min
1000      * @param max
1001      * @param offset 从指定下标开始
1002      * @param count 输出指定元素数量
1003      * @return
1004      */
1005     public Set<ZSetOperations.TypedTuple<Object>> rangeByScoreWithScores(String key, double min, double max, long offset, long count){
1006         return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max, offset, count);
1007     }
1008 
1009     /**
1010      * 获取索引区间内的排序结果集合(从0开始,从大到小,只有列名)
1011      * @param key
1012      * @param start
1013      * @param end
1014      * @return
1015      */
1016     public Set<Object> reverseRange(String key,long start,long end){
1017         return redisTemplate.opsForZSet().reverseRange(key, start, end);
1018     }
1019 
1020     /**
1021      * 获取索引区间内的排序结果集合(从0开始,从大到小,带上分数)
1022      * @param key
1023      * @param start
1024      * @param end
1025      * @return
1026      */
1027     public Set<ZSetOperations.TypedTuple<Object>> reverseRangeWithScores(String key, long start, long end){
1028         return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
1029     }
1030 
1031     /**
1032      * 获取分数范围内的 [min,max] 的排序结果集合 (从大到小,集合不带分数)
1033      * @param key
1034      * @param min
1035      * @param max
1036      * @return
1037      */
1038     public Set<Object> reverseRangeByScore(String key,double min,double max){
1039         return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
1040     }
1041 
1042     /**
1043      * 获取分数范围内的 [min,max] 的排序结果集合 (从大到小,集合带分数)
1044      * @param key
1045      * @param min
1046      * @param max
1047      * @return
1048      */
1049     public Set<ZSetOperations.TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max){
1050         return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max);
1051     }
1052 
1053     /**
1054      * 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从大到小,不带分数的集合)
1055      * @param key
1056      * @param min
1057      * @param max
1058      * @param offset 从指定下标开始
1059      * @param count 输出指定元素数量
1060      * @return
1061      */
1062     public Set<Object> reverseRangeByScore(String key,double min,double max,long offset,long count){
1063         return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max, offset, count);
1064     }
1065 
1066     /**
1067      * 返回 分数范围内 指定 count 数量的元素集合, 并且从 offset 下标开始(从大到小,带分数的集合)
1068      * @param key
1069      * @param min
1070      * @param max
1071      * @param offset 从指定下标开始
1072      * @param count 输出指定元素数量
1073      * @return
1074      */
1075     public Set<ZSetOperations.TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max, long offset, long count){
1076         return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max, offset, count);
1077     }
1078 
1079     /**
1080      * 返回指定分数区间 [min,max] 的元素个数
1081      * @param key
1082      * @param min
1083      * @param max
1084      * @return
1085      */
1086     public long countZSet(String key,double min,double max){
1087         return redisTemplate.opsForZSet().count(key, min, max);
1088     }
1089 
1090     /**
1091      * 返回 zset 集合数量
1092      * @param key
1093      * @return
1094      */
1095     public long sizeZset(String key){
1096         return redisTemplate.opsForZSet().size(key);
1097     }
1098 
1099     /**
1100      * 获取指定成员的 score 值
1101      * @param key
1102      * @param value
1103      * @return
1104      */
1105     public Double score(String key,Object value){
1106         return redisTemplate.opsForZSet().score(key, value);
1107     }
1108 
1109     /**
1110      * 删除指定索引位置的成员,其中成员分数按( 从小到大 )
1111      * @param key
1112      * @param start
1113      * @param end
1114      * @return
1115      */
1116     public Long removeRange(String key,long start ,long end){
1117         return redisTemplate.opsForZSet().removeRange(key, start, end);
1118     }
1119 
1120     /**
1121      * 删除指定 分数范围 内的成员 [main,max],其中成员分数按( 从小到大 )
1122      * @param key
1123      * @param min
1124      * @param max
1125      * @return
1126      */
1127     public Long removeRangeByScore(String key,double min ,double max){
1128         return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
1129     }
1130 
1131     /**
1132      *  key 和 other 两个集合的并集,保存在 destKey 集合中, 列名相同的 score 相加
1133      * @param key
1134      * @param otherKey
1135      * @param destKey
1136      * @return
1137      */
1138     public Long unionAndStoreZset(String key,String otherKey,String destKey){
1139         return redisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);
1140     }
1141 
1142     /**
1143      *  key 和 otherKeys 多个集合的并集,保存在 destKey 集合中, 列名相同的 score 相加
1144      * @param key
1145      * @param otherKeys
1146      * @param destKey
1147      * @return
1148      */
1149     public Long unionAndStoreZset(String key,Collection<String> otherKeys,String destKey){
1150         return redisTemplate.opsForZSet().unionAndStore(key, otherKeys, destKey);
1151     }
1152 
1153     /**
1154      *  key 和 otherKey 两个集合的交集,保存在 destKey 集合中
1155      * @param key
1156      * @param otherKey
1157      * @param destKey
1158      * @return
1159      */
1160     public Long intersectAndStore(String key,String otherKey,String destKey){
1161         return redisTemplate.opsForZSet().intersectAndStore(key, otherKey, destKey);
1162     }
1163 
1164     /**
1165      *  key 和 otherKeys 多个集合的交集,保存在 destKey 集合中
1166      * @param key
1167      * @param otherKeys
1168      * @param destKey
1169      * @return
1170      */
1171     public Long intersectAndStore(String key,Collection<String> otherKeys,String destKey){
1172         return redisTemplate.opsForZSet().intersectAndStore(key, otherKeys, destKey);
1173     }
1174 
1175 
1176 }

 

posted @ 2021-02-20 13:48  空气微量  阅读(33)  评论(0)    收藏  举报