SSCAN
- 如果不使用通配符,
通常情况下
返回新游标和集合中的所有成员
- 如果使用通配符,返回新游标和匹配到的成员,或者未匹配到时,返回新游标和空列表
Command
$ redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> sadd sparrow lake lose piano guitar
(integer) 4
127.0.0.1:6379> sscan sparrow 0
1) "0"
2) 1) "piano"
2) "guitar"
3) "lose"
4) "lake"
127.0.0.1:6379> sscan sparrow 0 match l*
1) "0"
2) 1) "lose"
2) "lake"
127.0.0.1:6379> sscan sparrow 0 match fake*
1) "0"
2) (empty list or set)
Code
func sscan(c redis.Conn) {
defer c.Do("DEL", "sparrow")
c.Do("SADD", "sparrow", "lake", "lose", "piano", "guitar")
// 1. If you don't use wildcards, will return new cursor and all the members in general.
fmt.Println("If you don't use wildcards, will return new cursor and all the members in general.")
sscanResult, err := redis.Values(c.Do("sscan", "sparrow", 0))
if err != nil {
colorlog.Error(err.Error())
return
}
var cursor int
var members []string
if _, err = redis.Scan(sscanResult, &cursor, &members); err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("The new cursor is:", cursor)
for i, v := range members {
fmt.Println("member", i, "is:", v)
}
// If use wildcards, will return new cursor and the matched members or empty list.
fmt.Println("If use wildcards, will return new cursor and the matched members or empty list.")
sscanResult, err = redis.Values(c.Do("sscan", "sparrow", 0, "MATCH", "l*"))
if err != nil {
colorlog.Error(err.Error())
return
}
if _, err = redis.Scan(sscanResult, &cursor, &members); err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("The new cursor is:", cursor)
for i, v := range members {
fmt.Println("matched member", i, "is:", v)
}
sscanResult, err = redis.Values(c.Do("sscan", "sparrow", 0, "MATCH", "fake*"))
if err != nil {
colorlog.Error(err.Error())
return
}
if _, err = redis.Scan(sscanResult, &cursor, &members); err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("The new cursor is:", cursor)
if len(members) == 0 {
fmt.Println("If no matched members, will return empty list.")
}
}
Output
$ go run main.go
If you don't use wildcards, will return new cursor and all the members in general.
The new cursor is: 0
member 0 is: piano
member 1 is: guitar
member 2 is: lose
member 3 is: lake
If use wildcards, will return new cursor and the matched members or empty list.
The new cursor is: 0
matched member 0 is: lose
matched member 1 is: lake
The new cursor is: 0
If no matched members, will return empty list.