-
Notifications
You must be signed in to change notification settings - Fork 15
/
example_test.go
149 lines (131 loc) · 4.08 KB
/
example_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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package redispipe_test
import (
"context"
"fmt"
"log"
"github.com/joomcode/redispipe/redis"
"github.com/joomcode/redispipe/rediscluster"
"github.com/joomcode/redispipe/redisconn"
)
const databaseno = 0
const password = ""
var myhandle interface{} = nil
func Example_usage() {
ctx := context.Background()
cluster := false
SingleRedis := func(ctx context.Context) (redis.Sender, error) {
opts := redisconn.Opts{
DB: databaseno,
Password: password,
Logger: redisconn.NoopLogger{}, // shut up logging. Could be your custom implementation.
Handle: myhandle, // custom data, useful for custom logging
// Other parameters (usually, no need to change)
// IOTimeout, DialTimeout, ReconnectTimeout, TCPKeepAlive, Concurrency, WritePause, AsyncDial
}
conn, err := redisconn.Connect(ctx, "127.0.0.1:6379", opts)
return conn, err
}
ClusterRedis := func(ctx context.Context) (redis.Sender, error) {
opts := rediscluster.Opts{
HostOpts: redisconn.Opts{
// No DB
Password: password,
// Usually, no need for special logger
},
Name: "mycluster", // name of a cluster
Logger: rediscluster.NoopLogger{}, // shut up logging. Could be your custom implementation.
Handle: myhandle, // custom data, useful for custom logging
// Other parameters (usually, no need to change):
// ConnsPerHost, ConnHostPolicy, CheckInterval, MovedRetries, WaitToMigrate, RoundRobinSeed,
}
addresses := []string{"127.0.0.1:20001"} // one or more of cluster addresses
cluster, err := rediscluster.NewCluster(ctx, addresses, opts)
return cluster, err
}
var sender redis.Sender
var err error
if cluster {
sender, err = ClusterRedis(ctx)
} else {
sender, err = SingleRedis(ctx)
}
if err != nil {
log.Fatal(err)
}
defer sender.Close()
sync := redis.SyncCtx{sender} // wrapper for synchronous api
res := sync.Do(ctx, "SET", "key", "ho")
if err := redis.AsError(res); err != nil {
log.Fatal(err)
}
fmt.Printf("result: %q\n", res)
res = sync.Do(ctx, "GET", "key")
if err := redis.AsError(res); err != nil {
log.Fatal(err)
}
fmt.Printf("result: %q\n", res)
res = sync.Send(ctx, redis.Req("HMSET", "hashkey", "field1", "val1", "field2", "val2"))
if err := redis.AsError(res); err != nil {
log.Fatal(err)
}
res = sync.Send(ctx, redis.Req("HMGET", "hashkey", "field1", "field2", "field3"))
if err := redis.AsError(res); err != nil {
log.Fatal(err)
}
for i, v := range res.([]interface{}) {
fmt.Printf("%d: %T %q\n", i, v, v)
}
res = sync.Send(ctx, redis.Req("HMGET", "key", "field1"))
if err := redis.AsError(res); err != nil {
if rerr := redis.AsErrorx(res); rerr != nil && rerr.IsOfType(redis.ErrResult) {
fmt.Printf("expected error: %v\n", rerr)
} else {
fmt.Printf("unexpected error: %v\n", err)
}
} else {
fmt.Printf("unexpected missed error\n")
}
results := sync.SendMany(ctx, []redis.Request{
redis.Req("GET", "key"),
redis.Req("HMGET", "hashkey", "field1", "field3"),
})
// results is []interface{}, each element is result for corresponding request
for i, res := range results {
fmt.Printf("result[%d]: %T %q\n", i, res, res)
}
results, err = sync.SendTransaction(ctx, []redis.Request{
redis.Req("SET", "a{x}", "b"),
redis.Req("SET", "b{x}", 0),
redis.Req("INCRBY", "b{x}", 3),
})
if err != nil {
log.Fatal(err)
}
for i, res := range results {
fmt.Printf("tresult[%d]: %T %q\n", i, res, res)
}
scanner := sync.Scanner(ctx, redis.ScanOpts{Match: "*key*"})
for {
keys, err := scanner.Next()
if err != nil {
if err != redis.ScanEOF {
log.Fatal(err)
}
break
}
fmt.Printf("keys: %q", keys)
}
// Output:
// result: "OK"
// result: "ho"
// 0: []uint8 "val1"
// 1: []uint8 "val2"
// 2: <nil> %!q(<nil>)
// expected error: redispipe.result: WRONGTYPE Operation against a key holding the wrong kind of value {request: Req("HMGET", ["key" "field1"]), address: 127.0.0.1:6379}
// result[0]: []uint8 "ho"
// result[1]: []interface {} ["val1" <nil>]
// tresult[0]: string "OK"
// tresult[1]: string "OK"
// tresult[2]: int64 '\x03'
// keys: ["key" "hashkey"]
}