]>
git.saurik.com Git - redis.git/blob - client-libraries/ruby/lib/redis.rb
3 require File
.join(File
.dirname(__FILE__
),'server')
6 class RedisError
< StandardError
8 class RedisRenameError
< StandardError
21 def initialize(opts={})
22 @opts = {:host => 'localhost
', :port => '6379', :db => 0}.merge(opts)
23 $debug = @opts[:debug]
25 @server = Server.new(@opts[:host], @opts[:port])
40 def with_socket_management(server, &block)
42 block.call(server.socket)
43 #Timeout or server down
44 rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNREFUSED => e
46 puts "Client (#{server.inspect}) disconnected from server: #{e.inspect}\n" if $debug
49 rescue NoMethodError => e
50 puts "Client (#{server.inspect}) tryin server that is down: #{e.inspect}\n Dying!" if $debug
51 raise Errno::ECONNREFUSED
57 with_socket_management(@server) do |socket|
58 trap("INT
") { puts "\nGot ^C! Dying!
"; exit }
60 puts "Now Monitoring
..."
75 write "SELECT
#{index}\r\n"
86 puts "Warning!
\nFlushing
*ALL
* databases!
\n5 Seconds to Hit ^C!
"
87 trap('INT') {quit; return false}
109 k,v = kv.split(':', 2)
110 k,v = k.chomp, v = v.chomp
120 puts "bulk_reply read value is
#{x.inspect}" if $debug
123 puts "error
in bulk_reply
#{e}" if $debug
129 with_socket_management(@server) do |socket|
130 puts "writing
: #{data}" if $debug
136 with_socket_management(@server) do |socket|
137 len = [0, len.to_i].max
138 res = socket.read(len + 2)
139 res
= res
.chomp
if res
144 def read(length
= read_proto
)
145 with_socket_management(@server) do |socket
|
146 res
= socket
.read(length
)
147 puts
"read is #{res.inspect}" if $debug
153 write
"KEYS #{glob}\r\n"
154 get_response
.split(' ')
157 def rename!
(oldkey
, newkey
)
158 write
"RENAME #{oldkey} #{newkey}\r\n"
162 def rename(oldkey
, newkey
)
163 write
"RENAMENX #{oldkey} #{newkey}\r\n"
166 raise RedisRenameError
, "source key: #{oldkey} does not exist"
168 raise RedisRenameError
, "target key: #{oldkey} already exists"
170 raise RedisRenameError
, "source and destination keys are the same"
177 write
"EXISTS #{key}\r\n"
182 write
"DEL #{key}\r\n"
191 write
"GET #{key}\r\n"
196 write
"MGET #{keys.join(' ')}\r\n"
200 def incr(key
, increment
=nil)
202 write
"INCRBY #{key} #{increment}\r\n"
204 write
"INCR #{key}\r\n"
209 def decr(key
, decrement
=nil)
211 write
"DECRBY #{key} #{decrement}\r\n"
213 write
"DECR #{key}\r\n"
219 write
"RANDOMKEY\r\n"
224 write
"LLEN #{key}\r\n"
225 case i
= get_response
227 raise RedisError
, "key: #{key} does not hold a list value"
234 write
"TYPE #{key}\r\n"
238 def push_tail(key
, string
)
239 write
"RPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
243 def push_head(key
, string
)
244 write
"LPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
249 write
"LPOP #{key}\r\n"
254 write
"RPOP #{key}\r\n"
258 def list_set(key
, index
, val
)
259 write
"LSET #{key} #{index} #{val.to_s.size}\r\n#{val}\r\n"
264 write
"LLEN #{key}\r\n"
265 case i
= get_response
267 raise RedisError
, "key: #{key} does not hold a list value"
273 def list_range(key
, start
, ending
)
274 write
"LRANGE #{key} #{start} #{ending}\r\n"
278 def list_trim(key
, start
, ending
)
279 write
"LTRIM #{key} #{start} #{ending}\r\n"
283 def list_index(key
, index
)
284 write
"LINDEX #{key} #{index}\r\n"
288 def list_rm(key
, count
, value
)
289 write
"LREM #{key} #{count} #{value.to_s.size}\r\n#{value}\r\n"
290 case num
= get_response
292 raise RedisError
, "key: #{key} does not exist"
294 raise RedisError
, "key: #{key} does not hold a list value"
300 def set_add(key
, member
)
301 write
"SADD #{key} #{member.to_s.size}\r\n#{member}\r\n"
308 raise RedisError
, "key: #{key} contains a non set value"
312 def set_delete(key
, member
)
313 write
"SREM #{key} #{member.to_s.size}\r\n#{member}\r\n"
320 raise RedisError
, "key: #{key} contains a non set value"
325 write
"SCARD #{key}\r\n"
326 case i
= get_response
328 raise RedisError
, "key: #{key} contains a non set value"
334 def set_member
?(key
, member
)
335 write
"SISMEMBER #{key} #{member.to_s.size}\r\n#{member}\r\n"
342 raise RedisError
, "key: #{key} contains a non set value"
347 write
"SMEMBERS #{key}\r\n"
348 Set
.new(get_response
)
351 def set_intersect(*keys
)
352 write
"SINTER #{keys.join(' ')}\r\n"
353 Set
.new(get_response
)
356 def set_inter_store(destkey
, *keys
)
357 write
"SINTERSTORE #{destkey} #{keys.join(' ')}\r\n"
361 def sort(key
, opts
={})
363 cmd
<< " BY #{opts[:by]}" if opts
[:by]
364 cmd
<< " GET #{opts[:get]}" if opts
[:get]
365 cmd
<< " INCR #{opts[:incr]}" if opts
[:incr]
366 cmd
<< " DEL #{opts[:del]}" if opts
[:del]
367 cmd
<< " DECR #{opts[:decr]}" if opts
[:decr]
368 cmd
<< " #{opts[:order]}" if opts
[:order]
369 cmd
<< " LIMIT #{opts[:limit].join(' ')}" if opts
[:limit]
377 puts
"mb res is #{res.inspect}" if $debug
379 Integer(res
).times
do
381 puts
"curren vf is #{vf.inspect}" if $debug
383 puts
"current list is #{list.inspect}" if $debug
391 raise RedisError
if (r
== "\r" || r
== "\n")
403 def set(key
, val
, expiry
=nil)
404 write("SET #{key} #{val.to_s.size}\r\n#{val}\r\n")
405 s
= get_response
== OK
406 return expire(key
, expiry
) if s
&& expiry
410 def expire(key
, expiry
=nil)
411 write("EXPIRE #{key} #{expiry}\r\n")
415 def set_unless_exists(key
, val
)
416 write
"SETNX #{key} #{val.to_s.size}\r\n#{val}\r\n"
420 def status_code_reply
423 if res
.index('-') == 0
424 raise RedisError
, res
437 raise RedisError
, e
.inspect
439 puts
"reply_type is #{rtype.inspect}" if $debug
450 raise RedisError
, single_line
452 raise RedisError
, "Unknown response.."
462 while buff
[-2..-1] !
= "\r\n"
465 puts
"single_line value is #{buff[0..-3].inspect}" if $debug
470 with_socket_management(@server) do |socket
|
473 puts
"read_proto is #{x.inspect}\n\n" if $debug