【问题标题】:redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_script:1: malformed number nearredis.clients.jedis.exceptions.JedisDataException:ERR错误编译脚本(新功能):user_script:1:附近的格式错误
【发布时间】:2020-05-18 20:08:14
【问题描述】:

我在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) ;

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

【问题讨论】:

    标签: exception redis lua jedis


    【解决方案1】:

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

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

    【讨论】:

    • 哇,你太棒了。成功了,非常感谢。
    • @Arrow.Tao,很乐意为您提供帮助,如果解决了您的问题,请将答案标记为已接受,如果您喜欢,请点赞
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 2023-02-13
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2013-03-13
    相关资源
    最近更新 更多