class Redis::Connection::Ruby

Constants

ASTERISK
COLON
DOLLAR
MINUS
PLUS

Public Class Methods

connect(config) click to toggle source
# File lib/redis/connection/ruby.rb, line 253
def self.connect(config)
  if config[:scheme] == "unix"
    raise ArgumentError, "SSL incompatible with unix sockets" if config[:ssl]
    sock = UNIXSocket.connect(config[:path], config[:connect_timeout])
  elsif config[:scheme] == "rediss" || config[:ssl]
    sock = SSLSocket.connect(config[:host], config[:port], config[:connect_timeout], config[:ssl_params])
  else
    sock = TCPSocket.connect(config[:host], config[:port], config[:connect_timeout])
  end

  instance = new(sock)
  instance.timeout = config[:timeout]
  instance.write_timeout = config[:write_timeout]
  instance.set_tcp_keepalive config[:tcp_keepalive]
  instance
end
new(sock) click to toggle source
# File lib/redis/connection/ruby.rb, line 297
def initialize(sock)
  @sock = sock
end

Public Instance Methods

connected?() click to toggle source
# File lib/redis/connection/ruby.rb, line 301
def connected?
  !! @sock
end
disconnect() click to toggle source
# File lib/redis/connection/ruby.rb, line 305
def disconnect
  @sock.close
rescue
ensure
  @sock = nil
end
format_bulk_reply(line) click to toggle source
# File lib/redis/connection/ruby.rb, line 358
def format_bulk_reply(line)
  bulklen = line.to_i
  return if bulklen == -1
  reply = encode(@sock.read(bulklen))
  @sock.read(2) # Discard CRLF.
  reply
end
format_error_reply(line) click to toggle source
# File lib/redis/connection/ruby.rb, line 346
def format_error_reply(line)
  CommandError.new(line.strip)
end
format_integer_reply(line) click to toggle source
# File lib/redis/connection/ruby.rb, line 354
def format_integer_reply(line)
  line.to_i
end
format_multi_bulk_reply(line) click to toggle source
# File lib/redis/connection/ruby.rb, line 366
def format_multi_bulk_reply(line)
  n = line.to_i
  return if n == -1

  Array.new(n) { read }
end
format_reply(reply_type, line) click to toggle source
# File lib/redis/connection/ruby.rb, line 335
def format_reply(reply_type, line)
  case reply_type
  when MINUS    then format_error_reply(line)
  when PLUS     then format_status_reply(line)
  when COLON    then format_integer_reply(line)
  when DOLLAR   then format_bulk_reply(line)
  when ASTERISK then format_multi_bulk_reply(line)
  else raise ProtocolError.new(reply_type)
  end
end
format_status_reply(line) click to toggle source
# File lib/redis/connection/ruby.rb, line 350
def format_status_reply(line)
  line.strip
end
get_tcp_keepalive() click to toggle source
# File lib/redis/connection/ruby.rb, line 280
def get_tcp_keepalive
  {
    :time   => @sock.getsockopt(Socket::SOL_TCP, Socket::TCP_KEEPIDLE).int,
    :intvl  => @sock.getsockopt(Socket::SOL_TCP, Socket::TCP_KEEPINTVL).int,
    :probes => @sock.getsockopt(Socket::SOL_TCP, Socket::TCP_KEEPCNT).int,
  }
end
read() click to toggle source
# File lib/redis/connection/ruby.rb, line 326
def read
  line = @sock.gets
  reply_type = line.slice!(0, 1)
  format_reply(reply_type, line)

rescue Errno::EAGAIN
  raise TimeoutError
end
set_tcp_keepalive(keepalive) click to toggle source
# File lib/redis/connection/ruby.rb, line 271
def set_tcp_keepalive(keepalive)
  return unless keepalive.is_a?(Hash)

  @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE,  true)
  @sock.setsockopt(Socket::SOL_TCP,    Socket::TCP_KEEPIDLE,  keepalive[:time])
  @sock.setsockopt(Socket::SOL_TCP,    Socket::TCP_KEEPINTVL, keepalive[:intvl])
  @sock.setsockopt(Socket::SOL_TCP,    Socket::TCP_KEEPCNT,   keepalive[:probes])
end
timeout=(timeout) click to toggle source
# File lib/redis/connection/ruby.rb, line 312
def timeout=(timeout)
  if @sock.respond_to?(:timeout=)
    @sock.timeout = timeout
  end
end
write(command) click to toggle source
# File lib/redis/connection/ruby.rb, line 322
def write(command)
  @sock.write(build_command(command))
end
write_timeout=(timeout) click to toggle source
# File lib/redis/connection/ruby.rb, line 318
def write_timeout=(timeout)
  @sock.write_timeout = timeout
end