44
55class Redis
66 class Cluster
7- class TransactionAdapter < RedisClient ::Cluster ::Transaction
7+ class TransactionAdapter
8+ class Internal < RedisClient ::Cluster ::Transaction
9+ def initialize ( client , router , command_builder , node : nil , slot : nil , asking : false )
10+ @client = client
11+ super ( router , command_builder , node : node , slot : slot , asking : asking )
12+ end
13+
14+ def multi
15+ raise ( Redis ::Cluster ::TransactionConsistencyError , "Can't nest multi transaction" )
16+ end
17+
18+ def exec
19+ # no need to do anything
20+ end
21+
22+ def discard
23+ # no need to do anything
24+ end
25+
26+ def watch ( *_ )
27+ raise ( Redis ::Cluster ::TransactionConsistencyError , "Can't use watch in a transaction" )
28+ end
29+
30+ def unwatch
31+ # no need to do anything
32+ end
33+
34+ private
35+
36+ def method_missing ( name , *args , **kwargs , &block )
37+ return call ( name , *args , **kwargs , &block ) if @client . respond_to? ( name )
38+
39+ super
40+ end
41+
42+ def respond_to_missing? ( name , include_private = false )
43+ return true if @client . respond_to? ( name )
44+
45+ super
46+ end
47+ end
48+
849 def initialize ( client , router , command_builder , node : nil , slot : nil , asking : false )
950 @client = client
10- super ( router , command_builder , node : node , slot : slot , asking : asking )
51+ @router = router
52+ @command_builder = command_builder
53+ @node = node
54+ @slot = slot
55+ @asking = asking
56+ @lock_released = false
57+ end
58+
59+ def lock_released?
60+ @lock_released
1161 end
1262
1363 def multi
14- yield self
64+ @lock_released = true
65+ transaction = Redis ::Cluster ::TransactionAdapter ::Internal . new (
66+ @client , @router , @command_builder , node : @node , slot : @slot , asking : @asking
67+ )
68+ yield transaction
69+ transaction . execute
1570 end
1671
1772 def exec
@@ -23,20 +78,18 @@ def discard
2378 end
2479
2580 def watch ( *_ )
26- raise (
27- Redis ::Cluster ::TransactionConsistencyError ,
28- 'You should pass all the keys to a watch method if you use the cluster client.'
29- )
81+ raise ( Redis ::Cluster ::TransactionConsistencyError , "Can't nest watch command if you use the cluster client" )
3082 end
3183
3284 def unwatch
33- # no need to do anything
85+ @lock_released = true
86+ @node . call ( 'UNWATCH' )
3487 end
3588
3689 private
3790
3891 def method_missing ( name , *args , **kwargs , &block )
39- return call ( name , *args , **kwargs , &block ) if @client . respond_to? ( name )
92+ return @client . public_send ( name , *args , **kwargs , &block ) if @client . respond_to? ( name )
4093
4194 super
4295 end
0 commit comments