Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redis module support #107

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions lib/resty/rediscluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local char = string.char
local table_insert = table.insert
local string_find = string.find
local redis_crc = xmodem.redis_crc

redis:register_module_prefix("json")
local DEFAULT_SHARED_DICT_NAME = "redis_cluster_slot_locks"
local DEFAULT_REFRESH_DICT_NAME = "refresh_lock"
local DEFAULT_MAX_REDIRECTION = 5
Expand All @@ -29,6 +29,8 @@ local DEFAULT_CONNECTION_TIMEOUT = 1000
local DEFAULT_SEND_TIMEOUT = 1000
local DEFAULT_READ_TIMEOUT = 1000



local function parse_key(key_str)
local left_tag_single_index = string_find(key_str, "{", 0)
local right_tag_single_index = string_find(key_str, "}", 0)
Expand Down Expand Up @@ -58,6 +60,14 @@ local cluster_invalid_cmds = {
["shutdown"] = true
}

function _M.register_module_prefix(mod)
_M[mod] = function(self)
self._module_prefix = mod
return self
end
end


local function redis_slot(str)
return redis_crc(parse_key(str))
end
Expand Down Expand Up @@ -578,6 +588,13 @@ local function _do_cmd(self, cmd, key, ...)
return _do_cmd_master(self, cmd, key, ...)
end


local module_prefix = rawget(self, "_module_prefix")
if module_prefix then
self._module_prefix = nil
cmd = module_prefix .. "." .. cmd
end

local res, err = handle_command_with_retry(self, nil, nil, false, cmd, key, ...)
return res, err
end
Expand Down Expand Up @@ -706,21 +723,20 @@ function _M.commit_pipeline(self)
config.read_timeout or DEFAULT_READ_TIMEOUT)
local ok, err = redis_client:connect(ip, port, self.config.connect_opts)

if ok then
local authok, autherr = check_auth(self, redis_client)
if autherr then
return nil, autherr
end
local authok, autherr = check_auth(self, redis_client)
if autherr then
return nil, autherr
end

if slave then
--set readonly
local ok, err = redis_client:readonly()
if not ok then
self:refresh_slots()
return nil, err
end
if slave then
--set readonly
local ok, err = redis_client:readonly()
if not ok then
self:refresh_slots()
return nil, err
end

end
if ok then
redis_client:init_pipeline()
for i = 1, #reqs do
local req = reqs[i]
Expand Down