Class: Redis2::Connection::Ruby
- Inherits:
-
Object
- Object
- Redis2::Connection::Ruby
show all
- Includes:
- CommandHelper
- Defined in:
- lib/redis2/connection/ruby.rb
Constant Summary
collapse
- MINUS =
"-".freeze
- PLUS =
"+".freeze
- COLON =
":".freeze
- DOLLAR =
"$".freeze
- ASTERISK =
"*".freeze
CommandHelper::COMMAND_DELIMITER
Class Method Summary
collapse
Instance Method Summary
collapse
#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
251
252
253
|
# File 'lib/redis2/connection/ruby.rb', line 251
def connected?
!! @sock
end
|
#disconnect ⇒ Object
255
256
257
258
259
260
|
# File 'lib/redis2/connection/ruby.rb', line 255
def disconnect
@sock.close
rescue
ensure
@sock = nil
end
|
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) reply
end
|
292
293
294
|
# File 'lib/redis2/connection/ruby.rb', line 292
def format_error_reply(line)
CommandError.new(line.strip)
end
|
300
301
302
|
# File 'lib/redis2/connection/ruby.rb', line 300
def format_integer_reply(line)
line.to_i
end
|
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
|
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
|
296
297
298
|
# File 'lib/redis2/connection/ruby.rb', line 296
def format_status_reply(line)
line.strip
end
|
#get_tcp_keepalive ⇒ Object
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
|
#read ⇒ Object
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
|