Skip to content

Commit fa7ed36

Browse files
(refactor) cache more functions at module level
1 parent ca1ed97 commit fa7ed36

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

lib/resty/rediscluster.lua

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ local ipairs = ipairs
1515
local tonumber = tonumber
1616
local match = string.match
1717
local char = string.char
18-
18+
local table_insert = table.insert
19+
local string_find = string.find
20+
local redis_crc = xmodem.redis_crc
1921

2022
local DEFAULT_SHARED_DICT_NAME = "redis_cluster_slot_locks"
2123
local DEFAULT_MAX_REDIRECTION = 5
@@ -27,8 +29,8 @@ local DEFAULT_SEND_TIMEOUT = 1000
2729
local DEFAULT_READ_TIMEOUT = 1000
2830

2931
local function parse_key(key_str)
30-
local left_tag_single_index = string.find(key_str, "{", 0)
31-
local right_tag_single_index = string.find(key_str, "}", 0)
32+
local left_tag_single_index = string_find(key_str, "{", 0)
33+
local right_tag_single_index = string_find(key_str, "}", 0)
3234
if left_tag_single_index and right_tag_single_index then
3335
--parse hashtag
3436
return key_str.sub(key_str, left_tag_single_index + 1, right_tag_single_index - 1)
@@ -56,7 +58,7 @@ local cluster_invalid_cmds = {
5658
}
5759

5860
local function redis_slot(str)
59-
return xmodem.redis_crc(parse_key(str))
61+
return redis_crc(parse_key(str))
6062
end
6163

6264
local function check_auth(self, redis_client)
@@ -89,7 +91,7 @@ end
8991
local function split(s, delimiter)
9092
local result = {};
9193
for m in (s..delimiter):gmatch("(.-)"..delimiter) do
92-
table.insert(result, m);
94+
table_insert(result, m);
9395
end
9496
return result;
9597
end
@@ -115,14 +117,14 @@ local function try_hosts_slots(self, serv_list)
115117
if ok then break end
116118
if err then
117119
ngx.log(ngx.ERR,"unable to connect, attempt nr ", k, " : error: ", err)
118-
table.insert(errors, err)
120+
table_insert(errors, err)
119121
end
120122
end
121123

122124
if ok then
123125
local _, autherr = check_auth(self, redis_client)
124126
if autherr then
125-
table.insert(errors, autherr)
127+
table_insert(errors, autherr)
126128
return nil, errors
127129
end
128130
local slots_info
@@ -155,7 +157,7 @@ local function try_hosts_slots(self, serv_list)
155157
slot_cache[self.config.name] = slots
156158
slot_cache[self.config.name .. "serv_list"] = servers
157159
else
158-
table.insert(errors, err)
160+
table_insert(errors, err)
159161
end
160162
-- cache master nodes
161163
local nodes_res, nerr = redis_client:cluster("nodes")
@@ -167,19 +169,19 @@ local function try_hosts_slots(self, serv_list)
167169
local is_master = match(node_info[3], "master") ~= nil
168170
if is_master then
169171
local ip_port = split(split(node_info[2], "@")[1], ":")
170-
table.insert(master_nodes, {
172+
table_insert(master_nodes, {
171173
ip = ip_port[1],
172174
port = tonumber(ip_port[2])
173175
})
174176
end
175177
end
176178
end
177179
else
178-
table.insert(errors, nerr)
180+
table_insert(errors, nerr)
179181
end
180182
release_connection(redis_client, config)
181183
else
182-
table.insert(errors, err)
184+
table_insert(errors, err)
183185
end
184186
if #errors == 0 then
185187
return true, nil
@@ -502,7 +504,7 @@ local function _do_cmd_master(self, cmd, key, ...)
502504
_, err = redis_client[cmd](redis_client, key, ...)
503505
end
504506
if err then
505-
table.insert(errors, err)
507+
table_insert(errors, err)
506508
end
507509
release_connection(redis_client, self.config)
508510
end
@@ -518,7 +520,7 @@ local function _do_cmd(self, cmd, key, ...)
518520
if _reqs then
519521
local args = { ... }
520522
local t = { cmd = cmd, key = key, args = args }
521-
table.insert(_reqs, t)
523+
table_insert(_reqs, t)
522524
return
523525
end
524526

0 commit comments

Comments
 (0)