Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Redis::Distributed#mset_nonatomic & mapped_mset_nonatomic #1215

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# 5.0.8

- Add `Redis::Distributed#mset_nonatomic` & `mapped_mset_nonatomic`

# 5.0.7

- Fix compatibility with `redis-client 0.15.0` when using Redis Sentinel. Fix #1209.
Expand Down
24 changes: 24 additions & 0 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,22 @@ def mset(*)
raise CannotDistribute, :mset
end

def mset_nonatomic(*args)
group_args_by_node(args.each_slice(2)) do |node, subargs|
node.mset(*subargs)
end
end

def mapped_mset(_hash)
raise CannotDistribute, :mapped_mset
end

def mapped_mset_nonatomic(hash)
group_hash_by_node(hash) do |node, subhash|
node.mapped_mset(subhash)
end
end

# Set multiple keys to multiple values, only if none of the keys exist.
def msetnx(*)
raise CannotDistribute, :msetnx
Expand Down Expand Up @@ -1089,5 +1101,17 @@ def ensure_same_node(command, keys)

yield(node_for(keys.first))
end

def group_args_by_node(args)
nodes = args.each_slice(2).group_by { |k, _v| node_for(k) }
nodes.each do |node, grouped_args|
yield node, grouped_args.flatten
end
end

def group_hash_by_node(hash, &block)
nodes = hash.group_by { |k, _v| node_for(k) }
nodes.each(&block)
end
end
end
14 changes: 14 additions & 0 deletions test/distributed/commands_on_strings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,26 @@ def test_mset
end
end

def test_mset_nonatomic
r.mset_nonatomic(:foo, "s1", :bar, "s2")

assert_equal 's1', r.get('foo')
assert_equal 's2', r.get('bar')
end

def test_mset_mapped
assert_raises Redis::Distributed::CannotDistribute do
r.mapped_mset(foo: "s1", bar: "s2")
end
end

def test_mset_mapped_nonatomic
r.mapped_mset_nonatomic(foo: "s1", bar: "s2")

assert_equal 's1', r.get('foo')
assert_equal 's2', r.get('bar')
end

def test_msetnx
assert_raises Redis::Distributed::CannotDistribute do
r.set("foo", "s1")
Expand Down
Loading