ZCARD
返回有序集合中成员数量。当key不存在时,返回0
Command
$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> zadd animal 1 cat 2 dog 3 fish
(integer) 3
127.0.0.1:6379> EXISTS fakeSortedSetKey
(integer) 0
127.0.0.1:6379> zcard fakeSortedSetKey
(integer) 0
Code
func zcard(c redis.Conn) {
defer c.Do("DEL", "animal")
c.Do("ZADD", "animal", 1, "cat", 2, "dog", 3, "fish")
countAllMembers, err := redis.Int(c.Do("ZCARD", "animal"))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("The number of all members is:", countAllMembers)
// If key doesn't exist, returns 0
isExist, _ := c.Do("EXISTS", "fakeSortedSetKey")
if isExist == 1 {
c.Do("DEL", "fakeSortedSetKey")
}
countAllMembers, err = redis.Int(c.Do("ZCARD", "fakeSortedSetKey"))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("The number of all members of fake key is:", countAllMembers)
}
Output
$ go run main.go
The number of all members is: 3
The number of all members of fake key is: 0
网友评论