Search code examples
luaredisserver-side

Redis Server Side Script - ZREM multiple values at once


I am building out a Redis server side script and my lack of Lua experience has me a little stuck.

Essentially have a large "main" sorted set (containing ~1,000 values) which I want to remove any keys found in a smaller "remove" set (containing ~100 values). Think sdiffstore but with the member scores in the "main" set being important.

A simple example:

"main"
ZADD main 6 "one"
ZADD main 7 "two"
ZADD main 8 "three"
ZADD main 9 "four"
ZADD main 10 "five"

"remove"
ZADD remove "two"
ZADD remove "four"

desired output (stored in a sorted set):
6 "one"
8 "three"
10 "five"

So on the lua server side script:

--This works

redis.call('zrem','main', 'two','four')

--This doesn't

local temp = redis.call('smembers','remove')    
redis.call('zrem','main', temp )

I have tried a number of iterations of my syntax to no avail. Any ideas?

I hope to avoid converting the "remove" members into some sort of string or loop though and removing one member at a time.

Thanks


Solution

  • If I correctly understand your problem, you should be able to use the unpack function:

    local temp = redis.call('smembers','remove')    
    redis.call('zrem','main', unpack(temp))
    

    unpack will "unpack" the table into it's members.