lib/redis.rb in winescout-redis-rb-0.0.3 vs lib/redis.rb in winescout-redis-rb-0.0.3.1

- old
+ new

@@ -17,12 +17,13 @@ attr_reader :server 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 def to_s "#{host}:#{port}" @@ -51,38 +52,47 @@ #exit end end def monitor - ensure_retry do - 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 + 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 + end + end def quit write "QUIT\r\n" end def select_db(index) + @db = index write "SELECT #{index}\r\n" get_response end def flush_db write "FLUSHDB\r\n" 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 end @@ -196,11 +206,11 @@ get_response end 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 get_response end @@ -390,10 +400,17 @@ end 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) write "SETNX #{key} #{val.to_s.size}\r\n#{val}\r\n" get_response == 1