mirror of https://github.com/go-redis/redis.git
Update doc.
This commit is contained in:
parent
075cf971dd
commit
2a4280a905
128
doc.go
128
doc.go
|
@ -1,109 +1,118 @@
|
||||||
// Copyright 2012 The Redis Client Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Package github.com/vmihailenco/redis implements a Redis client.
|
Package github.com/vmihailenco/redis implements a Redis client.
|
||||||
|
|
||||||
Let's start with connecting to Redis:
|
Let's start with connecting to Redis using TCP:
|
||||||
|
|
||||||
password := "" // no password set
|
password := "" // no password set
|
||||||
db := -1 // use default DB
|
db := int64(-1) // use default DB
|
||||||
client := redis.NewTCPClient("localhost:6379", password, db)
|
client := redis.NewTCPClient("localhost:6379", password, db)
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
|
ping := client.Ping()
|
||||||
|
fmt.Println(ping.Err(), ping.Val())
|
||||||
|
// Output: <nil> PONG
|
||||||
|
|
||||||
|
or using Unix socket:
|
||||||
|
|
||||||
|
client := redis.NewUnixClient("/tmp/redis.sock", "", -1)
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
ping := client.Ping()
|
||||||
|
fmt.Println(ping.Err(), ping.Val())
|
||||||
|
// Output: <nil> PONG
|
||||||
|
|
||||||
Then we can start sending commands:
|
Then we can start sending commands:
|
||||||
|
|
||||||
if err := client.Set("foo", "bar"); err != nil { panic(err) }
|
set := client.Set("foo", "bar")
|
||||||
|
fmt.Println(set.Err(), set.Val())
|
||||||
|
|
||||||
get := client.Get("foo")
|
get := client.Get("foo")
|
||||||
if err := get.Err(); err != nil { panic(err) }
|
fmt.Println(get.Err(), get.Val())
|
||||||
fmt.Println(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
|
var set *redis.StatusReq
|
||||||
var get *redis.StringReq
|
var get *redis.StringReq
|
||||||
reqs, err := client.Pipelined(func(c *redis.PipelineClient)) {
|
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
||||||
set = c.Set("key1", "hello1")
|
set = c.Set("key1", "hello1")
|
||||||
get = c.Get("key2")
|
get = c.Get("key2")
|
||||||
}
|
})
|
||||||
if err != nil { panic(err) }
|
fmt.Println(err, reqs)
|
||||||
if err := set.Err(); err != nil { panic(err) }
|
fmt.Println(set)
|
||||||
if err := get.Err(); err != nil { panic(err) }
|
fmt.Println(get)
|
||||||
fmt.Println(get.Val())
|
// Output: <nil> [SET key1 hello1: OK GET key2: (nil)]
|
||||||
fmt.Println(reqs[0] == set)
|
// SET key1 hello1: OK
|
||||||
fmt.Println(reqs[1] == get)
|
// GET key2: (nil)
|
||||||
|
|
||||||
or:
|
or:
|
||||||
|
|
||||||
pipeline, err := client.PipelineClient()
|
var set *redis.StatusReq
|
||||||
if err != nil {
|
var get *redis.StringReq
|
||||||
panic(err)
|
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
||||||
}
|
set = c.Set("key1", "hello1")
|
||||||
defer pipeline.Close()
|
get = c.Get("key2")
|
||||||
|
})
|
||||||
set := pipeline.Set("key1", "hello1")
|
fmt.Println(err, reqs)
|
||||||
get := pipleine.Get("key2")
|
fmt.Println(set)
|
||||||
|
fmt.Println(get)
|
||||||
reqs, err := pipeline.RunQueued()
|
// Output: <nil> [SET key1 hello1 GET key2]
|
||||||
if err != nil { panic(err) }
|
// SET key1 hello1
|
||||||
|
// GET key2
|
||||||
if err := set.Err(); err != nil { panic(err) }
|
|
||||||
if err := get.Err(); err != nil { panic(err) }
|
|
||||||
fmt.Println(get.Val())
|
|
||||||
fmt.Println(reqs[0] == set)
|
|
||||||
fmt.Println(reqs[1] == get)
|
|
||||||
|
|
||||||
We can also send several commands in transaction:
|
We can also send several commands in transaction:
|
||||||
|
|
||||||
func incrKeyInTransaction(multi *redis.MultiClient) ([]redis.Req, error) {
|
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
|
||||||
get := multi.Get("key")
|
get := multi.Get("key")
|
||||||
if err := get.Err(); err != nil {
|
if err := get.Err(); err != nil && err != redis.Nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
val, err := strconv.ParseInt(get.Val(), 10, 64)
|
val, _ := strconv.ParseInt(get.Val(), 10, 64)
|
||||||
if err != nil { panic(err) }
|
|
||||||
|
|
||||||
reqs, err = multi.Exec(func() {
|
reqs, err := multi.Exec(func() {
|
||||||
multi.Set("key", val + 1)
|
multi.Set("key", strconv.FormatInt(val+1, 10))
|
||||||
})
|
})
|
||||||
// Transaction failed. Repeat.
|
// Transaction failed. Repeat.
|
||||||
if err == redis.Nil {
|
if err == redis.Nil {
|
||||||
return incrKeyInTransaction(multi)
|
return transaction(multi)
|
||||||
}
|
}
|
||||||
return reqs, err
|
return reqs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
multi, err := client.MultiClient()
|
multi, err := client.MultiClient()
|
||||||
if err != nil { panic(err) }
|
_ = err
|
||||||
defer multi.Close()
|
defer multi.Close()
|
||||||
|
|
||||||
watch := multi.Watch("key")
|
watch := multi.Watch("key")
|
||||||
if err := watch.Err(); err != nil { panic(err) }
|
_ = watch.Err()
|
||||||
|
|
||||||
reqs, err := incrKeyInTransaction(multi)
|
reqs, err := transaction(multi)
|
||||||
if err != nil { panic(err) }
|
fmt.Println(err, reqs)
|
||||||
for _, req := range reqs {
|
|
||||||
// ...
|
// Output: <nil> [SET key 1: OK]
|
||||||
}
|
|
||||||
|
|
||||||
To subscribe to the channel:
|
To subscribe to the channel:
|
||||||
|
|
||||||
pubsub, err := client.PubSubClient()
|
pubsub, err := client.PubSubClient()
|
||||||
if err != nil { panic(err) }
|
|
||||||
defer pubsub.Close()
|
defer pubsub.Close()
|
||||||
|
|
||||||
ch, err := pubsub.Subscribe("mychannel")
|
ch, err := pubsub.Subscribe("mychannel")
|
||||||
if err != nil { panic(err) }
|
_ = err
|
||||||
|
|
||||||
go func() {
|
subscribeMsg := <-ch
|
||||||
for msg := range ch {
|
fmt.Println(subscribeMsg.Err, subscribeMsg.Name)
|
||||||
if err := msg.Err; err != nil { panic(err) }
|
|
||||||
message := msg.Message
|
pub := client.Publish("mychannel", "hello")
|
||||||
}
|
_ = pub.Err()
|
||||||
}
|
|
||||||
|
msg := <-ch
|
||||||
|
fmt.Println(msg.Err, msg.Message)
|
||||||
|
|
||||||
|
// Output: <nil> subscribe
|
||||||
|
// <nil> hello
|
||||||
|
|
||||||
You can also write custom commands:
|
You can also write custom commands:
|
||||||
|
|
||||||
|
@ -113,8 +122,9 @@ You can also write custom commands:
|
||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
get := Get(redisClient, "key")
|
get := Get(client, "key_does_not_exist")
|
||||||
if err := get.Err(); err != nil && err != redis.Nil { panic(err) }
|
fmt.Println(get.Err(), get.Val())
|
||||||
|
// Output: (nil)
|
||||||
|
|
||||||
Client uses connection pool to send commands. You can change maximum number of connections with:
|
Client uses connection pool to send commands. You can change maximum number of connections with:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue