Python redis ordered set sorted set checks whether a key exists

created at 07-24-2021 views: 11

Redis ordered set (Sorted Set) command

ZADD
ZREM
ZCARD
ZCOUNT
ZSCORE
ZINCRBY
ZRANGE
ZREVRANGE
ZRANGEBYSCORE
ZREVRANGEBYSCORE
ZRANK
ZREVRANK
ZREMRANGEBYRANK
ZREMRANGEBYSCORE
ZINTERSTORE
ZUNIONSTORE

As you can see from the above command, the ordered set of redis (Sorted Set) has no command to determine whether the key exists in the ordered set.

After some research, it can be solved by the zrank() method.

zrank

Function: Return the ranking of the specified member in the ordered set.

The basic syntax of the command is as follows:

redis 127.0.0.1:6379> ZRANK key member

demo

# Display all members and their score values
>> ZRANGE salary 0 -1 WITHSCORES        
1) "peter"
2) "3500"
3) "tom"
4) "4000"
5) "jack"
6) "5000"

>> ZRANK salary tom  
(integer) 1

>> ZRANK salary linly
(None)
from redis import Redis

conn=Redis()

def zexist(self, name, value):
    if conn.zrank(name, value):
        return True
    return False
created at:07-24-2021
edited at: 07-24-2021: