python redis 有序集合sorted set检查某个键是否存在

前端之家收集整理的这篇文章主要介绍了python redis 有序集合sorted set检查某个键是否存在前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

redis有序集合(Sorted Set)命令

ZADD
ZREM
ZCARD
ZCOUNT
Zscore
ZINCRBY
ZRANGE
ZREVRANGE
ZRANGEBYscore
ZREVRANGEBYscore
ZRANK
ZREVRANK
ZREMRANGEBYRANK
ZREMRANGEBYscore
ZINTERSTORE
ZUNIONSTORE

从上面命令中看到, redis的有序集合(Sorted Set)没有命令判断键是否存在于有序集合中。

经过一番研究,可以通过zrank()方法解决

zrank 

功能:返回有序集中指定成员的排名。

命令基本语法如下:

redis 127.0.0.1:6379> ZRANK key member

demo

>> ZRANGE salary 0 -1 WITHscoreS        # 显示所有成员及其 score 值
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


猜你在找的Redis相关文章