在项目中,需要用lua脚本操作redis cluster中的多个key,但是非同slot的时候会报错,例如下面test3、test6在同一个node,但是却不是同一个slot。redis使用lua脚本可以这样redis-cli -a xxxxx--eval demo.lua key1 key2 , val1 val2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
banfushen@ma100:~/redis-cluster$ redis-cli -p 16380 -c 192.168.88.7:6379> set test3 3333 -> Redirected to slot [13026] located at 192.168.88.3:6379 OK 192.168.88.6:6379> set test5 3333 -> Redirected to slot [4644] located at 192.168.88.5:6379 OK 192.168.88.5:6379> set test6 3333 -> Redirected to slot [8775] located at 192.168.88.3:6379 OK 192.168.88.3:6379> cluster keyslot test3 (integer) 13026 192.168.88.3:6379> cluster keyslot test6 (integer) 8775 192.168.88.3:6379>
通过key传入
一般在redis cluster中使用lua脚本,会碰到(error) CROSSSLOT Keys in request don't hash to the same slot
1 2 3 4 5 6 7 8 9 10
banfushen@ma100:~/test$ cat get.lua local key1 = KEYS[1] local key1 = KEYS[2]
local value1 = redis.call("GET", key1) local value2 = redis.call("GET", key2)
return {value1, value2} banfushen@ma100:~/test$ redis-cli -p 16380 -c --eval get.lua test3 test6 (error) CROSSSLOT Keys in request don't hash to the same slot