小编典典

无法使用Redis模板进行扫描

redis

我正在尝试使用SCAN
http://redis.io/commands/scan来遍历redis中存在的所有键。但是spring提供的Redis模板没有任何scan()方法。有什么技巧可以使用以上内容吗?

谢谢


阅读 238

收藏
2020-06-20

共1个答案

小编典典

您可以使用RedisCallbackon RedisOperations来这样做。

redisTemplate.execute(new RedisCallback<Iterable<byte[]>>() {

  @Override
  public Iterable<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {

    List<byte[]> binaryKeys = new ArrayList<byte[]>();

    Cursor<byte[]> cursor = connection.scan(ScanOptions.NONE);
    while (cursor.hasNext()) {
      binaryKeys.add(cursor.next());
    }

    try {
      cursor.close();
    } catch (IOException e) {
      // do something meaningful
    }

    return binaryKeys;
  }
});
2020-06-20