Class RedisClusterClient

    • Constructor Detail

      • RedisClusterClient

        protected RedisClusterClient()
        Non-private constructor to make RedisClusterClient proxyable.
      • RedisClusterClient

        protected RedisClusterClient​(ClientResources clientResources,
                                     Iterable<RedisURI> redisURIs)
        Initialize the client with a list of cluster URI's. All uris are tried in sequence for connecting initially to the cluster. If any uri is successful for connection, the others are not tried anymore. The initial uri is needed to discover the cluster structure for distributing the requests.
        Parameters:
        clientResources - the client resources. If null, the client will create a new dedicated instance of client resources and keep track of them.
        redisURIs - iterable of initial cluster URIs. Must not be null and not empty.
    • Method Detail

      • create

        public static RedisClusterClient create​(String uri)
        Create a new client that connects to the supplied uri with default ClientResources. You can connect to different Redis servers but you must supply a RedisURI on connecting.
        Parameters:
        uri - the Redis URI, must not be empty or null.
        Returns:
        a new instance of RedisClusterClient
      • create

        public static RedisClusterClient create​(ClientResources clientResources,
                                                RedisURI redisURI)
        Create a new client that connects to the supplied uri with shared ClientResources. You need to shut down the ClientResources upon shutting down your application.You can connect to different Redis servers but you must supply a RedisURI on connecting.
        Parameters:
        clientResources - the client resources, must not be null
        redisURI - the Redis URI, must not be null
        Returns:
        a new instance of RedisClusterClient
      • create

        public static RedisClusterClient create​(ClientResources clientResources,
                                                String uri)
        Create a new client that connects to the supplied uri with shared ClientResources.You need to shut down the ClientResources upon shutting down your application. You can connect to different Redis servers but you must supply a RedisURI on connecting.
        Parameters:
        clientResources - the client resources, must not be null
        uri - the Redis URI, must not be empty or null.
        Returns:
        a new instance of RedisClusterClient
      • create

        public static RedisClusterClient create​(ClientResources clientResources,
                                                Iterable<RedisURI> redisURIs)
        Create a new client that connects to the supplied uri with shared ClientResources. You need to shut down the ClientResources upon shutting down your application.You can connect to different Redis servers but you must supply a RedisURI on connecting.
        Parameters:
        clientResources - the client resources, must not be null
        redisURIs - one or more Redis URI, must not be null and not empty
        Returns:
        a new instance of RedisClusterClient
      • setOptions

        public void setOptions​(ClusterClientOptions clientOptions)
        Set the ClusterClientOptions for the client.
        Parameters:
        clientOptions - client options for the client and connections that are created after setting the options
      • getPartitions

        public Partitions getPartitions()
        Retrieve the cluster view. Partitions are shared amongst all connections opened by this client instance.
        Returns:
        the partitions.
      • getTopologyRefreshSource

        protected Iterable<RedisURI> getTopologyRefreshSource()
        Returns the seed RedisURI for the topology refreshing. This method is called before each topology refresh to provide an Iterable of RedisURI that is used to perform the next topology refresh.

        Subclasses of RedisClusterClient may override that method.

        Returns:
        Iterable of RedisURI for the next topology refresh.
      • connect

        public StatefulRedisClusterConnection<String,​String> connect()
        Connect to a Redis Cluster and treat keys and values as UTF-8 strings.

        What to expect from this connection:

        • A default connection is created to the node with the lowest latency
        • Keyless commands are send to the default connection
        • Single-key keyspace commands are routed to the appropriate node
        • Multi-key keyspace commands require the same slot-hash and are routed to the appropriate node
        • Pub/sub commands are sent to the node that handles the slot derived from the pub/sub channel
        Returns:
        A new stateful Redis Cluster connection
      • connect

        public <K,​V> StatefulRedisClusterConnection<K,​V> connect​(RedisCodec<K,​V> codec)
        Connect to a Redis Cluster. Use the supplied codec to encode/decode keys and values.

        What to expect from this connection:

        • A default connection is created to the node with the lowest latency
        • Keyless commands are send to the default connection
        • Single-key keyspace commands are routed to the appropriate node
        • Multi-key keyspace commands require the same slot-hash and are routed to the appropriate node
        • Pub/sub commands are sent to the node that handles the slot derived from the pub/sub channel
        Type Parameters:
        K - Key type
        V - Value type
        Parameters:
        codec - Use this codec to encode/decode keys and values, must not be null
        Returns:
        A new stateful Redis Cluster connection
      • connectAsync

        public <K,​V> CompletableFuture<StatefulRedisClusterConnection<K,​V>> connectAsync​(RedisCodec<K,​V> codec)
        Connect asynchronously to a Redis Cluster. Use the supplied codec to encode/decode keys and values. Connecting asynchronously requires an initialized topology. Call getPartitions() first, otherwise the connect will fail with aIllegalStateException.

        What to expect from this connection:

        • A default connection is created to the node with the lowest latency
        • Keyless commands are send to the default connection
        • Single-key keyspace commands are routed to the appropriate node
        • Multi-key keyspace commands require the same slot-hash and are routed to the appropriate node
        • Pub/sub commands are sent to the node that handles the slot derived from the pub/sub channel
        Type Parameters:
        K - Key type
        V - Value type
        Parameters:
        codec - Use this codec to encode/decode keys and values, must not be null
        Returns:
        a CompletableFuture that is notified with the connection progress.
        Since:
        5.1
      • connectPubSub

        public StatefulRedisClusterPubSubConnection<String,​String> connectPubSub()
        Connect to a Redis Cluster using pub/sub connections and treat keys and values as UTF-8 strings.

        What to expect from this connection:

        • A default connection is created to the node with the least number of clients
        • Pub/sub commands are sent to the node with the least number of clients
        • Keyless commands are send to the default connection
        • Single-key keyspace commands are routed to the appropriate node
        • Multi-key keyspace commands require the same slot-hash and are routed to the appropriate node
        Returns:
        A new stateful Redis Cluster connection
      • connectPubSub

        public <K,​V> StatefulRedisClusterPubSubConnection<K,​V> connectPubSub​(RedisCodec<K,​V> codec)
        Connect to a Redis Cluster using pub/sub connections. Use the supplied codec to encode/decode keys and values.

        What to expect from this connection:

        • A default connection is created to the node with the least number of clients
        • Pub/sub commands are sent to the node with the least number of clients
        • Keyless commands are send to the default connection
        • Single-key keyspace commands are routed to the appropriate node
        • Multi-key keyspace commands require the same slot-hash and are routed to the appropriate node
        Type Parameters:
        K - Key type
        V - Value type
        Parameters:
        codec - Use this codec to encode/decode keys and values, must not be null
        Returns:
        A new stateful Redis Cluster connection
      • connectPubSubAsync

        public <K,​V> CompletableFuture<StatefulRedisClusterPubSubConnection<K,​V>> connectPubSubAsync​(RedisCodec<K,​V> codec)
        Connect asynchronously to a Redis Cluster using pub/sub connections. Use the supplied codec to encode/decode keys and values. Connecting asynchronously requires an initialized topology. Call getPartitions() first, otherwise the connect will fail with aIllegalStateException.

        What to expect from this connection:

        • A default connection is created to the node with the least number of clients
        • Pub/sub commands are sent to the node with the least number of clients
        • Keyless commands are send to the default connection
        • Single-key keyspace commands are routed to the appropriate node
        • Multi-key keyspace commands require the same slot-hash and are routed to the appropriate node
        Type Parameters:
        K - Key type
        V - Value type
        Parameters:
        codec - Use this codec to encode/decode keys and values, must not be null
        Returns:
        a CompletableFuture that is notified with the connection progress.
        Since:
        5.1
      • reloadPartitions

        @Deprecated
        public void reloadPartitions()
        Deprecated.
        since 6.0. Renamed to refreshPartitions().
        Refresh partitions and re-initialize the routing table.
      • refreshPartitions

        public void refreshPartitions()
        Refresh partitions and re-initialize the routing table.
        Since:
        6.0
      • refreshPartitionsAsync

        public CompletionStage<Void> refreshPartitionsAsync()
        Asynchronously reload partitions and re-initialize the distribution table.
        Returns:
        a CompletionStage that signals completion.
        Since:
        6.0
      • suspendTopologyRefresh

        public void suspendTopologyRefresh()
        Suspend periodic topology refresh if it was activated previously. Suspending cancels the periodic schedule without interrupting any running topology refresh. Suspension is in place until obtaining a new connection.
        Since:
        6.3
      • isTopologyRefreshInProgress

        public boolean isTopologyRefreshInProgress()
        Return whether a scheduled or adaptive topology refresh is in progress.
        Returns:
        true if a topology refresh is in progress.
        Since:
        6.3
      • updatePartitionsInConnections

        protected void updatePartitionsInConnections()
      • loadPartitions

        protected Partitions loadPartitions()
        Retrieve partitions. Nodes within Partitions are ordered by latency. Lower latency nodes come first.
        Returns:
        Partitions
      • loadPartitionsAsync

        protected CompletableFuture<Partitions> loadPartitionsAsync()
        Retrieve partitions. Nodes within Partitions are ordered by latency. Lower latency nodes come first.
        Returns:
        future that emits Partitions upon a successful topology lookup.
        Since:
        6.0
      • setPartitions

        public void setPartitions​(Partitions partitions)
        Sets the new cluster topology. The partitions are not applied to existing connections.
        Parameters:
        partitions - partitions object
      • shutdownAsync

        public CompletableFuture<Void> shutdownAsync​(long quietPeriod,
                                                     long timeout,
                                                     TimeUnit timeUnit)
        Shutdown this client and close all open connections asynchronously. The client should be discarded after calling shutdown.
        Overrides:
        shutdownAsync in class AbstractRedisClient
        Parameters:
        quietPeriod - the quiet period as described in the documentation
        timeout - the maximum amount of time to wait until the executor is shutdown regardless if a task was submitted during the quiet period
        timeUnit - the unit of quietPeriod and timeout
        Since:
        4.4
        See Also:
        EventExecutorGroup.shutdownGracefully(long, long, TimeUnit)
      • forEachClusterPubSubConnection

        protected void forEachClusterPubSubConnection​(Consumer<io.lettuce.core.cluster.StatefulRedisClusterPubSubConnectionImpl<?,​?>> function)
        Apply a Consumer of StatefulRedisClusterPubSubConnectionImpl to all active connections.
        Parameters:
        function - the Consumer.