Sha256: 3d5958e4a8d7e6e0ef71a5af1e976c6de832144c6a4d311ed9d6c3830b095e98

Contents?: true

Size: 1.42 KB

Versions: 52

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require_relative '../../polyphony'

require 'redis'
require 'hiredis/reader'

# Polyphony-based Redis driver
class Polyphony::RedisDriver
  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

  def initialize(host, port)
    @connection = Polyphony::Net.tcp_connect(host, port)
    @reader = ::Hiredis::Reader.new
  end

  def connected?
    @connection && !@connection.closed?
  end

  def timeout=(timeout)
    # ignore timeout for now
  end

  def disconnect
    @connection.close
    @connection = nil
  end

  def write(command)
    @connection.write(format_command(command))
  end

  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

  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
end

Redis::Connection.drivers << Polyphony::RedisDriver

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
polyphony-0.79 lib/polyphony/adapters/redis.rb
polyphony-0.78 lib/polyphony/adapters/redis.rb
polyphony-0.77 lib/polyphony/adapters/redis.rb
polyphony-0.76 lib/polyphony/adapters/redis.rb
polyphony-0.75 lib/polyphony/adapters/redis.rb
polyphony-0.74 lib/polyphony/adapters/redis.rb
polyphony-0.73.1 lib/polyphony/adapters/redis.rb
polyphony-0.73 lib/polyphony/adapters/redis.rb
polyphony-0.72 lib/polyphony/adapters/redis.rb
polyphony-0.71 lib/polyphony/adapters/redis.rb
polyphony-0.70 lib/polyphony/adapters/redis.rb
polyphony-0.69 lib/polyphony/adapters/redis.rb
polyphony-0.68 lib/polyphony/adapters/redis.rb
polyphony-0.67 lib/polyphony/adapters/redis.rb
polyphony-0.66 lib/polyphony/adapters/redis.rb
polyphony-0.65 lib/polyphony/adapters/redis.rb
polyphony-0.64 lib/polyphony/adapters/redis.rb
polyphony-0.63 lib/polyphony/adapters/redis.rb
polyphony-0.62 lib/polyphony/adapters/redis.rb
polyphony-0.61 lib/polyphony/adapters/redis.rb