BeachRunnerFred
BeachRunnerFred

Reputation: 18578

How can I pop from a Redis sorted set?

I'm using a Redis sorted set as a queue to maintain a list of users and I need to pop the first item in the list and use it, but I'm having issues. Here's my attempt...

keys = []
$redis.multi do
    # Get the item at the top of the sorted set
    keys = $redis.zrange("users:waiting", 0, 0)

    # Remove it from the set
    $redis.zrem("users:waiting", keys[0])
end

# Get the item from the range
@user_id = keys[0]

...but this code just doesn't work. Help! Thanks in advance for your wisdom!

Upvotes: 1

Views: 1296

Answers (1)

Joe Doherty
Joe Doherty

Reputation: 3958

The reason it does not work is because of MULTI.

keys is not assigned. The call to EXEC which happens at end is when these results are returned

See https://github.com/redis/redis-rb#executing-commands-atomically

and just below it https://github.com/redis/redis-rb#futures

Should show the difference :) Apologies if this is not entirely correct as I don't use Ruby.

Upvotes: 1

Related Questions