SISMEMBER
- 如果成员存在于集合,返回1
- 如果成员不存在于集合,返回0
- 如果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
网友评论