class Redis::Connection::Hiredis

Public Class Methods

connect(config) click to toggle source
# File lib/redis/connection/hiredis.rb, line 10
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
new(connection) click to toggle source
# File lib/redis/connection/hiredis.rb, line 29
def initialize(connection)
  @connection = connection
end

Public Instance Methods

connected?() click to toggle source
# File lib/redis/connection/hiredis.rb, line 33
def connected?
  @connection&.connected?
end
disconnect() click to toggle source
# File lib/redis/connection/hiredis.rb, line 42
def disconnect
  @connection.disconnect
  @connection = nil
end
read() click to toggle source
# File lib/redis/connection/hiredis.rb, line 53
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) click to toggle source
# File lib/redis/connection/hiredis.rb, line 37
def timeout=(timeout)
  # Hiredis works with microsecond timeouts
  @connection.timeout = Integer(timeout * 1_000_000)
end
write(command) click to toggle source
# File lib/redis/connection/hiredis.rb, line 47
def write(command)
  @connection.write(command.flatten(1))
rescue Errno::EAGAIN
  raise TimeoutError
end