Isaac Lewis
Isaac Lewis

Reputation: 1199

Return new, unique items from redis set

I am using redis to store a list of items in a set. I add a very similar list to the set periodically, and obviously, the sorted set only adds new items if they don't already exist. Is there a way to get that list of new items that were just added? The ones that didn't already exist in the set?

Danke schoen.

Upvotes: 1

Views: 474

Answers (2)

The Real Bill
The Real Bill

Reputation: 15773

In addition to iterating over your new list as described by Sergio, you could store the keys from your sorted set in a set, store your new list in a set and do an sdiff or sdiffstore on them to get the difference.

Which route will be better is a judgement call for you based in your code and data set. If the new list is short if probably go with simple iteration using the zadd command like Sergio describes. Otherwise I'd test to see which is better for my use case.

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

ZADD command, if called with one score/member pair, returns 1 if member was new and 0 if it already existed. You can use this. Add elements one by one and check return values.

Upvotes: 3

Related Questions