SRANDMEMBER
- 不指定count时,默认随机返回一个成员
- 指定count时,如果count小于集合成员数,则返回一个包含count个随机元素的List。
- 指定count时,如果count大于等于集合成员数,则以List形式返回整个集合
- 如果count为负数,且count的绝对值小于集合成员数,则返回一个包含count个随机元素的List,但是元素可能会重复出现多次
- 如果count为负数,且count的绝对值等于或者大于集合成员数,则返回一个包含count个随即元素的List,元素同样可能会重复多次。但是,即使count绝对值大于集合成员数,但是也不一定会返回所有集合成员
- 如果指定count为0,则返回一个空列表
Command
$ redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> sadd boa 1 2 3 4
(integer) 4
127.0.0.1:6379> srandmember boa
"4"
127.0.0.1:6379> srandmember boa 2
1) "3"
2) "1"
127.0.0.1:6379> srandmember boa 5
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> srandmember boa -3
1) "3"
2) "3"
3) "4"
127.0.0.1:6379> srandmember boa -4
1) "3"
2) "2"
3) "1"
4) "3"
127.0.0.1:6379> srandmember boa -5
1) "1"
2) "3"
3) "4"
4) "4"
5) "1"
127.0.0.1:6379> srandmember boa 0
(empty list or set)
Code
func srandmember(c redis.Conn) {
defer c.Do("DEL", "boa")
c.Do("SADD", "boa", 1, 2, 3, 4)
// 1. If you don't specify count, will return a rand member as default.
randMember, err := redis.Int(c.Do("SRANDMEMBER", "boa"))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("If you don't specify a number, will return a rand member as default:", randMember)
// 2. If specified count is less than the number of members of set,
// will return a list containing count random members.
randMembers, err := redis.Ints(c.Do("SRANDMEMBER", "boa", 2))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("If specified count is less than the number of members of set,")
fmt.Println("will return a list containing count random members:", randMembers)
// 3. If specified count is greater than or equal with the number of members of set,
// will return a list containing all the members.
randMembers, err = redis.Ints(c.Do("SRANDMEMBER", "boa", 5))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("If specified count is greater than or equal with the number of members of set,")
fmt.Println("will return a list containing all the members:", randMembers)
// 4. If specified count is a negative number and count's absolute value is less than the number of members of set
// will return a list containing count random members.
// But some elements may be repeated many times.
randMembers, err = redis.Ints(c.Do("SRANDMEMBER", "boa", -3))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("If specified count is a negative number, " +
"and count's absolute value is less than the number of members of set")
fmt.Println("will return a list containing count random members.")
fmt.Println("But some elements may be repeated many times:", randMembers)
// 5. If specified count is a negative number and count's absolute value is greater than or equal with
// the number of members of set, will return a list containing count random members.
// But some elements may be repeated many times.
randMembers, err = redis.Ints(c.Do("SRANDMEMBER", "boa", -4))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("If specified count is a negative number " +
"and count's absolute value is greater than or equal with the number of members of set,")
fmt.Println("will return a list containing count random members.")
fmt.Println("But some elements may be repeated many times:", randMembers)
randMembers, err = redis.Ints(c.Do("SRANDMEMBER", "boa", -5))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("But some elements may be repeated many times:", randMembers)
// 6. If count is 0, will return an empty list.
nilRandMember, err := redis.Ints(c.Do("SRANDMEMBER", "boa", 0))
if err != nil {
colorlog.Error(err.Error())
return
}
if len(nilRandMember) == 0 {
fmt.Println("If count is 0, will return an empty list.")
}
}
Output
$ go run main.go
If you don't specify a number, will return a rand member as default: 4
If specified count is less than the number of members of set,
will return a list containing count random members: [3 1]
If specified count is greater than or equal with the number of members of set,
will return a list containing all the members: [1 2 3 4]
If specified count is a negative number, and count's absolute value is less than the number of members of set
will return a list containing count random members.
But some elements may be repeated many times: [1 3 3]
If specified count is a negative number and count's absolute value is greater than or equal with the number of members of set,
will return a list containing count random members.But some elements may be repeated many times: [4 4 1 3]
But some elements may be repeated many times: [3 1 3 3 1]
If count is 0, will return an empty list.
网友评论