Skip to content

Commit 8fdd23b

Browse files
authored
perf: lessen memory consumptions in a emulated mget method (#360)
1 parent a13e363 commit 8fdd23b

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

lib/redis_client/cluster/key_slot_converter.rb

+11
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ def extract_hash_tag(key)
7070

7171
key[s + 1..e - 1]
7272
end
73+
74+
def hash_tag_included?(key)
75+
key = key.to_s
76+
s = key.index(LEFT_BRACKET)
77+
return false if s.nil?
78+
79+
e = key.index(RIGHT_BRACKET, s + 1)
80+
return false if e.nil?
81+
82+
s + 1 < e
83+
end
7384
end
7485
end
7586
end

lib/redis_client/cluster/router.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def send_watch_command(command)
336336

337337
def send_multiple_keys_command(cmd, method, command, args, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
338338
key_step = @command.determine_key_step(cmd)
339-
if command.size <= key_step + 1 || !::RedisClient::Cluster::KeySlotConverter.extract_hash_tag(command[1]).empty? # rubocop:disable Style/IfUnlessModifier
339+
if command.size <= key_step + 1 || ::RedisClient::Cluster::KeySlotConverter.hash_tag_included?(command[1]) # rubocop:disable Style/IfUnlessModifier
340340
return try_send(assign_node(command), method, command, args, &block)
341341
end
342342

test/redis_client/cluster/test_key_slot_converter.rb

+21
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ def test_extract_hash_tag
4848
assert_equal(c[:want], got, msg)
4949
end
5050
end
51+
52+
def test_hash_tag_included?
53+
[
54+
{ key: 'foo', want: false },
55+
{ key: 'foo{bar}baz', want: true },
56+
{ key: 'foo{bar}baz{qux}quuc', want: true },
57+
{ key: 'foo}bar{baz', want: false },
58+
{ key: 'foo{bar', want: false },
59+
{ key: 'foo}bar', want: false },
60+
{ key: 'foo{}bar', want: false },
61+
{ key: '{}foo', want: false },
62+
{ key: 'foo{}', want: false },
63+
{ key: '{}', want: false },
64+
{ key: '', want: false },
65+
{ key: nil, want: false }
66+
].each_with_index do |c, idx|
67+
msg = "Case: #{idx}"
68+
got = ::RedisClient::Cluster::KeySlotConverter.hash_tag_included?(c[:key])
69+
assert_equal(c[:want], got, msg)
70+
end
71+
end
5172
end
5273
end
5374
end

0 commit comments

Comments
 (0)