Replace client with redisdb in examples

This commit is contained in:
Vladimir Mihailenco 2018-08-12 11:19:25 +03:00
parent d5fc79d4ee
commit fea1be3727
3 changed files with 63 additions and 64 deletions

View File

@ -25,7 +25,6 @@ type Cmder interface {
readTimeout() *time.Duration readTimeout() *time.Duration
Err() error Err() error
// fmt.Stringer
} }
func setCmdsErr(cmds []Cmder, e error) { func setCmdsErr(cmds []Cmder, e error) {

View File

@ -7,10 +7,10 @@ import (
) )
func Example_instrumentation() { func Example_instrumentation() {
cl := redis.NewClient(&redis.Options{ redisdb := redis.NewClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
cl.WrapProcess(func(old func(cmd redis.Cmder) error) func(cmd redis.Cmder) error { redisdb.WrapProcess(func(old func(cmd redis.Cmder) error) func(cmd redis.Cmder) error {
return func(cmd redis.Cmder) error { return func(cmd redis.Cmder) error {
fmt.Printf("starting processing: <%s>\n", cmd) fmt.Printf("starting processing: <%s>\n", cmd)
err := old(cmd) err := old(cmd)
@ -19,17 +19,17 @@ func Example_instrumentation() {
} }
}) })
cl.Ping() redisdb.Ping()
// Output: starting processing: <ping: > // Output: starting processing: <ping: >
// finished processing: <ping: PONG> // finished processing: <ping: PONG>
} }
func ExamplePipeline_instrumentation() { func ExamplePipeline_instrumentation() {
client := redis.NewClient(&redis.Options{ redisdb := redis.NewClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
client.WrapProcessPipeline(func(old func([]redis.Cmder) error) func([]redis.Cmder) error { redisdb.WrapProcessPipeline(func(old func([]redis.Cmder) error) func([]redis.Cmder) error {
return func(cmds []redis.Cmder) error { return func(cmds []redis.Cmder) error {
fmt.Printf("pipeline starting processing: %v\n", cmds) fmt.Printf("pipeline starting processing: %v\n", cmds)
err := old(cmds) err := old(cmds)
@ -38,7 +38,7 @@ func ExamplePipeline_instrumentation() {
} }
}) })
client.Pipelined(func(pipe redis.Pipeliner) error { redisdb.Pipelined(func(pipe redis.Pipeliner) error {
pipe.Ping() pipe.Ping()
pipe.Ping() pipe.Ping()
return nil return nil

View File

@ -9,10 +9,10 @@ import (
"github.com/go-redis/redis" "github.com/go-redis/redis"
) )
var client *redis.Client var redisdb *redis.Client
func init() { func init() {
client = redis.NewClient(&redis.Options{ redisdb = redis.NewClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
DialTimeout: 10 * time.Second, DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second, ReadTimeout: 30 * time.Second,
@ -20,17 +20,17 @@ func init() {
PoolSize: 10, PoolSize: 10,
PoolTimeout: 30 * time.Second, PoolTimeout: 30 * time.Second,
}) })
client.FlushDB() redisdb.FlushDB()
} }
func ExampleNewClient() { func ExampleNewClient() {
client := redis.NewClient(&redis.Options{ redisdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379", Addr: "localhost:6379",
Password: "", // no password set Password: "", // no password set
DB: 0, // use default DB DB: 0, // use default DB
}) })
pong, err := client.Ping().Result() pong, err := redisdb.Ping().Result()
fmt.Println(pong, err) fmt.Println(pong, err)
// Output: PONG <nil> // Output: PONG <nil>
} }
@ -55,20 +55,20 @@ func ExampleParseURL() {
func ExampleNewFailoverClient() { func ExampleNewFailoverClient() {
// See http://redis.io/topics/sentinel for instructions how to // See http://redis.io/topics/sentinel for instructions how to
// setup Redis Sentinel. // setup Redis Sentinel.
client := redis.NewFailoverClient(&redis.FailoverOptions{ redisdb := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "master", MasterName: "master",
SentinelAddrs: []string{":26379"}, SentinelAddrs: []string{":26379"},
}) })
client.Ping() redisdb.Ping()
} }
func ExampleNewClusterClient() { func ExampleNewClusterClient() {
// See http://redis.io/topics/cluster-tutorial for instructions // See http://redis.io/topics/cluster-tutorial for instructions
// how to setup Redis Cluster. // how to setup Redis Cluster.
client := redis.NewClusterClient(&redis.ClusterOptions{ redisdb := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"}, Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"},
}) })
client.Ping() redisdb.Ping()
} }
// Following example creates a cluster from 2 master nodes and 2 slave nodes // Following example creates a cluster from 2 master nodes and 2 slave nodes
@ -103,44 +103,44 @@ func ExampleNewClusterClient_manualSetup() {
return slots, nil return slots, nil
} }
client := redis.NewClusterClient(&redis.ClusterOptions{ redisdb := redis.NewClusterClient(&redis.ClusterOptions{
ClusterSlots: clusterSlots, ClusterSlots: clusterSlots,
RouteRandomly: true, RouteRandomly: true,
}) })
client.Ping() redisdb.Ping()
// ReloadState reloads cluster state. It calls ClusterSlots func // ReloadState reloads cluster state. It calls ClusterSlots func
// to get cluster slots information. // to get cluster slots information.
err := client.ReloadState() err := redisdb.ReloadState()
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
func ExampleNewRing() { func ExampleNewRing() {
client := redis.NewRing(&redis.RingOptions{ redisdb := redis.NewRing(&redis.RingOptions{
Addrs: map[string]string{ Addrs: map[string]string{
"shard1": ":7000", "shard1": ":7000",
"shard2": ":7001", "shard2": ":7001",
"shard3": ":7002", "shard3": ":7002",
}, },
}) })
client.Ping() redisdb.Ping()
} }
func ExampleClient() { func ExampleClient() {
err := client.Set("key", "value", 0).Err() err := redisdb.Set("key", "value", 0).Err()
if err != nil { if err != nil {
panic(err) panic(err)
} }
val, err := client.Get("key").Result() val, err := redisdb.Get("key").Result()
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("key", val) fmt.Println("key", val)
val2, err := client.Get("missing_key").Result() val2, err := redisdb.Get("missing_key").Result()
if err == redis.Nil { if err == redis.Nil {
fmt.Println("missing_key does not exist") fmt.Println("missing_key does not exist")
} else if err != nil { } else if err != nil {
@ -155,20 +155,20 @@ func ExampleClient() {
func ExampleClient_Set() { func ExampleClient_Set() {
// Last argument is expiration. Zero means the key has no // Last argument is expiration. Zero means the key has no
// expiration time. // expiration time.
err := client.Set("key", "value", 0).Err() err := redisdb.Set("key", "value", 0).Err()
if err != nil { if err != nil {
panic(err) panic(err)
} }
// key2 will expire in an hour. // key2 will expire in an hour.
err = client.Set("key2", "value", time.Hour).Err() err = redisdb.Set("key2", "value", time.Hour).Err()
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
func ExampleClient_Incr() { func ExampleClient_Incr() {
result, err := client.Incr("counter").Result() result, err := redisdb.Incr("counter").Result()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -178,12 +178,12 @@ func ExampleClient_Incr() {
} }
func ExampleClient_BLPop() { func ExampleClient_BLPop() {
if err := client.RPush("queue", "message").Err(); err != nil { if err := redisdb.RPush("queue", "message").Err(); err != nil {
panic(err) panic(err)
} }
// use `client.BLPop(0, "queue")` for infinite waiting time // use `redisdb.BLPop(0, "queue")` for infinite waiting time
result, err := client.BLPop(1*time.Second, "queue").Result() result, err := redisdb.BLPop(1*time.Second, "queue").Result()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -193,9 +193,9 @@ func ExampleClient_BLPop() {
} }
func ExampleClient_Scan() { func ExampleClient_Scan() {
client.FlushDB() redisdb.FlushDB()
for i := 0; i < 33; i++ { for i := 0; i < 33; i++ {
err := client.Set(fmt.Sprintf("key%d", i), "value", 0).Err() err := redisdb.Set(fmt.Sprintf("key%d", i), "value", 0).Err()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -206,7 +206,7 @@ func ExampleClient_Scan() {
for { for {
var keys []string var keys []string
var err error var err error
keys, cursor, err = client.Scan(cursor, "", 10).Result() keys, cursor, err = redisdb.Scan(cursor, "", 10).Result()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -222,7 +222,7 @@ func ExampleClient_Scan() {
func ExampleClient_Pipelined() { func ExampleClient_Pipelined() {
var incr *redis.IntCmd var incr *redis.IntCmd
_, err := client.Pipelined(func(pipe redis.Pipeliner) error { _, err := redisdb.Pipelined(func(pipe redis.Pipeliner) error {
incr = pipe.Incr("pipelined_counter") incr = pipe.Incr("pipelined_counter")
pipe.Expire("pipelined_counter", time.Hour) pipe.Expire("pipelined_counter", time.Hour)
return nil return nil
@ -232,7 +232,7 @@ func ExampleClient_Pipelined() {
} }
func ExampleClient_Pipeline() { func ExampleClient_Pipeline() {
pipe := client.Pipeline() pipe := redisdb.Pipeline()
incr := pipe.Incr("pipeline_counter") incr := pipe.Incr("pipeline_counter")
pipe.Expire("pipeline_counter", time.Hour) pipe.Expire("pipeline_counter", time.Hour)
@ -242,7 +242,7 @@ func ExampleClient_Pipeline() {
// INCR pipeline_counter // INCR pipeline_counter
// EXPIRE pipeline_counts 3600 // EXPIRE pipeline_counts 3600
// //
// using one client-server roundtrip. // using one redisdb-server roundtrip.
_, err := pipe.Exec() _, err := pipe.Exec()
fmt.Println(incr.Val(), err) fmt.Println(incr.Val(), err)
// Output: 1 <nil> // Output: 1 <nil>
@ -250,7 +250,7 @@ func ExampleClient_Pipeline() {
func ExampleClient_TxPipelined() { func ExampleClient_TxPipelined() {
var incr *redis.IntCmd var incr *redis.IntCmd
_, err := client.TxPipelined(func(pipe redis.Pipeliner) error { _, err := redisdb.TxPipelined(func(pipe redis.Pipeliner) error {
incr = pipe.Incr("tx_pipelined_counter") incr = pipe.Incr("tx_pipelined_counter")
pipe.Expire("tx_pipelined_counter", time.Hour) pipe.Expire("tx_pipelined_counter", time.Hour)
return nil return nil
@ -260,7 +260,7 @@ func ExampleClient_TxPipelined() {
} }
func ExampleClient_TxPipeline() { func ExampleClient_TxPipeline() {
pipe := client.TxPipeline() pipe := redisdb.TxPipeline()
incr := pipe.Incr("tx_pipeline_counter") incr := pipe.Incr("tx_pipeline_counter")
pipe.Expire("tx_pipeline_counter", time.Hour) pipe.Expire("tx_pipeline_counter", time.Hour)
@ -272,7 +272,7 @@ func ExampleClient_TxPipeline() {
// EXPIRE pipeline_counts 3600 // EXPIRE pipeline_counts 3600
// EXEC // EXEC
// //
// using one client-server roundtrip. // using one redisdb-server roundtrip.
_, err := pipe.Exec() _, err := pipe.Exec()
fmt.Println(incr.Val(), err) fmt.Println(incr.Val(), err)
// Output: 1 <nil> // Output: 1 <nil>
@ -283,7 +283,7 @@ func ExampleClient_Watch() {
// Transactionally increments key using GET and SET commands. // Transactionally increments key using GET and SET commands.
incr = func(key string) error { incr = func(key string) error {
err := client.Watch(func(tx *redis.Tx) error { err := redisdb.Watch(func(tx *redis.Tx) error {
n, err := tx.Get(key).Int64() n, err := tx.Get(key).Int64()
if err != nil && err != redis.Nil { if err != nil && err != redis.Nil {
return err return err
@ -315,13 +315,13 @@ func ExampleClient_Watch() {
} }
wg.Wait() wg.Wait()
n, err := client.Get("counter3").Int64() n, err := redisdb.Get("counter3").Int64()
fmt.Println(n, err) fmt.Println(n, err)
// Output: 100 <nil> // Output: 100 <nil>
} }
func ExamplePubSub() { func ExamplePubSub() {
pubsub := client.Subscribe("mychannel1") pubsub := redisdb.Subscribe("mychannel1")
// Wait for confirmation that subscription is created before publishing anything. // Wait for confirmation that subscription is created before publishing anything.
_, err := pubsub.Receive() _, err := pubsub.Receive()
@ -333,7 +333,7 @@ func ExamplePubSub() {
ch := pubsub.Channel() ch := pubsub.Channel()
// Publish a message. // Publish a message.
err = client.Publish("mychannel1", "hello").Err() err = redisdb.Publish("mychannel1", "hello").Err()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -356,7 +356,7 @@ func ExamplePubSub() {
} }
func ExamplePubSub_Receive() { func ExamplePubSub_Receive() {
pubsub := client.Subscribe("mychannel2") pubsub := redisdb.Subscribe("mychannel2")
defer pubsub.Close() defer pubsub.Close()
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
@ -370,7 +370,7 @@ func ExamplePubSub_Receive() {
case *redis.Subscription: case *redis.Subscription:
fmt.Println("subscribed to", msg.Channel) fmt.Println("subscribed to", msg.Channel)
_, err := client.Publish("mychannel2", "hello").Result() _, err := redisdb.Publish("mychannel2", "hello").Result()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -381,7 +381,7 @@ func ExamplePubSub_Receive() {
} }
} }
// sent message to 1 client // sent message to 1 redisdb
// received hello from mychannel2 // received hello from mychannel2
} }
@ -393,15 +393,15 @@ func ExampleScript() {
return false return false
`) `)
n, err := IncrByXX.Run(client, []string{"xx_counter"}, 2).Result() n, err := IncrByXX.Run(redisdb, []string{"xx_counter"}, 2).Result()
fmt.Println(n, err) fmt.Println(n, err)
err = client.Set("xx_counter", "40", 0).Err() err = redisdb.Set("xx_counter", "40", 0).Err()
if err != nil { if err != nil {
panic(err) panic(err)
} }
n, err = IncrByXX.Run(client, []string{"xx_counter"}, 2).Result() n, err = IncrByXX.Run(redisdb, []string{"xx_counter"}, 2).Result()
fmt.Println(n, err) fmt.Println(n, err)
// Output: <nil> redis: nil // Output: <nil> redis: nil
@ -409,25 +409,25 @@ func ExampleScript() {
} }
func Example_customCommand() { func Example_customCommand() {
Get := func(client *redis.Client, key string) *redis.StringCmd { Get := func(redisdb *redis.Client, key string) *redis.StringCmd {
cmd := redis.NewStringCmd("get", key) cmd := redis.NewStringCmd("get", key)
client.Process(cmd) redisdb.Process(cmd)
return cmd return cmd
} }
v, err := Get(client, "key_does_not_exist").Result() v, err := Get(redisdb, "key_does_not_exist").Result()
fmt.Printf("%q %s", v, err) fmt.Printf("%q %s", v, err)
// Output: "" redis: nil // Output: "" redis: nil
} }
func Example_customCommand2() { func Example_customCommand2() {
v, err := client.Do("get", "key_does_not_exist").String() v, err := redisdb.Do("get", "key_does_not_exist").String()
fmt.Printf("%q %s", v, err) fmt.Printf("%q %s", v, err)
// Output: "" redis: nil // Output: "" redis: nil
} }
func ExampleScanIterator() { func ExampleScanIterator() {
iter := client.Scan(0, "", 0).Iterator() iter := redisdb.Scan(0, "", 0).Iterator()
for iter.Next() { for iter.Next() {
fmt.Println(iter.Val()) fmt.Println(iter.Val())
} }
@ -437,7 +437,7 @@ func ExampleScanIterator() {
} }
func ExampleScanCmd_Iterator() { func ExampleScanCmd_Iterator() {
iter := client.Scan(0, "", 0).Iterator() iter := redisdb.Scan(0, "", 0).Iterator()
for iter.Next() { for iter.Next() {
fmt.Println(iter.Val()) fmt.Println(iter.Val())
} }
@ -447,29 +447,29 @@ func ExampleScanCmd_Iterator() {
} }
func ExampleNewUniversalClient_simple() { func ExampleNewUniversalClient_simple() {
client := redis.NewUniversalClient(&redis.UniversalOptions{ redisdb := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{":6379"}, Addrs: []string{":6379"},
}) })
defer client.Close() defer redisdb.Close()
client.Ping() redisdb.Ping()
} }
func ExampleNewUniversalClient_failover() { func ExampleNewUniversalClient_failover() {
client := redis.NewUniversalClient(&redis.UniversalOptions{ redisdb := redis.NewUniversalClient(&redis.UniversalOptions{
MasterName: "master", MasterName: "master",
Addrs: []string{":26379"}, Addrs: []string{":26379"},
}) })
defer client.Close() defer redisdb.Close()
client.Ping() redisdb.Ping()
} }
func ExampleNewUniversalClient_cluster() { func ExampleNewUniversalClient_cluster() {
client := redis.NewUniversalClient(&redis.UniversalOptions{ redisdb := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"}, Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"},
}) })
defer client.Close() defer redisdb.Close()
client.Ping() redisdb.Ping()
} }