]>
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)
35 k
,v
= k
.chomp
, v
= v
.chomp
42 def initialize(opts
={})
43 opts
= {:host => 'localhost', :port => '6379', :db => 0}.merge(opts
)
51 "Redis Client connected to #{@host}:#{@port} against DB #{@db}"
55 @sock = TCPSocket
.new(@host, @port, 0)
56 call_command(["select",@db]) if @db !
= 0
59 def method_missing(*argv)
63 def call_command(argv)
64 # this wrapper to raw_call_command handle reconnection on socket
65 # error. We try to reconnect just one time, otherwise let the error
68 raw_call_command(argv)
69 rescue Errno
::ECONNRESET
72 raw_call_command(argv)
76 def raw_call_command(argv)
78 argv[0] = argv[0].to_s
.downcase
79 if BulkCommands
[argv[0]]
81 argv[-1] = bulk
.length
83 @sock.write(argv.join(" ")+
"\r\n")
84 @sock.write(bulk+
"\r\n") if bulk
86 # Post process the reply if needed
87 processor
= ReplyProcessor
[argv[0]]
88 processor
? processor
.call(read_reply
) : read_reply
92 raise "SELECT not allowed, use the :db option when creating the object"
105 raise Errno
::ECONNRESET,"Connection lost" if !line
114 bulklen = line[1..-1].to_i
115 return nil if bulklen == -1
116 data = @sock.read(bulklen)
120 objects = line[1..-1].to_i
121 return nil if bulklen == -1