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
#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
end
def select_db(index)
+ @db = index
write "SELECT #{index}\r\n"
get_response
end
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
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
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
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)