Class: Redis2::Connection::Ruby

Inherits:
Object
  • Object
show all
Includes:
CommandHelper
Defined in:
lib/redis2/connection/ruby.rb

Constant Summary collapse

MINUS =
"-".freeze
PLUS =
"+".freeze
COLON =
":".freeze
DOLLAR =
"$".freeze
ASTERISK =
"*".freeze

Constants included from CommandHelper

CommandHelper::COMMAND_DELIMITER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#build_command

Constructor Details

#initialize(sock) ⇒ Ruby

Returns a new instance of Ruby.



247
248
249
# File 'lib/redis2/connection/ruby.rb', line 247

def initialize(sock)
  @sock = sock
end

Class Method Details

.connect(config) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/redis2/connection/ruby.rb', line 207

def self.connect(config)
  if config[:scheme] == "unix"
    sock = UNIXSocket.connect(config[:path], config[:timeout])
  else
    sock = TCPSocket.connect(config[:host], config[:port], config[:timeout])
  end

  instance = new(sock)
  instance.timeout = config[:timeout]
  instance.set_tcp_keepalive config[:tcp_keepalive]
  instance
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/redis2/connection/ruby.rb', line 251

def connected?
  !! @sock
end

#disconnectObject



255
256
257
258
259
260
# File 'lib/redis2/connection/ruby.rb', line 255

def disconnect
  @sock.close
rescue
ensure
  @sock = nil
end

#format_bulk_reply(line) ⇒ Object



304
305
306
307
308
309
310
# File 'lib/redis2/connection/ruby.rb', line 304

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) ⇒ Object



292
293
294
# File 'lib/redis2/connection/ruby.rb', line 292

def format_error_reply(line)
  CommandError.new(line.strip)
end

#format_integer_reply(line) ⇒ Object



300
301
302
# File 'lib/redis2/connection/ruby.rb', line 300

def format_integer_reply(line)
  line.to_i
end

#format_multi_bulk_reply(line) ⇒ Object



312
313
314
315
316
317
# File 'lib/redis2/connection/ruby.rb', line 312

def format_multi_bulk_reply(line)
  n = line.to_i
  return if n == -1

  Array.new(n) { read }
end

#format_reply(reply_type, line) ⇒ Object



281
282
283
284
285
286
287
288
289
290
# File 'lib/redis2/connection/ruby.rb', line 281

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) ⇒ Object



296
297
298
# File 'lib/redis2/connection/ruby.rb', line 296

def format_status_reply(line)
  line.strip
end

#get_tcp_keepaliveObject



230
231
232
233
234
235
236
# File 'lib/redis2/connection/ruby.rb', line 230

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

#readObject



272
273
274
275
276
277
278
279
# File 'lib/redis2/connection/ruby.rb', line 272

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) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/redis2/connection/ruby.rb', line 221

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) ⇒ Object



262
263
264
265
266
# File 'lib/redis2/connection/ruby.rb', line 262

def timeout=(timeout)
  if @sock.respond_to?(:timeout=)
    @sock.timeout = timeout
  end
end

#write(command) ⇒ Object



268
269
270
# File 'lib/redis2/connection/ruby.rb', line 268

def write(command)
  @sock.write(build_command(command))
end