Add Ring example. Improve existing examples.

This commit is contained in:
Vladimir Mihailenco 2015-06-05 15:02:57 +03:00
parent a972489416
commit faa7ed46bd
2 changed files with 66 additions and 49 deletions

View File

@ -4,13 +4,13 @@ Redis client for Golang [![Build Status](https://travis-ci.org/go-redis/redis.pn
Supports: Supports:
- Redis 3 commands except QUIT, MONITOR, SLOWLOG and SYNC. - Redis 3 commands except QUIT, MONITOR, SLOWLOG and SYNC.
- Pub/Sub. - [Pub/Sub](http://godoc.org/gopkg.in/redis.v3#example-PubSub).
- Transactions. - [Transactions](http://godoc.org/gopkg.in/redis.v3#example-Multi).
- Pipelines. - [Pipelining](http://godoc.org/gopkg.in/redis.v3#example-Client-Pipelined).
- Connection pool. Client can be safely used from multiple goroutines. - [Timeouts](http://godoc.org/gopkg.in/redis.v3#Options).
- Timeouts.
- [Redis Sentinel](http://godoc.org/gopkg.in/redis.v3#NewFailoverClient). - [Redis Sentinel](http://godoc.org/gopkg.in/redis.v3#NewFailoverClient).
- [Redis Cluster](http://godoc.org/gopkg.in/redis.v3#NewClusterClient). - [Redis Cluster](http://godoc.org/gopkg.in/redis.v3#NewClusterClient).
- [Ring](http://godoc.org/gopkg.in/redis.v3#NewRing).
API docs: http://godoc.org/gopkg.in/redis.v3. API docs: http://godoc.org/gopkg.in/redis.v3.
Examples: http://godoc.org/gopkg.in/redis.v3#pkg-examples. Examples: http://godoc.org/gopkg.in/redis.v3#pkg-examples.

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net" "net"
"strconv" "strconv"
"sync"
"time" "time"
"gopkg.in/redis.v3" "gopkg.in/redis.v3"
@ -49,6 +50,17 @@ func ExampleNewClusterClient() {
client.Ping() client.Ping()
} }
func ExampleNewRing() {
client := redis.NewRing(&redis.RingOptions{
Addrs: map[string]string{
"shard1": ":7000",
"shard2": ":7001",
"shard3": ":7002",
},
})
client.Ping()
}
func ExampleClient() { func ExampleClient() {
err := client.Set("key", "value", 0).Err() err := client.Set("key", "value", 0).Err()
if err != nil { if err != nil {
@ -84,68 +96,73 @@ func ExampleClient_Incr() {
} }
func ExampleClient_Pipelined() { func ExampleClient_Pipelined() {
cmds, err := client.Pipelined(func(c *redis.Pipeline) error { var incr *redis.IntCmd
c.Set("key1", "hello1", 0) _, err := client.Pipelined(func(pipe *redis.Pipeline) error {
c.Get("key1") incr = pipe.Incr("counter1")
pipe.Expire("counter1", time.Hour)
return nil return nil
}) })
fmt.Println(err) fmt.Println(incr.Val(), err)
set := cmds[0].(*redis.StatusCmd) // Output: 1 <nil>
fmt.Println(set)
get := cmds[1].(*redis.StringCmd)
fmt.Println(get)
// Output: <nil>
// SET key1 hello1: OK
// GET key1: hello1
} }
func ExamplePipeline() { func ExamplePipeline() {
pipeline := client.Pipeline() pipe := client.Pipeline()
set := pipeline.Set("key1", "hello1", 0) defer pipe.Close()
get := pipeline.Get("key1")
cmds, err := pipeline.Exec() incr := pipe.Incr("counter2")
fmt.Println(cmds, err) pipe.Expire("counter2", time.Hour)
fmt.Println(set) _, err := pipe.Exec()
fmt.Println(get) fmt.Println(incr.Val(), err)
// Output: [SET key1 hello1: OK GET key1: hello1] <nil> // Output: 1 <nil>
// SET key1 hello1: OK
// GET key1: hello1
} }
func ExampleMulti() { func ExampleMulti() {
incr := func(tx *redis.Multi) ([]redis.Cmder, error) { // Transactionally increments key using GET and SET commands.
s, err := tx.Get("key").Result() incr := func(tx *redis.Multi, key string) error {
if err != nil && err != redis.Nil { err := tx.Watch(key).Err()
return nil, err if err != nil {
return err
} }
n, _ := strconv.ParseInt(s, 10, 64)
return tx.Exec(func() error { n, err := tx.Get(key).Int64()
tx.Set("key", strconv.FormatInt(n+1, 10), 0) if err != nil && err != redis.Nil {
return err
}
_, err = tx.Exec(func() error {
tx.Set(key, strconv.FormatInt(n+1, 10), 0)
return nil return nil
}) })
return err
} }
client.Del("key") var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
tx := client.Multi() tx := client.Multi()
defer tx.Close() defer tx.Close()
watch := tx.Watch("key") for {
_ = watch.Err() err := incr(tx, "counter3")
if err == redis.TxFailedErr {
for { // Retry.
cmds, err := incr(tx) continue
if err == redis.TxFailedErr { } else if err != nil {
continue panic(err)
} else if err != nil { }
panic(err) break
} }
fmt.Println(cmds, err) }()
break
} }
wg.Wait()
// Output: [SET key 1: OK] <nil> n, err := client.Get("counter3").Int64()
fmt.Println(n, err)
// Output: 10 <nil>
} }
func ExamplePubSub() { func ExamplePubSub() {