Debian Codegraph @

example_test.go

raw ◀ ▶
1 Debian patch touch this file
1 package redis_test
2
3 import (
•4 // "fmt"
5 // "strconv"
6 // "sync"
7 "time"
8
9 "gopkg.in/redis.v5"
10 )
11
12 var client *redis.Client
13
14 func init() {
15 client = redis.NewClient(&redis.Options{
16 Addr: ":6379",
17 DialTimeout: 10 * time.Second,
18 ReadTimeout: 30 * time.Second,
19 WriteTimeout: 30 * time.Second,
20 PoolSize: 10,
21 PoolTimeout: 30 * time.Second,
22 })
23 client.FlushDb()
24 }
25
•26 //func ExampleNewClient() {
27 // client := redis.NewClient(&redis.Options{
28 // Addr: "localhost:6379",
29 // Password: "", // no password set
30 // DB: 0, // use default DB
31 // })
32 //
33 // pong, err := client.Ping().Result()
34 // fmt.Println(pong, err)
35 // // Output: PONG <nil>
36 //}
37
38 func ExampleNewFailoverClient() {
39 // See http://redis.io/topics/sentinel for instructions how to
40 // setup Redis Sentinel.
41 client := redis.NewFailoverClient(&redis.FailoverOptions{
42 MasterName: "master",
43 SentinelAddrs: []string{":26379"},
44 })
45 client.Ping()
46 }
47
48 func ExampleNewClusterClient() {
49 // See http://redis.io/topics/cluster-tutorial for instructions
50 // how to setup Redis Cluster.
51 client := redis.NewClusterClient(&redis.ClusterOptions{
52 Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"},
53 })
54 client.Ping()
55 }
56
57 func ExampleNewRing() {
58 client := redis.NewRing(&redis.RingOptions{
59 Addrs: map[string]string{
60 "shard1": ":7000",
61 "shard2": ":7001",
62 "shard3": ":7002",
63 },
64 })
65 client.Ping()
66 }
67
•68 //func ExampleClient() {
69 // err := client.Set("key", "value", 0).Err()
70 // if err != nil {
71 // panic(err)
72 // }
73 //
74 // val, err := client.Get("key").Result()
75 // if err != nil {
76 // panic(err)
77 // }
78 // fmt.Println("key", val)
79 //
80 // val2, err := client.Get("key2").Result()
81 // if err == redis.Nil {
82 // fmt.Println("key2 does not exists")
83 // } else if err != nil {
84 // panic(err)
85 // } else {
86 // fmt.Println("key2", val2)
87 // }
88 // // Output: key value
89 // // key2 does not exists
90 //}
91
92 func ExampleClient_Set() {
93 // Last argument is expiration. Zero means the key has no
94 // expiration time.
95 err := client.Set("key", "value", 0).Err()
96 if err != nil {
97 panic(err)
98 }
99
100 // key2 will expire in an hour.
101 err = client.Set("key2", "value", time.Hour).Err()
102 if err != nil {
103 panic(err)
104 }
105 }
106
•107 //func ExampleClient_Incr() {
108 // if err := client.Incr("counter").Err(); err != nil {
109 // panic(err)
110 // }
111 //
112 // n, err := client.Get("counter").Int64()
113 // fmt.Println(n, err)
114 // / / Output: 1 <nil>
115 //}
116 //
117 //func ExampleClient_BLPop() {
118 // if err := client.RPush("queue", "message").Err(); err != nil {
119 // panic(err)
120 // }
121 //
122 // // use `client.BLPop(0, "queue")` for infinite waiting time
123 // result, err := client.BLPop(1*time.Second, "queue").Result()
124 // if err != nil {
125 // panic(err)
126 // }
127 //
128 // fmt.Println(result[0], result[1])
129 // // Output: queue message
130 //}
131 //
132 //func ExampleClient_Scan() {
133 // client.FlushDb()
134 // for i := 0; i < 33; i++ {
135 // err := client.Set(fmt.Sprintf("key%d", i), "value", 0).Err()
136 // if err != nil {
137 // panic(err)
138 // }
139 // }
140 //
141 // var cursor uint64
142 // var n int
143 // for {
144 // var keys []string
145 // var err error
146 // keys, cursor, err = client.Scan(cursor, "", 10).Result()
147 // if err != nil {
148 // panic(err)
149 // }
150 // n += len(keys)
151 // if cursor == 0 {
152 // break
153 // }
154 // }
155 //
156 // fmt.Printf("found %d keys\n", n)
157 // // Output: found 33 keys
158 //}
159 //
160 //func ExampleClient_Pipelined() {
161 // var incr *redis.IntCmd
162 // _, err := client.Pipelined(func(pipe *redis.Pipeline) error {
163 // incr = pipe.Incr("pipelined_counter")
164 // pipe.Expire("pipelined_counter", time.Hour)
165 // return nil
166 // })
167 // fmt.Println(incr.Val(), err)
168 // // Output: 1 <nil>
169 //}
170 //
171 //func ExampleClient_Pipeline() {
172 // pipe := client.Pipeline()
173 //
174 // incr := pipe.Incr("pipeline_counter")
175 // pipe.Expire("pipeline_counter", time.Hour)
176 //
177 // // Execute
178 // //
179 // // INCR pipeline_counter
180 // // EXPIRE pipeline_counts 3600
181 // //
182 // // using one client-server roundtrip.
183 // _, err := pipe.Exec()
184 // fmt.Println(incr.Val(), err)
185 // // Output: 1 <nil>
186 //}
187 //
188 //func ExampleClient_TxPipelined() {
189 // var incr *redis.IntCmd
190 // _, err := client.TxPipelined(func(pipe *redis.Pipeline) error {
191 // incr = pipe.Incr("tx_pipelined_counter")
192 // pipe.Expire("tx_pipelined_counter", time.Hour)
193 // return nil
194 // })
195 // fmt.Println(incr.Val(), err)
196 // // Output: 1 <nil>
197 //}
198 //
199 //func ExampleClient_TxPipeline() {
200 // pipe := client.TxPipeline()
201 //
202 // incr := pipe.Incr("tx_pipeline_counter")
203 // pipe.Expire("tx_pipeline_counter", time.Hour)
204 //
205 // // Execute
206 // //
207 // // MULTI
208 // // INCR pipeline_counter
209 // // EXPIRE pipeline_counts 3600
210 // // EXEC
211 // //
212 // // using one client-server roundtrip.
213 // _, err := pipe.Exec()
214 // fmt.Println(incr.Val(), err)
215 // // Output: 1 <nil>
216 //}
217 //
218 //func ExampleClient_Watch() {
219 // var incr func(string) error
220 //
221 // // Transactionally increments key using GET and SET commands.
222 // incr = func(key string) error {
223 // err := client.Watch(func(tx *redis.Tx) error {
224 // n, err := tx.Get(key).Int64()
225 // if err != nil && err != redis.Nil {
226 // return err
227 // }
228 //
229 // _, err = tx.Pipelined(func(pipe *redis.Pipeline) error {
230 // pipe.Set(key, strconv.FormatInt(n+1, 10), 0)
231 // return nil
232 // })
233 // return err
234 // }, key)
235 // if err == redis.TxFailedErr {
236 // return incr(key)
237 // }
238 // return err
239 // }
240 //
241 // var wg sync.WaitGroup
242 // for i := 0; i < 100; i++ {
243 // wg.Add(1)
244 // go func() {
245 // defer wg.Done()
246 //
247 // err := incr("counter3")
248 // if err != nil {
249 // panic(err)
250 // }
251 // }()
252 // }
253 // wg.Wait()
254 //
255 // n, err := client.Get("counter3").Int64()
256 // fmt.Println(n, err)
257 // // Output: 100 <nil>
258 //}
259 //
260 //func ExamplePubSub() {
261 // pubsub, err := client.Subscribe("mychannel1")
262 // if err != nil {
263 // panic(err)
264 // }
265 // defer pubsub.Close()
266 //
267 // err = client.Publish("mychannel1", "hello").Err()
268 // if err != nil {
269 // panic(err)
270 // }
271 //
272 // msg, err := pubsub.ReceiveMessage()
273 // if err != nil {
274 // panic(err)
275 // }
276 //
277 // fmt.Println(msg.Channel, msg.Payload)
278 // // Output: mychannel1 hello
279 //}
280 //
281 //func ExamplePubSub_Receive() {
282 // pubsub, err := client.Subscribe("mychannel2")
283 // if err != nil {
284 // panic(err)
285 // }
286 // defer pubsub.Close()
287 //
288 // n, err := client.Publish("mychannel2", "hello").Result()
289 // if err != nil {
290 // panic(err)
291 // }
292 // fmt.Println(n, "clients received message")
293 //
294 // for i := 0; i < 2; i++ {
295 // // ReceiveTimeout is a low level API. Use ReceiveMessage instead.
296 // msgi, err := pubsub.ReceiveTimeout(5 * time.Second)
297 // if err != nil {
298 // break
299 // }
300 //
301 // switch msg := msgi.(type) {
302 // case *redis.Subscription:
303 // fmt.Println("subscribed to", msg.Channel)
304 // case *redis.Message:
305 // fmt.Println("received", msg.Payload, "from", msg.Channel)
306 // default:
307 // panic(fmt.Errorf("unknown message: %#v", msgi))
308 // }
309 // }
310 //
311 // // Output: 1 clients received message
312 // // subscribed to mychannel2
313 // // received hello from mychannel2
314 //}
315 //
316 //func ExampleScript() {
317 // IncrByXX := redis.NewScript(`
318 // if redis.call("GET", KEYS[1]) ~= false then
319 // return redis.call("INCRBY", KEYS[1], ARGV[1])
320 // end
321 // return false
322 // `)
323 //
324 // n, err := IncrByXX.Run(client, []string{"xx_counter"}, 2).Result()
325 // fmt.Println(n, err)
326 //
327 // err = client.Set("xx_counter", "40", 0).Err()
328 // if err != nil {
329 // panic(err)
330 // }
331 //
332 // n, err = IncrByXX.Run(client, []string{"xx_counter"}, 2).Result()
333 // fmt.Println(n, err)
334 //
335 // // Output: <nil> redis: nil
336 // // 42 <nil>
337 //}
338 //
339 //func Example_customCommand() {
340 // Get := func(client *redis.Client, key string) *redis.StringCmd {
341 // cmd := redis.NewStringCmd("GET", key)
342 // client.Process(cmd)
343 // return cmd
344 // }
345 //
346 // v, err := Get(client, "key_does_not_exist").Result()
347 // fmt.Printf("%q %s", v, err)
348 // // Output: "" redis: nil
349 //}
350 //
351 //func ExampleScanIterator() {
352 // iter := client.Scan(0, "", 0).Iterator()
353 // for iter.Next() {
354 // fmt.Println(iter.Val())
355 // }
356 // if err := iter.Err(); err != nil {
357 // panic(err)
358 // }
359 //}
360 //
361 //func ExampleScanCmd_Iterator() {
362 // iter := client.Scan(0, "", 0).Iterator()
363 // for iter.Next() {
364 // fmt.Println(iter.Val())
365 // }
366 // if err := iter.Err(); err != nil {
367 // panic(err)
368 // }
369 //}