Kevvvvyp
Kevvvvyp

Reputation: 1774

How to pop multiple items from a REDIS SET atomically without using SPOP <set> <count>?

SPOP [set] [count] was introduced in Redis v3.2 - https://redis.io/commands/spop , my REDIS version is 2.7.

How can I atomically pop several items from a SET using cli commands?

Is it possible to do something like...?

MULTI 
a = SPOP myset //It would be nice if I could store this in a variable?
b = SPOP myset
...
SREM a
SREM b
...
EXEC
//

Upvotes: 0

Views: 801

Answers (1)

Efran Cobisi
Efran Cobisi

Reputation: 6484

Yes, MULTI combined with a sequence of SPOPs would return the results as part of the EXEC call:

each element being the reply to each of the commands in the atomic transaction

source: https://redis.io/commands/exec

MULTI
SPOP myset
SPOP myset
EXEC

As an alternative, you could also use a Lua script, with EVAL being introduced in Redis 2.6: this would allow you to use variables (hosted within the scope of the script itself, which is being run on the Redis process) and alike but may be more complex, possibly overkill for your scenario.

As a side note, SPOP is already removing items, so there is no need to SREM them.

Upvotes: 2

Related Questions