]>
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 ConvertToBool
= lambda
{|r
| r
== 0 ? false : r
}
20 "exists" => ConvertToBool
,
21 "sismember"=> ConvertToBool
,
22 "sadd"=> ConvertToBool
,
23 "srem"=> ConvertToBool
,
24 "smove"=> ConvertToBool
,
25 "move"=> ConvertToBool
,
26 "setnx"=> ConvertToBool
,
27 "del"=> ConvertToBool
,
28 "renamenx"=> ConvertToBool
,
29 "expire"=> ConvertToBool
,
30 "keys" => lambda
{|r
| r
.split(" ")},
34 k
,v
= kv
.split(":",2).map
{|x
| x
.chomp
}
41 def initialize(opts
={})
42 opts
= {:host => 'localhost', :port => '6379', :db => 0}.merge(opts
)
50 "Redis Client connected to #{@host}:#{@port} against DB #{@db}"
54 @sock = TCPSocket
.new(@host, @port, 0)
55 call_command(["select",@db]) if @db !
= 0
58 def method_missing(*argv)
62 def call_command(argv)
63 # this wrapper to raw_call_command handle reconnection on socket
64 # error. We try to reconnect just one time, otherwise let the error
67 raw_call_command(argv)
68 rescue Errno
::ECONNRESET
71 raw_call_command(argv)
75 def raw_call_command(argv)
77 argv[0] = argv[0].to_s
.downcase
78 if BulkCommands
[argv[0]]
80 argv[-1] = bulk
.length
82 @sock.write(argv.join(" ")+
"\r\n")
83 @sock.write(bulk+
"\r\n") if bulk
85 # Post process the reply if needed
86 processor
= ReplyProcessor
[argv[0]]
87 processor
? processor
.call(read_reply
) : read_reply
91 raise "SELECT not allowed, use the :db option when creating the object"
104 raise Errno
::ECONNRESET,"Connection lost" if !line
113 bulklen = line[1..-1].to_i
114 return nil if bulklen == -1
115 data = @sock.read(bulklen)
119 objects = line[1..-1].to_i
120 return nil if bulklen == -1