Skip to content

Commit

Permalink
perf: lessen memory consumptions in a emulated mget method (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
supercaracal authored May 4, 2024
1 parent a13e363 commit 8fdd23b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/redis_client/cluster/key_slot_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ def extract_hash_tag(key)

key[s + 1..e - 1]
end

def hash_tag_included?(key)
key = key.to_s
s = key.index(LEFT_BRACKET)
return false if s.nil?

e = key.index(RIGHT_BRACKET, s + 1)
return false if e.nil?

s + 1 < e
end
end
end
end
2 changes: 1 addition & 1 deletion lib/redis_client/cluster/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def send_watch_command(command)

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

Expand Down
21 changes: 21 additions & 0 deletions test/redis_client/cluster/test_key_slot_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ def test_extract_hash_tag
assert_equal(c[:want], got, msg)
end
end

def test_hash_tag_included?
[
{ key: 'foo', want: false },
{ key: 'foo{bar}baz', want: true },
{ key: 'foo{bar}baz{qux}quuc', want: true },
{ key: 'foo}bar{baz', want: false },
{ key: 'foo{bar', want: false },
{ key: 'foo}bar', want: false },
{ key: 'foo{}bar', want: false },
{ key: '{}foo', want: false },
{ key: 'foo{}', want: false },
{ key: '{}', want: false },
{ key: '', want: false },
{ key: nil, want: false }
].each_with_index do |c, idx|
msg = "Case: #{idx}"
got = ::RedisClient::Cluster::KeySlotConverter.hash_tag_included?(c[:key])
assert_equal(c[:want], got, msg)
end
end
end
end
end

0 comments on commit 8fdd23b

Please sign in to comment.