X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/575acbf1f72b783bba13aee3ddf3bb1110f114fc..9495122b1853a97b3f15f0ddbf69d69698f9c82b:/client-libraries/ruby/lib/redis.rb diff --git a/client-libraries/ruby/lib/redis.rb b/client-libraries/ruby/lib/redis.rb index b68e9766..96b8244e 100644 --- a/client-libraries/ruby/lib/redis.rb +++ b/client-libraries/ruby/lib/redis.rb @@ -19,8 +19,9 @@ class Redis def initialize(opts={}) - @opts = {:host => 'localhost', :port => '6379'}.merge(opts) + @opts = {:host => 'localhost', :port => '6379', :db => 0}.merge(opts) $debug = @opts[:debug] + @db = @opts[:db] @server = Server.new(@opts[:host], @opts[:port]) end @@ -47,7 +48,21 @@ class Redis #Server down rescue NoMethodError => e puts "Client (#{server.inspect}) tryin server that is down: #{e.inspect}\n Dying!" if $debug - exit + raise Errno::ECONNREFUSED + #exit + end + end + + def monitor + with_socket_management(@server) do |socket| + trap("INT") { puts "\nGot ^C! Dying!"; exit } + write "MONITOR\r\n" + puts "Now Monitoring..." + socket.read(12) + loop do + x = socket.gets + puts x unless x.nil? + end end end @@ -56,6 +71,7 @@ class Redis end def select_db(index) + @db = index write "SELECT #{index}\r\n" get_response end @@ -65,6 +81,16 @@ class Redis get_response == OK end + def flush_all + ensure_retry do + puts "Warning!\nFlushing *ALL* databases!\n5 Seconds to Hit ^C!" + trap('INT') {quit; return false} + sleep 5 + write "FLUSHALL\r\n" + get_response == OK + end + end + def last_save write "LASTSAVE\r\n" get_response.to_i @@ -79,7 +105,7 @@ class Redis info = {} write("INFO\r\n") x = get_response - x.each_line do |kv| + x.each do |kv| k,v = kv.split(':', 2) k,v = k.chomp, v = v.chomp info[k.to_sym] = v @@ -182,7 +208,7 @@ class Redis def decr(key, decrement=nil) if decrement - write "DECRRBY #{key} #{decrement}\r\n" + write "DECRBY #{key} #{decrement}\r\n" else write "DECR #{key}\r\n" end @@ -376,7 +402,14 @@ class Redis def set(key, val, expiry=nil) write("SET #{key} #{val.to_s.size}\r\n#{val}\r\n") - get_response == OK + s = get_response == OK + return expire(key, expiry) if s && expiry + s + end + + def expire(key, expiry=nil) + write("EXPIRE #{key} #{expiry}\r\n") + get_response == 1 end def set_unless_exists(key, val)