Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if something is a member of an ordered set?

From what I see there's no command for this, but I need something similar to the SISMEMBER command, but for ordered sets. Given that there's no command for this, what's the best way to determine if something is a member of an ordered set ? Maybe ask for the score of the member with ZCORE and if there's no score than there's no member ?

like image 827
João Pinto Jerónimo Avatar asked May 25 '11 01:05

João Pinto Jerónimo


1 Answers

As you suggested, I would just use ZSCORE. If nil is returned, then the requested member is not in the set. ZRANK would also work, but it's O(log n) and ZSCORE is O(1).

redis> zadd orderedset 1 key1
(integer) 1
redis> zadd orderedset 2 key2
(integer) 1
redis> zscore orderedset key1
"1"
redis> zscore orderedset badkey
(nil)
like image 150
overthink Avatar answered Oct 16 '22 19:10

overthink