class Polyphony::RedisDriver

Polyphony-based Redis driver

Public Class Methods

connect(config) click to toggle source
# File lib/polyphony/adapters/redis.rb, line 10
def self.connect(config)
  raise 'unix sockets not supported' if config[:scheme] == 'unix'

  # connection.connect_unix(config[:path], connect_timeout)

  raise 'ssl not supported' if config[:scheme] == 'rediss' || config[:ssl]

  # raise NotImplementedError, "SSL not supported by hiredis driver"

  new(config[:host], config[:port])
  # connection.connect(config[:host], config[:port], connect_timeout)
end
new(host, port) click to toggle source
# File lib/polyphony/adapters/redis.rb, line 23
def initialize(host, port)
  @connection = Polyphony::Net.tcp_connect(host, port)
  @reader = ::Hiredis::Reader.new
end

Public Instance Methods

connected?() click to toggle source
# File lib/polyphony/adapters/redis.rb, line 28
def connected?
  @connection && !@connection.closed?
end
disconnect() click to toggle source
# File lib/polyphony/adapters/redis.rb, line 36
def disconnect
  @connection.close
  @connection = nil
end
format_command(args) click to toggle source
# File lib/polyphony/adapters/redis.rb, line 45
def format_command(args)
  args = args.flatten
  (+"*#{args.size}\r\n").tap do |s|
    args.each do |a|
      a = a.to_s
      s << "$#{a.bytesize}\r\n#{a}\r\n"
    end
  end
end
read() click to toggle source
# File lib/polyphony/adapters/redis.rb, line 55
def read
  reply = @reader.gets
  return reply if reply

  @connection.read_loop do |data|
    @reader.feed(data)
    reply = @reader.gets
    return reply unless reply == false
  end
end
timeout=(timeout) click to toggle source
# File lib/polyphony/adapters/redis.rb, line 32
def timeout=(timeout)
  # ignore timeout for now
end
write(command) click to toggle source
# File lib/polyphony/adapters/redis.rb, line 41
def write(command)
  @connection.write(format_command(command))
end