Skip to content

Commit

Permalink
Implement variadic hset
Browse files Browse the repository at this point in the history
  • Loading branch information
motoroller authored and byroot committed Jun 9, 2020
1 parent ba82682 commit ad7191f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
14 changes: 8 additions & 6 deletions lib/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2105,15 +2105,17 @@ def hlen(key)
end
end

# Set the string value of a hash field.
# Set one or more hash values.
#
# @example
# redis.hset("hash", "f1", "v1", "f2", "v2") # => 2
#
# @param [String] key
# @param [String] field
# @param [String] value
# @return [Boolean] whether or not the field was **added** to the hash
def hset(key, field, value)
# @param [Array<String>] attrs array of fields and values
# @return [Integer] The number of fields that were added to the hash
def hset(key, *attrs)
synchronize do |client|
client.call([:hset, key, field, value], &Boolify)
client.call([:hset, key, *attrs])
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,9 @@ def hlen(key)
node_for(key).hlen(key)
end

# Set the string value of a hash field.
def hset(key, field, value)
node_for(key).hset(key, field, value)
# Set multiple hash fields to multiple values.
def hset(key, *attrs)
node_for(key).hset(key, *attrs)
end

# Set the value of a hash field, only if the field does not exist.
Expand Down
11 changes: 10 additions & 1 deletion test/lint/hashes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ module Lint
module Hashes

def test_hset_and_hget
r.hset("foo", "f1", "s1")
assert_equal 1, r.hset("foo", "f1", "s1")

assert_equal "s1", r.hget("foo", "f1")
end

def test_variadic_hset
target_version "4.0.0" do
assert_equal 2, r.hset("foo", "f1", "s1", "f2", "s2")

assert_equal "s1", r.hget("foo", "f1")
assert_equal "s2", r.hget("foo", "f2")
end
end

def test_hsetnx
r.hset("foo", "f1", "s1")
r.hsetnx("foo", "f1", "s2")
Expand Down

0 comments on commit ad7191f

Please sign in to comment.