]> git.saurik.com Git - redis.git/blob - client-libraries/ruby_2/rubyredis.rb
RubyRedis info postprocessor rewritten in a more functional way
[redis.git] / client-libraries / ruby_2 / rubyredis.rb
1 # RubyRedis is an alternative implementatin of Ruby client library written
2 # by Salvatore Sanfilippo.
3 #
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.
7
8 require 'socket'
9
10 class RedisClient
11 BulkCommands = {
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
15 }
16
17 ConvertToBool = lambda{|r| r == 0 ? false : r}
18
19 ReplyProcessor = {
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(" ")},
31 "info" => lambda{|r|
32 info = {}
33 r.each_line {|kv|
34 k,v = kv.split(":",2).map{|x| x.chomp}
35 info[k.to_sym] = v
36 }
37 info
38 }
39 }
40
41 def initialize(opts={})
42 opts = {:host => 'localhost', :port => '6379', :db => 0}.merge(opts)
43 @host = opts[:host]
44 @port = opts[:port]
45 @db = opts[:db]
46 connect_to_server
47 end
48
49 def to_s
50 "Redis Client connected to #{@host}:#{@port} against DB #{@db}"
51 end
52
53 def connect_to_server
54 @sock = TCPSocket.new(@host, @port, 0)
55 call_command(["select",@db]) if @db != 0
56 end
57
58 def method_missing(*argv)
59 call_command(argv)
60 end
61
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
65 # araise.
66 begin
67 raw_call_command(argv)
68 rescue Errno::ECONNRESET
69 @sock.close
70 connect_to_server
71 raw_call_command(argv)
72 end
73 end
74
75 def raw_call_command(argv)
76 bulk = nil
77 argv[0] = argv[0].to_s.downcase
78 if BulkCommands[argv[0]]
79 bulk = argv[-1].to_s
80 argv[-1] = bulk.length
81 end
82 @sock.write(argv.join(" ")+"\r\n")
83 @sock.write(bulk+"\r\n") if bulk
84
85 # Post process the reply if needed
86 processor = ReplyProcessor[argv[0]]
87 processor ? processor.call(read_reply) : read_reply
88 end
89
90 def select(*args)
91 raise "SELECT not allowed, use the :db option when creating the object"
92 end
93
94 def [](key)
95 get(key)
96 end
97
98 def []=(key,value)
99 set(key,value)
100 end
101
102 def read_reply
103 line = @sock.gets
104 raise Errno::ECONNRESET,"Connection lost" if !line
105 case line[0..0]
106 when "-"
107 raise line.strip
108 when "+"
109 line[1..-1].strip
110 when ":"
111 line[1..-1].to_i
112 when "$"
113 bulklen = line[1..-1].to_i
114 return nil if bulklen == -1
115 data = @sock.read(bulklen)
116 @sock.read(2) # CRLF
117 data
118 when "*"
119 objects = line[1..-1].to_i
120 return nil if bulklen == -1
121 res = []
122 objects.times {
123 res << read_reply
124 }
125 res
126 end
127 end
128 end