redis.clients.jedis.exceptions.JedisDataException: ERR 错误编译脚本(新功能):user_script:1: malformed number near

redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_script:1: malformed number near

我在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: malformed number near '262b4ca69c1805485d135aa6298c2b00bc7c8c09' 

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

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

它returns

(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) ;

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

您想使用 jedis.evalsha 而不是 jedis.eval

您收到的错误是 Redis 服务器试图将 262b4ca69c1805485d135aa6298c2b00bc7c8c09 解释为实际脚本。要调用之前加载的脚本,您可以使用 EVALSHA command.