/
transaction_template.go
46 lines (42 loc) · 1.04 KB
/
transaction_template.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
//事务处理器
//@author zxm
//@date 2020-06-02
package client
import (
"Go_Redis/src/client/handler"
"Go_Redis/src/protocol"
"fmt"
)
//开启事务
func (client *Client) Multi() (interface{}, error) {
pool := client.getConnectPool()
conn, err := pool.GetConn()
if err != nil {
return nil, fmt.Errorf("get conn fail")
}
defer pool.PutConn(conn)
result := SendCommand(conn, protocol.MULTI)
return handler.HandleReply(result)
}
//执行事务
func (client *Client) Exec() (interface{}, error) {
pool := client.getConnectPool()
conn, err := pool.GetConn()
if err != nil {
return nil, fmt.Errorf("get conn fail")
}
defer pool.PutConn(conn)
result := SendCommand(conn, protocol.EXEC)
return handler.HandleTransactionReply(result)
}
//终止事务
func (client *Client) Discard() (interface{}, error) {
pool := client.getConnectPool()
conn, err := pool.GetConn()
if err != nil {
return nil, fmt.Errorf("get conn fail")
}
defer pool.PutConn(conn)
result := SendCommand(conn, protocol.DISCARD)
return handler.HandleReply(result)
}