Class: Redis::Connection::Hiredis

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Hiredis

Returns a new instance of Hiredis.



30
31
32
# File 'lib/redis/connection/hiredis.rb', line 30

def initialize(connection)
  @connection = connection
end

Class Method Details

.connect(config) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redis/connection/hiredis.rb', line 11

def self.connect(config)
  connection = ::Hiredis::Connection.new
  connect_timeout = (config.fetch(:connect_timeout, 0) * 1_000_000).to_i

  if config[:scheme] == "unix"
    connection.connect_unix(config[:path], connect_timeout)
  elsif config[:scheme] == "rediss" || config[:ssl]
    raise NotImplementedError, "SSL not supported by hiredis driver"
  else
    connection.connect(config[:host], config[:port], connect_timeout)
  end

  instance = new(connection)
  instance.timeout = config[:read_timeout]
  instance
rescue Errno::ETIMEDOUT
  raise TimeoutError
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/redis/connection/hiredis.rb', line 34

def connected?
  @connection&.connected?
end

#disconnectObject



43
44
45
46
# File 'lib/redis/connection/hiredis.rb', line 43

def disconnect
  @connection.disconnect
  @connection = nil
end

#readObject



54
55
56
57
58
59
60
61
62
# File 'lib/redis/connection/hiredis.rb', line 54

def read
  reply = @connection.read
  reply = CommandError.new(reply.message) if reply.is_a?(RuntimeError)
  reply
rescue Errno::EAGAIN
  raise TimeoutError
rescue RuntimeError => err
  raise ProtocolError, err.message
end

#timeout=(timeout) ⇒ Object



38
39
40
41
# File 'lib/redis/connection/hiredis.rb', line 38

def timeout=(timeout)
  # Hiredis works with microsecond timeouts
  @connection.timeout = Integer(timeout * 1_000_000)
end

#write(command) ⇒ Object



48
49
50
51
52
# File 'lib/redis/connection/hiredis.rb', line 48

def write(command)
  @connection.write(command.flatten(1))
rescue Errno::EAGAIN
  raise TimeoutError
end