skip to Main Content

Following is my lua script

if redis.call('sismember',KEYS[1],ARGV[1])==1
then redis.call('srem',KEYS[1],ARGV[1])
else return 0
end
store = tonumber(redis.call('hget',KEYS[2],'capacity'))
store = store + 1
redis.call('hset',KEYS[2],'capacity',store)
return 1

when I run this srcipt in Java, An exception like

@user_script:1: WRONGTYPE Operation against a key holding the wrong kind of value

throws, the Java code is like

Object ojb = jedis.evalsha(sha,2,userName.getBytes(),
                id.getBytes(),id.getBytes()) ;

where userName is “tau” and id is “002” in my code,
and I test the type of “tau” and “002” as follows,

127.0.0.1:6379> type tau
set
127.0.0.1:6379> type 002
hash

and exactly, the content of them are :

127.0.0.1:6379> hgetall 002
name
"鏁版嵁搴撲粠鍒犲簱鍒拌窇璺?
teacher
"taochq"
capacity
54
127.0.0.1:6379> smembers tau
002
004
001
127.0.0.1:6379>

Now I’m so confused and don’t know what’s wrong, any help will be grateful

2

Answers


  1. The error is quite verbose – you’re trying to perform an operation on key of the wrong type.

    Run MONITOR alongside and then your script – then you’ll be able to spot the error easily.

    Login or Signup to reply.
  2. Try your script as:

    EVAL "if redis.call('sismember',KEYS[1],ARGV[1])==1 n then redis.call('srem',KEYS[1],ARGV[1]) n else return 0 n end n local store = tonumber(redis.call('hget',KEYS[2],'capacity')) n store = store + 1 n redis.call('hset',KEYS[2],'capacity',store) n return 1" 2 tau 002 002
    

    You’ll see if it works. Most likely, userName.getBytes() and id.getBytes() are not returning what you expect. Use MONITOR as Itamar suggests to see what’s actually reaching the server.

    You’ll get a separate a different issue: Script attempted to create global variable 'store'. Add local to the 5th line:

    local store = tonumber(redis.call('hget',KEYS[2],'capacity'))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search