Skip to content

Commit 0d7da29

Browse files
authored
feat: support more compatibility with redis client (#85)
1 parent 559bf53 commit 0d7da29

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

docs/class_diagrams_redis_cluster_client.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ classDiagram
9494
RedisClient_Cluster ..> RedisClient_Cluster_PubSub : new
9595
RedisClient_Cluster ..> RedisClient_Cluster_Router : new
9696
97+
RedisClient_Cluster_Pipeline ..> RedisClient_Cluster_Router : use
98+
RedisClient_Cluster_PubSub ..> RedisClient_Cluster_Router : use
99+
97100
RedisClient_Cluster_Router ..> RedisClient_Cluster_Node : new
98101
RedisClient_Cluster_Router ..> RedisClient_Cluster_Command : new
99102
RedisClient_Cluster_Router ..> module_RedisClient_Cluster_KeySlotConverter : call

lib/redis_client/cluster.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class Cluster
1111

1212
def initialize(config, pool: nil, **kwargs)
1313
@router = ::RedisClient::Cluster::Router.new(config, pool: pool, **kwargs)
14-
@command_builder = config.command_builder
1514
end
1615

1716
def inspect
@@ -78,19 +77,16 @@ def close
7877
def method_missing(name, *args, **kwargs)
7978
if @router.command_exists?(name)
8079
args.unshift(name)
81-
args = @command_builder.generate!(args, kwargs)
82-
@router.send_command(:call, *args)
83-
else
84-
super
80+
return @router.send_command(:call, *args)
8581
end
82+
83+
super
8684
end
8785

8886
def respond_to_missing?(name, include_private = false)
89-
if @router.command_exists?(name)
90-
true
91-
else
92-
super
93-
end
87+
return true if @router.command_exists?(name)
88+
89+
super
9490
end
9591
end
9692
end

lib/redis_client/cluster/pipeline.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ def initialize(router)
1515
end
1616

1717
def call(*command, **kwargs)
18-
node_key = @router.find_node_key(*command, primary_only: true)
18+
node_key = @router.find_node_key(*command, primary_only: true, **kwargs)
1919
@grouped[node_key] += [[@size, :call, command, kwargs]]
2020
@size += 1
2121
end
2222

2323
def call_once(*command, **kwargs)
24-
node_key = @router.find_node_key(*command, primary_only: true)
24+
node_key = @router.find_node_key(*command, primary_only: true, **kwargs)
2525
@grouped[node_key] += [[@size, :call_once, command, kwargs]]
2626
@size += 1
2727
end
2828

2929
def blocking_call(timeout, *command, **kwargs)
30-
node_key = @router.find_node_key(*command, primary_only: true)
30+
node_key = @router.find_node_key(*command, primary_only: true, **kwargs)
3131
@grouped[node_key] += [[@size, :blocking_call, timeout, command, kwargs]]
3232
@size += 1
3333
end

lib/redis_client/cluster/pub_sub.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(router)
1010

1111
def call(*command, **kwargs)
1212
close
13-
@pubsub = @router.assign_node(*command).pubsub
13+
@pubsub = @router.assign_node(*command, **kwargs).pubsub
1414
@pubsub.call(*command, **kwargs)
1515
end
1616

lib/redis_client/cluster/router.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ def initialize(config, pool: nil, **kwargs)
2020
@client_kwargs = kwargs
2121
@node = fetch_cluster_info(@config, pool: @pool, **@client_kwargs)
2222
@command = ::RedisClient::Cluster::Command.load(@node)
23+
@command_builder = @config.command_builder
2324
@mutex = Mutex.new
2425
end
2526

2627
def send_command(method, *args, **kwargs, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
2728
command = method == :blocking_call && args.size > 1 ? args[1..] : args
29+
command = @command_builder.generate!(command, kwargs)
2830

2931
cmd = command.first.to_s.downcase
3032
case cmd
@@ -95,6 +97,8 @@ def try_send(node, method, *args, retry_count: 3, **kwargs, &block) # rubocop:di
9597
end
9698

9799
def scan(*command, **kwargs) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
100+
command = @command_builder.generate!(command, kwargs)
101+
98102
command[1] = ZERO_CURSOR_FOR_SCAN if command.size == 1
99103
input_cursor = Integer(command[1])
100104

@@ -116,12 +120,14 @@ def scan(*command, **kwargs) # rubocop:disable Metrics/MethodLength, Metrics/Abc
116120
[((result_cursor << 8) + client_index).to_s, result_keys]
117121
end
118122

119-
def assign_node(*command)
120-
node_key = find_node_key(*command)
123+
def assign_node(*command, **kwargs)
124+
node_key = find_node_key(*command, **kwargs)
121125
find_node(node_key)
122126
end
123127

124-
def find_node_key(*command, primary_only: false)
128+
def find_node_key(*command, primary_only: false, **kwargs)
129+
command = @command_builder.generate!(command, kwargs)
130+
125131
key = @command.extract_first_key(command)
126132
slot = key.empty? ? nil : ::RedisClient::Cluster::KeySlotConverter.convert(key)
127133

@@ -163,6 +169,7 @@ def send_wait_command(method, *args, retry_count: 3, **kwargs, &block)
163169

164170
def send_config_command(method, *args, **kwargs, &block)
165171
command = method == :blocking_call && args.size > 1 ? args[1..] : args
172+
command = @command_builder.generate!(command, kwargs)
166173

167174
case command[1].to_s.downcase
168175
when 'resetstat', 'rewrite', 'set'
@@ -173,6 +180,7 @@ def send_config_command(method, *args, **kwargs, &block)
173180

174181
def send_memory_command(method, *args, **kwargs, &block)
175182
command = method == :blocking_call && args.size > 1 ? args[1..] : args
183+
command = @command_builder.generate!(command, kwargs)
176184

177185
case command[1].to_s.downcase
178186
when 'stats' then @node.call_all(method, *args, **kwargs, &block)
@@ -183,6 +191,7 @@ def send_memory_command(method, *args, **kwargs, &block)
183191

184192
def send_client_command(method, *args, **kwargs, &block)
185193
command = method == :blocking_call && args.size > 1 ? args[1..] : args
194+
command = @command_builder.generate!(command, kwargs)
186195

187196
case command[1].to_s.downcase
188197
when 'list' then @node.call_all(method, *args, **kwargs, &block).flatten
@@ -194,6 +203,7 @@ def send_client_command(method, *args, **kwargs, &block)
194203

195204
def send_cluster_command(method, *args, **kwargs, &block) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
196205
command = method == :blocking_call && args.size > 1 ? args[1..] : args
206+
command = @command_builder.generate!(command, kwargs)
197207
subcommand = command[1].to_s.downcase
198208

199209
case subcommand
@@ -211,6 +221,7 @@ def send_cluster_command(method, *args, **kwargs, &block) # rubocop:disable Metr
211221

212222
def send_script_command(method, *args, **kwargs, &block)
213223
command = method == :blocking_call && args.size > 1 ? args[1..] : args
224+
command = @command_builder.generate!(command, kwargs)
214225

215226
case command[1].to_s.downcase
216227
when 'debug', 'kill'
@@ -223,6 +234,7 @@ def send_script_command(method, *args, **kwargs, &block)
223234

224235
def send_pubsub_command(method, *args, **kwargs, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
225236
command = method == :blocking_call && args.size > 1 ? args[1..] : args
237+
command = @command_builder.generate!(command, kwargs)
226238

227239
case command[1].to_s.downcase
228240
when 'channels' then @node.call_all(method, *args, **kwargs, &block).flatten.uniq.sort_by(&:to_s)

0 commit comments

Comments
 (0)