ZCARD

作者: NotFoundW | 来源:发表于2020-04-17 14:36 被阅读0次

    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
    

    相关文章

      网友评论

          本文标题:ZCARD

          本文链接:https://www.haomeiwen.com/subject/afxyvhtx.html