1
0
mirror of https://github.com/redis/go-redis.git synced 2025-06-12 14:21:52 +03:00

Extract pipeline and multi/exec support to separate files.

This commit is contained in:
Vladimir Mihailenco
2012-08-11 17:42:10 +03:00
parent 83664bb3a8
commit 2f4156dd04
8 changed files with 1273 additions and 1021 deletions

View File

@ -1,7 +1,6 @@
package redis
import (
"fmt"
"strconv"
)
@ -843,138 +842,3 @@ func (c *Client) ZUnionStore(
c.Process(req)
return req
}
//------------------------------------------------------------------------------
func (c *Client) PubSubClient() (*PubSubClient, error) {
return newPubSubClient(c)
}
func (c *Client) Publish(channel, message string) *IntReq {
req := NewIntReq("PUBLISH", channel, message)
c.Process(req)
return req
}
//------------------------------------------------------------------------------
func (c *Client) PipelineClient() (*Client, error) {
return &Client{
ConnPool: c.ConnPool,
InitConn: c.InitConn,
reqs: make([]Req, 0),
}, nil
}
//------------------------------------------------------------------------------
func (c *Client) MultiClient() (*Client, error) {
return &Client{
ConnPool: NewSingleConnPool(c.ConnPool),
InitConn: c.InitConn,
}, nil
}
func (c *Client) Multi() {
c.reqs = make([]Req, 0)
}
func (c *Client) Watch(keys ...string) *StatusReq {
args := append([]string{"WATCH"}, keys...)
req := NewStatusReq(args...)
c.Process(req)
return req
}
func (c *Client) Unwatch(keys ...string) *StatusReq {
args := append([]string{"UNWATCH"}, keys...)
req := NewStatusReq(args...)
c.Process(req)
return req
}
func (c *Client) Discard() {
c.mtx.Lock()
c.reqs = c.reqs[:0]
c.mtx.Unlock()
}
func (c *Client) Exec() ([]Req, error) {
c.mtx.Lock()
if len(c.reqs) == 0 {
c.mtx.Unlock()
return c.reqs, nil
}
reqs := c.reqs
c.reqs = nil
c.mtx.Unlock()
conn, err := c.conn()
if err != nil {
return nil, err
}
err = c.ExecReqs(reqs, conn)
if err != nil {
c.ConnPool.Remove(conn)
return nil, err
}
c.ConnPool.Add(conn)
return reqs, nil
}
func (c *Client) ExecReqs(reqs []Req, conn *Conn) error {
multiReq := make([]byte, 0, 1024)
multiReq = append(multiReq, PackReq([]string{"MULTI"})...)
for _, req := range reqs {
multiReq = append(multiReq, req.Req()...)
}
multiReq = append(multiReq, PackReq([]string{"EXEC"})...)
err := c.WriteReq(multiReq, conn)
if err != nil {
return err
}
statusReq := NewStatusReq()
// Parse MULTI command reply.
_, err = statusReq.ParseReply(conn.Rd)
if err != nil {
return err
}
// Parse queued replies.
for _ = range reqs {
_, err = statusReq.ParseReply(conn.Rd)
if err != nil {
return err
}
}
// Parse number of replies.
line, err := readLine(conn.Rd)
if err != nil {
return err
}
if line[0] != '*' {
return fmt.Errorf("Expected '*', but got line %q", line)
}
if isNilReplies(line) {
return Nil
}
// Parse replies.
for i := 0; i < len(reqs); i++ {
req := reqs[i]
val, err := req.ParseReply(conn.Rd)
if err != nil {
req.SetErr(err)
} else {
req.SetVal(val)
}
}
return nil
}