From: antirez Date: Wed, 8 Apr 2009 11:20:03 +0000 (+0200) Subject: Ruby client updated X-Git-Url: https://git.saurik.com/redis.git/commitdiff_plain/9495122b1853a97b3f15f0ddbf69d69698f9c82b?ds=sidebyside;hp=928394cd12eb75c35eac72952b29e4dc9e75d154 Ruby client updated --- diff --git a/client-libraries/README b/client-libraries/README index 6b824d1f..e4a265ab 100644 --- a/client-libraries/README +++ b/client-libraries/README @@ -15,6 +15,7 @@ it's possible to grab the most recent version: Ruby lib source code: http://github.com/ezmobius/redis-rb/tree/master +git://github.com/ezmobius/redis-rb.git Erlang lib source code: http://bitbucket.org/adroll/erldis/ diff --git a/client-libraries/ruby/lib/better_timeout.rb b/client-libraries/ruby/lib/better_timeout.rb deleted file mode 100644 index 818909a8..00000000 --- a/client-libraries/ruby/lib/better_timeout.rb +++ /dev/null @@ -1,191 +0,0 @@ -#-- -# = timeout.rb -# -# execution timeout -# -# = Copyright -# -# Copyright - (C) 2008 Evan Phoenix -# Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc. -# Copyright:: (C) 2000 Information-technology Promotion Agency, Japan -# -#++ -# -# = Description -# -# A way of performing a potentially long-running operation in a thread, and -# terminating it's execution if it hasn't finished within fixed amount of -# time. -# -# Previous versions of timeout didn't use a module for namespace. This version -# provides both Timeout.timeout, and a backwards-compatible #timeout. -# -# = Synopsis -# -# require 'timeout' -# status = Timeout::timeout(5) { -# # Something that should be interrupted if it takes too much time... -# } -# - -require 'thread' - -module Timeout - - ## - # Raised by Timeout#timeout when the block times out. - - class Error b.left } - end - - slept_for = sleep(min.left) - - @mutex.synchronize do - @requests.delete_if do |r| - if r.elapsed(slept_for) - r.cancel - true - else - false - end - end - end - - end - end - - req = TimeoutRequest.new(time, Thread.current, exc) - - @mutex.synchronize do - @requests << req - end - - @controller.run - - return req - end - - ## - # Executes the method's block. If the block execution terminates before +sec+ - # seconds has passed, it returns true. If not, it terminates the execution - # and raises +exception+ (which defaults to Timeout::Error). - # - # Note that this is both a method of module Timeout, so you can 'include - # Timeout' into your classes so they have a #timeout method, as well as a - # module method, so you can call it directly as Timeout.timeout(). - - def timeout(sec, exception=Error) - return yield if sec == nil or sec.zero? - raise ThreadError, "timeout within critical session" if Thread.critical - - req = Timeout.add_timeout sec, exception - - begin - yield sec - ensure - req.abort - end - end - - module_function :timeout - -end - -## -# Identical to: -# -# Timeout::timeout(n, e, &block). -# -# Defined for backwards compatibility with earlier versions of timeout.rb, see -# Timeout#timeout. - -def timeout(n, e=Timeout::Error, &block) # :nodoc: - Timeout::timeout(n, e, &block) -end - -## -# Another name for Timeout::Error, defined for backwards compatibility with -# earlier versions of timeout.rb. - -class Object - remove_const(:TimeoutError) if const_defined?(:TimeoutError) -end -TimeoutError = Timeout::Error # :nodoc: - -if __FILE__ == $0 - p timeout(5) { - 45 - } - p timeout(5, TimeoutError) { - 45 - } - p timeout(nil) { - 54 - } - p timeout(0) { - 54 - } - p timeout(5) { - loop { - p 10 - sleep 1 - } - } -end - 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) diff --git a/client-libraries/ruby/lib/server.rb b/client-libraries/ruby/lib/server.rb index a52fbcbf..789ef152 100644 --- a/client-libraries/ruby/lib/server.rb +++ b/client-libraries/ruby/lib/server.rb @@ -88,12 +88,9 @@ class Server end def connect_to(host, port, timeout=nil) - addrs = Socket.getaddrinfo('localhost', nil) + addrs = Socket.getaddrinfo(host, nil) addr = addrs.detect { |ad| ad[0] == 'AF_INET' } sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) - #addr = Socket.getaddrinfo(host, nil) - #sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0) - if timeout secs = Integer(timeout) usecs = Integer((timeout - secs) * 1_000_000) @@ -101,7 +98,7 @@ class Server sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval end - sock.connect(Socket.pack_sockaddr_in('6379', addr[3])) + sock.connect(Socket.pack_sockaddr_in(port, addr[3])) sock end diff --git a/client-libraries/ruby/spec/redis_spec.rb b/client-libraries/ruby/spec/redis_spec.rb index 8fc471dd..2cf02f18 100644 --- a/client-libraries/ruby/spec/redis_spec.rb +++ b/client-libraries/ruby/spec/redis_spec.rb @@ -12,14 +12,20 @@ class Foo end describe "redis" do - before(:each) do + before(:all) do @r = Redis.new @r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data + end + + before(:each) do @r['foo'] = 'bar' + end + + after(:each) do + @r.keys('*').each {|k| @r.delete k} end - - after do - @r.keys('*').each {|k| @r.delete k } + + after(:all) do @r.quit end @@ -33,6 +39,13 @@ describe "redis" do @r['foo'].should == 'nik' end + it "should be able to SET a key with an expiry" do + @r.set('foo', 'bar', 1) + @r['foo'].should == 'bar' + sleep 2 + @r['foo'].should == nil + end + it "should be able to SETNX(set_unless_exists)" do @r['foo'] = 'nik' @r['foo'].should == 'nik' @@ -53,8 +66,7 @@ describe "redis" do @r.incr('counter').should == 2 @r.incr('counter').should == 3 @r.decr('counter').should == 2 - @r.decr('counter').should == 1 - @r.decr('counter').should == 0 + @r.decr('counter', 2).should == 0 end # it "should be able to RANDKEY(return a random key)" do @@ -78,6 +90,14 @@ describe "redis" do @r['bar'].should == 'ohai' end # + it "should be able to EXPIRE a key" do + @r['foo'] = 'bar' + @r.expire('foo', 1) + @r['foo'].should == "bar" + sleep 2 + @r['foo'].should == nil + end + # it "should be able to EXISTS(check if key exists)" do @r['foo'] = 'nik' @r.key?('foo').should be_true diff --git a/client-libraries/ruby/tasks/redis.tasks.rb b/client-libraries/ruby/tasks/redis.tasks.rb index b22c70e9..657d248d 100644 --- a/client-libraries/ruby/tasks/redis.tasks.rb +++ b/client-libraries/ruby/tasks/redis.tasks.rb @@ -31,7 +31,7 @@ class RedisRunner end def self.stop - sh 'killall redis-server' + sh 'echo "SHUTDOWN" | nc localhost 6379' end end