-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
64 lines (55 loc) · 1.05 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
package client_test
import (
"fmt"
"github.com/bsm/pool"
"github.com/wangaoone/redeo/client"
"github.com/wangaoone/redeo/resp"
)
func ExamplePool() {
pool, _ := client.New(&pool.Options{
InitialSize: 1,
}, nil)
defer pool.Close()
cn, _ := pool.Get()
defer pool.Put(cn)
// Build pipeline
cn.WriteCmdString("PING")
cn.WriteCmdString("ECHO", "HEllO")
cn.WriteCmdString("GET", "key")
cn.WriteCmdString("SET", "key", "value")
cn.WriteCmdString("DEL", "key")
// Flush pipeline to socket
if err := cn.Flush(); err != nil {
cn.MarkFailed()
panic(err)
}
// Consume responses
for i := 0; i < 5; i++ {
t, err := cn.PeekType()
if err != nil {
return
}
switch t {
case resp.TypeInline:
s, _ := cn.ReadInlineString()
fmt.Println(s)
case resp.TypeBulk:
s, _ := cn.ReadBulkString()
fmt.Println(s)
case resp.TypeInt:
n, _ := cn.ReadInt()
fmt.Println(n)
case resp.TypeNil:
_ = cn.ReadNil()
fmt.Println(nil)
default:
panic("unexpected response type")
}
}
// Output:
// PONG
// HEllO
// <nil>
// OK
// 1
}