]> git.saurik.com Git - redis.git/blobdiff - client-libraries/ruby/lib/redis.rb
EXPIRE behaviour changed a bit, a negative TTL or an EXPIREAT with unix time in the...
[redis.git] / client-libraries / ruby / lib / redis.rb
index 4138e9c59bce4120fbc78c0fe4227e8ae42b3306..bbe5343ef22766e737bfad0d29e3c1e0fae35cbc 100644 (file)
@@ -36,7 +36,7 @@ class Redis
     "smove"     => true
   }
 
-  BOOLEAN_PROCESSOR = lambda{|r| r == 0 ? false : r}
+  BOOLEAN_PROCESSOR = lambda{|r| r == }
 
   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)