redis缓存

   一、redis大部分被用来做缓存使用,核心类:public class StringRedisTemplate extends RedisTemplate<String, String>

  二、工具类

  redis客户端工具:redis desktop manager 2019.4

   操作redis的工具类:

   1 @Component
   2 public class RedisUtils {
   3 
   4     @Autowired
   5     private StringRedisTemplate redisTemplate;
   6 
   7     public StringRedisTemplate getRedisTemplate() {
   8         return redisTemplate;
   9     }
  10 /** -------------------key相关操作--------------------- */
  11 
  12     /**
  13      * 删除key
  14      *
  15      * @param key
  16      */
  17     public void delete(String key) {
  18         redisTemplate.delete(key);
  19     }
  20     /**
  21      * 删除 前缀模糊匹配 索引
  22      *
  23      * @param
  24      */
  25     public void deleteByPrefix(String prefix) {
  26         Set<String> keys = redisTemplate.keys(prefix + "*");
  27         redisTemplate.delete(keys);
  28     }
  29     /**
  30      * 批量删除key
  31      *
  32      * @param keys
  33      */
  34     public void delete(Collection<String> keys) {
  35         redisTemplate.delete(keys);
  36     }
  37 
  38     /**
  39      * 序列化key
  40      *
  41      * @param key
  42      * @return
  43      */
  44     public byte[] dump(String key) {
  45         return redisTemplate.dump(key);
  46     }
  47 
  48     /**
  49      * 是否存在key
  50      *
  51      * @param key
  52      * @return
  53      */
  54     public Boolean hasKey(String key) {
  55         return redisTemplate.hasKey(key);
  56     }
  57 
  58 
  59     /***
  60      * 是否存在haskey
  61      */
  62 
  63     public Boolean hasHKey(String key,String hKey) {
  64         return redisTemplate.opsForHash().hasKey(key,hKey);
  65     }
  66 
  67 
  68     /**
  69      * 设置过期时间
  70      *
  71      * @param key
  72      * @param timeout
  73      * @param unit
  74      * @return
  75      */
  76     public Boolean expire(String key, long timeout, TimeUnit unit) {
  77         return redisTemplate.expire(key, timeout, unit);
  78     }
  79 
  80     /**
  81      * 设置过期时间
  82      *
  83      * @param key
  84      * @param date
  85      * @return
  86      */
  87     public Boolean expireAt(String key, Date date) {
  88         return redisTemplate.expireAt(key, date);
  89     }
  90 
  91     /**
  92      * 查找匹配的key
  93      *
  94      * @param pattern
  95      * @return
  96      */
  97     public Set<String> keys(String pattern) {
  98         return redisTemplate.keys(pattern);
  99     }
 100 
 101     /**
 102      * 将当前数据库的 key 移动到给定的数据库 db 当中
 103      *
 104      * @param key
 105      * @param dbIndex
 106      * @return
 107      */
 108     public Boolean move(String key, int dbIndex) {
 109         return redisTemplate.move(key, dbIndex);
 110     }
 111 
 112     /**
 113      * 移除 key 的过期时间,key 将持久保持
 114      *
 115      * @param key
 116      * @return
 117      */
 118     public Boolean persist(String key) {
 119         return redisTemplate.persist(key);
 120     }
 121 
 122     /**
 123      * 返回 key 的剩余的过期时间
 124      *
 125      * @param key
 126      * @param unit
 127      * @return
 128      */
 129     public Long getExpire(String key, TimeUnit unit) {
 130         return redisTemplate.getExpire(key, unit);
 131     }
 132 
 133     /**
 134      * 返回 key 的剩余的过期时间
 135      *
 136      * @param key
 137      * @return
 138      */
 139     public Long getExpire(String key) {
 140         return redisTemplate.getExpire(key);
 141     }
 142 
 143     /**
 144      * 从当前数据库中随机返回一个 key
 145      *
 146      * @return
 147      */
 148     public String randomKey() {
 149         return redisTemplate.randomKey();
 150     }
 151 
 152     /**
 153      * 修改 key 的名称
 154      *
 155      * @param oldKey
 156      * @param newKey
 157      */
 158     public void rename(String oldKey, String newKey) {
 159         redisTemplate.rename(oldKey, newKey);
 160     }
 161 
 162     /**
 163      * 仅当 newkey 不存在时,将 oldKey 改名为 newkey
 164      *
 165      * @param oldKey
 166      * @param newKey
 167      * @return
 168      */
 169     public Boolean renameIfAbsent(String oldKey, String newKey) {
 170         return redisTemplate.renameIfAbsent(oldKey, newKey);
 171     }
 172 
 173     /**
 174      * 返回 key 所储存的值的类型
 175      *
 176      * @param key
 177      * @return
 178      */
 179     public DataType type(String key) {
 180         return redisTemplate.type(key);
 181     }
 182 
 183     /** -------------------string相关操作--------------------- */
 184 
 185     /**
 186      * 设置指定 key 的值
 187      * @param key
 188      * @param value
 189      */
 190     public void set(String key, String value) {
 191         redisTemplate.opsForValue().set(key, value);
 192     }
 193 
 194     public boolean set(final String key, Map<String, Object> map) {
 195         boolean result = false;
 196         try {
 197             redisTemplate.opsForHash().putAll(key, map);
 198             result = true;
 199         } catch (Exception e) {
 200             e.printStackTrace();
 201         }
 202         return result;
 203     }
 204 
 205     /**
 206      * 获取指定 key 的值
 207      * @param key
 208      * @return
 209      */
 210     public String get(String key) {
 211         return redisTemplate.opsForValue().get(key);
 212     }
 213 
 214     /**
 215      * 返回 key 中字符串值的子字符
 216      * @param key
 217      * @param start
 218      * @param end
 219      * @return
 220      */
 221     public String getRange(String key, long start, long end) {
 222         return redisTemplate.opsForValue().get(key, start, end);
 223     }
 224 
 225     /**
 226      * 将给定 key 的值设为 value ,并返回 key 的旧值(old value)
 227      *
 228      * @param key
 229      * @param value
 230      * @return
 231      */
 232     public String getAndSet(String key, String value) {
 233         return redisTemplate.opsForValue().getAndSet(key, value);
 234     }
 235 
 236     /**
 237      * 对 key 所储存的字符串值,获取指定偏移量上的位(bit)
 238      *
 239      * @param key
 240      * @param offset
 241      * @return
 242      */
 243     public Boolean getBit(String key, long offset) {
 244         return redisTemplate.opsForValue().getBit(key, offset);
 245     }
 246 
 247     /**
 248      * 批量获取
 249      *
 250      * @param keys
 251      * @return
 252      */
 253     public List<String> multiGet(Collection<String> keys) {
 254         return redisTemplate.opsForValue().multiGet(keys);
 255     }
 256 
 257     /**
 258      * 设置ASCII码, 字符串'a'的ASCII码是97, 转为二进制是'01100001', 此方法是将二进制第offset位值变为value
 259      *
 260      * @param key
 261      *            位置
 262      * @param value
 263      *            值,true为1, false为0
 264      * @return
 265      */
 266     public boolean setBit(String key, long offset, boolean value) {
 267         return redisTemplate.opsForValue().setBit(key, offset, value);
 268     }
 269 
 270     /**
 271      * 将值 value 关联到 key ,并将 key 的过期时间设为 timeout
 272      *
 273      * @param key
 274      * @param value
 275      * @param timeout
 276      *            过期时间
 277      * @param unit
 278      *            时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES
 279      *            秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
 280      */
 281     public void setEx(String key, String value, long timeout, TimeUnit unit) {
 282         redisTemplate.opsForValue().set(key, value, timeout, unit);
 283     }
 284 
 285     /**
 286      * 只有在 key 不存在时设置 key 的值
 287      *
 288      * @param key
 289      * @param value
 290      * @return 之前已经存在返回false,不存在返回true
 291      */
 292     public boolean setIfAbsent(String key, String value) {
 293         return redisTemplate.opsForValue().setIfAbsent(key, value);
 294     }
 295 
 296     /**
 297      * 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始
 298      *
 299      * @param key
 300      * @param value
 301      * @param offset
 302      *            从指定位置开始覆写
 303      */
 304     public void setRange(String key, String value, long offset) {
 305         redisTemplate.opsForValue().set(key, value, offset);
 306     }
 307 
 308     /**
 309      * 获取字符串的长度
 310      *
 311      * @param key
 312      * @return
 313      */
 314     public Long size(String key) {
 315         return redisTemplate.opsForValue().size(key);
 316     }
 317 
 318     /**
 319      * 批量添加
 320      *
 321      * @param maps
 322      */
 323     public void multiSet(Map<String, String> maps) {
 324         redisTemplate.opsForValue().multiSet(maps);
 325     }
 326 
 327     /**
 328      * 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
 329      *
 330      * @param maps
 331      * @return 之前已经存在返回false,不存在返回true
 332      */
 333     public boolean multiSetIfAbsent(Map<String, String> maps) {
 334         return redisTemplate.opsForValue().multiSetIfAbsent(maps);
 335     }
 336 
 337     /**
 338      * 增加(自增长), 负数则为自减
 339      *
 340      * @param key
 341      * @return
 342      */
 343     public Long incrBy(String key, long increment) {
 344         return redisTemplate.opsForValue().increment(key, increment);
 345     }
 346 
 347     /**
 348      *
 349      * @param key
 350      * @return
 351      */
 352     public Double incrByFloat(String key, double increment) {
 353         return redisTemplate.opsForValue().increment(key, increment);
 354     }
 355 
 356     /**
 357      * 追加到末尾
 358      *
 359      * @param key
 360      * @param value
 361      * @return
 362      */
 363     public Integer append(String key, String value) {
 364         return redisTemplate.opsForValue().append(key, value);
 365     }
 366 
 367     /** -------------------hash相关操作------------------------- */
 368 
 369     /**
 370      * 获取存储在哈希表中指定字段的值
 371      *
 372      * @param key
 373      * @param field
 374      * @return
 375      */
 376     public Object hGet(String key, String field) {
 377         return redisTemplate.opsForHash().get(key, field);
 378     }
 379 
 380     /**
 381      * 获取所有给定字段的值
 382      *
 383      * @param key
 384      * @return
 385      */
 386     public Map<Object, Object> hGetAll(String key) {
 387         return redisTemplate.opsForHash().entries(key);
 388     }
 389 
 390     /**
 391      * 获取所有给定字段的值
 392      *
 393      * @param key
 394      * @param fields
 395      * @return
 396      */
 397     public List<Object> hMultiGet(String key, Collection<Object> fields) {
 398         return redisTemplate.opsForHash().multiGet(key, fields);
 399     }
 400 
 401     public void hPut(String key, String hashKey, String value) {
 402         redisTemplate.opsForHash().put(key, hashKey, value);
 403     }
 404 
 405     public void hPutAll(String key, Map<String, String> maps) {
 406         redisTemplate.opsForHash().putAll(key, maps);
 407     }
 408 
 409     public void hPutAllObject(String key, Map<Object, Object> maps) {
 410         redisTemplate.opsForHash().putAll(key, maps);
 411     }
 412 
 413     /**
 414      * 仅当hashKey不存在时才设置
 415      *
 416      * @param key
 417      * @param hashKey
 418      * @param value
 419      * @return
 420      */
 421     public Boolean hPutIfAbsent(String key, String hashKey, String value) {
 422         return redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
 423     }
 424 
 425     /**
 426      * 删除一个或多个哈希表字段
 427      *
 428      * @param key
 429      * @param fields
 430      * @return
 431      */
 432     public Long hDelete(String key, Object... fields) {
 433         return redisTemplate.opsForHash().delete(key, fields);
 434     }
 435 
 436     /**
 437      * 查看哈希表 key 中,指定的字段是否存在
 438      *
 439      * @param key
 440      * @param field
 441      * @return
 442      */
 443     public boolean hExists(String key, String field) {
 444         return redisTemplate.opsForHash().hasKey(key, field);
 445     }
 446 
 447     /**
 448      * 为哈希表 key 中的指定字段的整数值加上增量 increment
 449      *
 450      * @param key
 451      * @param field
 452      * @param increment
 453      * @return
 454      */
 455     public Long hIncrBy(String key, Object field, long increment) {
 456         return redisTemplate.opsForHash().increment(key, field, increment);
 457     }
 458 
 459     /**
 460      * 为哈希表 key 中的指定字段的整数值加上增量 increment
 461      *
 462      * @param key
 463      * @param field
 464      * @param delta
 465      * @return
 466      */
 467     public Double hIncrByFloat(String key, Object field, double delta) {
 468         return redisTemplate.opsForHash().increment(key, field, delta);
 469     }
 470 
 471     /**
 472      * 获取所有哈希表中的字段
 473      *
 474      * @param key
 475      * @return
 476      */
 477     public Set<Object> hKeys(String key) {
 478         return redisTemplate.opsForHash().keys(key);
 479     }
 480 
 481     /**
 482      * 获取哈希表中字段的数量
 483      *
 484      * @param key
 485      * @return
 486      */
 487     public Long hSize(String key) {
 488         return redisTemplate.opsForHash().size(key);
 489     }
 490 
 491     /**
 492      * 获取哈希表中所有值
 493      *
 494      * @param key
 495      * @return
 496      */
 497     public List<Object> hValues(String key) {
 498         return redisTemplate.opsForHash().values(key);
 499     }
 500 
 501     /**
 502      * 迭代哈希表中的键值对
 503      *
 504      * @param key
 505      * @param options
 506      * @return
 507      */
 508     public Cursor<Entry<Object, Object>> hScan(String key, ScanOptions options) {
 509         return redisTemplate.opsForHash().scan(key, options);
 510     }
 511 
 512     /** ------------------------list相关操作---------------------------- */
 513 
 514     /**
 515      * 通过索引获取列表中的元素
 516      *
 517      * @param key
 518      * @param index
 519      * @return
 520      */
 521     public String lIndex(String key, long index) {
 522         return redisTemplate.opsForList().index(key, index);
 523     }
 524 
 525     /**
 526      * 获取列表指定范围内的元素
 527      *
 528      * @param key
 529      * @param start
 530      *            开始位置, 0是开始位置
 531      * @param end
 532      *            结束位置, -1返回所有
 533      * @return
 534      */
 535     public List<String> lRange(String key, long start, long end) {
 536         return redisTemplate.opsForList().range(key, start, end);
 537     }
 538 
 539     /**
 540      * 存储在list头部
 541      *
 542      * @param key
 543      * @param value
 544      * @return
 545      */
 546     public Long lLeftPush(String key, String value) {
 547         return redisTemplate.opsForList().leftPush(key, value);
 548     }
 549 
 550     /**
 551      *
 552      * @param key
 553      * @param value
 554      * @return
 555      */
 556     public Long lLeftPushAll(String key, String... value) {
 557         return redisTemplate.opsForList().leftPushAll(key, value);
 558     }
 559 
 560     /**
 561      *
 562      * @param key
 563      * @param value
 564      * @return
 565      */
 566     public Long lLeftPushAll(String key, Collection<String> value) {
 567         return redisTemplate.opsForList().leftPushAll(key, value);
 568     }
 569 
 570     /**
 571      * 当list存在的时候才加入
 572      *
 573      * @param key
 574      * @param value
 575      * @return
 576      */
 577     public Long lLeftPushIfPresent(String key, String value) {
 578         return redisTemplate.opsForList().leftPushIfPresent(key, value);
 579     }
 580 
 581     /**
 582      * 如果pivot存在,再pivot前面添加
 583      *
 584      * @param key
 585      * @param pivot
 586      * @param value
 587      * @return
 588      */
 589     public Long lLeftPush(String key, String pivot, String value) {
 590         return redisTemplate.opsForList().leftPush(key, pivot, value);
 591     }
 592 
 593     /**
 594      *
 595      * @param key
 596      * @param value
 597      * @return
 598      */
 599     public Long lRightPush(String key, String value) {
 600         return redisTemplate.opsForList().rightPush(key, value);
 601     }
 602 
 603     /**
 604      *
 605      * @param key
 606      * @param value
 607      * @return
 608      */
 609     public Long lRightPushAll(String key, String... value) {
 610         return redisTemplate.opsForList().rightPushAll(key, value);
 611     }
 612 
 613     /**
 614      *
 615      * @param key
 616      * @param value
 617      * @return
 618      */
 619     public Long lRightPushAll(String key, Collection<String> value) {
 620         return redisTemplate.opsForList().rightPushAll(key, value);
 621     }
 622 
 623     /**
 624      * 为已存在的列表添加值
 625      *
 626      * @param key
 627      * @param value
 628      * @return
 629      */
 630     public Long lRightPushIfPresent(String key, String value) {
 631         return redisTemplate.opsForList().rightPushIfPresent(key, value);
 632     }
 633 
 634     /**
 635      * 在pivot元素的右边添加值
 636      *
 637      * @param key
 638      * @param pivot
 639      * @param value
 640      * @return
 641      */
 642     public Long lRightPush(String key, String pivot, String value) {
 643         return redisTemplate.opsForList().rightPush(key, pivot, value);
 644     }
 645 
 646     /**
 647      * 通过索引设置列表元素的值
 648      *
 649      * @param key
 650      * @param index
 651      *            位置
 652      * @param value
 653      */
 654     public void lSet(String key, long index, String value) {
 655         redisTemplate.opsForList().set(key, index, value);
 656     }
 657 
 658     /**
 659      * 移出并获取列表的第一个元素
 660      *
 661      * @param key
 662      * @return 删除的元素
 663      */
 664     public String lLeftPop(String key) {
 665         return redisTemplate.opsForList().leftPop(key);
 666     }
 667 
 668     /**
 669      * 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
 670      *
 671      * @param key
 672      * @param timeout
 673      *            等待时间
 674      * @param unit
 675      *            时间单位
 676      * @return
 677      */
 678     public String lBLeftPop(String key, long timeout, TimeUnit unit) {
 679         return redisTemplate.opsForList().leftPop(key, timeout, unit);
 680     }
 681 
 682     /**
 683      * 移除并获取列表最后一个元素
 684      *
 685      * @param key
 686      * @return 删除的元素
 687      */
 688     public String lRightPop(String key) {
 689         return redisTemplate.opsForList().rightPop(key);
 690     }
 691 
 692     /**
 693      * 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
 694      *
 695      * @param key
 696      * @param timeout
 697      *            等待时间
 698      * @param unit
 699      *            时间单位
 700      * @return
 701      */
 702     public String lBRightPop(String key, long timeout, TimeUnit unit) {
 703         return redisTemplate.opsForList().rightPop(key, timeout, unit);
 704     }
 705 
 706     /**
 707      * 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
 708      *
 709      * @param sourceKey
 710      * @param destinationKey
 711      * @return
 712      */
 713     public String lRightPopAndLeftPush(String sourceKey, String destinationKey) {
 714         return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,
 715                 destinationKey);
 716     }
 717 
 718     /**
 719      * 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
 720      *
 721      * @param sourceKey
 722      * @param destinationKey
 723      * @param timeout
 724      * @param unit
 725      * @return
 726      */
 727     public String lBRightPopAndLeftPush(String sourceKey, String destinationKey,
 728                                         long timeout, TimeUnit unit) {
 729         return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,
 730                 destinationKey, timeout, unit);
 731     }
 732 
 733     /**
 734      * 删除集合中值等于value得元素
 735      *
 736      * @param key
 737      * @param index
 738      *            index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素;
 739      *            index<0, 从尾部开始删除第一个值等于value的元素;
 740      * @param value
 741      * @return
 742      */
 743     public Long lRemove(String key, long index, String value) {
 744         return redisTemplate.opsForList().remove(key, index, value);
 745     }
 746 
 747     /**
 748      * 裁剪list
 749      *
 750      * @param key
 751      * @param start
 752      * @param end
 753      */
 754     public void lTrim(String key, long start, long end) {
 755         redisTemplate.opsForList().trim(key, start, end);
 756     }
 757 
 758     /**
 759      * 获取列表长度
 760      *
 761      * @param key
 762      * @return
 763      */
 764     public Long lLen(String key) {
 765         return redisTemplate.opsForList().size(key);
 766     }
 767 
 768     /** --------------------set相关操作-------------------------- */
 769 
 770     /**
 771      * set添加元素
 772      *
 773      * @param key
 774      * @param values
 775      * @return
 776      */
 777     public Long sAdd(String key, String... values) {
 778         return redisTemplate.opsForSet().add(key, values);
 779     }
 780 
 781     /**
 782      * set移除元素
 783      *
 784      * @param key
 785      * @param values
 786      * @return
 787      */
 788     public Long sRemove(String key, Object... values) {
 789         return redisTemplate.opsForSet().remove(key, values);
 790     }
 791 
 792     /**
 793      * 移除并返回集合的一个随机元素
 794      *
 795      * @param key
 796      * @return
 797      */
 798     public String sPop(String key) {
 799         return redisTemplate.opsForSet().pop(key);
 800     }
 801 
 802     /**
 803      * 将元素value从一个集合移到另一个集合
 804      *
 805      * @param key
 806      * @param value
 807      * @param destKey
 808      * @return
 809      */
 810     public Boolean sMove(String key, String value, String destKey) {
 811         return redisTemplate.opsForSet().move(key, value, destKey);
 812     }
 813 
 814     /**
 815      * 获取集合的大小
 816      *
 817      * @param key
 818      * @return
 819      */
 820     public Long sSize(String key) {
 821         return redisTemplate.opsForSet().size(key);
 822     }
 823 
 824     /**
 825      * 判断集合是否包含value
 826      *
 827      * @param key
 828      * @param value
 829      * @return
 830      */
 831     public Boolean sIsMember(String key, Object value) {
 832         return redisTemplate.opsForSet().isMember(key, value);
 833     }
 834 
 835     /**
 836      * 获取两个集合的交集
 837      *
 838      * @param key
 839      * @param otherKey
 840      * @return
 841      */
 842     public Set<String> sIntersect(String key, String otherKey) {
 843         return redisTemplate.opsForSet().intersect(key, otherKey);
 844     }
 845 
 846     /**
 847      * 获取key集合与多个集合的交集
 848      *
 849      * @param key
 850      * @param otherKeys
 851      * @return
 852      */
 853     public Set<String> sIntersect(String key, Collection<String> otherKeys) {
 854         return redisTemplate.opsForSet().intersect(key, otherKeys);
 855     }
 856 
 857     /**
 858      * key集合与otherKey集合的交集存储到destKey集合中
 859      *
 860      * @param key
 861      * @param otherKey
 862      * @param destKey
 863      * @return
 864      */
 865     public Long sIntersectAndStore(String key, String otherKey, String destKey) {
 866         return redisTemplate.opsForSet().intersectAndStore(key, otherKey,
 867                 destKey);
 868     }
 869 
 870     /**
 871      * key集合与多个集合的交集存储到destKey集合中
 872      *
 873      * @param key
 874      * @param otherKeys
 875      * @param destKey
 876      * @return
 877      */
 878     public Long sIntersectAndStore(String key, Collection<String> otherKeys,
 879                                    String destKey) {
 880         return redisTemplate.opsForSet().intersectAndStore(key, otherKeys,
 881                 destKey);
 882     }
 883 
 884     /**
 885      * 获取两个集合的并集
 886      *
 887      * @param key
 888      * @param otherKeys
 889      * @return
 890      */
 891     public Set<String> sUnion(String key, String otherKeys) {
 892         return redisTemplate.opsForSet().union(key, otherKeys);
 893     }
 894 
 895     /**
 896      * 获取key集合与多个集合的并集
 897      *
 898      * @param key
 899      * @param otherKeys
 900      * @return
 901      */
 902     public Set<String> sUnion(String key, Collection<String> otherKeys) {
 903         return redisTemplate.opsForSet().union(key, otherKeys);
 904     }
 905 
 906     /**
 907      * key集合与otherKey集合的并集存储到destKey中
 908      *
 909      * @param key
 910      * @param otherKey
 911      * @param destKey
 912      * @return
 913      */
 914     public Long sUnionAndStore(String key, String otherKey, String destKey) {
 915         return redisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);
 916     }
 917 
 918     /**
 919      * key集合与多个集合的并集存储到destKey中
 920      *
 921      * @param key
 922      * @param otherKeys
 923      * @param destKey
 924      * @return
 925      */
 926     public Long sUnionAndStore(String key, Collection<String> otherKeys,
 927                                String destKey) {
 928         return redisTemplate.opsForSet().unionAndStore(key, otherKeys, destKey);
 929     }
 930 
 931     /**
 932      * 获取两个集合的差集
 933      *
 934      * @param key
 935      * @param otherKey
 936      * @return
 937      */
 938     public Set<String> sDifference(String key, String otherKey) {
 939         return redisTemplate.opsForSet().difference(key, otherKey);
 940     }
 941 
 942     /**
 943      * 获取key集合与多个集合的差集
 944      *
 945      * @param key
 946      * @param otherKeys
 947      * @return
 948      */
 949     public Set<String> sDifference(String key, Collection<String> otherKeys) {
 950         return redisTemplate.opsForSet().difference(key, otherKeys);
 951     }
 952 
 953     /**
 954      * key集合与otherKey集合的差集存储到destKey中
 955      *
 956      * @param key
 957      * @param otherKey
 958      * @param destKey
 959      * @return
 960      */
 961     public Long sDifference(String key, String otherKey, String destKey) {
 962         return redisTemplate.opsForSet().differenceAndStore(key, otherKey,
 963                 destKey);
 964     }
 965 
 966     /**
 967      * key集合与多个集合的差集存储到destKey中
 968      *
 969      * @param key
 970      * @param otherKeys
 971      * @param destKey
 972      * @return
 973      */
 974     public Long sDifference(String key, Collection<String> otherKeys,
 975                             String destKey) {
 976         return redisTemplate.opsForSet().differenceAndStore(key, otherKeys,
 977                 destKey);
 978     }
 979 
 980     /**
 981      * 获取集合所有元素
 982      *
 983      * @param key
 984      * @return
 985      */
 986     public Set<String> setMembers(String key) {
 987         return redisTemplate.opsForSet().members(key);
 988     }
 989 
 990     /**
 991      * 随机获取集合中的一个元素
 992      *
 993      * @param key
 994      * @return
 995      */
 996     public String sRandomMember(String key) {
 997         return redisTemplate.opsForSet().randomMember(key);
 998     }
 999 
1000     /**
1001      * 随机获取集合中count个元素
1002      *
1003      * @param key
1004      * @param count
1005      * @return
1006      */
1007     public List<String> sRandomMembers(String key, long count) {
1008         return redisTemplate.opsForSet().randomMembers(key, count);
1009     }
1010 
1011     /**
1012      * 随机获取集合中count个元素并且去除重复的
1013      *
1014      * @param key
1015      * @param count
1016      * @return
1017      */
1018     public Set<String> sDistinctRandomMembers(String key, long count) {
1019         return redisTemplate.opsForSet().distinctRandomMembers(key, count);
1020     }
1021 
1022     /**
1023      *
1024      * @param key
1025      * @param options
1026      * @return
1027      */
1028     public Cursor<String> sScan(String key, ScanOptions options) {
1029         return redisTemplate.opsForSet().scan(key, options);
1030     }
1031 
1032     /**------------------zSet相关操作--------------------------------*/
1033 
1034     /**
1035      * 添加元素,有序集合是按照元素的score值由小到大排列
1036      *
1037      * @param key
1038      * @param value
1039      * @param score
1040      * @return
1041      */
1042     public Boolean zAdd(String key, String value, double score) {
1043         return redisTemplate.opsForZSet().add(key, value, score);
1044     }
1045 
1046     /**
1047      *
1048      * @param key
1049      * @param values
1050      * @return
1051      */
1052     public Long zAdd(String key, Set<TypedTuple<String>> values) {
1053         return redisTemplate.opsForZSet().add(key, values);
1054     }
1055 
1056     /**
1057      *
1058      * @param key
1059      * @param values
1060      * @return
1061      */
1062     public Long zRemove(String key, Object... values) {
1063         return redisTemplate.opsForZSet().remove(key, values);
1064     }
1065 
1066     /**
1067      * 增加元素的score值,并返回增加后的值
1068      *
1069      * @param key
1070      * @param value
1071      * @param delta
1072      * @return
1073      */
1074     public Double zIncrementScore(String key, String value, double delta) {
1075         return redisTemplate.opsForZSet().incrementScore(key, value, delta);
1076     }
1077 
1078     /**
1079      * 返回元素在集合的排名,有序集合是按照元素的score值由小到大排列
1080      *
1081      * @param key
1082      * @param value
1083      * @return 0表示第一位
1084      */
1085     public Long zRank(String key, Object value) {
1086         return redisTemplate.opsForZSet().rank(key, value);
1087     }
1088 
1089     /**
1090      * 返回元素在集合的排名,按元素的score值由大到小排列
1091      *
1092      * @param key
1093      * @param value
1094      * @return
1095      */
1096     public Long zReverseRank(String key, Object value) {
1097         return redisTemplate.opsForZSet().reverseRank(key, value);
1098     }
1099 
1100     /**
1101      * 获取集合的元素, 从小到大排序
1102      *
1103      * @param key
1104      * @param start
1105      *            开始位置
1106      * @param end
1107      *            结束位置, -1查询所有
1108      * @return
1109      */
1110     public Set<String> zRange(String key, long start, long end) {
1111         return redisTemplate.opsForZSet().range(key, start, end);
1112     }
1113 
1114     /**
1115      * 获取集合元素, 并且把score值也获取
1116      *
1117      * @param key
1118      * @param start
1119      * @param end
1120      * @return
1121      */
1122     public Set<TypedTuple<String>> zRangeWithScores(String key, long start,
1123                                                     long end) {
1124         return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
1125     }
1126 
1127     /**
1128      * 根据Score值查询集合元素
1129      *
1130      * @param key
1131      * @param min
1132      *            最小值
1133      * @param max
1134      *            最大值
1135      * @return
1136      */
1137     public Set<String> zRangeByScore(String key, double min, double max) {
1138         return redisTemplate.opsForZSet().rangeByScore(key, min, max);
1139     }
1140 
1141     /**
1142      * 根据Score值查询集合元素, 从小到大排序
1143      *
1144      * @param key
1145      * @param min
1146      *            最小值
1147      * @param max
1148      *            最大值
1149      * @return
1150      */
1151     public Set<TypedTuple<String>> zRangeByScoreWithScores(String key,
1152                                                            double min, double max) {
1153         return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
1154     }
1155 
1156     /**
1157      *
1158      * @param key
1159      * @param min
1160      * @param max
1161      * @param start
1162      * @param end
1163      * @return
1164      */
1165     public Set<TypedTuple<String>> zRangeByScoreWithScores(String key,
1166                                                            double min, double max, long start, long end) {
1167         return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max,
1168                 start, end);
1169     }
1170 
1171     /**
1172      * 获取集合的元素, 从大到小排序
1173      *
1174      * @param key
1175      * @param start
1176      * @param end
1177      * @return
1178      */
1179     public Set<String> zReverseRange(String key, long start, long end) {
1180         return redisTemplate.opsForZSet().reverseRange(key, start, end);
1181     }
1182 
1183     /**
1184      * 获取集合的元素, 从大到小排序, 并返回score值
1185      *
1186      * @param key
1187      * @param start
1188      * @param end
1189      * @return
1190      */
1191     public Set<TypedTuple<String>> zReverseRangeWithScores(String key,
1192                                                            long start, long end) {
1193         return redisTemplate.opsForZSet().reverseRangeWithScores(key, start,
1194                 end);
1195     }
1196 
1197     /**
1198      * 根据Score值查询集合元素, 从大到小排序
1199      *
1200      * @param key
1201      * @param min
1202      * @param max
1203      * @return
1204      */
1205     public Set<String> zReverseRangeByScore(String key, double min,
1206                                             double max) {
1207         return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
1208     }
1209 
1210     /**
1211      * 根据Score值查询集合元素, 从大到小排序
1212      *
1213      * @param key
1214      * @param min
1215      * @param max
1216      * @return
1217      */
1218     public Set<TypedTuple<String>> zReverseRangeByScoreWithScores(
1219             String key, double min, double max) {
1220         return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key,
1221                 min, max);
1222     }
1223 
1224     /**
1225      *
1226      * @param key
1227      * @param min
1228      * @param max
1229      * @param start
1230      * @param end
1231      * @return
1232      */
1233     public Set<String> zReverseRangeByScore(String key, double min,
1234                                             double max, long start, long end) {
1235         return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max,
1236                 start, end);
1237     }
1238 
1239     /**
1240      * 根据score值获取集合元素数量
1241      *
1242      * @param key
1243      * @param min
1244      * @param max
1245      * @return
1246      */
1247     public Long zCount(String key, double min, double max) {
1248         return redisTemplate.opsForZSet().count(key, min, max);
1249     }
1250 
1251     /**
1252      * 获取集合大小
1253      *
1254      * @param key
1255      * @return
1256      */
1257     public Long zSize(String key) {
1258         return redisTemplate.opsForZSet().size(key);
1259     }
1260 
1261     /**
1262      * 获取集合大小
1263      *
1264      * @param key
1265      * @return
1266      */
1267     public Long zZCard(String key) {
1268         return redisTemplate.opsForZSet().zCard(key);
1269     }
1270 
1271     /**
1272      * 获取集合中value元素的score值
1273      *
1274      * @param key
1275      * @param value
1276      * @return
1277      */
1278     public Double zScore(String key, Object value) {
1279         return redisTemplate.opsForZSet().score(key, value);
1280     }
1281 
1282     /**
1283      * 移除指定索引位置的成员
1284      *
1285      * @param key
1286      * @param start
1287      * @param end
1288      * @return
1289      */
1290     public Long zRemoveRange(String key, long start, long end) {
1291         return redisTemplate.opsForZSet().removeRange(key, start, end);
1292     }
1293 
1294     /**
1295      * 根据指定的score值的范围来移除成员
1296      *
1297      * @param key
1298      * @param min
1299      * @param max
1300      * @return
1301      */
1302     public Long zRemoveRangeByScore(String key, double min, double max) {
1303         return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
1304     }
1305 
1306     /**
1307      * 获取key和otherKey的并集并存储在destKey中
1308      *
1309      * @param key
1310      * @param otherKey
1311      * @param destKey
1312      * @return
1313      */
1314     public Long zUnionAndStore(String key, String otherKey, String destKey) {
1315         return redisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);
1316     }
1317 
1318     /**
1319      *
1320      * @param key
1321      * @param otherKeys
1322      * @param destKey
1323      * @return
1324      */
1325     public Long zUnionAndStore(String key, Collection<String> otherKeys,
1326                                String destKey) {
1327         return redisTemplate.opsForZSet()
1328                 .unionAndStore(key, otherKeys, destKey);
1329     }
1330 
1331     /**
1332      * 交集
1333      *
1334      * @param key
1335      * @param otherKey
1336      * @param destKey
1337      * @return
1338      */
1339     public Long zIntersectAndStore(String key, String otherKey,
1340                                    String destKey) {
1341         return redisTemplate.opsForZSet().intersectAndStore(key, otherKey,
1342                 destKey);
1343     }
1344 
1345     /**
1346      * 交集
1347      *
1348      * @param key
1349      * @param otherKeys
1350      * @param destKey
1351      * @return
1352      */
1353     public Long zIntersectAndStore(String key, Collection<String> otherKeys,
1354                                    String destKey) {
1355         return redisTemplate.opsForZSet().intersectAndStore(key, otherKeys,
1356                 destKey);
1357     }
1358 
1359     /**
1360      *
1361      * @param key
1362      * @param options
1363      * @return
1364      */
1365     public Cursor<TypedTuple<String>> zScan(String key, ScanOptions options) {
1366         return redisTemplate.opsForZSet().scan(key, options);
1367     }
1368 
1369     /**
1370      * 删除缓存
1371      */
1372     public boolean deleteMap(String key, String hKey) {
1373         boolean result = false;
1374         try {
1375             redisTemplate.opsForHash().delete(key, hKey);
1376             result = true;
1377         } catch (Exception e) {
1378             e.printStackTrace();
1379         }
1380         return result;
1381     }
1382 
1383 
1384     /**
1385      * 删除缓存
1386      */
1387     public boolean deleteMapKey(String key, String hKey) {
1388         boolean result = false;
1389         try {
1390             redisTemplate.opsForHash().delete(key, hKey);
1391             result = true;
1392         } catch (Exception e) {
1393             e.printStackTrace();
1394         }
1395         return result;
1396     }
1397     /**
1398      * 查询
1399      */
1400     public Object getMapValue(String key, String hashkey) {
1401         return redisTemplate.opsForHash().get(key, hashkey);
1402     }
1403 
1404 
1405     /**
1406      *  过期时间
1407      */
1408     public boolean setAndExpire(final String key, String value, int expireTime) {
1409         boolean result = false;
1410         try {
1411             redisTemplate.opsForValue().set(key, value);
1412             redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
1413             result = true;
1414         } catch (Exception e) {
1415             e.printStackTrace();
1416         }
1417         return result;
1418     }
1419 
1420     /**
1421      *  获取map的数量
1422      */
1423     public int getMapCount(String key) {
1424         Integer num = 0;
1425         try {
1426             List<Object> objs = redisTemplate.opsForHash().values(key);
1427             num = objs.size();
1428         } catch (Exception e) {
1429             e.printStackTrace();
1430         }
1431         return num;
1432     }
1433 
1434     /**
1435      * 模糊删除hkey
1436      */
1437     public boolean belDeleteMap(String key, String hKey) {
1438         boolean result = false;
1439         try {
1440             Set<Object> resultMapSet = redisTemplate.opsForHash().keys(key);
1441             if (!resultMapSet.isEmpty()) {
1442                 for (Object o : resultMapSet) {
1443                     if (o.toString().contains(hKey)) {
1444                         redisTemplate.opsForHash().delete(key, o.toString());
1445                     }
1446                 }
1447             }
1448             result = true;
1449         } catch (Exception e) {
1450             e.printStackTrace();
1451         }
1452         return result;
1453     }
1454 
1455     /**
1456      *  模糊删除hkey
1457      */
1458     public boolean DeleteMapLike(String key,String hKey) {
1459         boolean result = false;
1460         try {
1461             Set<Object> resultMapSet = redisTemplate.opsForHash().keys(key);
1462             if(!resultMapSet.isEmpty()){
1463                 for (Object str:resultMapSet) {
1464                     if(str != null){
1465                         String [] str1 = String.valueOf(str).split("-");
1466                         if(str1[1].equals(hKey)){
1467                             redisTemplate.opsForHash().delete(key, str);
1468                             result = true;
1469                         }
1470                     }
1471                 }
1472             }
1473         } catch (Exception e) {
1474             e.printStackTrace();
1475         }
1476         return result;
1477     }
1478 
1479     /**
1480      * 模糊获取 hkey
1481      * @param key
1482      * @param hKey
1483      * @return
1484      */
1485     public List<Object> hGetLikeAll(String key,String hKey) {
1486         List<Object> result = new ArrayList<>();
1487         try {
1488             Set<Object> resultMapSet = redisTemplate.opsForHash().keys(key);
1489             if(!resultMapSet.isEmpty()){
1490                 for(Object object:resultMapSet){
1491                     if(object.toString().contains(hKey)){
1492                         result.add(object);
1493                     }
1494                 }
1495             }
1496         } catch (Exception e) {
1497             e.printStackTrace();
1498         }
1499         return result;
1500     }
1501 
1502     /**
1503      * 模糊删除
1504      * @param likeKey
1505      */
1506     public void deleteLikeKey(String likeKey) {
1507         Set<String> keys = redisTemplate.keys(likeKey+"*");
1508         redisTemplate.delete(keys);
1509     }
1510 
1511     /**
1512      * 模糊获取所有hKey
1513      * @param key
1514      * @param likeKey
1515      * @return
1516      */
1517     public Map<String,Object> getKeysByLikeKey(String key,String likeKey) {
1518         Map<String,Object> result = new HashMap<>();
1519         try {
1520             Set<Object> resultMapSet = redisTemplate.opsForHash().keys(key);
1521             if(!resultMapSet.isEmpty()){
1522                 for(Object object:resultMapSet){
1523                     if(object.toString().contains(likeKey)){
1524                         result.put(object.toString(),redisTemplate.opsForHash().get(key, object.toString()));
1525                     }
1526                 }
1527             }
1528         } catch (Exception e) {
1529             e.printStackTrace();
1530         }
1531         return result;
1532     }
1533 
1534     /**
1535      * 模糊获取内容
1536      * @param likeKey
1537      */
1538     public List<Object> getObjectByLikeKey(String likeKey) {
1539         Set<String> keys = redisTemplate.keys(likeKey+"*");
1540         List<Object> result = new ArrayList<>();
1541         if(keys !=null){
1542             for (String key:keys){
1543                 result.add(redisTemplate.opsForValue().get(key));
1544 
1545             }
1546         }
1547         return result;
1548     }
1549 
1550     public <T> Map<String, T> hMap(String redisKey, Class<T> classType) {
1551         return redisTemplate.opsForHash().entries(redisKey).entrySet().stream().collect(Collectors.toMap(
1552                 data -> String.valueOf(data.getKey()),
1553                 value -> JSON.parseObject(String.valueOf(value.getValue()), classType)
1554         ));
1555     }
1556 
1557     public <T> void hPut(String redisKey, String hasKey, T value) {
1558         redisTemplate.opsForHash().put(redisKey, hasKey, JSON.toJSONString(value));
1559     }
1560 
1561     public void hDelete(String redisKey, String valueKey) {
1562         redisTemplate.opsForHash().delete(redisKey, valueKey);
1563     }
1564 }
View Code

三、应用案例

//删除键
redisUtils.delete("imine_main_data:switchlist");
//删除Hash具体的字段
redisUtils.deleteMapKey(RedisConstants.SIGNAL_CONCERT_INFO, swDelete.getSwitchHid());
//增加hash
redisUtils.hPutAll(RedisConstants.SIGNAL_CONCERT_INFO,cacheMap);
//增加hash的一个值
redisUtils.hPut(RedisConstants.SIGNAL_CONCERT_INFO, sw.getSwitchHid(), JSON.toJSONString(signalConcertDto));

  

 
posted @ 2022-02-11 15:18  zhangtianhong511  阅读(27)  评论(0)    收藏  举报