主要没看见啥好的文档讲解,自己翻了翻源码,记录一下用法,见笑了
原理
在调用pipeline每次append命令时,会返回一个对应的xxxxCmd对象指针,保留这个指针即可,在Exec()函数执行完成后,会将结果写入对应的对象内
demo
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
)
var (
client *redis.Client
)
func init() {
client = redis.NewClient(&redis.Options{
Addr: "local.arch:6379",
})
}
func batchSet() {
pipeline := client.Pipeline()
ctx := context.Background()
for i := 0; i < 100; i++ {
key := fmt.Sprintf("%d", i)
pipeline.HSet(ctx, key, map[string]interface{}{key: key})
}
_, err := pipeline.Exec(ctx)
if err != nil {
panic(err)
}
}
func batchGet() {
pipeline := client.Pipeline()
ctx := context.Background()
result := make([]*redis.StringStringMapCmd, 0)
for i := 0; i < 100; i++ {
key := fmt.Sprintf("%d", i)
result = append(result, pipeline.HGetAll(ctx, key))
}
_, _ = pipeline.Exec(ctx)
for _, r := range result {
v, err := r.Result()
if err != nil {
panic(err)
//fmt.Println(err)
}
fmt.Println(v)
}
}
func main() {
batchGet()
}
|
执行结果