Method: Redis#initialize

Defined in:
lib/redis.rb

#initialize(options = {}) ⇒ Redis

Create a new client instance

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :url (String) — default: value of the environment variable REDIS_URL —

    a Redis URL, for a TCP connection: redis://:[password]@[hostname]:[port]/[db] (password, port and database are optional), for a unix socket connection: unix://[path to Redis socket]. This overrides all other options.

  • :host (String) — default: "127.0.0.1" —

    server hostname

  • :port (Integer) — default: 6379 —

    server port

  • :path (String) —

    path to server socket (overrides host and port)

  • :timeout (Float) — default: 1.0 —

    timeout in seconds

  • :connect_timeout (Float) — default: same as timeout —

    timeout for initial connect in seconds

  • :username (String) —

    Username to authenticate against server

  • :password (String) —

    Password to authenticate against server

  • :db (Integer) — default: 0 —

    Database to select after connect and on reconnects

  • :driver (Symbol) —

    Driver to use, currently supported: :ruby, :hiredis

  • :protocol (Integer) — default: 3 —

    RESP protocol version to negotiate (HELLO). Defaults to RESP3; set to 2 for RESP2. Servers without RESP3 support automatically fall back to RESP2.

  • :id (String) —

    ID for the client connection, assigns name to current connection by sending CLIENT SETNAME

  • :driver_info (String, Array<String>, false) —

    Identity a library built on top of redis-rb reports to the server via CLIENT SETINFO, shown as lib-name=redis-rb(<driver_info>) in CLIENT LIST. The recommended format is <name>_v<version>; an Array is joined with ;. Pass false to disable client identification entirely.

  • :reconnect_attempts (Integer, Array<Integer, Float>) —

    Number of attempts trying to connect, or a list of sleep duration between attempts.

  • :inherit_socket (Boolean) — default: false —

    Whether to use socket in forked process or not

  • :name (String) —

    The name of the server group to connect to.

  • :sentinels (Array) —

    List of sentinels to contact



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/redis.rb', line 71

def initialize(options = {})
  @monitor = Monitor.new
  @options = options.dup
  @options[:reconnect_attempts] = 1 unless @options.key?(:reconnect_attempts)
  if ENV["REDIS_URL"] && SERVER_URL_OPTIONS.none? { |o| @options.key?(o) }
    @options[:url] = ENV["REDIS_URL"]
  end
  # Kept as state, not just a local: the RESP3->RESP2 fallback rebuilds @client and must re-apply
  # socket inheritance, otherwise fork safety would be silently lost after a downgrade.
  @inherit_socket = @options.delete(:inherit_socket)
  # HIMPORT fieldsets are server session state that dies with the physical connection; the
  # registry remembers each prepared schema so a lost session can be repaired (see the
  # himport_* overrides below). Deleted from @options so it never reaches RedisClient::Config.
  @himport_auto_prepare = @options.delete(:himport_auto_prepare) != false
  @himport_fieldsets = {}
  @subscription_client = nil

  @client = build_client
end