lua redis字符串比较不起作用

时间:2016-05-01 20:20:03

标签: nginx lua redis string-comparison openresty

local password = json_string["password"] or "None"
local redisPassword = red:hmget(userName,"password") or None
local redisAuthtoken = red:hmget(userName,"authToken")
if (tostring(password)   ==  tostring(redisPassword))
then
    ngx.say(redisAuthtoken) 
else
    ngx.say("Error User or Service Not found 1510")
end 

密码= admin redisPassword = admin

我能够看到两个密码作为输出管理员,但它在lua代码中不匹配,控件总是转到其他部分。

当我像这样比较时

if(tostring(password)==“admin”)

它工作正常,这意味着问题是redis值,但我在redis中设置了密码值admin。

1 个答案:

答案 0 :(得分:2)

我的理解是hmget返回multi-bulk reply,其结果在lua表中,所以你应该做这样的事情:

local res, err = red:hmget(userName,"password")
local redisPassword
if res and res ~= ngx.null then redisPassword = res[1] end
ngx.say(redisPassword)

来自documentation

  

非零Redis"多批量回复"导致Lua表保存所有组成值(如果有的话)。如果任何组成值是有效的redis错误值,那么它将是一个双元素表{false,err}。 nil多批量回复以ngx.null值返回。

请注意ngx.null值与Lua nil值不同,因此您可能需要单独检查。

另请注意,您将None用作变量,将"None"用作两个不同位置的字符串,这可能无法达到预期效果。