redis: atomic LPOP and SADD possible?

Is there anyway to atomically pop an item from a list and add it to a set?

My case scenario is that I have a "work queue" list of unique items, and I want to track what's being worked on in a "in progress" set. This would also allow the items in the "in progress" set to be re-queued if my worker process crashes while working on an item.

I'd prefer it to be atomic so that anything popped from the list will always be in the set. I just can't figure out how to do this with MULTI/EXEC, ie:

redis> MULTI
OK
redis> LPOP workqueue
"foobar"
redis> SADD inprog "foobar"
redis> EXEC

Answers


Why do you want your "in progress" collection be a Set? You could simply use a List for the in progress items.

The command RPOPLPUSH "Right Pop, Left Push" was made exactly for this use case.

Atomically returns and removes the last element (tail) of the list stored at source, and pushes the element at the first element (head) of the list stored at destination

If you do want to use a Set for your in progress items, you will have to use a lua script and call it using eval.


Need Your Help

Select categories from cat_table where main_table items with that category not empty

mysql select join group-by

I couldn't think of the proper way to phrase this and maybe if I knew the correct phrase I could Google it properly but as of yet I am struggling.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.