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

205
v2/doc.go
View File

@ -3,131 +3,160 @@ Package github.com/vmihailenco/redis implements a Redis client.
Let's start with connecting to Redis using TCP: Let's start with connecting to Redis using TCP:
password := "" // no password set client := redis.NewTCPClient(&redis.Options{
db := int64(-1) // use default DB Addr: "localhost:6379",
client := redis.NewTCPClient("localhost:6379", password, db) Password: "", // no password set
defer client.Close() DB: 0, // use default DB
})
defer client.Close()
ping := client.Ping() ping := client.Ping()
fmt.Println(ping.Err(), ping.Val()) fmt.Println(ping.Err(), ping.Val())
// Output: <nil> PONG // Output: <nil> PONG
or using Unix socket: or using Unix socket:
client := redis.NewUnixClient("/tmp/redis.sock", "", -1) client := redis.NewUnixClient(&redis.Options{
defer client.Close() Addr: "/tmp/redis.sock",
})
defer client.Close()
ping := client.Ping() ping := client.Ping()
fmt.Println(ping.Err(), ping.Val()) fmt.Println(ping.Err(), ping.Val())
// Output: <nil> PONG // Output: <nil> PONG
Then we can start sending commands: Then we can start sending commands:
set := client.Set("foo", "bar") client := redis.NewTCPClient(&redis.Options{
fmt.Println(set.Err(), set.Val()) Addr: ":6379",
})
defer client.Close()
get := client.Get("foo") set := client.Set("foo", "bar")
fmt.Println(get.Err(), get.Val()) fmt.Println(set.Err(), set.Val())
// Output: <nil> OK get := client.Get("foo")
// <nil> bar fmt.Println(get.Err(), get.Val())
// Output: <nil> OK
// <nil> bar
We can also pipeline two commands together: We can also pipeline two commands together:
var set *redis.StatusReq client := redis.NewTCPClient(&redis.Options{
var get *redis.StringReq Addr: ":6379",
reqs, err := client.Pipelined(func(c *redis.PipelineClient) { })
set = c.Set("key1", "hello1") defer client.Close()
get = c.Get("key2")
}) cmds, err := client.Pipelined(func(c *redis.Pipeline) {
fmt.Println(err, reqs) c.Set("key1", "hello1")
fmt.Println(set) c.Get("key2")
fmt.Println(get) })
// Output: <nil> [SET key1 hello1: OK GET key2: (nil)] fmt.Println(cmds, err)
// SET key1 hello1: OK // Output: [SET key1 hello1: OK GET key2: (nil)] (nil)
// GET key2: (nil)
or: or:
var set *redis.StatusReq client := redis.NewTCPClient(&redis.Options{
var get *redis.StringReq Addr: ":6379",
reqs, err := client.Pipelined(func(c *redis.PipelineClient) { })
set = c.Set("key1", "hello1") defer client.Close()
get = c.Get("key2")
}) pipeline := client.Pipeline()
fmt.Println(err, reqs) set := pipeline.Set("key1", "hello1")
fmt.Println(set) get := pipeline.Get("key2")
fmt.Println(get) cmds, err := pipeline.Exec()
// Output: <nil> [SET key1 hello1 GET key2] fmt.Println(cmds, err)
// SET key1 hello1 fmt.Println(set)
// GET key2 fmt.Println(get)
// Output: [SET key1 hello1: OK GET key2: (nil)] (nil)
// SET key1 hello1: OK
// GET key2: (nil)
We can also send several commands in transaction: We can also send several commands in transaction:
func transaction(multi *redis.MultiClient) ([]redis.Req, error) { incr := func(tx *redis.Multi) ([]redis.Cmder, error) {
get := multi.Get("key") get := tx.Get("key")
if err := get.Err(); err != nil && err != redis.Nil { if err := get.Err(); err != nil && err != redis.Nil {
return nil, err return nil, err
} }
val, _ := strconv.ParseInt(get.Val(), 10, 64) val, _ := strconv.ParseInt(get.Val(), 10, 64)
reqs, err := multi.Exec(func() { return tx.Exec(func() {
multi.Set("key", strconv.FormatInt(val+1, 10)) tx.Set("key", strconv.FormatInt(val+1, 10))
}) })
// Transaction failed. Repeat. }
if err == redis.Nil {
return transaction(multi)
}
return reqs, err
}
multi, err := client.MultiClient() client := redis.NewTCPClient(&redis.Options{
_ = err Addr: ":6379",
defer multi.Close() })
defer client.Close()
watch := multi.Watch("key") client.Del("key")
_ = watch.Err()
reqs, err := transaction(multi) tx := client.Multi()
fmt.Println(err, reqs) defer tx.Close()
// Output: <nil> [SET key 1: OK] watch := tx.Watch("key")
_ = watch.Err()
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]
To subscribe to the channel: To subscribe to the channel:
pubsub, err := client.PubSubClient() client := redis.NewTCPClient(&redis.Options{
defer pubsub.Close() Addr: ":6379",
})
defer client.Close()
ch, err := pubsub.Subscribe("mychannel") pubsub := client.PubSub()
_ = err defer pubsub.Close()
subscribeMsg := <-ch err := pubsub.Subscribe("mychannel")
fmt.Println(subscribeMsg.Err, subscribeMsg.Name) _ = err
pub := client.Publish("mychannel", "hello") msg, err := pubsub.Receive()
_ = pub.Err() fmt.Println(msg, err)
msg := <-ch pub := client.Publish("mychannel", "hello")
fmt.Println(msg.Err, msg.Message) _ = pub.Err()
// Output: <nil> subscribe msg, err = pubsub.Receive()
// <nil> hello fmt.Println(msg, err)
// Output: &{subscribe mychannel 1} <nil>
// &{mychannel hello} <nil>
You can also write custom commands: You can also write custom commands:
func Get(client *redis.Client, key string) *redis.StringReq { func Get(client *redis.Client, key string) *redis.StringCmd {
req := redis.NewStringReq("GET", key) cmd := redis.NewStringCmd("GET", key)
client.Process(req) client.Process(cmd)
return req return cmd
} }
get := Get(client, "key_does_not_exist") func ExampleCustomCommand() {
fmt.Println(get.Err(), get.Val()) client := redis.NewTCPClient(&redis.Options{
// Output: (nil) Addr: ":6379",
})
defer client.Close()
Client uses connection pool to send commands. You can change maximum number of connections with: get := Get(client, "key_does_not_exist")
fmt.Println(get.Err(), get.Val())
client.ConnPool.(*redis.MultiConnPool).MaxCap = 1 // Output: (nil)
}
*/ */
package redis package redis

View File

@ -47,45 +47,52 @@ func ExampleSet() {
// <nil> bar // <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() { func ExamplePipeline() {
client := redis.NewTCPClient(&redis.Options{ client := redis.NewTCPClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
defer client.Close() defer client.Close()
var set *redis.StatusCmd pipeline := client.Pipeline()
var get *redis.StringCmd set := pipeline.Set("key1", "hello1")
cmds, err := client.Pipelined(func(c *redis.Pipeline) { get := pipeline.Get("key2")
set = c.Set("key1", "hello1") cmds, err := pipeline.Exec()
get = c.Get("key2") fmt.Println(cmds, err)
})
fmt.Println(err, cmds)
fmt.Println(set) fmt.Println(set)
fmt.Println(get) 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 // SET key1 hello1: OK
// GET key2: (nil) // 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() { 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{ client := redis.NewTCPClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
@ -99,8 +106,17 @@ func ExampleMulti() {
watch := tx.Watch("key") watch := tx.Watch("key")
_ = watch.Err() _ = watch.Err()
cmds, err := incr(tx) for {
fmt.Println(err, cmds) 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] // Output: <nil> [SET key 1: OK]
} }