Class RedisConf

java.lang.Object
com.kamikazejam.kamicommon.redis.util.RedisConf

public class RedisConf extends Object
Connection settings for a single Redis server.

Redis 6 introduced ACL users, so a connection is identified by a username as well as a password. A configuration that supplies only a password authenticates as default, which is the pre-ACL behaviour and remains supported.

Instances are value objects and are used as cache keys by RedisConnector, so two configurations describing the same connection share one client.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Redis' own default port, used whenever a port is not given.
  • Constructor Summary

    Constructors
    Constructor
    Description
    RedisConf(@NotNull String address, int port, @Nullable String password)
    Creates a config with the given address, port, and password, authenticating as default.
    RedisConf(@NotNull String address, int port, @Nullable String username, @Nullable String password)
    Creates a config with the given address, port, username, and password.
    RedisConf(@NotNull String address, int port, @Nullable String username, @Nullable String password, int database, boolean ssl)
    Creates a config with every connection setting stated.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    static @NotNull RedisConf
    fromUrl(@NotNull String url)
    Parses a Redis connection URL.
    @NotNull io.lettuce.core.RedisURI
     
    int
     
    static @NotNull RedisConf
    of(@NotNull String address)
    Creates a RedisConfig object with the given address Defaults: port 6379, no authentication
    static @NotNull RedisConf
    of(@NotNull String address, int port)
    Creates a RedisConfig object with the given address and port Defaults: no authentication
    static @NotNull RedisConf
    of(@NotNull String address, int port, @NotNull String password)
    Creates a RedisConfig object with the given address, port, and password Defaults: authenticates as default
    static @NotNull RedisConf
    of(@NotNull String address, int port, @Nullable String username, @NotNull String password)
    Creates a RedisConfig object with the given address, port, username, and password
    Never includes the password.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • DEFAULT_PORT

      public static final int DEFAULT_PORT
      Redis' own default port, used whenever a port is not given.
      See Also:
  • Constructor Details

    • RedisConf

      public RedisConf(@NotNull @NotNull String address, int port, @Nullable @Nullable String password)
      Creates a config with the given address, port, and password, authenticating as default.
      Parameters:
      password - Nullable (for no authentication)
    • RedisConf

      public RedisConf(@NotNull @NotNull String address, int port, @Nullable @Nullable String username, @Nullable @Nullable String password)
      Creates a config with the given address, port, username, and password.
      Parameters:
      username - Nullable (to authenticate as default)
      password - Nullable (for no authentication)
    • RedisConf

      public RedisConf(@NotNull @NotNull String address, int port, @Nullable @Nullable String username, @Nullable @Nullable String password, int database, boolean ssl)
      Creates a config with every connection setting stated.
      Parameters:
      username - Nullable (to authenticate as default)
      password - Nullable (for no authentication)
      database - Logical database index
      ssl - Whether to connect over TLS
  • Method Details

    • getURI

      @NotNull public @NotNull io.lettuce.core.RedisURI getURI()
    • of

      @NotNull public static @NotNull RedisConf of(@NotNull @NotNull String address)
      Creates a RedisConfig object with the given address Defaults: port 6379, no authentication
    • of

      @NotNull public static @NotNull RedisConf of(@NotNull @NotNull String address, int port)
      Creates a RedisConfig object with the given address and port Defaults: no authentication
    • of

      @NotNull public static @NotNull RedisConf of(@NotNull @NotNull String address, int port, @NotNull @NotNull String password)
      Creates a RedisConfig object with the given address, port, and password Defaults: authenticates as default
    • of

      @NotNull public static @NotNull RedisConf of(@NotNull @NotNull String address, int port, @Nullable @Nullable String username, @NotNull @NotNull String password)
      Creates a RedisConfig object with the given address, port, username, and password
    • fromUrl

      @NotNull public static @NotNull RedisConf fromUrl(@NotNull @NotNull String url)
      Parses a Redis connection URL.

      Accepted forms, where everything after the host is optional:

        redis://host
        redis://host:6380
        redis://:password@host
        redis://username:password@host:6380/3
        rediss://username:password@host      (TLS)
      

      Credentials must be written with a colon. redis://value@host is rejected rather than guessed at. Lettuce reads a colon-less userInfo as the password, so redis://myuser@host against a server with requirepass set would connect successfully as default while the caller believed they were authenticating as myuser. That failure is silent and is the one this whole ACL arrangement exists to prevent, so the ambiguous form is an error. Write redis://myuser:secret@host for an ACL user, or redis://:secret@host for a password alone.

      Parameters:
      url - the connection URL
      Returns:
      the parsed configuration
      Throws:
      IllegalArgumentException - if the URL is not a Redis URL this can represent. The message never contains the URL itself, because it would carry the password into the log.
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Never includes the password. This type is a natural thing to put in a log line.
      Overrides:
      toString in class Object