Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua script and redis - how to test for None value

Tags:

redis

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.

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}

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}

When using nil..

ResponseError: ERR Error running script (call to f_1400713412b0063a26eb0dc063f53a4e3be26380): user_script:12: Script attempted to access unexisting global variable 'meta'
like image 446
Tampa Avatar asked Dec 21 '22 14:12

Tampa


1 Answers

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.

Try

 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}
like image 109
Jane T Avatar answered Jan 11 '23 15:01

Jane T