]>
git.saurik.com Git - redis.git/blob - client-libraries/ruby_2/rubyredis.rb
1 # RubyRedis is an alternative implementatin of Ruby client library written
2 # by Salvatore Sanfilippo.
4 # The aim of this library is to create an alternative client library that is
5 # much simpler and does not implement every command explicitly but uses
6 # method_missing instead.
12 "set"=>true, "setnx"=>true, "rpush"=>true, "lpush"=>true, "lset"=>true,
13 "lrem"=>true, "sadd"=>true, "srem"=>true, "sismember"=>true,
14 "echo"=>true, "getset"=>true, "smove"=>true
17 def initialize(opts
={})
18 opts
= {:host => 'localhost', :port => '6379', :db => 0}.merge(opts
)
26 "Redis Client connected to #{@host}:#{@port} against DB #{@db}"
30 @sock = TCPSocket
.new(@host, @port, 0)
31 call_command(["select",@db]) if @db !
= 0
34 def method_missing(*argv)
38 def call_command(argv)
39 # this wrapper to raw_call_command handle reconnection on socket
40 # error. We try to reconnect just one time, otherwise let the error
43 raw_call_command(argv)
44 rescue Errno
::ECONNRESET
47 raw_call_command(argv)
51 def raw_call_command(argv)
53 argv[0] = argv[0].to_s
.downcase
54 if BulkCommands
[argv[0]]
56 argv[-1] = bulk
.length
58 @sock.write(argv.join(" ")+
"\r\n")
59 @sock.write(bulk+
"\r\n") if bulk
64 raise "SELECT not allowed, use the :db option when creating the object"
77 raise Errno
::ECONNRESET,"Connection lost" if !line
86 bulklen = line[1..-1].to_i
87 return nil if bulklen == -1
88 data = @sock.read(bulklen)
92 objects = line[1..-1].to_i
93 return nil if bulklen == -1