Lua脚本和Redis-如何测试None值 [英] Lua script and redis - how to test for None value

查看:116
本文介绍了Lua脚本和Redis-如何测试None值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的lua脚本:

Below is my lua script:

     local ckid = redis.pcall('get',KEYS[1])
     local meta = redis.call('hgetall', ckid)
     return {ckid, meta}

如果第一次调用没有退出键,我将不希望执行第二次调用.

if the key does not exits for the first call I would not like to execute the second.

所以...我不知道None的返回值是什么.在python中,返回值为None.

So...I don't what the return value is for None. In python the return value would be None.

if ckid ~= '???????' then 
    local meta = redis.call('hgetall', ckid)
else
     local meta = 'empty'
retrun {ckid, meta}

那么...我该怎么办lua?

So...how do I do this is lua?

     local ckid = redis.pcall('get',KEYS[1])
     if ckid ~= nil then
         local meta = redis.call('hgetall', ckid)
     else
         local meta = 'none'
         local ckid = 'none'
     end

     return {ckid, meta}

使用nil时.

ResponseError: ERR Error running script (call to f_1400713412b0063a26eb0dc063f53a4e3be26380): user_script:12: Script attempted to access unexisting global variable 'meta'

推荐答案

如果在if语句中定义局部变量,则仅在if语句中存在.在手册中查看局部变量范围.

If you define a Local variable with in an if statement it only exists for the if statement. Check out local variable scope in the manual.

尝试

 local ckid = redis.pcall('get',KEYS[1])
 local meta
 if ckid ~= nil then
     meta = redis.call('hgetall', ckid)
 else
     meta = 'none'
     ckid = 'none'
 end

 return {ckid, meta}

这篇关于Lua脚本和Redis-如何测试None值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆