EVALSHA vs FUNCTION LOAD execution context

Viewed 31

I’m trying to come up with the Lua script that would work with both - SCRIPT LOAD/EVALSHA and FUNCTION LOAD/FCALL (New feature of Redis 7.0).

As I understand it now - all i need is to figure out the execution context - if script is being called as EVALSHA vs. FUNCTION LOAD.

local function myfunction(KEYS, ARGV)
   -- do useful stuff
end

if running_as_evalsha then
  --called by EVALSHA
  --Redis 6.x version (no function support)
  --redis-cli -x script load < myscript.lua
  --evalsha 30d00b1eee6b536de87503593446e879578d31e2 1 key1 arg1

  myfunction(KEYS, ARGV)
else
  --called by FUNCTION LOAD
  --Version for Redis 7.0 with function support
  --cat myscript.lua | redis-cli -p 7000 -x FUNCTION LOAD Lua mylib REPLACE
  --127.0.0.1:7000> FCALL myfunction 1 key1 arg1

  redis.register_function('myfunction', myfunction)
end

What can I use here as running_as_evalsha? (edited)

1 Answers

Here is the answer I got from the Redis folks: You can check if you have redis.register_function, if you do you are in a context of function load, otherwise eval...

127.0.0.1:6379> eval "if redis.register_function == nil then redis.log(redis.LOG_NOTICE, 'eval') else redis.log(redis.LOG_NOTICE, 'function') end" 0
(nil)
127.0.0.1:6379> function load lua test replace "if redis.register_function == nil then redis.log(redis.LOG_NOTICE, 'eval') else redis.log(redis.LOG_NOTICE, 'function') end"
(error) ERR No functions registered

The first print eval to the log and the second prints function.

Related