| 1 |
package redis |
| 2 |
|
| 3 |
import ( |
| 4 |
"gopkg.in/redis.v5/internal" |
| 5 |
"gopkg.in/redis.v5/internal/pool" |
| 6 |
) |
| 7 |
|
| 8 |
// Redis transaction failed. |
| 9 |
const TxFailedErr = internal.RedisError("redis: transaction failed") |
| 10 |
|
| 11 |
// Tx implements Redis transactions as described in |
| 12 |
// http://redis.io/topics/transactions. It's NOT safe for concurrent use |
| 13 |
// by multiple goroutines, because Exec resets list of watched keys. |
| 14 |
// If you don't need WATCH it is better to use Pipeline. |
| 15 |
type Tx struct { |
| 16 |
cmdable |
| 17 |
statefulCmdable |
| 18 |
baseClient |
| 19 |
} |
| 20 |
|
| 21 |
func (c *Client) newTx() *Tx { |
| 22 |
tx := Tx{ |
| 23 |
baseClient: baseClient{ |
| 24 |
opt: c.opt, |
| 25 |
connPool: pool.NewStickyConnPool(c.connPool.(*pool.ConnPool), true), |
| 26 |
}, |
| 27 |
} |
| 28 |
tx.cmdable.process = tx.Process |
| 29 |
tx.statefulCmdable.process = tx.Process |
| 30 |
return &tx |
| 31 |
} |
| 32 |
|
| 33 |
func (c *Client) Watch(fn func(*Tx) error, keys ...string) error { |
| 34 |
tx := c.newTx() |
| 35 |
if len(keys) > 0 { |
| 36 |
if err := tx.Watch(keys...).Err(); err != nil { |
| 37 |
_ = tx.Close() |
| 38 |
return err |
| 39 |
} |
| 40 |
} |
| 41 |
firstErr := fn(tx) |
| 42 |
if err := tx.Close(); err != nil && firstErr == nil { |
| 43 |
firstErr = err |
| 44 |
} |
| 45 |
return firstErr |
| 46 |
} |
| 47 |
|
| 48 |
// close closes the transaction, releasing any open resources. |
| 49 |
func (c *Tx) Close() error { |
| 50 |
_ = c.Unwatch().Err() |
| 51 |
return c.baseClient.Close() |
| 52 |
} |
| 53 |
|
| 54 |
// Watch marks the keys to be watched for conditional execution |
| 55 |
// of a transaction. |
| 56 |
func (c *Tx) Watch(keys ...string) *StatusCmd { |
| 57 |
args := make([]interface{}, 1+len(keys)) |
| 58 |
args[0] = "WATCH" |
| 59 |
for i, key := range keys { |
| 60 |
args[1+i] = key |
| 61 |
} |
| 62 |
cmd := NewStatusCmd(args...) |
| 63 |
c.Process(cmd) |
| 64 |
return cmd |
| 65 |
} |
| 66 |
|
| 67 |
// Unwatch flushes all the previously watched keys for a transaction. |
| 68 |
func (c *Tx) Unwatch(keys ...string) *StatusCmd { |
| 69 |
args := make([]interface{}, 1+len(keys)) |
| 70 |
args[0] = "UNWATCH" |
| 71 |
for i, key := range keys { |
| 72 |
args[1+i] = key |
| 73 |
} |
| 74 |
cmd := NewStatusCmd(args...) |
| 75 |
c.Process(cmd) |
| 76 |
return cmd |
| 77 |
} |
| 78 |
|
| 79 |
func (c *Tx) Pipeline() *Pipeline { |
| 80 |
pipe := Pipeline{ |
| 81 |
exec: c.pipelineExecer(c.txPipelineProcessCmds), |
| 82 |
} |
| 83 |
pipe.cmdable.process = pipe.Process |
| 84 |
pipe.statefulCmdable.process = pipe.Process |
| 85 |
return &pipe |
| 86 |
} |
| 87 |
|
| 88 |
// Pipelined executes commands queued in the fn in a transaction |
| 89 |
// and restores the connection state to normal. |
| 90 |
// |
| 91 |
// When using WATCH, EXEC will execute commands only if the watched keys |
| 92 |
// were not modified, allowing for a check-and-set mechanism. |
| 93 |
// |
| 94 |
// Exec always returns list of commands. If transaction fails |
| 95 |
// TxFailedErr is returned. Otherwise Exec returns error of the first |
| 96 |
// failed command or nil. |
| 97 |
func (c *Tx) Pipelined(fn func(*Pipeline) error) ([]Cmder, error) { |
| 98 |
return c.Pipeline().pipelined(fn) |
| 99 |
} |