lib/redis.rb in redis-3.0.2 vs lib/redis.rb in redis-3.0.3
- old
+ new
@@ -179,11 +179,11 @@
synchronize do |client|
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]
+ end.compact]
if cmd && cmd.to_s == "commandstats"
# Extract nested hashes for INFO COMMANDSTATS
reply = Hash[reply.map do |k, v|
v = v.split(",").map { |e| e.split("=") }
@@ -357,10 +357,32 @@
synchronize do |client|
client.call([:pttl, key])
end
end
+ # Return a serialized version of the value stored at a key.
+ #
+ # @param [String] key
+ # @return [String] serialized_value
+ def dump(key)
+ synchronize do |client|
+ client.call([:dump, key])
+ end
+ end
+
+ # Create a key using the serialized value, previously obtained using DUMP.
+ #
+ # @param [String] key
+ # @param [String] ttl
+ # @param [String] serialized_value
+ # @return `"OK"`
+ def restore(key, ttl, serialized_value)
+ synchronize do |client|
+ client.call([:restore, key, ttl, serialized_value])
+ end
+ end
+
# Delete one or more keys.
#
# @param [String, Array<String>] keys
# @return [Fixnum] number of keys that were deleted
def del(*keys)
@@ -603,11 +625,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.to_s])
end
end
alias :[]= :set
@@ -617,11 +639,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.to_s])
end
end
# Set the time to live in milliseconds of a key.
#
@@ -629,22 +651,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.to_s])
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.to_s], &_boolify)
end
end
# Set one or more values.
#
@@ -760,11 +782,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.to_s])
end
end
# Get a substring of the string stored at a key.
#
@@ -811,19 +833,43 @@
synchronize do |client|
client.call([:append, key, value])
end
end
+ # Count the number of set bits in a range of the string value stored at key.
+ #
+ # @param [String] key
+ # @param [Fixnum] start start index
+ # @param [Fixnum] stop stop index
+ # @return [Fixnum] the number of bits set to 1
+ def bitcount(key, start = 0, stop = -1)
+ synchronize do |client|
+ client.call([:bitcount, key, start, stop])
+ end
+ end
+
+ # Perform a bitwise operation between strings and store the resulting string in a key.
+ #
+ # @param [String] operation e.g. `and`, `or`, `xor`, `not`
+ # @param [String] destkey destination key
+ # @param [String, Array<String>] keys one or more source keys to perform `operation`
+ # @return [Fixnum] the length of the string stored in `destkey`
+ def bitop(operation, destkey, *keys)
+ synchronize do |client|
+ client.call([:bitop, operation, destkey] + keys)
+ end
+ end
+
# Set the string value of a key and return its old value.
#
# @param [String] key
# @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.to_s])
end
end
# Get the length of the value stored in a key.
#
@@ -847,11 +893,11 @@
end
# Prepend one or more values to a list, creating the list if it doesn't exist
#
# @param [String] key
- # @param [String] value
+ # @param [String, Array] string value, or array of string values to push
# @return [Fixnum] the length of the list after the push operation
def lpush(key, value)
synchronize do |client|
client.call([:lpush, key, value])
end
@@ -938,11 +984,13 @@
keys = args.flatten
timeout = options[:timeout] || 0
synchronize do |client|
- client.call_without_timeout([cmd, keys, timeout])
+ command = [cmd, keys, timeout]
+ timeout += client.timeout if timeout > 0
+ client.call_with_timeout(command, timeout)
end
end
# Remove and get the first element in a list, or block until one is available.
#
@@ -1004,11 +1052,13 @@
end
timeout = options[:timeout] || 0
synchronize do |client|
- client.call_without_timeout([:brpoplpush, source, destination, timeout])
+ command = [:brpoplpush, source, destination, timeout]
+ timeout += client.timeout if timeout > 0
+ client.call_with_timeout(command, timeout)
end
end
# Get an element from a list by its index.
#
@@ -1148,16 +1198,21 @@
synchronize do |client|
client.call([:spop, key])
end
end
- # Get a random member from a set.
+ # Get one or more random members from a set.
#
# @param [String] key
+ # @param [Fixnum] count
# @return [String]
- def srandmember(key)
+ def srandmember(key, count = nil)
synchronize do |client|
- client.call([:srandmember, key])
+ if count.nil?
+ client.call([:srandmember, key])
+ else
+ client.call([:srandmember, key, count])
+ end
end
end
# Move a member from one set to another.
#