Module lettuce.core

Class RedisURI

java.lang.Object
io.lettuce.core.RedisURI
All Implemented Interfaces:
ConnectionPoint, Serializable

public class RedisURI
extends Object
implements Serializable, ConnectionPoint
Redis URI. Contains connection details for the Redis/Sentinel connections. You can provide the database, client name, password and timeouts within the RedisURI. You have the following possibilities to create a RedisURI:
  • Use an URI:

    RedisURI.create("redis://localhost/");

    See create(String) for more options
  • Use the Builder:

    RedisURI.Builder.redis("localhost", 6379).withPassword("password").withDatabase(1).build();

    See RedisURI.Builder.redis(String) and RedisURI.Builder.sentinel(String) for more options.
  • Construct your own instance:

    new RedisURI("localhost", 6379, Duration.ofSeconds(60));

    or

    RedisURI uri = new RedisURI(); uri.setHost("localhost");

URI syntax

Redis Standalone
redis://[[username:]password@]host [: port][/database][? [timeout=timeout[d|h|m|s|ms|us|ns]] [ &database=database] [&clientName=clientName] [&verifyPeer=NONE|CA|FULL]]
Redis Standalone (SSL)
rediss://[[username:]password@]host [: port][/database][? [timeout=timeout[d|h|m|s|ms|us|ns]] [ &database=database] [&clientName=clientName] [&verifyPeer=NONE|CA|FULL]]
Redis Standalone (Unix Domain Sockets)
redis-socket:// [[username:]password@]path[ ?[timeout=timeout[d|h|m|s|ms|us|ns]][&database=database] [&clientName=clientName] [&verifyPeer=NONE|CA|FULL]]
Redis Sentinel
redis-sentinel://[[username:]password@]host1 [: port1][, host2 [:port2]][, hostN [:portN]][/ database][? [timeout=timeout[d|h|m|s|ms|us|ns]] [ &sentinelMasterId=sentinelMasterId] [&database=database] [&clientName=clientName] [&verifyPeer=NONE|CA|FULL]]

Note: When using Redis Sentinel, the password from the URI applies to the data nodes only. Sentinel authentication must be configured for each sentinel node.

Note:Usernames are supported as of Redis 6.

Schemes

  • redis Redis Standalone
  • rediss Redis Standalone SSL
  • redis-socket Redis Standalone Unix Domain Socket
  • redis-sentinel Redis Sentinel
  • rediss-sentinel Redis Sentinel SSL

Timeout units

  • d Days
  • h Hours
  • m Minutes
  • s Seconds
  • ms Milliseconds
  • us Microseconds
  • ns Nanoseconds

Hint: The database parameter within the query part has higher precedence than the database in the path.

RedisURI supports Redis Standalone, Redis Sentinel and Redis Cluster with plain, SSL, TLS and unix domain socket connections.
Since:
3.0
Author:
Mark Paluch, Guy Korland, Johnny Lim
See Also:
Serialized Form
  • Field Details

  • Constructor Details

    • RedisURI

      public RedisURI()
      Default empty constructor.
    • RedisURI

      public RedisURI​(String host, int port, Duration timeout)
      Constructor with host/port and timeout.
      Parameters:
      host - the host
      port - the port
      timeout - timeout value
  • Method Details

    • builder

      public static RedisURI.Builder builder()
      Return a new RedisURI.Builder to construct a RedisURI.
      Returns:
      a new RedisURI.Builder to construct a RedisURI.
    • create

      public static RedisURI create​(String host, int port)
      Create a Redis URI from host and port.
      Parameters:
      host - the host
      port - the port
      Returns:
      An instance of RedisURI containing details from the host and port.
    • create

      public static RedisURI create​(String uri)
      Create a Redis URI from an URI string. The uri must follow conventions of URI
      Parameters:
      uri - The URI string.
      Returns:
      An instance of RedisURI containing details from the URI.
    • create

      public static RedisURI create​(URI uri)
      Create a Redis URI from an URI string: The uri must follow conventions of URI
      Parameters:
      uri - The URI.
      Returns:
      An instance of RedisURI containing details from the URI.
    • builder

      public static RedisURI.Builder builder​(RedisURI source)
      Create a new RedisURI.Builder that is initialized from a plain RedisURI.
      Parameters:
      source - the initialization source, must not be null.
      Returns:
      the initialized builder.
      Since:
      6.0
    • getHost

      public String getHost()
      Returns the host.
      Specified by:
      getHost in interface ConnectionPoint
      Returns:
      the host.
    • setHost

      public void setHost​(String host)
      Sets the Redis host.
      Parameters:
      host - the host
    • getSentinelMasterId

      public String getSentinelMasterId()
      Returns the Sentinel Master Id.
      Returns:
      the Sentinel Master Id.
    • setSentinelMasterId

      public void setSentinelMasterId​(String sentinelMasterId)
      Sets the Sentinel Master Id.
      Parameters:
      sentinelMasterId - the Sentinel Master Id.
    • getPort

      public int getPort()
      Returns the Redis port.
      Specified by:
      getPort in interface ConnectionPoint
      Returns:
      the Redis port
    • setPort

      public void setPort​(int port)
      Sets the Redis port. Defaults to DEFAULT_REDIS_PORT.
      Parameters:
      port - the Redis port
    • getSocket

      public String getSocket()
      Returns the Unix Domain Socket path.
      Specified by:
      getSocket in interface ConnectionPoint
      Returns:
      the Unix Domain Socket path.
    • setSocket

      public void setSocket​(String socket)
      Sets the Unix Domain Socket path.
      Parameters:
      socket - the Unix Domain Socket path.
    • applyAuthentication

      public void applyAuthentication​(RedisURI source)
      Apply authentication from another RedisURI. The authentication settings of the source URI will be applied to this URI. That is in particular username and password. If the source has authentication credentials configured, then this URI will use the same credentials. If this URI has authentication configured and the source URI has no authentication, then this URI's authentication credentials will be reset.
      Parameters:
      source - must not be null.
      Since:
      6.0
    • getUsername

      public String getUsername()
      Returns the username.
      Returns:
      the username
      Since:
      6.0
    • setUsername

      public void setUsername​(String username)
      Sets the username.
      Parameters:
      username - the username, must not be null.
      Since:
      6.0
    • getPassword

      public char[] getPassword()
      Returns the password.
      Returns:
      the password
    • setPassword

      @Deprecated public void setPassword​(String password)
      Deprecated.
      since 6.0. Use setPassword(CharSequence) or setPassword(char[]) to avoid String caching.
      Sets the password. Use empty string to skip authentication.

      This method is deprecated as of Lettuce 6.0. The reason is that String has a strong caching affinity and the JVM cannot easily GC String instances. Therefore we suggest using either char[] or a custom CharSequence (e.g. StringBuilder or netty's AsciiString).

      Parameters:
      password - the password, must not be null.
    • setPassword

      public void setPassword​(CharSequence password)
      Sets the password. Use empty string to skip authentication.
      Parameters:
      password - the password, must not be null.
      Since:
      5.2
    • setPassword

      public void setPassword​(char[] password)
      Sets the password. Use empty char array to skip authentication.
      Parameters:
      password - the password, can be null.
      Since:
      4.4
    • getTimeout

      public Duration getTimeout()
      Returns the command timeout for synchronous command execution.
      Returns:
      the Timeout
      Since:
      5.0
    • setTimeout

      public void setTimeout​(Duration timeout)
      Sets the command timeout for synchronous command execution. A zero timeout value indicates to not time out.
      Parameters:
      timeout - the command timeout for synchronous command execution.
      Since:
      5.0
    • getDatabase

      public int getDatabase()
      Returns the Redis database number. Databases are only available for Redis Standalone and Redis Master/Slave.
      Returns:
      the Redis database number
    • setDatabase

      public void setDatabase​(int database)
      Sets the Redis database number. Databases are only available for Redis Standalone and Redis Master/Slave.
      Parameters:
      database - the Redis database number.
    • getClientName

      public String getClientName()
      Returns the client name.
      Returns:
      the client name.
      Since:
      4.4
    • setClientName

      public void setClientName​(String clientName)
      Sets the client name to be applied on Redis connections.
      Parameters:
      clientName - the client name.
      Since:
      4.4
    • applySsl

      public void applySsl​(RedisURI source)
      Apply authentication from another RedisURI. The SSL settings of the source URI will be applied to this URI. That is in particular SSL usage, peer verification and StartTLS.
      Parameters:
      source - must not be null.
      Since:
      6.0
    • isSsl

      public boolean isSsl()
      Returns true if SSL mode is enabled.
      Returns:
      true if SSL mode is enabled.
    • setSsl

      public void setSsl​(boolean ssl)
      Sets whether to use SSL. Sets SSL also for already configured Redis Sentinel nodes.
      Parameters:
      ssl -
    • isVerifyPeer

      public boolean isVerifyPeer()
      Returns whether to verify peers when using SSL.
      Returns:
      true to verify peers when using SSL.
    • getVerifyMode

      public SslVerifyMode getVerifyMode()
      Returns the mode to verify peers when using SSL.
      Returns:
      the verification mode
      Since:
      6.1
    • setVerifyPeer

      public void setVerifyPeer​(boolean verifyPeer)
      Sets whether to verify peers when using SSL. Sets peer verification also for already configured Redis Sentinel nodes.
      Parameters:
      verifyPeer - true to verify peers when using SSL.
    • setVerifyPeer

      public void setVerifyPeer​(SslVerifyMode verifyMode)
      Sets how to verify peers when using SSL. Sets peer verification also for already configured Redis Sentinel nodes.
      Parameters:
      verifyMode - verification mode to use when using SSL.
      Since:
      6.1
    • isStartTls

      public boolean isStartTls()
      Returns true if StartTLS is enabled.
      Returns:
      true if StartTLS is enabled.
    • setStartTls

      public void setStartTls​(boolean startTls)
      Returns whether StartTLS is enabled. Sets StartTLS also for already configured Redis Sentinel nodes.
      Parameters:
      startTls - true if StartTLS is enabled.
    • getSentinels

      public List<RedisURI> getSentinels()
      Returns:
      the list of Redis Sentinel URIs.
    • toURI

      public URI toURI()
      Creates an URI based on the RedisURI if possible.

      An URI an represent a Standalone address using host and port or socket addressing or a Redis Sentinel address using host/port. A Redis Sentinel URI with multiple nodes using Unix Domain Sockets cannot be rendered to a URI.

      Returns:
      URI based on the RedisURI.
      Throws:
      IllegalStateException - if the URI cannot be rendered.
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      the RedisURL in a URI-like form.
    • equals

      public boolean equals​(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object