Illustrating the difference between redis.call () and redis.pcall ()

I tried the following erroneous eval command to understand the difference between redis.call () and redis.pcall ()

eval "return redis.call(ARGV[2],KEYS[1])" 1 key get   
eval "return redis.pcall(ARGV[2],KEYS[1])" 1 key get

In both cases, I got the error below,

(error) Lua redis() command arguments must be strings or integers

This error does not convey the difference between redis.call () and redis.pcall (), as shown in the documentation that says

"redis.call () is similar to redis.pcall (), the only difference is that if an error occurs when calling the Redis command, redis.call () will cause a Lua error, which in turn will cause EVAL to return the error to the caller the cmdlet, while redis.pcall will be an error trap that returns a Lua table representing the error. "

So, according to the documentation, when using redis.pcall (), the error should be trapped, right! In that case, why are both errors the same? If I misunderstood the difference, it would be better if someone could clearly illustrate the difference between the teams !!

+4
source share
2 answers

This is a complex case, because in your example, the command does not generate an error, you are using the wrong redis.calland redis.pcall(because ARGV[2]there are nil, according to the error message). Therefore, in both cases, the error is not restored.

Here is an example where a team really fails and you can see the difference:

redis 127.0.0.1:6379> set foo bar
OK
redis 127.0.0.1:6379> eval 'redis.call("hget","foo","bar")' 0
(error) ERR Error running script (call to f_9e6d82f0740926e0a70775430bda59a54d4e0664): ERR Operation against a key holding the wrong kind of value
redis 127.0.0.1:6379> eval 'redis.pcall("hget","foo","bar")' 0
(nil)

, pcall, script nil. , ?

redis 127.0.0.1:6379> eval 'return redis.call("hget","foo","bar")' 0
(error) ERR Error running script (call to f_d0a8dce7264708876edf262052788fc90a8e8325): ERR Operation against a key holding the wrong kind of value
redis 127.0.0.1:6379> eval 'return redis.pcall("hget","foo","bar")' 0
(error) ERR Operation against a key holding the wrong kind of value

call , ( - Java, Python ..) , .

pcall err, " " Redis, . ? !

redis 127.0.0.1:6379> eval 'local t = redis.pcall("hget","foo","bar"); local r = {type(t)}; for k,v in pairs(t) do r[#r+1] = k; r[#r+1] = v; end; return r' 0
1) "table"
2) "err"
3) "ERR Operation against a key holding the wrong kind of value"
+11

, Redis pcall. , , Redis (, get). pcall Redis, pcall.

, Redis ( redis.call).

> EVAL "return redis.call(ARGV[1],KEYS[1])" 1 key get
"100"

> EVAL "return redis.call(ARGV[1],KEYS[1])" 1 key born_to_fail
(error) ERR Error running script (call to f_2673dc91ae540aa65dedd262a952d5338e330b37): @user_script:1: @user_script: 1: Unknown Redis command called from Lua script

> EVAL "return redis.pcall(ARGV[1],KEYS[1])" 1 key born_to_fail
(error) @user_script: 1: Unknown Redis command called from Lua script

Redis Lua.

pcall, Lua, , .

+3

All Articles