将初始化修改为使用管道进行存储到redis中

This commit is contained in:
wanglei 2025-11-08 15:10:36 +08:00
parent a662650dc7
commit e95d2a0ee5
2 changed files with 11 additions and 8 deletions

View File

@ -53,6 +53,8 @@ local function handler()
--获取数据表中的记录数
local code, res = roleDao:getAllSystemRoles()
if res == nil then return end
--管道进行redis处理
red:init_pipeline()
--读取角色id和角色名称
for _, row in pairs(res) do
local id = row.id --:1
@ -65,12 +67,13 @@ local function handler()
local perm = ret.permission_code
local key = name.."-"..perm
--role_name-permission_code 组成key进行验证 存储到redis中
local ok, err = red:set(key, "1")
if not ok then
ngx.log(ngx.ERR, "redis failed to set key: "..err)
end
red:set(key, "1")
end
end
local results, err = red:commit_pipeline()
if not results then
ngx.log(ngx.ERR, "init failed to commit the pipelined requests: ", err)
end
--关闭redis连接
red:close()

View File

@ -56,13 +56,13 @@ function _M:getRolePermissions(role_name)
return allPermissions
end
--获取redis中所有匹配的数据内容
local prefix = role_name.."-"
local pattern = role_name.."-*"
local cursor = "0"
local result = red:call('SCAN', cursor, 'MATCH', prefix .. '*', 'COUNT')
if result == false then
local result, err = red:scan(cursor, 'MATCH', pattern)
if not result then
return allPermissions
end
--cursor = result[1]
cursor = result[1]
for _, key in ipairs(result[2]) do
table.insert(allPermissions, key)
end