Skip to content

Commit 349ddb1

Browse files
committed
Use redis-client call_v interface
Saves splatting commands all the time
1 parent 54763f1 commit 349ddb1

File tree

10 files changed

+35
-36
lines changed

10 files changed

+35
-36
lines changed

lib/redis.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,19 @@ def synchronize
156156

157157
def send_command(command, &block)
158158
@monitor.synchronize do
159-
@client.call(command, &block)
159+
@client.call_v(command, &block)
160160
end
161161
end
162162

163163
def send_blocking_command(command, timeout, &block)
164164
@monitor.synchronize do
165-
@client.blocking_call(timeout, command, &block)
165+
@client.blocking_call_v(timeout, command, &block)
166166
end
167167
end
168168

169169
def _subscription(method, timeout, channels, block)
170170
if @subscription_client
171-
return @subscription_client.call([method] + channels)
171+
return @subscription_client.call_v([method] + channels)
172172
end
173173

174174
begin

lib/redis/client.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ def password
6565
config.password
6666
end
6767

68-
def call(command, &block)
69-
super(*command, &block)
68+
undef_method :call
69+
undef_method :call_once
70+
undef_method :blocking_call
71+
72+
def call_v(command, &block)
73+
super(command, &block)
7074
rescue ::RedisClient::Error => error
7175
raise ERROR_MAPPING.fetch(error.class), error.message, error.backtrace
7276
end
@@ -83,9 +87,9 @@ def pipelined
8387
raise ERROR_MAPPING.fetch(error.class), error.message, error.backtrace
8488
end
8589

86-
def blocking_call(timeout, command, &block)
90+
def blocking_call_v(timeout, command, &block)
8791
timeout += self.timeout if timeout && timeout > 0
88-
super(timeout, *command, &block)
92+
super(timeout, command, &block)
8993
rescue ::RedisClient::Error => error
9094
raise ERROR_MAPPING.fetch(error.class), error.message, error.backtrace
9195
end

lib/redis/cluster.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def _scan(command, &block)
244244

245245
command[1] = raw_cursor.to_s
246246

247-
result_cursor, result_keys = client.call(command, &block)
247+
result_cursor, result_keys = client.call_v(command, &block)
248248
result_cursor = Integer(result_cursor)
249249

250250
if result_cursor == 0

lib/redis/commands/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def select(db)
4242
# @return [String] `OK`
4343
def quit
4444
synchronize do |client|
45-
client.call([:quit])
45+
client.call_v([:quit])
4646
rescue ConnectionError
4747
ensure
4848
client.close

lib/redis/commands/server.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def lastsave
120120
def monitor
121121
synchronize do |client|
122122
client = client.pubsub
123-
client.call([:monitor])
123+
client.call_v([:monitor])
124124
loop do
125125
yield client.next_event
126126
end
@@ -138,7 +138,7 @@ def save
138138
def shutdown
139139
synchronize do |client|
140140
client.disable_reconnection do
141-
client.call([:shutdown])
141+
client.call_v([:shutdown])
142142
rescue ConnectionError
143143
# This means Redis has probably exited.
144144
nil
@@ -157,11 +157,9 @@ def slaveof(host, port)
157157
# @param [Integer] length maximum number of entries to return
158158
# @return [Array<String>, Integer, String] depends on subcommand
159159
def slowlog(subcommand, length = nil)
160-
synchronize do |client|
161-
args = [:slowlog, subcommand]
162-
args << length if length
163-
client.call args
164-
end
160+
args = [:slowlog, subcommand]
161+
args << length if length
162+
send_command(args)
165163
end
166164

167165
# Internal command used for replication.

lib/redis/commands/streams.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ module Streams
2121
# @return [Array<Hash>] information of the consumers if subcommand is `consumers`
2222
def xinfo(subcommand, key, group = nil)
2323
args = [:xinfo, subcommand, key, group].compact
24-
synchronize do |client|
25-
client.call(args) do |reply|
26-
case subcommand.to_s.downcase
27-
when 'stream' then Hashify.call(reply)
28-
when 'groups', 'consumers' then reply.map { |arr| Hashify.call(arr) }
29-
else reply
30-
end
31-
end
24+
block = case subcommand.to_s.downcase
25+
when 'stream' then Hashify
26+
when 'groups', 'consumers' then proc { |r| r.map(&Hashify) }
3227
end
28+
29+
send_command(args, &block)
3330
end
3431

3532
# Add new entry to the stream.
@@ -113,7 +110,7 @@ def xdel(key, *ids)
113110
def xrange(key, start = '-', range_end = '+', count: nil)
114111
args = [:xrange, key, start, range_end]
115112
args.concat(['COUNT', count]) if count
116-
synchronize { |client| client.call(args, &HashifyStreamEntries) }
113+
send_command(args, &HashifyStreamEntries)
117114
end
118115

119116
# Fetches entries of the stream in descending order.

lib/redis/commands/transactions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def multi
6060
# @see #multi
6161
def watch(*keys)
6262
synchronize do |client|
63-
res = client.call([:watch, *keys])
63+
res = client.call_v([:watch] + keys)
6464

6565
if block_given?
6666
begin

lib/redis/pipeline.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def multi
2323
size = @futures.size
2424
yield transaction
2525
multi_future = MultiFuture.new(@futures[size..-1])
26-
@pipeline.call(:exec) do |result|
26+
@pipeline.call_v([:exec]) do |result|
2727
multi_future._set(result)
2828
end
2929
@futures << multi_future
@@ -38,7 +38,7 @@ def synchronize
3838

3939
def send_command(command, &block)
4040
future = Future.new(command, block)
41-
@pipeline.call(*command) do |result|
41+
@pipeline.call_v(command) do |result|
4242
future._set(result)
4343
end
4444
@futures << future
@@ -47,7 +47,7 @@ def send_command(command, &block)
4747

4848
def send_blocking_command(command, timeout, &block)
4949
future = Future.new(command, block)
50-
@pipeline.blocking_call(timeout, *command) do |result|
50+
@pipeline.blocking_call_v(timeout, command) do |result|
5151
future._set(result)
5252
end
5353
@futures << future

lib/redis/subscribe.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ def initialize(client)
66
@client = client
77
end
88

9-
def call(command)
10-
@client.call(command)
9+
def call_v(command)
10+
@client.call_v(command)
1111
end
1212

1313
def subscribe(*channels, &block)
@@ -27,19 +27,19 @@ def psubscribe_with_timeout(timeout, *channels, &block)
2727
end
2828

2929
def unsubscribe(*channels)
30-
call([:unsubscribe, *channels])
30+
call_v([:unsubscribe, *channels])
3131
end
3232

3333
def punsubscribe(*channels)
34-
call([:punsubscribe, *channels])
34+
call_v([:punsubscribe, *channels])
3535
end
3636

3737
protected
3838

3939
def subscription(start, stop, channels, block, timeout = 0)
4040
sub = Subscription.new(&block)
4141

42-
@client.call([start, *channels])
42+
@client.call_v([start, *channels])
4343
while event = @client.next_event(timeout)
4444
if event.is_a?(::RedisClient::CommandError)
4545
raise Client::ERROR_MAPPING.fetch(event.class), event.message

test/redis/internals_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,15 @@ def close_on_connection(seq, &block)
196196

197197
def test_retry_on_write_error_by_default
198198
close_on_connection([0]) do |redis|
199-
assert_equal "1", redis._client.call(["x" * 128 * 1024])
199+
assert_equal "1", redis._client.call_v(["x" * 128 * 1024])
200200
end
201201
end
202202

203203
def test_dont_retry_on_write_error_when_wrapped_in_without_reconnect
204204
close_on_connection([0]) do |redis|
205205
assert_raises Redis::ConnectionError do
206206
redis.without_reconnect do
207-
redis._client.call(["x" * 128 * 1024])
207+
redis._client.call_v(["x" * 128 * 1024])
208208
end
209209
end
210210
end

0 commit comments

Comments
 (0)