1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-28 06:42:00 +03:00

Update example and doc.

This commit is contained in:
Vladimir Mihailenco
2013-09-29 11:31:57 +03:00
parent 331f882d34
commit 37e2d9f22a
2 changed files with 161 additions and 116 deletions

View File

@ -47,45 +47,52 @@ func ExampleSet() {
// <nil> bar
}
func ExamplePipelined() {
client := redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
defer client.Close()
cmds, err := client.Pipelined(func(c *redis.Pipeline) {
c.Set("key1", "hello1")
c.Get("key2")
})
fmt.Println(cmds, err)
// Output: [SET key1 hello1: OK GET key2: (nil)] (nil)
}
func ExamplePipeline() {
client := redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
defer client.Close()
var set *redis.StatusCmd
var get *redis.StringCmd
cmds, err := client.Pipelined(func(c *redis.Pipeline) {
set = c.Set("key1", "hello1")
get = c.Get("key2")
})
fmt.Println(err, cmds)
pipeline := client.Pipeline()
set := pipeline.Set("key1", "hello1")
get := pipeline.Get("key2")
cmds, err := pipeline.Exec()
fmt.Println(cmds, err)
fmt.Println(set)
fmt.Println(get)
// Output: (nil) [SET key1 hello1: OK GET key2: (nil)]
// Output: [SET key1 hello1: OK GET key2: (nil)] (nil)
// SET key1 hello1: OK
// GET key2: (nil)
}
func incr(tx *redis.Multi) ([]redis.Cmder, error) {
get := tx.Get("key")
if err := get.Err(); err != nil && err != redis.Nil {
return nil, err
}
val, _ := strconv.ParseInt(get.Val(), 10, 64)
cmds, err := tx.Exec(func() {
tx.Set("key", strconv.FormatInt(val+1, 10))
})
// Transaction failed. Repeat.
if err == redis.Nil {
return incr(tx)
}
return cmds, err
}
func ExampleMulti() {
incr := func(tx *redis.Multi) ([]redis.Cmder, error) {
get := tx.Get("key")
if err := get.Err(); err != nil && err != redis.Nil {
return nil, err
}
val, _ := strconv.ParseInt(get.Val(), 10, 64)
return tx.Exec(func() {
tx.Set("key", strconv.FormatInt(val+1, 10))
})
}
client := redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
@ -99,8 +106,17 @@ func ExampleMulti() {
watch := tx.Watch("key")
_ = watch.Err()
cmds, err := incr(tx)
fmt.Println(err, cmds)
for {
cmds, err := incr(tx)
if err == redis.Nil {
// Transaction failed. Repeat.
continue
} else if err != nil {
panic(err)
}
fmt.Println(err, cmds)
break
}
// Output: <nil> [SET key 1: OK]
}