温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_s
exception lua redis jedis

其他 - redis.clients.jedis.exceptions.JedisDataException:ERR错误编译脚本(新功能):user_s

发布于 2020-04-13 17:11:33

我正在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

它返回

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

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

查看更多

提问者
Arrow.Tao
被浏览
98
LeoMurillo 2020-02-03 12:16

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

您遇到的错误是Redis服务器想解释262b4ca69c1805485d135aa6298c2b00bc7c8c09为实际脚本。要调用以前加载的脚本,请使用EVALSHAcommand