EVALSHA 与 FUNCTION LOAD 执行上下文

EVALSHA vs FUNCTION LOAD execution context

我正在尝试想出可以与 SCRIPT LOAD/EVALSHA 和 FUNCTION LOAD/FCALL(Redis 7.0 的新功能)一起使用的 Lua 脚本。

据我现在的理解 - 我需要的只是弄清楚执行上下文 - 如果脚本被调用为 EVALSHA 与 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

running_as_evalsha 我可以在这里使用什么? (已编辑)

这是我从 Redis 人那里得到的答案:你可以检查你是否有 redis.register_function,如果你有,你就在函数加载的上下文中,否则 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

第一次打印 eval 到日志,第二次打印 function.