2

I have created a redis lua script to execute a command based on key data type :-

local keyType = redis.call("TYPE", KEYS[1])
if (keyType == "string")
then
return redis.call("GET",KEYS[1])
else
return nil
end

It is returning null every time I am executing it.Can any please help in correcting the script.

2 Answers 2

4

The response to redis.call returns as a table that looks like this: {"ok": "string"} (if the type is a string of course)

So in order to properly check, you should change your code to:

local keyType = redis.call("TYPE", KEYS[1]).ok

and the rest of the code will work fine.

The issue is this: the TYPE command is one of the few commands that return a "simple string" or "status" redis reply (see the redis protocol specs for the response types). In the redis lua documentation it's stated that:

Redis status reply -> Lua table with a single ok field containing the status

Which is what happened here.

Sign up to request clarification or add additional context in comments.

Comments

0

Using [1] will also work.local keyType = redis.call("TYPE", KEYS[1]) return keyType[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.