redisTemplate执行lua脚本报错‘for‘ initial value must be a number

1.lua脚本以及springboot代码

java 复制代码
    /**
     * 批量设置Bit位(start到end置1)
     */
    private void batchSetBit(String bitKey, long start, long end) {
        String luaScript = """
                    local key = KEYS[1]
                     local start_val = ARGV[1]
                     local end_val = ARGV[2]
                     local start_num = tonumber(start_val)
                     local end_num = tonumber(end_val)
                     local count = 0
                     for i = start_num, end_num do
                         redis.call('SETBIT', key, i, 1)
                         count = count + 1
                     end
                     return count
                """;
        DefaultRedisScript<Long> script = new DefaultRedisScript<>();
        script.setScriptText(luaScript);
        script.setResultType(Long.class);
        redisTemplate.execute(script, Collections.singletonList(bitKey), String.valueOf(start), String.valueOf(end));
    }

报错: @user_script:7: user_script:7: 'for' initial value must be a number

分析,首先修改lua脚本,打印出传递的值

java 复制代码
String luaScript = """
                    local key = KEYS[1]
                     local start_val = ARGV[1]
                     local end_val = ARGV[2]
                     local start_num = tonumber(start_val)
                     local end_num = tonumber(end_val)
                     if start_num == nil or end_num == nil then
                         return redis.error_reply("Invalid range: start=" .. tostring(start_val) .. ", end=" .. tostring(end_val))
                     end
                     local count = 0
                     for i = start_num, end_num do
                         redis.call('SETBIT', key, i, 1)
                         count = count + 1
                     end
                     return count
                """;

重新执行后报错: Invalid range: start="0", end="2999"

问题 1:Value 序列化器不兼容 Lua 脚本调用(根源问题)

你的 redisTemplate 中 valueSerializer 使用了 Jackson2JsonRedisSerializer(JSON 序列化),而 Lua 脚本需要接收纯字符串格式的数字(如 "0" "999"),但 JSON 序列化会把字符串参数包装成带引号的 JSON 字符串(比如传 "0" 会被序列化成 ""0""),导致 Lua 中 tonumber(ARGV1) 解析失败,这也是之前报 Invalid numeric arguments 的根本原因。

2.解决方法

(1)使用StringRedisTemplate

默认序列化器 全链路 StringRedisSerializer(纯字符串)

参数传递形式 输入 "0" → 直接转字节数组 48(纯 UTF-8 编码)

(2)将参数的双引号去掉

bash 复制代码
    start_val = string.gsub(start_val, "^[\"']*(.-)[\"']*$", "%1")
    end_val = string.gsub(end_val, "^[\"']*(.-)[\"']*$", "%1")
相关推荐
小南家的青蛙8 小时前
基于Spring Boot 2.x开发的微服务访问Redis的方案
redis·springboot
这个昵称不容易9 小时前
旅游管理系统的设计与实现(SpringBoot + MySQL + Vue 前后端分离)
java·springboot·毕设
这个昵称不容易1 天前
SpringBoot 美容院管理系统:232个Java文件315个接口,5套角色从预约到发货一整条链路
java·springboot·毕设
这个昵称不容易2 天前
SpringBoot「衣依」服装销售平台:144个Java文件180个接口,从挑衣服到收货评价整条链路都在
java·springboot·毕设
这个昵称不容易3 天前
SpringBoot 读书笔记共享平台部署实战:97 个 Java 文件 / 113 个接口 / 10 张表
java·springboot·毕设
努力努力再努力wz3 天前
【Lua进阶系列】Lua 调试机制详解:从虚拟机调用栈、错误传播到 debug 库
junit·单元测试·lua
RuoyiOffice4 天前
Vue3+Gantt:把任务排期、依赖关系和里程碑放进项目管理
项目管理·vue3·springboot·dhtmlx-gantt·wbs·gantt·ruoyi office
AI源码5 天前
学生成绩管理系统
springboot
炸薯条!5 天前
博客系统测试报告
开发语言·lua
无所谓^_^6 天前
个人健康管理系统 | Springboot3 | Vue3
java·vue·毕业设计·springboot·课程设计·毕设·前后端分离