mirror of https://github.com/go-redis/redis.git
Improve Tx example.
This commit is contained in:
parent
d1e774fa21
commit
f766eb0209
|
@ -159,13 +159,16 @@ func ExamplePipeline() {
|
|||
// Output: 1 <nil>
|
||||
}
|
||||
|
||||
func ExampleMulti() {
|
||||
func ExampleWatch() {
|
||||
var incr func(string) error
|
||||
|
||||
// Transactionally increments key using GET and SET commands.
|
||||
incr := func(tx *redis.Multi, key string) error {
|
||||
err := tx.Watch(key).Err()
|
||||
incr = func(key string) error {
|
||||
tx, err := client.Watch(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Close()
|
||||
|
||||
n, err := tx.Get(key).Int64()
|
||||
if err != nil && err != redis.Nil {
|
||||
|
@ -176,27 +179,21 @@ func ExampleMulti() {
|
|||
tx.Set(key, strconv.FormatInt(n+1, 10), 0)
|
||||
return nil
|
||||
})
|
||||
if err == redis.TxFailedErr {
|
||||
return incr(key)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 10; i++ {
|
||||
for i := 0; i < 100; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
tx := client.Multi()
|
||||
defer tx.Close()
|
||||
|
||||
for {
|
||||
err := incr(tx, "counter3")
|
||||
if err == redis.TxFailedErr {
|
||||
// Retry.
|
||||
continue
|
||||
} else if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
break
|
||||
err := incr("counter3")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
@ -204,7 +201,7 @@ func ExampleMulti() {
|
|||
|
||||
n, err := client.Get("counter3").Int64()
|
||||
fmt.Println(n, err)
|
||||
// Output: 10 <nil>
|
||||
// Output: 100 <nil>
|
||||
}
|
||||
|
||||
func ExamplePubSub() {
|
||||
|
|
12
multi.go
12
multi.go
|
@ -23,6 +23,18 @@ type Multi struct {
|
|||
closed bool
|
||||
}
|
||||
|
||||
// Watch marks the keys to be watched for conditional execution
|
||||
// of a transaction.
|
||||
func (c *Client) Watch(keys ...string) (*Multi, error) {
|
||||
tx := c.Multi()
|
||||
if err := tx.Watch(keys...).Err(); err != nil {
|
||||
tx.Close()
|
||||
return nil, err
|
||||
}
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
// Deprecated. Use Watch instead.
|
||||
func (c *Client) Multi() *Multi {
|
||||
multi := &Multi{
|
||||
base: &baseClient{
|
||||
|
|
Loading…
Reference in New Issue