redis中使用scan正则表达式检索所有key

  /**
   * 使用scan正则表达式检索
   *
   * @param regex 正则表达式
   * @param count 增量迭代
   */
  public Set<String> scan(String regex, long count) {
    ScanOptions options = ScanOptions.scanOptions()
        .match(regex)
        .count(count)
        .build();
    return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
      Set<String> keys = new HashSet<>();
      Cursor<byte[]> cursor = connection.scan(options);
      while (cursor.hasNext()) {
        keys.add(new String(cursor.next()));
      }
      return keys;
    });
  }

参考
https://www.jianshu.com/p/4c842c41ba41

posted @ 2022-03-23 13:43  vaen  阅读(959)  评论(0)    收藏  举报