-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathtopk_tutorial_test.go
77 lines (58 loc) · 1.24 KB
/
topk_tutorial_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// EXAMPLE: topk_tutorial
// HIDE_START
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
// HIDE_END
func ExampleClient_topk() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// REMOVE_START
// start with fresh database
rdb.FlushDB(ctx)
rdb.Del(ctx, "bikes:keywords")
// REMOVE_END
// STEP_START topk
res1, err := rdb.TopKReserve(ctx, "bikes:keywords", 5).Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> OK
res2, err := rdb.TopKAdd(ctx, "bikes:keywords",
"store",
"seat",
"handlebars",
"handles",
"pedals",
"tires",
"store",
"seat",
).Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> [ handlebars ]
res3, err := rdb.TopKList(ctx, "bikes:keywords").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // [store seat pedals tires handles]
res4, err := rdb.TopKQuery(ctx, "bikes:keywords", "store", "handlebars").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // [true false]
// STEP_END
// Output:
// OK
// [ handlebars ]
// [store seat pedals tires handles]
// [true false]
}