Warm tip: This article is reproduced from stackoverflow.com, please click
exception lua redis jedis

redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_s

发布于 2020-04-13 09:44:47

I'm writing a lua script in redis, and execute it in spring, the content is as simple as

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

but when I run this script, an exception throws

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

And I tried the following script in redis-cli

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

It returns

(integer) 100

And the Java code is showing as follows:

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

Now I'm so confused and any help will be grateful

Questioner
Arrow.Tao
Viewed
82
LeoMurillo 2020-02-03 12:16

You want to use jedis.evalsha instead of jedis.eval.

The error you are getting is Redis server trying to interpret 262b4ca69c1805485d135aa6298c2b00bc7c8c09 as an actual script. To invoke a previously loaded script you use EVALSHA command.