lib/redis.rb in redis-3.0.1 vs lib/redis.rb in redis-3.0.2

- old
+ new

@@ -53,51 +53,51 @@ # @param [String] password must match the password specified in the # `requirepass` directive in the configuration file # @return [String] `OK` def auth(password) synchronize do |client| - client.call [:auth, password] + client.call([:auth, password]) end end # Change the selected database for the current connection. # # @param [Fixnum] db zero-based index of the DB to use (0 to 15) # @return [String] `OK` def select(db) synchronize do |client| client.db = db - client.call [:select, db] + client.call([:select, db]) end end # Ping the server. # # @return [String] `PONG` def ping synchronize do |client| - client.call [:ping] + client.call([:ping]) end end # Echo the given string. # # @param [String] value # @return [String] def echo(value) synchronize do |client| - client.call [:echo, value] + client.call([:echo, value]) end end # Close the connection. # # @return [String] `OK` def quit synchronize do |client| begin - client.call [:quit] + client.call([:quit]) rescue ConnectionError ensure client.disconnect end end @@ -106,33 +106,33 @@ # Asynchronously rewrite the append-only file. # # @return [String] `OK` def bgrewriteaof synchronize do |client| - client.call [:bgrewriteaof] + client.call([:bgrewriteaof]) end end # Asynchronously save the dataset to disk. # # @return [String] `OK` def bgsave synchronize do |client| - client.call [:bgsave] + client.call([:bgsave]) end end # Get or set server configuration parameters. # # @param [String] action e.g. `get`, `set`, `resetstat` # @return [String, Hash] string reply, or hash when retrieving more than one # property with `CONFIG GET` def config(action, *args) synchronize do |client| - client.call [:config, action, *args] do |reply| + client.call([:config, action] + args) do |reply| if reply.kind_of?(Array) && action == :get - Hash[*reply] + Hash[reply.each_slice(2).to_a] else reply end end end @@ -141,54 +141,55 @@ # Return the number of keys in the selected database. # # @return [Fixnum] def dbsize synchronize do |client| - client.call [:dbsize] + client.call([:dbsize]) end end def debug(*args) synchronize do |client| - client.call [:debug, *args] + client.call([:debug] + args) end end # Remove all keys from all databases. # # @return [String] `OK` def flushall synchronize do |client| - client.call [:flushall] + client.call([:flushall]) end end # Remove all keys from the current database. # # @return [String] `OK` def flushdb synchronize do |client| - client.call [:flushdb] + client.call([:flushdb]) end end # Get information and statistics about the server. # # @param [String, Symbol] cmd e.g. "commandstats" # @return [Hash<String, String>] def info(cmd = nil) synchronize do |client| - client.call [:info, cmd].compact do |reply| + client.call([:info, cmd].compact) do |reply| if reply.kind_of?(String) reply = Hash[reply.split("\r\n").map do |line| line.split(":", 2) unless line =~ /^(#|$)/ end] if cmd && cmd.to_s == "commandstats" # Extract nested hashes for INFO COMMANDSTATS reply = Hash[reply.map do |k, v| - [k[/^cmdstat_(.*)$/, 1], Hash[*v.split(/,|=/)]] + v = v.split(",").map { |e| e.split("=") } + [k[/^cmdstat_(.*)$/, 1], Hash[v]] end] end end reply @@ -199,11 +200,11 @@ # Get the UNIX time stamp of the last successful save to disk. # # @return [Fixnum] def lastsave synchronize do |client| - client.call [:lastsave] + client.call([:lastsave]) end end # Listen for all requests received by the server in real time. # @@ -220,20 +221,20 @@ # Synchronously save the dataset to disk. # # @return [String] def save synchronize do |client| - client.call [:save] + client.call([:save]) end end # Synchronously save the dataset to disk and then shut down the server. def shutdown synchronize do |client| client.with_reconnect(false) do begin - client.call [:shutdown] + client.call([:shutdown]) rescue ConnectionError # This means Redis has probably exited. nil end end @@ -241,11 +242,11 @@ end # Make the server a slave of another instance, or promote it as master. def slaveof(host, port) synchronize do |client| - client.call [:slaveof, host, port] + client.call([:slaveof, host, port]) end end # Interact with the slowlog (get, len, reset) # @@ -261,11 +262,11 @@ end # Internal command used for replication. def sync synchronize do |client| - client.call [:sync] + client.call([:sync]) end end # Return the server time. # @@ -274,11 +275,11 @@ # # @return [Array<Fixnum>] tuple of seconds since UNIX epoch and # microseconds in the current second def time synchronize do |client| - client.call [:time] do |reply| + client.call([:time]) do |reply| reply.map(&:to_i) if reply end end end @@ -286,107 +287,107 @@ # # @param [String] key # @return [Boolean] whether the timeout was removed or not def persist(key) synchronize do |client| - client.call [:persist, key], &_boolify + client.call([:persist, key], &_boolify) end end # Set a key's time to live in seconds. # # @param [String] key # @param [Fixnum] seconds time to live # @return [Boolean] whether the timeout was set or not def expire(key, seconds) synchronize do |client| - client.call [:expire, key, seconds], &_boolify + client.call([:expire, key, seconds], &_boolify) end end # Set the expiration for a key as a UNIX timestamp. # # @param [String] key # @param [Fixnum] unix_time expiry time specified as a UNIX timestamp # @return [Boolean] whether the timeout was set or not def expireat(key, unix_time) synchronize do |client| - client.call [:expireat, key, unix_time], &_boolify + client.call([:expireat, key, unix_time], &_boolify) end end # Get the time to live (in seconds) for a key. # # @param [String] key # @return [Fixnum] remaining time to live in seconds, or -1 if the # key does not exist or does not have a timeout def ttl(key) synchronize do |client| - client.call [:ttl, key] + client.call([:ttl, key]) end end # Set a key's time to live in milliseconds. # # @param [String] key # @param [Fixnum] milliseconds time to live # @return [Boolean] whether the timeout was set or not def pexpire(key, milliseconds) synchronize do |client| - client.call [:pexpire, key, milliseconds], &_boolify + client.call([:pexpire, key, milliseconds], &_boolify) end end # Set the expiration for a key as number of milliseconds from UNIX Epoch. # # @param [String] key # @param [Fixnum] ms_unix_time expiry time specified as number of milliseconds from UNIX Epoch. # @return [Boolean] whether the timeout was set or not def pexpireat(key, ms_unix_time) synchronize do |client| - client.call [:pexpireat, key, ms_unix_time], &_boolify + client.call([:pexpireat, key, ms_unix_time], &_boolify) end end # Get the time to live (in milliseconds) for a key. # # @param [String] key # @return [Fixnum] remaining time to live in milliseconds, or -1 if the # key does not exist or does not have a timeout def pttl(key) synchronize do |client| - client.call [:pttl, key] + client.call([:pttl, key]) end end # Delete one or more keys. # # @param [String, Array<String>] keys # @return [Fixnum] number of keys that were deleted def del(*keys) synchronize do |client| - client.call [:del, *keys] + client.call([:del] + keys) end end # Determine if a key exists. # # @param [String] key # @return [Boolean] def exists(key) synchronize do |client| - client.call [:exists, key], &_boolify + client.call([:exists, key], &_boolify) end end # Find all keys matching the given pattern. # # @param [String] pattern # @return [Array<String>] def keys(pattern = "*") synchronize do |client| - client.call [:keys, pattern] do |reply| + client.call([:keys, pattern]) do |reply| if reply.kind_of?(String) reply.split(" ") else reply end @@ -413,48 +414,48 @@ # @param [String] key # @param [Fixnum] db # @return [Boolean] whether the key was moved or not def move(key, db) synchronize do |client| - client.call [:move, key, db], &_boolify + client.call([:move, key, db], &_boolify) end end def object(*args) synchronize do |client| - client.call [:object, *args] + client.call([:object] + args) end end # Return a random key from the keyspace. # # @return [String] def randomkey synchronize do |client| - client.call [:randomkey] + client.call([:randomkey]) end end # Rename a key. If the new key already exists it is overwritten. # # @param [String] old_name # @param [String] new_name # @return [String] `OK` def rename(old_name, new_name) synchronize do |client| - client.call [:rename, old_name, new_name] + client.call([:rename, old_name, new_name]) end end # Rename a key, only if the new key does not exist. # # @param [String] old_name # @param [String] new_name # @return [Boolean] whether the key was renamed or not def renamenx(old_name, new_name) synchronize do |client| - client.call [:renamenx, old_name, new_name], &_boolify + client.call([:renamenx, old_name, new_name], &_boolify) end end # Sort the elements in a list, set or sorted set. # @@ -483,26 +484,26 @@ # - when `:store` is specified, the number of elements in the stored result def sort(key, options = {}) args = [] by = options[:by] - args.concat ["BY", by] if by + args.concat(["BY", by]) if by limit = options[:limit] - args.concat ["LIMIT", *limit] if limit + args.concat(["LIMIT"] + limit) if limit get = Array(options[:get]) - args.concat ["GET"].product(get).flatten unless get.empty? + args.concat(["GET"].product(get).flatten) unless get.empty? order = options[:order] - args.concat order.split(" ") if order + args.concat(order.split(" ")) if order store = options[:store] - args.concat ["STORE", store] if store + args.concat(["STORE", store]) if store synchronize do |client| - client.call [:sort, key, *args] do |reply| + client.call([:sort, key] + args) do |reply| if get.size > 1 if reply reply.each_slice(get.size).to_a end else @@ -516,11 +517,11 @@ # # @param [String] key # @return [String] `string`, `list`, `set`, `zset`, `hash` or `none` def type(key) synchronize do |client| - client.call [:type, key] + client.call([:type, key]) end end # Decrement the integer value of a key by one. # @@ -530,11 +531,11 @@ # # @param [String] key # @return [Fixnum] value after decrementing it def decr(key) synchronize do |client| - client.call [:decr, key] + client.call([:decr, key]) end end # Decrement the integer value of a key by the given number. # @@ -545,11 +546,11 @@ # @param [String] key # @param [Fixnum] decrement # @return [Fixnum] value after decrementing it def decrby(key, decrement) synchronize do |client| - client.call [:decrby, key, decrement] + client.call([:decrby, key, decrement]) end end # Increment the integer value of a key by one. # @@ -559,11 +560,11 @@ # # @param [String] key # @return [Fixnum] value after incrementing it def incr(key) synchronize do |client| - client.call [:incr, key] + client.call([:incr, key]) end end # Increment the integer value of a key by the given integer number. # @@ -574,11 +575,11 @@ # @param [String] key # @param [Fixnum] increment # @return [Fixnum] value after incrementing it def incrby(key, increment) synchronize do |client| - client.call [:incrby, key, increment] + client.call([:incrby, key, increment]) end end # Increment the numeric value of a key by the given float number. # @@ -589,12 +590,12 @@ # @param [String] key # @param [Float] increment # @return [Float] value after incrementing it def incrbyfloat(key, increment) synchronize do |client| - client.call [:incrbyfloat, key, increment] do |reply| - Float(reply) if reply + client.call([:incrbyfloat, key, increment]) do |reply| + _floatify(reply) if reply end end end # Set the string value of a key. @@ -602,11 +603,11 @@ # @param [String] key # @param [String] value # @return `"OK"` def set(key, value) synchronize do |client| - client.call [:set, key, value] + client.call([:set, key, value]) end end alias :[]= :set @@ -616,11 +617,11 @@ # @param [Fixnum] ttl # @param [String] value # @return `"OK"` def setex(key, ttl, value) synchronize do |client| - client.call [:setex, key, ttl, value] + client.call([:setex, key, ttl, value]) end end # Set the time to live in milliseconds of a key. # @@ -628,22 +629,22 @@ # @param [Fixnum] ttl # @param [String] value # @return `"OK"` def psetex(key, ttl, value) synchronize do |client| - client.call [:psetex, key, ttl, value] + client.call([:psetex, key, ttl, value]) end end # Set the value of a key, only if the key does not exist. # # @param [String] key # @param [String] value # @return [Boolean] whether the key was set or not def setnx(key, value) synchronize do |client| - client.call [:setnx, key, value], &_boolify + client.call([:setnx, key, value], &_boolify) end end # Set one or more values. # @@ -655,11 +656,11 @@ # @return `"OK"` # # @see #mapped_mset def mset(*args) synchronize do |client| - client.call [:mset, *args] + client.call([:mset] + args) end end # Set one or more values. # @@ -670,11 +671,11 @@ # @param [Hash] hash keys mapping to values # @return `"OK"` # # @see #mset def mapped_mset(hash) - mset(*hash.to_a.flatten) + mset(hash.to_a.flatten) end # Set one or more values, only if none of the keys exist. # # @example @@ -685,11 +686,11 @@ # @return [Boolean] whether or not all values were set # # @see #mapped_msetnx def msetnx(*args) synchronize do |client| - client.call [:msetnx, *args], &_boolify + client.call([:msetnx] + args, &_boolify) end end # Set one or more values, only if none of the keys exist. # @@ -700,20 +701,20 @@ # @param [Hash] hash keys mapping to values # @return [Boolean] whether or not all values were set # # @see #msetnx def mapped_msetnx(hash) - msetnx(*hash.to_a.flatten) + msetnx(hash.to_a.flatten) end # Get the value of a key. # # @param [String] key # @return [String] def get(key) synchronize do |client| - client.call [:get, key] + client.call([:get, key]) end end alias :[] :get @@ -727,11 +728,11 @@ # @return [Array<String>] an array of values for the specified keys # # @see #mapped_mget def mget(*keys, &blk) synchronize do |client| - client.call [:mget, *keys], &blk + client.call([:mget] + keys, &blk) end end # Get the values of all the given keys. # @@ -744,15 +745,11 @@ # # @see #mget def mapped_mget(*keys) mget(*keys) do |reply| if reply.kind_of?(Array) - hash = Hash.new - keys.zip(reply).each do |field, value| - hash[field] = value - end - hash + Hash[keys.zip(reply)] else reply end end end @@ -763,11 +760,11 @@ # @param [Fixnum] offset byte offset # @param [String] value # @return [Fixnum] length of the string after it was modified def setrange(key, offset, value) synchronize do |client| - client.call [:setrange, key, offset, value] + client.call([:setrange, key, offset, value]) end end # Get a substring of the string stored at a key. # @@ -776,11 +773,11 @@ # @param [Fixnum] stop zero-based end offset. Use -1 for representing # the end of the string # @return [Fixnum] `0` or `1` def getrange(key, start, stop) synchronize do |client| - client.call [:getrange, key, start, stop] + client.call([:getrange, key, start, stop]) end end # Sets or clears the bit at offset in the string value stored at key. # @@ -788,33 +785,33 @@ # @param [Fixnum] offset bit offset # @param [Fixnum] value bit value `0` or `1` # @return [Fixnum] the original bit value stored at `offset` def setbit(key, offset, value) synchronize do |client| - client.call [:setbit, key, offset, value] + client.call([:setbit, key, offset, value]) end end # Returns the bit value at offset in the string value stored at key. # # @param [String] key # @param [Fixnum] offset bit offset # @return [Fixnum] `0` or `1` def getbit(key, offset) synchronize do |client| - client.call [:getbit, key, offset] + client.call([:getbit, key, offset]) end end # Append a value to a key. # # @param [String] key # @param [String] value value to append # @return [Fixnum] length of the string after appending def append(key, value) synchronize do |client| - client.call [:append, key, value] + client.call([:append, key, value]) end end # Set the string value of a key and return its old value. # @@ -822,107 +819,107 @@ # @param [String] value value to replace the current value with # @return [String] the old value stored in the key, or `nil` if the key # did not exist def getset(key, value) synchronize do |client| - client.call [:getset, key, value] + client.call([:getset, key, value]) end end # Get the length of the value stored in a key. # # @param [String] key # @return [Fixnum] the length of the value stored in the key, or 0 # if the key does not exist def strlen(key) synchronize do |client| - client.call [:strlen, key] + client.call([:strlen, key]) end end # Get the length of a list. # # @param [String] key # @return [Fixnum] def llen(key) synchronize do |client| - client.call [:llen, key] + client.call([:llen, key]) end end # Prepend one or more values to a list, creating the list if it doesn't exist # # @param [String] key # @param [String] value # @return [Fixnum] the length of the list after the push operation def lpush(key, value) synchronize do |client| - client.call [:lpush, key, value] + client.call([:lpush, key, value]) end end # Prepend a value to a list, only if the list exists. # # @param [String] key # @param [String] value # @return [Fixnum] the length of the list after the push operation def lpushx(key, value) synchronize do |client| - client.call [:lpushx, key, value] + client.call([:lpushx, key, value]) end end # Append one or more values to a list, creating the list if it doesn't exist # # @param [String] key # @param [String] value # @return [Fixnum] the length of the list after the push operation def rpush(key, value) synchronize do |client| - client.call [:rpush, key, value] + client.call([:rpush, key, value]) end end # Append a value to a list, only if the list exists. # # @param [String] key # @param [String] value # @return [Fixnum] the length of the list after the push operation def rpushx(key, value) synchronize do |client| - client.call [:rpushx, key, value] + client.call([:rpushx, key, value]) end end # Remove and get the first element in a list. # # @param [String] key # @return [String] def lpop(key) synchronize do |client| - client.call [:lpop, key] + client.call([:lpop, key]) end end # Remove and get the last element in a list. # # @param [String] key # @return [String] def rpop(key) synchronize do |client| - client.call [:rpop, key] + client.call([:rpop, key]) end end # Remove the last element in a list, append it to another list and return it. # # @param [String] source source key # @param [String] destination destination key # @return [nil, String] the element, or nil when the source key does not exist def rpoplpush(source, destination) synchronize do |client| - client.call [:rpoplpush, source, destination] + client.call([:rpoplpush, source, destination]) end end def _bpop(cmd, args) options = {} @@ -941,11 +938,11 @@ keys = args.flatten timeout = options[:timeout] || 0 synchronize do |client| - client.call_without_timeout [cmd, keys, timeout] + client.call_without_timeout([cmd, keys, timeout]) end end # Remove and get the first element in a list, or block until one is available. # @@ -1007,22 +1004,22 @@ end timeout = options[:timeout] || 0 synchronize do |client| - client.call_without_timeout [:brpoplpush, source, destination, timeout] + client.call_without_timeout([:brpoplpush, source, destination, timeout]) end end # Get an element from a list by its index. # # @param [String] key # @param [Fixnum] index # @return [String] def lindex(key, index) synchronize do |client| - client.call [:lindex, key, index] + client.call([:lindex, key, index]) end end # Insert an element before or after another element in a list. # @@ -1032,11 +1029,11 @@ # @param [String] value # @return [Fixnum] length of the list after the insert operation, or `-1` # when the element `pivot` was not found def linsert(key, where, pivot, value) synchronize do |client| - client.call [:linsert, key, where, pivot, value] + client.call([:linsert, key, where, pivot, value]) end end # Get a range of elements from a list. # @@ -1044,11 +1041,11 @@ # @param [Fixnum] start start index # @param [Fixnum] stop stop index # @return [Array<String>] def lrange(key, start, stop) synchronize do |client| - client.call [:lrange, key, start, stop] + client.call([:lrange, key, start, stop]) end end # Remove elements from a list. # @@ -1059,11 +1056,11 @@ # remove all occurrences of `value` from the list. # @param [String] value # @return [Fixnum] the number of removed elements def lrem(key, count, value) synchronize do |client| - client.call [:lrem, key, count, value] + client.call([:lrem, key, count, value]) end end # Set the value of an element in a list by its index. # @@ -1071,11 +1068,11 @@ # @param [Fixnum] index # @param [String] value # @return [String] `OK` def lset(key, index, value) synchronize do |client| - client.call [:lset, key, index, value] + client.call([:lset, key, index, value]) end end # Trim a list to the specified range. # @@ -1083,21 +1080,21 @@ # @param [Fixnum] start start index # @param [Fixnum] stop stop index # @return [String] `OK` def ltrim(key, start, stop) synchronize do |client| - client.call [:ltrim, key, start, stop] + client.call([:ltrim, key, start, stop]) end end # Get the number of members in a set. # # @param [String] key # @return [Fixnum] def scard(key) synchronize do |client| - client.call [:scard, key] + client.call([:scard, key]) end end # Add one or more members to a set. # @@ -1107,11 +1104,11 @@ # holding whether or not adding the member succeeded, or `Fixnum` when an # array of members is specified, holding the number of members that were # successfully added def sadd(key, member) synchronize do |client| - client.call [:sadd, key, member] do |reply| + client.call([:sadd, key, member]) do |reply| if member.is_a? Array # Variadic: return integer reply else # Single argument: return boolean @@ -1129,11 +1126,11 @@ # holding whether or not removing the member succeeded, or `Fixnum` when an # array of members is specified, holding the number of members that were # successfully removed def srem(key, member) synchronize do |client| - client.call [:srem, key, member] do |reply| + client.call([:srem, key, member]) do |reply| if member.is_a? Array # Variadic: return integer reply else # Single argument: return boolean @@ -1147,21 +1144,21 @@ # # @param [String] key # @return [String] def spop(key) synchronize do |client| - client.call [:spop, key] + client.call([:spop, key]) end end # Get a random member from a set. # # @param [String] key # @return [String] def srandmember(key) synchronize do |client| - client.call [:srandmember, key] + client.call([:srandmember, key]) end end # Move a member from one set to another. # @@ -1169,95 +1166,95 @@ # @param [String] destination destination key # @param [String] member member to move from `source` to `destination` # @return [Boolean] def smove(source, destination, member) synchronize do |client| - client.call [:smove, source, destination, member], &_boolify + client.call([:smove, source, destination, member], &_boolify) end end # Determine if a given value is a member of a set. # # @param [String] key # @param [String] member # @return [Boolean] def sismember(key, member) synchronize do |client| - client.call [:sismember, key, member], &_boolify + client.call([:sismember, key, member], &_boolify) end end # Get all the members in a set. # # @param [String] key # @return [Array<String>] def smembers(key) synchronize do |client| - client.call [:smembers, key] + client.call([:smembers, key]) end end # Subtract multiple sets. # # @param [String, Array<String>] keys keys pointing to sets to subtract # @return [Array<String>] members in the difference def sdiff(*keys) synchronize do |client| - client.call [:sdiff, *keys] + client.call([:sdiff] + keys) end end # Subtract multiple sets and store the resulting set in a key. # # @param [String] destination destination key # @param [String, Array<String>] keys keys pointing to sets to subtract # @return [Fixnum] number of elements in the resulting set def sdiffstore(destination, *keys) synchronize do |client| - client.call [:sdiffstore, destination, *keys] + client.call([:sdiffstore, destination] + keys) end end # Intersect multiple sets. # # @param [String, Array<String>] keys keys pointing to sets to intersect # @return [Array<String>] members in the intersection def sinter(*keys) synchronize do |client| - client.call [:sinter, *keys] + client.call([:sinter] + keys) end end # Intersect multiple sets and store the resulting set in a key. # # @param [String] destination destination key # @param [String, Array<String>] keys keys pointing to sets to intersect # @return [Fixnum] number of elements in the resulting set def sinterstore(destination, *keys) synchronize do |client| - client.call [:sinterstore, destination, *keys] + client.call([:sinterstore, destination] + keys) end end # Add multiple sets. # # @param [String, Array<String>] keys keys pointing to sets to unify # @return [Array<String>] members in the union def sunion(*keys) synchronize do |client| - client.call [:sunion, *keys] + client.call([:sunion] + keys) end end # Add multiple sets and store the resulting set in a key. # # @param [String] destination destination key # @param [String, Array<String>] keys keys pointing to sets to unify # @return [Fixnum] number of elements in the resulting set def sunionstore(destination, *keys) synchronize do |client| - client.call [:sunionstore, destination, *keys] + client.call([:sunionstore, destination] + keys) end end # Get the number of members in a sorted set. # @@ -1267,11 +1264,11 @@ # # @param [String] key # @return [Fixnum] def zcard(key) synchronize do |client| - client.call [:zcard, key] + client.call([:zcard, key]) end end # Add one or more members to a sorted set, or update the score for members # that already exist. @@ -1293,14 +1290,14 @@ # pairs that were **added** to the sorted set def zadd(key, *args) synchronize do |client| if args.size == 1 && args[0].is_a?(Array) # Variadic: return integer - client.call [:zadd, key] + args[0] + client.call([:zadd, key] + args[0]) elsif args.size == 2 # Single pair: return boolean - client.call [:zadd, key, args[0], args[1]], &_boolify + client.call([:zadd, key, args[0], args[1]], &_boolify) else raise ArgumentError, "wrong number of arguments" end end end @@ -1315,12 +1312,12 @@ # @param [Float] increment # @param [String] member # @return [Float] score of the member after incrementing it def zincrby(key, increment, member) synchronize do |client| - client.call [:zincrby, key, increment, member] do |reply| - Float(reply) if reply + client.call([:zincrby, key, increment, member]) do |reply| + _floatify(reply) if reply end end end # Remove one or more members from a sorted set. @@ -1340,11 +1337,11 @@ # was removed from the sorted set # - `Fixnum` when an array of pairs is specified, holding the number of # members that were removed to the sorted set def zrem(key, member) synchronize do |client| - client.call [:zrem, key, member] do |reply| + client.call([:zrem, key, member]) do |reply| if member.is_a? Array # Variadic: return integer reply else # Single argument: return boolean @@ -1363,12 +1360,12 @@ # @param [String] key # @param [String] member # @return [Float] score of the member def zscore(key, member) synchronize do |client| - client.call [:zscore, key, member] do |reply| - Float(reply) if reply + client.call([:zscore, key, member]) do |reply| + _floatify(reply) if reply end end end # Return a range of members in a sorted set, by index. @@ -1394,15 +1391,15 @@ with_scores = options[:with_scores] || options[:withscores] args << "WITHSCORES" if with_scores synchronize do |client| - client.call [:zrange, key, start, stop, *args] do |reply| + client.call([:zrange, key, start, stop] + args) do |reply| if with_scores if reply reply.each_slice(2).map do |member, score| - [member, Float(score)] + [member, _floatify(score)] end end else reply end @@ -1426,15 +1423,15 @@ with_scores = options[:with_scores] || options[:withscores] args << "WITHSCORES" if with_scores synchronize do |client| - client.call [:zrevrange, key, start, stop, *args] do |reply| + client.call([:zrevrange, key, start, stop] + args) do |reply| if with_scores if reply reply.each_slice(2).map do |member, score| - [member, Float(score)] + [member, _floatify(score)] end end else reply end @@ -1447,11 +1444,11 @@ # @param [String] key # @param [String] member # @return [Fixnum] def zrank(key, member) synchronize do |client| - client.call [:zrank, key, member] + client.call([:zrank, key, member]) end end # Determine the index of a member in a sorted set, with scores ordered from # high to low. @@ -1459,11 +1456,11 @@ # @param [String] key # @param [String] member # @return [Fixnum] def zrevrank(key, member) synchronize do |client| - client.call [:zrevrank, key, member] + client.call([:zrevrank, key, member]) end end # Remove all members in a sorted set within the given indexes. # @@ -1478,11 +1475,11 @@ # @param [Fixnum] start start index # @param [Fixnum] stop stop index # @return [Fixnum] number of members that were removed def zremrangebyrank(key, start, stop) synchronize do |client| - client.call [:zremrangebyrank, key, start, stop] + client.call([:zremrangebyrank, key, start, stop]) end end # Return a range of members in a sorted set, by score. # @@ -1513,21 +1510,21 @@ # - when `:with_scores` is specified, an array with `[member, score]` pairs def zrangebyscore(key, min, max, options = {}) args = [] with_scores = options[:with_scores] || options[:withscores] - args.concat ["WITHSCORES"] if with_scores + args.concat(["WITHSCORES"]) if with_scores limit = options[:limit] - args.concat ["LIMIT", *limit] if limit + args.concat(["LIMIT"] + limit) if limit synchronize do |client| - client.call [:zrangebyscore, key, min, max, *args] do |reply| + client.call([:zrangebyscore, key, min, max] + args) do |reply| if with_scores if reply reply.each_slice(2).map do |member, score| - [member, Float(score)] + [member, _floatify(score)] end end else reply end @@ -1551,21 +1548,21 @@ # @see #zrangebyscore def zrevrangebyscore(key, max, min, options = {}) args = [] with_scores = options[:with_scores] || options[:withscores] - args.concat ["WITHSCORES"] if with_scores + args.concat(["WITHSCORES"]) if with_scores limit = options[:limit] - args.concat ["LIMIT", *limit] if limit + args.concat(["LIMIT"] + limit) if limit synchronize do |client| - client.call [:zrevrangebyscore, key, max, min, *args] do |reply| + client.call([:zrevrangebyscore, key, max, min] + args) do |reply| if with_scores if reply reply.each_slice(2).map do |member, score| - [member, Float(score)] + [member, _floatify(score)] end end else reply end @@ -1590,11 +1587,11 @@ # - inclusive maximum score is specified verbatim # - exclusive maximum score is specified by prefixing `(` # @return [Fixnum] number of members that were removed def zremrangebyscore(key, min, max) synchronize do |client| - client.call [:zremrangebyscore, key, min, max] + client.call([:zremrangebyscore, key, min, max]) end end # Count the members in a sorted set with scores within the given values. # @@ -1613,11 +1610,11 @@ # - inclusive maximum score is specified verbatim # - exclusive maximum score is specified by prefixing `(` # @return [Fixnum] number of members in within the specified range def zcount(key, min, max) synchronize do |client| - client.call [:zcount, key, min, max] + client.call([:zcount, key, min, max]) end end # Intersect multiple sorted sets and store the resulting sorted set in a new # key. @@ -1635,17 +1632,17 @@ # @return [Fixnum] number of elements in the resulting sorted set def zinterstore(destination, keys, options = {}) args = [] weights = options[:weights] - args.concat ["WEIGHTS", *weights] if weights + args.concat(["WEIGHTS"] + weights) if weights aggregate = options[:aggregate] - args.concat ["AGGREGATE", aggregate] if aggregate + args.concat(["AGGREGATE", aggregate]) if aggregate synchronize do |client| - client.call [:zinterstore, destination, keys.size, *(keys + args)] + client.call([:zinterstore, destination, keys.size] + keys + args) end end # Add multiple sorted sets and store the resulting sorted set in a new key. # @@ -1662,27 +1659,27 @@ # @return [Fixnum] number of elements in the resulting sorted set def zunionstore(destination, keys, options = {}) args = [] weights = options[:weights] - args.concat ["WEIGHTS", *weights] if weights + args.concat(["WEIGHTS"] + weights) if weights aggregate = options[:aggregate] - args.concat ["AGGREGATE", aggregate] if aggregate + args.concat(["AGGREGATE", aggregate]) if aggregate synchronize do |client| - client.call [:zunionstore, destination, keys.size, *(keys + args)] + client.call([:zunionstore, destination, keys.size] + keys + args) end end # Get the number of fields in a hash. # # @param [String] key # @return [Fixnum] number of fields in the hash def hlen(key) synchronize do |client| - client.call [:hlen, key] + client.call([:hlen, key]) end end # Set the string value of a hash field. # @@ -1690,11 +1687,11 @@ # @param [String] field # @param [String] value # @return [Boolean] whether or not the field was **added** to the hash def hset(key, field, value) synchronize do |client| - client.call [:hset, key, field, value], &_boolify + client.call([:hset, key, field, value], &_boolify) end end # Set the value of a hash field, only if the field does not exist. # @@ -1702,11 +1699,11 @@ # @param [String] field # @param [String] value # @return [Boolean] whether or not the field was **added** to the hash def hsetnx(key, field, value) synchronize do |client| - client.call [:hsetnx, key, field, value], &_boolify + client.call([:hsetnx, key, field, value], &_boolify) end end # Set one or more hash values. # @@ -1719,11 +1716,11 @@ # @return `"OK"` # # @see #mapped_hmset def hmset(key, *attrs) synchronize do |client| - client.call [:hmset, key, *attrs] + client.call([:hmset, key] + attrs) end end # Set one or more hash values. # @@ -1735,21 +1732,21 @@ # @param [Hash] hash fields mapping to values # @return `"OK"` # # @see #hmset def mapped_hmset(key, hash) - hmset(key, *hash.to_a.flatten) + hmset(key, hash.to_a.flatten) end # Get the value of a hash field. # # @param [String] key # @param [String] field # @return [String] def hget(key, field) synchronize do |client| - client.call [:hget, key, field] + client.call([:hget, key, field]) end end # Get the values of all the given hash fields. # @@ -1762,11 +1759,11 @@ # @return [Array<String>] an array of values for the specified fields # # @see #mapped_hmget def hmget(key, *fields, &blk) synchronize do |client| - client.call [:hmget, key, *fields], &blk + client.call([:hmget, key] + fields, &blk) end end # Get the values of all the given hash fields. # @@ -1780,15 +1777,11 @@ # # @see #hmget def mapped_hmget(key, *fields) hmget(key, *fields) do |reply| if reply.kind_of?(Array) - hash = Hash.new - fields.zip(reply).each do |field, value| - hash[field] = value - end - hash + Hash[fields.zip(reply)] else reply end end end @@ -1798,22 +1791,22 @@ # @param [String] key # @param [String, Array<String>] field # @return [Fixnum] the number of fields that were removed from the hash def hdel(key, field) synchronize do |client| - client.call [:hdel, key, field] + client.call([:hdel, key, field]) end end # Determine if a hash field exists. # # @param [String] key # @param [String] field # @return [Boolean] whether or not the field exists in the hash def hexists(key, field) synchronize do |client| - client.call [:hexists, key, field], &_boolify + client.call([:hexists, key, field], &_boolify) end end # Increment the integer value of a hash field by the given integer number. # @@ -1821,11 +1814,11 @@ # @param [String] field # @param [Fixnum] increment # @return [Fixnum] value of the field after incrementing it def hincrby(key, field, increment) synchronize do |client| - client.call [:hincrby, key, field, increment] + client.call([:hincrby, key, field, increment]) end end # Increment the numeric value of a hash field by the given float number. # @@ -1833,50 +1826,50 @@ # @param [String] field # @param [Float] increment # @return [Float] value of the field after incrementing it def hincrbyfloat(key, field, increment) synchronize do |client| - client.call [:hincrbyfloat, key, field, increment] do |reply| - Float(reply) if reply + client.call([:hincrbyfloat, key, field, increment]) do |reply| + _floatify(reply) if reply end end end # Get all the fields in a hash. # # @param [String] key # @return [Array<String>] def hkeys(key) synchronize do |client| - client.call [:hkeys, key] + client.call([:hkeys, key]) end end # Get all the values in a hash. # # @param [String] key # @return [Array<String>] def hvals(key) synchronize do |client| - client.call [:hvals, key] + client.call([:hvals, key]) end end # Get all the fields and values in a hash. # # @param [String] key # @return [Hash<String, String>] def hgetall(key) synchronize do |client| - client.call [:hgetall, key], &_hashify + client.call([:hgetall, key], &_hashify) end end # Post a message to a channel. def publish(channel, message) synchronize do |client| - client.call [:publish, channel, message] + client.call([:publish, channel, message]) end end def subscribed? synchronize do |client| @@ -1944,15 +1937,15 @@ # # @see #unwatch # @see #multi def watch(*keys) synchronize do |client| - client.call [:watch, *keys] + client.call([:watch] + keys) if block_given? begin - yield + yield(self) rescue ConnectionError raise rescue StandardError unwatch raise @@ -1967,11 +1960,11 @@ # # @see #watch # @see #multi def unwatch synchronize do |client| - client.call [:unwatch] + client.call([:unwatch]) end end def pipelined synchronize do |client| @@ -2016,11 +2009,11 @@ # @see #watch # @see #unwatch def multi synchronize do |client| if !block_given? - client.call [:multi] + client.call([:multi]) else begin pipeline = Pipeline::Multi.new original, @client = @client, pipeline yield(self) @@ -2042,11 +2035,11 @@ # # @see #multi # @see #discard def exec synchronize do |client| - client.call [:exec] + client.call([:exec]) end end # Discard all commands issued after MULTI. # @@ -2056,11 +2049,11 @@ # # @see #multi # @see #exec def discard synchronize do |client| - client.call [:discard] + client.call([:discard]) end end # Control remote script registry. # @@ -2091,11 +2084,11 @@ if subcommand == "exists" synchronize do |client| arg = args.first - client.call [:script, :exists, arg] do |reply| + client.call([:script, :exists, arg]) do |reply| reply = reply.map { |r| _boolify.call(r) } if arg.is_a?(Array) reply else @@ -2103,11 +2096,11 @@ end end end else synchronize do |client| - client.call [:script, subcommand] + args + client.call([:script, subcommand] + args) end end end def _eval(cmd, args) @@ -2117,11 +2110,11 @@ keys = args.shift || options[:keys] || [] argv = args.shift || options[:argv] || [] synchronize do |client| - client.call [cmd, script, keys.length] + keys + argv + client.call([cmd, script, keys.length] + keys + argv) end end # Evaluate Lua script. # @@ -2185,11 +2178,11 @@ end end def method_missing(command, *args) synchronize do |client| - client.call [command, *args] + client.call([command] + args) end end private @@ -2210,11 +2203,19 @@ end hash } end + def _floatify(str) + if (inf = str.match(/^(-)?inf/i)) + (inf[1] ? -1.0 : 1.0) / 0.0 + else + Float str + end + end + def _subscription(method, channels, block) - return @client.call [method, *channels] if subscribed? + return @client.call([method] + channels) if subscribed? begin original, @client = @client, SubscribedClient.new(@client) @client.send(method, *channels, &block) ensure