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://:@[hostname]:/[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`

  • :id (String)

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

  • :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



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/redis.rb', line 63

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
  inherit_socket = @options.delete(:inherit_socket)
  @subscription_client = nil

  @client = initialize_client(@options)
  @client.inherit_socket! if inherit_socket
end