0

返されるカーソルが「0」になるまで呼び出す必要があるRedis のスキャン機能を使用しようとしています。

このアプローチは機能しますが、非常に複雑に感じます - 再帰なしでそれを行う方法はありますか?

Uni<Object> callScanRecursively(String cursorNum, List<String> results) {
  List<String> args = new ArrayList<>(List.of(cursorNum, "MATCH", "*"));
  return reactiveRedisClient.scan(args)
      .onItem()
      .transformToUni(item -> {
            List<String> keysList = new ArrayList<>();
            item.get(1).forEach(k -> {
              keysList.add(k.toString());
            });
            results.addAll(keysList);

            if (item.get(0).toString().equals("0")) {
              return Uni.createFrom().item(results);
            } else {
              String nextCursorNum = item.get(0).toString();
              return callScanRecursively(nextCursorNum, results);
            }
          }
      );
}

List<String> results = new ArrayList<>();
String startingCursorRedis = "0";
callScanRecursively(startingCursorRedis, results)
    .subscribe()
    .with(item ->
        LOGGER.info(item)
    );
4

1 に答える 1