You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Redis 7.x集群中Lua函数调用print报错问题排查

问题原因及解决方法

为什么print调用报错

Redis 7.x的Lua脚本执行环境没有提供全局的print函数。Redis的Lua沙箱环境做了严格限制,移除了这类直接操作标准输出的函数——一方面Redis服务器端没有绑定可直接输出的终端流,另一方面是为了保证脚本的安全性和执行一致性。

替代方案

1. 写入Redis日志

如果需要调试输出内容,可以使用Redis提供的redis.log()函数,将信息写入Redis的日志文件,支持LOG_DEBUG/LOG_INFO等不同日志级别:

#!lua name=printTestLib
local function printTest(keys, args)
    local time = redis.call('TIME');
    -- 拼接TIME返回的秒+微秒数组,写入日志
    redis.log(redis.LOG_INFO, "current timestamp in millis: " .. time[1] .. "." .. time[2])
end
redis.register_function('printTest', printTest)

2. 直接返回结果给客户端

如果只是要把时间信息返回给调用方,直接用return即可,不需要打印:

#!lua name=printTestLib
local function printTest(keys, args)
    local time = redis.call('TIME');
    -- 转换为毫秒级时间戳返回
    return "current timestamp in millis: " .. time[1] * 1000 + math.floor(time[2]/1000)
end
redis.register_function('printTest', printTest)

执行FCALL printTest 0时,客户端会直接收到格式化后的时间字符串。

内容的提问来源于stack exchange,提问作者RRM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 07:27:16