redis有值查询返回null_redis运行lua脚本hmget返回值为空判断问题-Go语言中文社区...
1、redis中準(zhǔn)備測(cè)試數(shù)據(jù)
hmset test abcd 123 ABCD 456
hgetall test
2、網(wǎng)上有人說(shuō)用內(nèi)置函數(shù)next()判斷
redis-cli -c -p 8000 -n 8 --eval 1.lua test
redis-cli -c -p 8000 -n 8 --eval 1.lua test1
測(cè)試結(jié)果 ?返回的類型都為number,
local table_res=redis.call('hmget', KEYS[1], "abcd", "ABCD", "other")
return table_res ?測(cè)試結(jié)果:
return next(table_res) ?測(cè)試結(jié)果:
return type(next(table_res)) ?測(cè)試結(jié)果:
if next(table_res)==nil then
return "table_res is null"
end
redis set中的key不存在時(shí),返回table元素的值為空,table不為空。?所以if條件判斷為假。
下面判斷table元素中的值是否為空:
if table_res[1]==nil then
return "table first value is null"
end
說(shuō)明直接判斷不行
查看return type(table_res[1]) 類型
所以我們修改為
if table_res[1]==false then
return "table first value is null"
else
return "not null"
end
總結(jié):第一次用redis里運(yùn)行l(wèi)ua腳本,一路磕磕噴噴;網(wǎng)上查了很多資料都沒有想過(guò)問(wèn)題的解決方案,把它記錄下來(lái)希望能給大家?guī)椭?#xff0c;少碰坑。
總結(jié)
以上是生活随笔為你收集整理的redis有值查询返回null_redis运行lua脚本hmget返回值为空判断问题-Go语言中文社区...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: gitlab合并分支后需要提交吗_git
- 下一篇: kuka机器人外部轴异步_(技术)机器人