lib/redis.rb in redis-3.0.4 vs lib/redis.rb in redis-3.0.5

- old
+ new

@@ -25,11 +25,11 @@ end include MonitorMixin def initialize(options = {}) - @client = Client.new(options) + @original_client = @client = Client.new(options) super() # Monitor#initialize end def synchronize @@ -46,10 +46,15 @@ # Run code without the client reconnecting def without_reconnect(&blk) with_reconnect(false, &blk) end + # Test whether or not the client is connected + def connected? + @original_client.connected? + end + # Authenticate to the server. # # @param [String] password must match the password specified in the # `requirepass` directive in the configuration file # @return [String] `OK` @@ -379,10 +384,30 @@ synchronize do |client| client.call([:restore, key, ttl, serialized_value]) end end + # Transfer a key from the connected instance to another instance. + # + # @param [String] key + # @param [Hash] options + # - `:host => String`: host of instance to migrate to + # - `:port => Integer`: port of instance to migrate to + # - `:db => Integer`: database to migrate to (default: same as source) + # - `:timeout => Integer`: timeout (default: same as connection timeout) + # @return [String] `"OK"` + def migrate(key, options) + host = options[:host] || raise(RuntimeError, ":host not specified") + port = options[:port] || raise(RuntimeError, ":port not specified") + db = (options[:db] || client.db).to_i + timeout = (options[:timeout] || client.timeout).to_i + + synchronize do |client| + client.call([:migrate, host, port, key, db, timeout]) + end + end + # Delete one or more keys. # # @param [String, Array<String>] keys # @return [Fixnum] number of keys that were deleted def del(*keys) @@ -428,11 +453,11 @@ # # => false # redis.select 2 # # => "OK" # redis.exists "foo" # # => true - # resis.get "foo" + # redis.get "foo" # # => "bar" # # @param [String] key # @param [Fixnum] db # @return [Boolean] whether the key was moved or not @@ -622,14 +647,37 @@ # Set the string value of a key. # # @param [String] key # @param [String] value - # @return `"OK"` - def set(key, value) + # @param [Hash] options + # - `:ex => Fixnum`: Set the specified expire time, in seconds. + # - `:px => Fixnum`: Set the specified expire time, in milliseconds. + # - `:nx => true`: Only set the key if it does not already exist. + # - `:xx => true`: Only set the key if it already exist. + # @return [String, Boolean] `"OK"` or true, false if `:nx => true` or `:xx => true` + def set(key, value, options = {}) + args = [] + + ex = options[:ex] + args.concat(["EX", ex]) if ex + + px = options[:px] + args.concat(["PX", px]) if px + + nx = options[:nx] + args.concat(["NX"]) if nx + + xx = options[:xx] + args.concat(["XX"]) if xx + synchronize do |client| - client.call([:set, key, value.to_s]) + if nx || xx + client.call([:set, key, value.to_s] + args, &_boolify_set) + else + client.call([:set, key, value.to_s] + args) + end end end alias :[]= :set @@ -2229,11 +2277,11 @@ end end def inspect synchronize do |client| - "#<Redis client v#{Redis::VERSION} for #{client.id}>" + "#<Redis client v#{Redis::VERSION} for #{@original_client.id}>" end end def method_missing(command, *args) synchronize do |client| @@ -2247,9 +2295,19 @@ # where the method call will return nil. Propagate the nil instead of falsely # returning false. def _boolify lambda { |value| value == 1 if value + } + end + + def _boolify_set + lambda { |value| + if value && "OK" == value + true + else + false + end } end def _hashify lambda { |array|