Skip to content

Commit c1b84c7

Browse files
authored
perf: faster extract_hash_tag (#160)
2 parents d1a78c8 + b68dbc7 commit c1b84c7

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/redis_client/cluster/command.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class RedisClient
88
class Cluster
99
class Command
1010
EMPTY_STRING = ''
11+
LEFT_BRACKET = '{'
12+
RIGHT_BRACKET = '}'
1113

1214
Detail = Struct.new(
1315
'RedisCommand',
@@ -104,10 +106,11 @@ def determine_optional_key_position(command, option_name) # rubocop:disable Metr
104106
# @see https://redis.io/topics/cluster-spec#keys-hash-tags Keys hash tags
105107
def extract_hash_tag(key)
106108
key = key.to_s
107-
s = key.index('{')
108-
e = key.index('}', s.to_i + 1)
109+
s = key.index(LEFT_BRACKET)
110+
return EMPTY_STRING if s.nil?
109111

110-
return EMPTY_STRING if s.nil? || e.nil?
112+
e = key.index(RIGHT_BRACKET, s + 1)
113+
return EMPTY_STRING if e.nil?
111114

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

0 commit comments

Comments
 (0)