Add more docs for Tx

This commit is contained in:
Vladimir Mihailenco 2018-07-12 15:57:03 +03:00
parent 5db3ab16e5
commit ab1a52f0c9
2 changed files with 10 additions and 3 deletions

View File

@ -31,6 +31,7 @@ type Pipeline struct {
closed bool closed bool
} }
// Process queues the cmd for later execution.
func (c *Pipeline) Process(cmd Cmder) error { func (c *Pipeline) Process(cmd Cmder) error {
c.mu.Lock() c.mu.Lock()
c.cmds = append(c.cmds, cmd) c.cmds = append(c.cmds, cmd)

12
tx.go
View File

@ -29,6 +29,10 @@ func (c *Client) newTx() *Tx {
return &tx return &tx
} }
// Watch prepares a transcaction and marks the keys to be watched
// for conditional execution if there are any keys.
//
// The transaction is automatically closed when the fn exits.
func (c *Client) Watch(fn func(*Tx) error, keys ...string) error { func (c *Client) Watch(fn func(*Tx) error, keys ...string) error {
tx := c.newTx() tx := c.newTx()
if len(keys) > 0 { if len(keys) > 0 {
@ -74,6 +78,7 @@ func (c *Tx) Unwatch(keys ...string) *StatusCmd {
return cmd return cmd
} }
// Pipeline creates a new pipeline. It is more convenient to use Pipelined.
func (c *Tx) Pipeline() Pipeliner { func (c *Tx) Pipeline() Pipeliner {
pipe := Pipeline{ pipe := Pipeline{
exec: c.processTxPipeline, exec: c.processTxPipeline,
@ -82,23 +87,24 @@ func (c *Tx) Pipeline() Pipeliner {
return &pipe return &pipe
} }
// Pipelined executes commands queued in the fn in a transaction // Pipelined executes commands queued in the fn in a transaction.
// and restores the connection state to normal.
// //
// When using WATCH, EXEC will execute commands only if the watched keys // When using WATCH, EXEC will execute commands only if the watched keys
// were not modified, allowing for a check-and-set mechanism. // were not modified, allowing for a check-and-set mechanism.
// //
// Exec always returns list of commands. If transaction fails // Exec always returns list of commands. If transaction fails
// TxFailedErr is returned. Otherwise Exec returns error of the first // TxFailedErr is returned. Otherwise Exec returns an error of the first
// failed command or nil. // failed command or nil.
func (c *Tx) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) { func (c *Tx) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
return c.Pipeline().Pipelined(fn) return c.Pipeline().Pipelined(fn)
} }
// TxPipelined is an alias for Pipelined.
func (c *Tx) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) { func (c *Tx) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
return c.Pipelined(fn) return c.Pipelined(fn)
} }
// TxPipeline is an alias for Pipeline.
func (c *Tx) TxPipeline() Pipeliner { func (c *Tx) TxPipeline() Pipeliner {
return c.Pipeline() return c.Pipeline()
} }