SINTER
返回给定所有给定集合的交集。
不存在的key被视为空集。当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。
Command
$ redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> sadd shrimp 1 2
(integer) 2
127.0.0.1:6379> sadd crab 2 3
(integer) 2
127.0.0.1:6379> sinter shrimp crab
1) "2"
127.0.0.1:6379> exists fakeSetKey
(integer) 0
127.0.0.1:6379> sinter shrimp fakeSetKey
(empty list or set)
Code
func sinter(c redis.Conn) {
defer c.Do("DEL", "shrimp")
defer c.Do("DEL", "crab")
c.Do("SADD", "shrimp", 1, 2)
c.Do("SADD", "crab", 2, 3)
// 1. If given sets all are not empty set, return the intersection.
interMemberList, err := redis.Ints(c.Do("SINTER", "shrimp", "crab"))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("If given sets all are not empty set, return the intersection:", interMemberList)
// 2. If one of given key doesn't exist, it will considered an empty set.
// And will return empty list.
isExist, _ := c.Do("EXISTS", "fakeSetKey")
if isExist == 1 {
c.Do("DEL", "fakeSetKey")
}
interMemberList, err = redis.Ints(c.Do("SINTER", "shrimp", "fakeSetKey"))
if err != nil {
colorlog.Error(err.Error())
return
}
if len(interMemberList) == 0 {
fmt.Println("If one of given key doesn't exist, will return empty list.")
}
}
Output
$ go run main.go
If given sets all are not empty set, return the intersection: [2]
If one of given key doesn't exist, will return empty list.
网友评论