代码为
delay_lua_script = """
local key = KEYS[1]
local threadId = ARGV[1]
local time = ARGV[2]
local id = redis.call('get', key)
-- 如果key存在并且id与threadId相等,就延时
if (id and id == threadId) then
redis.call('expire', key, time)
return 1
end
return 0 -- 否则说明没有延时成功
"""
print(delay_lua_script)
from utils.redis import Redis
print(Redis.register_script(delay_lua_script.encode('utf-8'),["name"],[1,300]))
结果出现
redis.exceptions.ResponseError: Error compiling script (new function): user_script:17: 'end' expecte
最后发现是lua脚本注释最好不要写到代码后面
lua脚本最后一行改为
-- 否则说明没有延时成功
return 0
成功解决问题