Skip to content

Commit 77dabc3

Browse files
docs: another ScanType example with "Hash" as the KeyType (#2212)
* docs: example ScanType call that passes in Hash and gets all keys * docs: added further example_scantype * docs: updated comment of ExampleClient_ScanType_Type_Hash
1 parent 400ce60 commit 77dabc3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

example_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,36 @@ func ExampleClient_ScanType() {
278278
// Output: found 33 keys
279279
}
280280

281+
// ExampleClient_ScanType_Type_Hash uses the KeyType "hash"
282+
// Uses ScanType toZZ loop through lots of "keys" in Redis, 10 at a time, and add each key to a []string
283+
func ExampleClient_ScanType_Type_Hash() {
284+
rdb.FlushDB(ctx)
285+
for i := 0; i < 33; i++ {
286+
err := rdb.HSet(context.TODO(), fmt.Sprintf("key%d", i), "value", "foo").Err()
287+
if err != nil {
288+
panic(err)
289+
}
290+
}
291+
292+
var allKeys []string
293+
var cursor uint64
294+
var err error
295+
296+
for {
297+
var keysFromScan []string
298+
keysFromScan, cursor, err = rdb.ScanType(context.TODO(), cursor, "key*", 10, "hash").Result()
299+
if err != nil {
300+
panic(err)
301+
}
302+
allKeys = append(allKeys, keysFromScan...)
303+
if cursor == 0 {
304+
break
305+
}
306+
}
307+
fmt.Printf("%d keys ready for use\n", len(allKeys))
308+
// Output: 33 keys ready for use\n
309+
}
310+
281311
// ExampleMapStringStringCmd_Scan shows how to scan the results of a map fetch
282312
// into a struct.
283313
func ExampleMapStringStringCmd_Scan() {

0 commit comments

Comments
 (0)