redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_script:1: 号码格式错误

我正在Redis中编写一个Lua脚本,并在Spring中执行它,内容如下

local store =   redis.call('hget',KEYS[1],'capacity')
print(store)
if store <= 0
then return 0
end
store = store - 1
redis.call('hset',KEYS[1],'capacity',store)
redis.call('sadd',KEYS[2],ARGV[1])
return 1

但当我运行这个脚本时,会抛出一个异常

redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_script:1: 号码格式错误 '262b4ca69c1805485d135aa6298c2b00bc7c8c09'

我在redis-cli中尝试以下脚本

eval "local s = tonumber(redis.call('hget',KEYS[1],'capacity')) return s" 1 001

它返回

(integer) 100

Java代码如下所示:

String script ="local store =   redis.call('hget',KEYS[1],'capacity')\n" +
                "print(store)\n" +
                "if store <= 0\n" +
                "then return 0\n" +
                "end\n" +
                "store = store - 1\n" +
                "redis.call('hset',KEYS[1],'capacity',store)\n" +
                "redis.call('sadd',KEYS[2],ARGV[1])\n" +
                "return 1\n" +
                "\n";

        if(sha==null){
            sha = jedis.scriptLoad(script) ;
            System.out.println("sha:"+sha);
        }
        Object ojb = jedis.eval(sha,2,id,userName,id) ;

现在我很困惑,任何帮助都将不胜感激

点赞
用户7017466
用户7017466

你应该使用jedis.evalsha而不是jedis.eval

你收到的错误是Redis服务器尝试将262b4ca69c1805485d135aa6298c2b00bc7c8c09解释为实际脚本。要调用以前加载的脚本,您需要使用EVALSHA命令

2020-02-03 04:16:46