]>
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 convert_to_bool(r
)
46 def initialize(opts
={})
47 opts
= {:host => 'localhost', :port => '6379', :db => 0}.merge(opts
)
55 "Redis Client connected to #{@host}:#{@port} against DB #{@db}"
59 @sock = TCPSocket
.new(@host, @port, 0)
60 call_command(["select",@db]) if @db !
= 0
63 def method_missing(*argv)
67 def call_command(argv)
68 # this wrapper to raw_call_command handle reconnection on socket
69 # error. We try to reconnect just one time, otherwise let the error
72 raw_call_command(argv)
73 rescue Errno
::ECONNRESET
76 raw_call_command(argv)
80 def raw_call_command(argv)
82 argv[0] = argv[0].to_s
.downcase
83 if BulkCommands
[argv[0]]
85 argv[-1] = bulk
.length
87 @sock.write(argv.join(" ")+
"\r\n")
88 @sock.write(bulk+
"\r\n") if bulk
90 # Post process the reply if needed
91 processor
= ReplyProcessor
[argv[0]]
92 processor
? processor
.call(read_reply
) : read_reply
96 raise "SELECT not allowed, use the :db option when creating the object"
109 raise Errno
::ECONNRESET,"Connection lost" if !line
118 bulklen = line[1..-1].to_i
119 return nil if bulklen == -1
120 data = @sock.read(bulklen)
124 objects = line[1..-1].to_i
125 return nil if bulklen == -1