mirror of
https://github.com/redis/go-redis.git
synced 2025-07-28 06:42:00 +03:00
Add ScanType command to Scan with 'type' option
As of version 6.0 you can use this 'type' option to ask SCAN to only return objects that match a given type, allowing you to iterate through the database looking for keys of a specific type.
This commit is contained in:
@ -248,6 +248,34 @@ func ExampleClient_Scan() {
|
||||
// Output: found 33 keys
|
||||
}
|
||||
|
||||
func ExampleClient_ScanType() {
|
||||
rdb.FlushDB(ctx)
|
||||
for i := 0; i < 33; i++ {
|
||||
err := rdb.Set(ctx, fmt.Sprintf("key%d", i), "value", 0).Err()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
var cursor uint64
|
||||
var n int
|
||||
for {
|
||||
var keys []string
|
||||
var err error
|
||||
keys, cursor, err = rdb.ScanType(ctx, cursor, "key*", 10, "string").Result()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
n += len(keys)
|
||||
if cursor == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("found %d keys\n", n)
|
||||
// Output: found 33 keys
|
||||
}
|
||||
|
||||
func ExampleClient_Pipelined() {
|
||||
var incr *redis.IntCmd
|
||||
_, err := rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
||||
|
Reference in New Issue
Block a user