mirror of https://github.com/go-redis/redis.git
Update example and doc.
This commit is contained in:
parent
331f882d34
commit
37e2d9f22a
139
v2/doc.go
139
v2/doc.go
|
@ -3,9 +3,11 @@ Package github.com/vmihailenco/redis implements a Redis client.
|
|||
|
||||
Let's start with connecting to Redis using TCP:
|
||||
|
||||
password := "" // no password set
|
||||
db := int64(-1) // use default DB
|
||||
client := redis.NewTCPClient("localhost:6379", password, db)
|
||||
client := redis.NewTCPClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
Password: "", // no password set
|
||||
DB: 0, // use default DB
|
||||
})
|
||||
defer client.Close()
|
||||
|
||||
ping := client.Ping()
|
||||
|
@ -14,7 +16,9 @@ Let's start with connecting to Redis using TCP:
|
|||
|
||||
or using Unix socket:
|
||||
|
||||
client := redis.NewUnixClient("/tmp/redis.sock", "", -1)
|
||||
client := redis.NewUnixClient(&redis.Options{
|
||||
Addr: "/tmp/redis.sock",
|
||||
})
|
||||
defer client.Close()
|
||||
|
||||
ping := client.Ping()
|
||||
|
@ -23,6 +27,11 @@ or using Unix socket:
|
|||
|
||||
Then we can start sending commands:
|
||||
|
||||
client := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":6379",
|
||||
})
|
||||
defer client.Close()
|
||||
|
||||
set := client.Set("foo", "bar")
|
||||
fmt.Println(set.Err(), set.Val())
|
||||
|
||||
|
@ -34,100 +43,120 @@ Then we can start sending commands:
|
|||
|
||||
We can also pipeline two commands together:
|
||||
|
||||
var set *redis.StatusReq
|
||||
var get *redis.StringReq
|
||||
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
||||
set = c.Set("key1", "hello1")
|
||||
get = c.Get("key2")
|
||||
client := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":6379",
|
||||
})
|
||||
fmt.Println(err, reqs)
|
||||
fmt.Println(set)
|
||||
fmt.Println(get)
|
||||
// Output: <nil> [SET key1 hello1: OK GET key2: (nil)]
|
||||
// SET key1 hello1: OK
|
||||
// GET key2: (nil)
|
||||
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)
|
||||
|
||||
or:
|
||||
|
||||
var set *redis.StatusReq
|
||||
var get *redis.StringReq
|
||||
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
||||
set = c.Set("key1", "hello1")
|
||||
get = c.Get("key2")
|
||||
client := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":6379",
|
||||
})
|
||||
fmt.Println(err, reqs)
|
||||
defer client.Close()
|
||||
|
||||
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 GET key2]
|
||||
// SET key1 hello1
|
||||
// GET key2
|
||||
// Output: [SET key1 hello1: OK GET key2: (nil)] (nil)
|
||||
// SET key1 hello1: OK
|
||||
// GET key2: (nil)
|
||||
|
||||
We can also send several commands in transaction:
|
||||
|
||||
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
|
||||
get := multi.Get("key")
|
||||
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)
|
||||
|
||||
reqs, err := multi.Exec(func() {
|
||||
multi.Set("key", strconv.FormatInt(val+1, 10))
|
||||
return tx.Exec(func() {
|
||||
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()
|
||||
_ = err
|
||||
defer multi.Close()
|
||||
client := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":6379",
|
||||
})
|
||||
defer client.Close()
|
||||
|
||||
watch := multi.Watch("key")
|
||||
client.Del("key")
|
||||
|
||||
tx := client.Multi()
|
||||
defer tx.Close()
|
||||
|
||||
watch := tx.Watch("key")
|
||||
_ = watch.Err()
|
||||
|
||||
reqs, err := transaction(multi)
|
||||
fmt.Println(err, reqs)
|
||||
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:
|
||||
|
||||
pubsub, err := client.PubSubClient()
|
||||
client := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":6379",
|
||||
})
|
||||
defer client.Close()
|
||||
|
||||
pubsub := client.PubSub()
|
||||
defer pubsub.Close()
|
||||
|
||||
ch, err := pubsub.Subscribe("mychannel")
|
||||
err := pubsub.Subscribe("mychannel")
|
||||
_ = err
|
||||
|
||||
subscribeMsg := <-ch
|
||||
fmt.Println(subscribeMsg.Err, subscribeMsg.Name)
|
||||
msg, err := pubsub.Receive()
|
||||
fmt.Println(msg, err)
|
||||
|
||||
pub := client.Publish("mychannel", "hello")
|
||||
_ = pub.Err()
|
||||
|
||||
msg := <-ch
|
||||
fmt.Println(msg.Err, msg.Message)
|
||||
msg, err = pubsub.Receive()
|
||||
fmt.Println(msg, err)
|
||||
|
||||
// Output: <nil> subscribe
|
||||
// <nil> hello
|
||||
// Output: &{subscribe mychannel 1} <nil>
|
||||
// &{mychannel hello} <nil>
|
||||
|
||||
You can also write custom commands:
|
||||
|
||||
func Get(client *redis.Client, key string) *redis.StringReq {
|
||||
req := redis.NewStringReq("GET", key)
|
||||
client.Process(req)
|
||||
return req
|
||||
func Get(client *redis.Client, key string) *redis.StringCmd {
|
||||
cmd := redis.NewStringCmd("GET", key)
|
||||
client.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func ExampleCustomCommand() {
|
||||
client := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":6379",
|
||||
})
|
||||
defer client.Close()
|
||||
|
||||
get := Get(client, "key_does_not_exist")
|
||||
fmt.Println(get.Err(), get.Val())
|
||||
// Output: (nil)
|
||||
|
||||
Client uses connection pool to send commands. You can change maximum number of connections with:
|
||||
|
||||
client.ConnPool.(*redis.MultiConnPool).MaxCap = 1
|
||||
}
|
||||
*/
|
||||
package redis
|
||||
|
|
|
@ -47,27 +47,40 @@ 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) {
|
||||
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
|
||||
|
@ -75,17 +88,11 @@ func incr(tx *redis.Multi) ([]redis.Cmder, error) {
|
|||
|
||||
val, _ := strconv.ParseInt(get.Val(), 10, 64)
|
||||
|
||||
cmds, err := tx.Exec(func() {
|
||||
return 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() {
|
||||
client := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":6379",
|
||||
})
|
||||
|
@ -99,8 +106,17 @@ func ExampleMulti() {
|
|||
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]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue