X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/c9a111acf47cb5bb2138d1f699253f87d68e53e8..43e5ccdf57e2144a8a43c04c5a64e91a155a78fb:/client-libraries/ruby/lib/redis.rb diff --git a/client-libraries/ruby/lib/redis.rb b/client-libraries/ruby/lib/redis.rb index 4138e9c5..bbe5343e 100644 --- a/client-libraries/ruby/lib/redis.rb +++ b/client-libraries/ruby/lib/redis.rb @@ -36,7 +36,7 @@ class Redis "smove" => true } - BOOLEAN_PROCESSOR = lambda{|r| r == 0 ? false : r} + BOOLEAN_PROCESSOR = lambda{|r| r == 1 } REPLY_PROCESSOR = { "exists" => BOOLEAN_PROCESSOR, @@ -95,21 +95,34 @@ class Redis "type?" => "type" } + DISABLED_COMMANDS = { + "monitor" => true, + "sync" => true + } + def initialize(options = {}) @host = options[:host] || '127.0.0.1' @port = (options[:port] || 6379).to_i @db = (options[:db] || 0).to_i @timeout = (options[:timeout] || 5).to_i - $debug = options[:debug] + @password = options[:password] + @logger = options[:logger] + + @logger.info { self.to_s } if @logger connect_to_server end def to_s - "Redis Client connected to #{@host}:#{@port} against DB #{@db}" + "Redis Client connected to #{server} against DB #{@db}" + end + + def server + "#{@host}:#{@port}" end def connect_to_server @sock = connect_to(@host, @port, @timeout == 0 ? nil : @timeout) + call_command(["auth",@password]) if @password call_command(["select",@db]) unless @db == 0 end @@ -147,15 +160,18 @@ class Redis end def call_command(argv) - puts argv.inspect if $debug + @logger.debug { argv.inspect } if @logger + # this wrapper to raw_call_command handle reconnection on socket # error. We try to reconnect just one time, otherwise let the error # araise. connect_to_server if !@sock + begin raw_call_command(argv.dup) rescue Errno::ECONNRESET, Errno::EPIPE @sock.close + @sock = nil connect_to_server raw_call_command(argv.dup) end @@ -176,12 +192,13 @@ class Redis bulk = nil argv[0] = argv[0].to_s.downcase argv[0] = ALIASES[argv[0]] if ALIASES[argv[0]] + raise "#{argv[0]} command is disabled" if DISABLED_COMMANDS[argv[0]] if BULK_COMMANDS[argv[0]] and argv.length > 1 bulk = argv[-1].to_s - argv[-1] = bulk.length + argv[-1] = bulk.respond_to?(:bytesize) ? bulk.bytesize : bulk.size end - command << argv.join(' ') + "\r\n" - command << bulk + "\r\n" if bulk + command << "#{argv.join(' ')}\r\n" + command << "#{bulk}\r\n" if bulk end @sock.write(command) @@ -199,7 +216,7 @@ class Redis end def [](key) - get(key) + self.get(key) end def []=(key,value) @@ -213,8 +230,8 @@ class Redis end def sort(key, options = {}) - cmd = [] - cmd << "SORT #{key}" + cmd = ["SORT"] + cmd << key cmd << "BY #{options[:by]}" if options[:by] cmd << "GET #{[options[:get]].flatten * ' GET '}" if options[:get] cmd << "#{options[:order]}" if options[:order] @@ -230,6 +247,15 @@ class Redis call_command(decrement ? ["decrby",key,decrement] : ["decr",key]) end + # Similar to memcache.rb's #get_multi, returns a hash mapping + # keys to values. + def mapped_mget(*keys) + mget(*keys).inject({}) do |hash, value| + key = keys.shift + value.nil? ? hash : hash.merge(key => value) + end + end + # Ruby defines a now deprecated type method so we need to override it here # since it will never hit method_missing def type(key)