I'm learning Lua. Given below, is a Lua script to handle some operations
local function reverseDb ()
local key = KEYS[1]
local value = ARGV[1]
redis.call('SREM',key,value)
return value
end
if pcall(reverseDb) then
print("success")
else
print("error")
redis.call('SADD',key,value)
end
While running it throws an error:
Script attempted to access nonexistent global variable 'print' script: 39f13cb3bddd638c815531acbc2dc6434a0329c6, on @user_script:17.
I don't understand why the error is there. Any clue?
Redis runs Lua script in a sandbox context, and limits the usage of global variables, only parts of global variables/functions are allowed. Obviously, print is not allowed.
In your case, you can return the error message instead of printing it.
if pcall(reverseDb) then
return "success"
else
redis.call('SADD',key,value)
return "error"
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With