Skip to content

Commit ead279b

Browse files
author
KJ Tsanaktsidis
committed
Re-implement RedisClient::Cluster#multi in terms of #with
This does change the behaviour ever so slightly (see updated tests), but I believe for the better.
1 parent 3bdd331 commit ead279b

File tree

4 files changed

+23
-72
lines changed

4 files changed

+23
-72
lines changed

lib/redis_client/cluster.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
require 'redis_client/cluster/pipeline'
66
require 'redis_client/cluster/pub_sub'
77
require 'redis_client/cluster/router'
8-
require 'redis_client/cluster/transaction'
98

109
class RedisClient
1110
class Cluster
@@ -90,8 +89,14 @@ def pipelined
9089
pipeline.execute
9190
end
9291

93-
def multi(watch: nil, &block)
94-
::RedisClient::Cluster::Transaction.new(@router, @command_builder).execute(watch: watch, &block)
92+
def multi(watch: nil, key: nil, &block)
93+
slot_key = key
94+
slot_key = watch&.first if slot_key.nil?
95+
raise ArgumentError, 'watch or key must be provided' if slot_key.nil?
96+
97+
with(key: slot_key) do |conn|
98+
conn.multi(watch: watch, &block)
99+
end
95100
end
96101

97102
def pubsub

lib/redis_client/cluster/errors.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,9 @@ def initialize(_ = '')
5757
end
5858

5959
class BlockingReadTimeoutError < ::RedisClient::ReadTimeoutError; end
60+
61+
module Transaction
62+
ConsistencyError = Class.new(::RedisClient::Error)
63+
end
6064
end
6165
end

lib/redis_client/cluster/transaction.rb

Lines changed: 0 additions & 57 deletions
This file was deleted.

test/redis_client/test_cluster.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def test_pipelined_with_many_commands
178178
end
179179

180180
def test_transaction_with_single_key
181-
got = @client.multi do |t|
181+
got = @client.multi(key: 'counter') do |t|
182182
t.call('SET', 'counter', '0')
183183
t.call('INCR', 'counter')
184184
t.call('INCR', 'counter')
@@ -190,7 +190,7 @@ def test_transaction_with_single_key
190190

191191
def test_transaction_with_multiple_key
192192
assert_raises(::RedisClient::Cluster::Transaction::ConsistencyError) do
193-
@client.multi do |t|
193+
@client.multi(key: 'key1') do |t|
194194
t.call('SET', 'key1', '1')
195195
t.call('SET', 'key2', '2')
196196
t.call('SET', 'key3', '3')
@@ -204,20 +204,19 @@ def test_transaction_with_multiple_key
204204

205205
def test_transaction_with_empty_block
206206
assert_raises(ArgumentError) { @client.multi {} }
207-
assert_raises(LocalJumpError) { @client.multi }
207+
assert_raises(LocalJumpError) { @client.multi(key: 'foo') }
208208
end
209209

210210
def test_transaction_with_keyless_commands
211-
assert_raises(::RedisClient::Cluster::Transaction::ConsistencyError) do
212-
@client.multi do |t|
213-
t.call('ECHO', 'foo')
214-
t.call('ECHO', 'bar')
215-
end
211+
got = @client.multi(key: 'hello') do |t|
212+
t.call('ECHO', 'foo')
213+
t.call('ECHO', 'bar')
216214
end
215+
assert_equal %w[foo bar], got
217216
end
218217

219218
def test_transaction_with_hashtag
220-
got = @client.multi do |t|
219+
got = @client.multi(key: '{key}') do |t|
221220
t.call('MSET', '{key}1', '1', '{key}2', '2')
222221
t.call('MSET', '{key}3', '3', '{key}4', '4')
223222
end
@@ -228,14 +227,14 @@ def test_transaction_with_hashtag
228227

229228
def test_transaction_without_hashtag
230229
assert_raises(::RedisClient::Cluster::Transaction::ConsistencyError) do
231-
@client.multi do |t|
230+
@client.multi(key: 'key1') do |t|
232231
t.call('MSET', 'key1', '1', 'key2', '2')
233232
t.call('MSET', 'key3', '3', 'key4', '4')
234233
end
235234
end
236235

237-
assert_raises(::RedisClient::CommandError, 'CROSSSLOT keys') do
238-
@client.multi do |t|
236+
assert_raises(::RedisClient::Cluster::Transaction::ConsistencyError) do
237+
@client.multi(key: 'key1') do |t|
239238
t.call('MSET', 'key1', '1', 'key2', '2')
240239
t.call('MSET', 'key1', '1', 'key3', '3')
241240
t.call('MSET', 'key1', '1', 'key4', '4')

0 commit comments

Comments
 (0)