SISMEMBER

作者: NotFoundW | 来源:发表于2020-04-16 12:44 被阅读0次

SISMEMBER

  1. 如果成员存在于集合,返回1
  2. 如果成员不存在于集合,返回0
  3. 如果key不存在,返回0

Command

$ redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> sadd mantis 1 2 3 4(integer) 4
127.0.0.1:6379> sismember mantis 4(integer) 1
127.0.0.1:6379> sismember mantis 8
(integer) 0
127.0.0.1:6379> exists fakeSetKey
(integer) 0
127.0.0.1:6379> sismember fakeSetKey 1
(integer) 0

Code

func sismember(c redis.Conn) {
    defer c.Do("DEL", "mantis")
    c.Do("SADD", "mantis", 1, 2, 3, 4)
    //  1. If specified member exists in set, will return 1.
    isMember, err := c.Do("SISMEMBER", "mantis", 4)
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("If specified member exists in set, will return:", isMember)
    //  2. If specified member doesn't exist in set, will return 0.
    isMember, err = c.Do("SISMEMBER", "mantis", 8)
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("If specified member doesn't exist in set, will return:", isMember)
    //  3. If key doesn't exist, will return 0
    keyIsExist, _ := c.Do("EXISTS", "fakeSetKey")
    if keyIsExist == 1 {
        c.Do("DEL", "fakeSetKey")
    }
    isMember, err = c.Do("SISMEMBER", "fakeSetKey", 1)
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("If key doesn't exist, will return:", isMember)
}

Output

$ go run main.go
If specified member exists in set, will return: 1
If specified member doesn't exist in set, will return: 0
If key doesn't exist, will return: 0

相关文章

  • SISMEMBER

    SISMEMBER 如果成员存在于集合,返回1 如果成员不存在于集合,返回0 如果key不存在,返回0 Comma...

  • redis 第七讲 Set

    单值多value sadd / smembers / sismember scard 获取集合里面的元素个数 sr...

网友评论

      本文标题:SISMEMBER

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