Class: Oxblood::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/oxblood/connection.rb

Overview

Class responsible for connection maintenance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Connection

Initialize connection to Redis server

Parameters:

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

    Connection options

Options Hash (opts):

  • :timeout (Float) — default: 1.0

    socket read/write timeout

  • :db (Integer)

    database number

  • :password (String)
  • :host (String) — default: 'localhost'

    Hostname or IP address to connect to

  • :port (Integer) — default: 6379

    Port Redis server listens on

  • :connect_timeout (Float) — default: 1.0

    socket connect timeout

  • :path (String)

    UNIX socket path



31
32
33
34
35
36
37
38
# File 'lib/oxblood/connection.rb', line 31

def initialize(opts = {})
  @in_transaction = false
  @socket = RSocket.new(opts)

  session = Session.new(self)
  session.auth(opts[:password]) if opts[:password]
  session.select(opts[:db]) if opts[:db]
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



11
12
13
# File 'lib/oxblood/connection.rb', line 11

def socket
  @socket
end

#transaction_modeBoolean

Returns transaction status.

Returns:

  • (Boolean)

    transaction status



16
17
18
# File 'lib/oxblood/connection.rb', line 16

def transaction_mode
  @transaction_mode
end

Instance Method Details

#in_transaction?Boolean?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Connection transaction status.

Returns:

  • (Boolean, nil)

    transaction status



73
74
75
# File 'lib/oxblood/connection.rb', line 73

def in_transaction?
  transaction_mode
end

#read_responseObject

TODO:

Raise specific error if server has nothing to answer.

Note:

Will raise RSocket::TimeoutError even if there is simply no response to read from server. For example, if you are trying to read response before sending command.

Read response from server

Raises:



65
66
67
# File 'lib/oxblood/connection.rb', line 65

def read_response
  Protocol.parse(@socket)
end

#run_command(*command) ⇒ Object

Send command to Redis server and read response from it

Examples:

run_command(‘PING’) => PONG

Parameters:

  • command (Array)

    Array of command name with it’s args



54
55
56
57
# File 'lib/oxblood/connection.rb', line 54

def run_command(*command)
  send_command(*command)
  read_response
end

#send_command(*command) ⇒ Integer

Send comand to Redis server

Examples:

send_command('CONFIG', 'GET', '*') => 32

Parameters:

  • command (Array)

    Array of command name with it’s args

Returns:

  • (Integer)

    Number of bytes written to socket



47
48
49
# File 'lib/oxblood/connection.rb', line 47

def send_command(*command)
  @socket.write(Protocol.build_command(*command))
end