]>
git.saurik.com Git - redis.git/blob - client-libraries/ruby/lib/redis.rb
3 require File
.join(File
.dirname(__FILE__
),'server')
4 require File
.join(File
.dirname(__FILE__
),'pipeline')
7 class RedisError
< StandardError
9 class RedisRenameError
< StandardError
22 def initialize(opts={})
23 @opts = {:host => 'localhost
', :port => '6379', :db => 0}.merge(opts)
24 $debug = @opts[:debug]
26 @server = Server.new(@opts[:host], @opts[:port], (@opts[:timeout]||10))
30 pipeline = Pipeline.new(self)
47 def with_socket_management(server, &block)
49 socket = server.socket
51 #Timeout or server down
52 rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNREFUSED, Timeout::Error => e
54 puts "Client (#{server.inspect}) disconnected from server: #{e.inspect}\n" if $debug
57 rescue NoMethodError => e
58 puts "Client (#{server.inspect}) tryin server that is down: #{e.inspect}\n Dying!" if $debug
59 raise Errno::ECONNREFUSED
65 with_socket_management(@server) do |socket|
66 trap("INT
") { puts "\nGot ^C! Dying!
"; exit }
68 puts "Now Monitoring
..."
83 write "SELECT
#{index}\r\n"
94 puts "Warning!
\nFlushing
*ALL
* databases!
\n5 Seconds to Hit ^C!
"
95 trap('INT') {quit; return false}
117 k,v = kv.split(':', 2)
118 k,v = k.chomp, v = v.chomp
128 puts "bulk_reply read value is
#{x.inspect}" if $debug
131 puts "error
in bulk_reply
#{e}" if $debug
137 with_socket_management(@server) do |socket|
138 puts "writing
: #{data}" if $debug
144 with_socket_management(@server) do |socket|
145 len = [0, len.to_i].max
146 res = socket.read(len + 2)
147 res
= res
.chomp
if res
152 def read(length
= read_proto
)
153 with_socket_management(@server) do |socket
|
154 res
= socket
.read(length
)
155 puts
"read is #{res.inspect}" if $debug
161 write
"KEYS #{glob}\r\n"
162 get_response
.split(' ')
165 def rename!
(oldkey
, newkey
)
166 write
"RENAME #{oldkey} #{newkey}\r\n"
170 def rename(oldkey
, newkey
)
171 write
"RENAMENX #{oldkey} #{newkey}\r\n"
174 raise RedisRenameError
, "source key: #{oldkey} does not exist"
176 raise RedisRenameError
, "target key: #{oldkey} already exists"
178 raise RedisRenameError
, "source and destination keys are the same"
185 write
"EXISTS #{key}\r\n"
190 write
"DEL #{key}\r\n"
199 write
"GET #{key}\r\n"
204 write
"MGET #{keys.join(' ')}\r\n"
208 def incr(key
, increment
=nil)
210 write
"INCRBY #{key} #{increment}\r\n"
212 write
"INCR #{key}\r\n"
217 def decr(key
, decrement
=nil)
219 write
"DECRBY #{key} #{decrement}\r\n"
221 write
"DECR #{key}\r\n"
227 write
"RANDOMKEY\r\n"
232 write
"LLEN #{key}\r\n"
233 case i
= get_response
235 raise RedisError
, "key: #{key} does not hold a list value"
242 write
"TYPE #{key}\r\n"
246 def push_tail(key
, string
)
247 write
"RPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
251 def push_head(key
, string
)
252 write
"LPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
257 write
"LPOP #{key}\r\n"
262 write
"RPOP #{key}\r\n"
266 def list_set(key
, index
, val
)
267 write
"LSET #{key} #{index} #{val.to_s.size}\r\n#{val}\r\n"
271 def list_range(key
, start
, ending
)
272 write
"LRANGE #{key} #{start} #{ending}\r\n"
276 def list_trim(key
, start
, ending
)
277 write
"LTRIM #{key} #{start} #{ending}\r\n"
281 def list_index(key
, index
)
282 write
"LINDEX #{key} #{index}\r\n"
286 def list_rm(key
, count
, value
)
287 write
"LREM #{key} #{count} #{value.to_s.size}\r\n#{value}\r\n"
288 case num
= get_response
290 raise RedisError
, "key: #{key} does not exist"
292 raise RedisError
, "key: #{key} does not hold a list value"
298 def set_add(key
, member
)
299 write
"SADD #{key} #{member.to_s.size}\r\n#{member}\r\n"
306 raise RedisError
, "key: #{key} contains a non set value"
310 def set_delete(key
, member
)
311 write
"SREM #{key} #{member.to_s.size}\r\n#{member}\r\n"
318 raise RedisError
, "key: #{key} contains a non set value"
323 write
"SCARD #{key}\r\n"
324 case i
= get_response
326 raise RedisError
, "key: #{key} contains a non set value"
332 def set_member
?(key
, member
)
333 write
"SISMEMBER #{key} #{member.to_s.size}\r\n#{member}\r\n"
340 raise RedisError
, "key: #{key} contains a non set value"
345 write
"SMEMBERS #{key}\r\n"
346 Set
.new(get_response
)
349 def set_intersect(*keys
)
350 write
"SINTER #{keys.join(' ')}\r\n"
351 Set
.new(get_response
)
354 def set_inter_store(destkey
, *keys
)
355 write
"SINTERSTORE #{destkey} #{keys.join(' ')}\r\n"
360 write
"SUNION #{keys.join(' ')}\r\n"
361 Set
.new(get_response
)
364 def set_union_store(destkey
, *keys
)
365 write
"SUNIONSTORE #{destkey} #{keys.join(' ')}\r\n"
370 write
"SDIFF #{keys.join(' ')}\r\n"
371 Set
.new(get_response
)
374 def set_diff_store(destkey
, *keys
)
375 write
"SDIFFSTORE #{destkey} #{keys.join(' ')}\r\n"
379 def set_move(srckey
, destkey
, member
)
380 write
"SMOVE #{srckey} #{destkey} #{member.to_s.size}\r\n#{member}\r\n"
384 def sort(key
, opts
={})
386 cmd
<< " BY #{opts[:by]}" if opts
[:by]
387 cmd
<< " GET #{opts[:get]}" if opts
[:get]
388 cmd
<< " INCR #{opts[:incr]}" if opts
[:incr]
389 cmd
<< " DEL #{opts[:del]}" if opts
[:del]
390 cmd
<< " DECR #{opts[:decr]}" if opts
[:decr]
391 cmd
<< " #{opts[:order]}" if opts
[:order]
392 cmd
<< " LIMIT #{opts[:limit].join(' ')}" if opts
[:limit]
400 puts
"mb res is #{res.inspect}" if $debug
402 Integer(res
).times
do
404 puts
"curren vf is #{vf.inspect}" if $debug
406 puts
"current list is #{list.inspect}" if $debug
414 raise RedisError
if (r
== "\r" || r
== "\n")
426 def set(key
, val
, expiry
=nil)
427 write("SET #{key} #{val.to_s.size}\r\n#{val}\r\n")
428 s
= get_response
== OK
429 return expire(key
, expiry
) if s
&& expiry
433 def expire(key
, expiry
=nil)
434 write("EXPIRE #{key} #{expiry}\r\n")
438 def set_unless_exists(key
, val
)
439 write
"SETNX #{key} #{val.to_s.size}\r\n#{val}\r\n"
443 def status_code_reply
446 if res
.index('-') == 0
447 raise RedisError
, res
460 raise RedisError
, e
.inspect
462 puts
"reply_type is #{rtype.inspect}" if $debug
473 raise RedisError
, single_line
475 raise RedisError
, "Unknown response.."
485 while buff
[-2..-1] !
= "\r\n"
488 puts
"single_line value is #{buff[0..-3].inspect}" if $debug
494 socket
= @server.socket
495 while res
= socket
.read(8096)
496 break if res
.size !
= 8096
498 #Timeout or server down
499 rescue Errno
::ECONNRESET, Errno
::EPIPE, Errno
::ECONNREFUSED => e
501 puts
"Client (#{server.inspect}) disconnected from server: #{e.inspect}\n" if $debug
503 rescue Timeout
::Error => e
504 #BTM - Ignore this error so we don't go into an endless loop
505 puts
"Client (#{server.inspect}) Timeout\n" if $debug
507 rescue NoMethodError
=> e
508 puts
"Client (#{server.inspect}) tryin server that is down: #{e.inspect}\n Dying!" if $debug
509 raise Errno::ECONNREFUSED
515 with_socket_management(@server) do |socket|
518 puts "read_proto is
#{x.inspect}\n\n" if $debug