github.com/therealbill/libredis@v0.0.0-20161227004305-7d50abda5ccf/client/pipelining.go (about) 1 package client 2 3 // Pipelined implements redis pipeline mode. 4 // A Request/Response server can be implemented so that it is able to process new requests 5 // even if the client didn't already read the old responses. 6 // This way it is possible to send multiple commands to the server without waiting for the replies at all, 7 // and finally read the replies in a single step. 8 type Pipelined struct { 9 redis *Redis 10 conn *connection 11 times int 12 } 13 14 // Pipelining new a Pipelined from *redis. 15 func (r *Redis) Pipelining() (*Pipelined, error) { 16 c, err := r.pool.Get() 17 if err != nil { 18 return nil, err 19 } 20 return &Pipelined{r, c, 0}, nil 21 } 22 23 // Close closes current pipeline mode. 24 func (p *Pipelined) Close() { 25 p.redis.pool.Put(p.conn) 26 p.times = 0 27 } 28 29 // Command send raw redis command and do not wait for response. 30 func (p *Pipelined) Command(args ...interface{}) error { 31 err := p.conn.SendCommand(args...) 32 if err == nil { 33 p.times++ 34 } 35 return err 36 } 37 38 // Receive wait for one the response. 39 func (p *Pipelined) Receive() (*Reply, error) { 40 rp, err := p.conn.RecvReply() 41 if err == nil { 42 p.times-- 43 } 44 return rp, err 45 } 46 47 // ReceiveAll wait for all the responses before. 48 func (p *Pipelined) ReceiveAll() ([]*Reply, error) { 49 if p.times <= 0 { 50 return nil, nil 51 } 52 rps := make([]*Reply, p.times) 53 num := p.times 54 for i := 0; i < num; i++ { 55 rp, err := p.Receive() 56 if err != nil { 57 return rps, err 58 } 59 rps[i] = rp 60 } 61 return rps, nil 62 }