forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
282 lines (241 loc) · 5.29 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package redis_test
import (
"fmt"
"strconv"
"sync"
"time"
"gopkg.in/redis.v3"
)
var client *redis.Client
func init() {
client = redis.NewClient(&redis.Options{
Addr: ":6379",
})
client.FlushDb()
}
func ExampleNewClient() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
pong, err := client.Ping().Result()
fmt.Println(pong, err)
// Output: PONG <nil>
}
func ExampleNewFailoverClient() {
// See http://redis.io/topics/sentinel for instructions how to
// setup Redis Sentinel.
client := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "master",
SentinelAddrs: []string{":26379"},
})
client.Ping()
}
func ExampleNewClusterClient() {
// See http://redis.io/topics/cluster-tutorial for instructions
// how to setup Redis Cluster.
client := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"},
})
client.Ping()
}
func ExampleNewRing() {
client := redis.NewRing(&redis.RingOptions{
Addrs: map[string]string{
"shard1": ":7000",
"shard2": ":7001",
"shard3": ":7002",
},
})
client.Ping()
}
func ExampleClient() {
err := client.Set("key", "value", 0).Err()
if err != nil {
panic(err)
}
val, err := client.Get("key").Result()
if err != nil {
panic(err)
}
fmt.Println("key", val)
val2, err := client.Get("key2").Result()
if err == redis.Nil {
fmt.Println("key2 does not exists")
} else if err != nil {
panic(err)
} else {
fmt.Println("key2", val2)
}
// Output: key value
// key2 does not exists
}
func ExampleClient_Set() {
// Last argument is expiration. Zero means the key has no
// expiration time.
err := client.Set("key", "value", 0).Err()
if err != nil {
panic(err)
}
// key2 will expire in an hour.
err = client.Set("key2", "value", time.Hour).Err()
if err != nil {
panic(err)
}
}
func ExampleClient_Incr() {
if err := client.Incr("counter").Err(); err != nil {
panic(err)
}
n, err := client.Get("counter").Int64()
fmt.Println(n, err)
// Output: 1 <nil>
}
func ExampleClient_Scan() {
client.FlushDb()
for i := 0; i < 33; i++ {
err := client.Set(fmt.Sprintf("key%d", i), "value", 0).Err()
if err != nil {
panic(err)
}
}
var cursor int64
var n int
for {
var keys []string
var err error
cursor, keys, err = client.Scan(cursor, "", 10).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 := client.Pipelined(func(pipe *redis.Pipeline) error {
incr = pipe.Incr("counter1")
pipe.Expire("counter1", time.Hour)
return nil
})
fmt.Println(incr.Val(), err)
// Output: 1 <nil>
}
func ExamplePipeline() {
pipe := client.Pipeline()
defer pipe.Close()
incr := pipe.Incr("counter2")
pipe.Expire("counter2", time.Hour)
_, err := pipe.Exec()
fmt.Println(incr.Val(), err)
// Output: 1 <nil>
}
func ExampleMulti() {
// Transactionally increments key using GET and SET commands.
incr := func(tx *redis.Multi, key string) error {
err := tx.Watch(key).Err()
if err != nil {
return err
}
n, err := tx.Get(key).Int64()
if err != nil && err != redis.Nil {
return err
}
_, err = tx.Exec(func() error {
tx.Set(key, strconv.FormatInt(n+1, 10), 0)
return nil
})
return err
}
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
tx := client.Multi()
defer tx.Close()
for {
err := incr(tx, "counter3")
if err == redis.TxFailedErr {
// Retry.
continue
} else if err != nil {
panic(err)
}
break
}
}()
}
wg.Wait()
n, err := client.Get("counter3").Int64()
fmt.Println(n, err)
// Output: 10 <nil>
}
func ExamplePubSub() {
pubsub, err := client.Subscribe("mychannel")
if err != nil {
panic(err)
}
defer pubsub.Close()
err = client.Publish("mychannel", "hello").Err()
if err != nil {
panic(err)
}
for i := 0; i < 4; i++ {
msgi, err := pubsub.ReceiveTimeout(100 * time.Millisecond)
if err != nil {
err := pubsub.Ping("")
if err != nil {
panic(err)
}
continue
}
switch msg := msgi.(type) {
case *redis.Subscription:
fmt.Println(msg.Kind, msg.Channel)
case *redis.Message:
fmt.Println(msg.Channel, msg.Payload)
case *redis.Pong:
fmt.Println(msg)
default:
panic(fmt.Sprintf("unknown message: %#v", msgi))
}
}
// Output: subscribe mychannel
// mychannel hello
// Pong
}
func ExampleScript() {
IncrByXX := redis.NewScript(`
if redis.call("GET", KEYS[1]) ~= false then
return redis.call("INCRBY", KEYS[1], ARGV[1])
end
return false
`)
n, err := IncrByXX.Run(client, []string{"xx_counter"}, []string{"2"}).Result()
fmt.Println(n, err)
err = client.Set("xx_counter", "40", 0).Err()
if err != nil {
panic(err)
}
n, err = IncrByXX.Run(client, []string{"xx_counter"}, []string{"2"}).Result()
fmt.Println(n, err)
// Output: <nil> redis: nil
// 42 <nil>
}
func Example_customCommand() {
Get := func(client *redis.Client, key string) *redis.StringCmd {
cmd := redis.NewStringCmd("GET", key)
client.Process(cmd)
return cmd
}
v, err := Get(client, "key_does_not_exist").Result()
fmt.Printf("%q %s", v, err)
// Output: "" redis: nil
}