package example;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/** Testing a few cluster operations from Jedis.
 * Author: Henrik Bærbak, MSDO 2021
 */
public class RedisCluster {
  public static void main(String[] args) {
    System.out.println("=== Cluster Demo for Redis ===");

    System.out.println("You FIRST have to set up a Redis cluster for this to work.");
    new RedisCluster();
  }

  public RedisCluster() {
    Set<HostAndPort> jedisClusterNodes = new HashSet<>();
    //Jedis Cluster will attempt to discover cluster nodes automatically
    jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001));
    JedisCluster jc = new JedisCluster(jedisClusterNodes);
    jc.set("foo", "bar");
    String value = jc.get("foo");
    System.out.println(" -> got value = " + value);

    System.out.println("=== Enumerating the cluster nodes: ===");
    jc.getClusterNodes().keySet().stream().forEach(System.out::println);
  }
}
