2013-07-02 15:17:31 +04:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/vmihailenco/redis/v2"
|
|
|
|
)
|
|
|
|
|
2013-09-29 12:18:47 +04:00
|
|
|
func ExampleNewTCPClient() {
|
2013-09-29 12:15:18 +04:00
|
|
|
client := redis.NewTCPClient(&redis.Options{
|
2013-09-11 20:22:10 +04:00
|
|
|
Addr: "localhost:6379",
|
|
|
|
Password: "", // no password set
|
|
|
|
DB: 0, // use default DB
|
|
|
|
})
|
2013-07-02 15:17:31 +04:00
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
ping := client.Ping()
|
|
|
|
fmt.Println(ping.Err(), ping.Val())
|
|
|
|
// Output: <nil> PONG
|
|
|
|
}
|
|
|
|
|
2013-09-29 12:18:47 +04:00
|
|
|
func ExampleNewUnixClient() {
|
2013-09-29 12:15:18 +04:00
|
|
|
client := redis.NewUnixClient(&redis.Options{
|
2013-09-11 20:22:10 +04:00
|
|
|
Addr: "/tmp/redis.sock",
|
|
|
|
})
|
2013-07-02 15:17:31 +04:00
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
ping := client.Ping()
|
|
|
|
fmt.Println(ping.Err(), ping.Val())
|
|
|
|
// Output: <nil> PONG
|
|
|
|
}
|
|
|
|
|
2013-09-29 12:18:47 +04:00
|
|
|
func ExampleSet() {
|
2013-09-29 12:15:18 +04:00
|
|
|
client := redis.NewTCPClient(&redis.Options{
|
2013-09-11 20:22:10 +04:00
|
|
|
Addr: ":6379",
|
|
|
|
})
|
2013-07-02 15:17:31 +04:00
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
set := client.Set("foo", "bar")
|
|
|
|
fmt.Println(set.Err(), set.Val())
|
|
|
|
|
|
|
|
get := client.Get("foo")
|
|
|
|
fmt.Println(get.Err(), get.Val())
|
|
|
|
|
|
|
|
// Output: <nil> OK
|
|
|
|
// <nil> bar
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExamplePipeline() {
|
2013-09-29 12:15:18 +04:00
|
|
|
client := redis.NewTCPClient(&redis.Options{
|
2013-09-11 20:22:10 +04:00
|
|
|
Addr: ":6379",
|
|
|
|
})
|
2013-07-02 15:17:31 +04:00
|
|
|
defer client.Close()
|
|
|
|
|
2013-09-29 12:06:49 +04:00
|
|
|
var set *redis.StatusCmd
|
|
|
|
var get *redis.StringCmd
|
|
|
|
cmds, err := client.Pipelined(func(c *redis.Pipeline) {
|
2013-07-02 15:17:31 +04:00
|
|
|
set = c.Set("key1", "hello1")
|
|
|
|
get = c.Get("key2")
|
|
|
|
})
|
2013-09-29 12:06:49 +04:00
|
|
|
fmt.Println(err, cmds)
|
2013-07-02 15:17:31 +04:00
|
|
|
fmt.Println(set)
|
|
|
|
fmt.Println(get)
|
2013-09-29 11:17:39 +04:00
|
|
|
// Output: (nil) [SET key1 hello1: OK GET key2: (nil)]
|
2013-07-02 15:17:31 +04:00
|
|
|
// SET key1 hello1: OK
|
|
|
|
// GET key2: (nil)
|
|
|
|
}
|
|
|
|
|
2013-09-29 12:06:49 +04:00
|
|
|
func incr(tx *redis.Multi) ([]redis.Cmder, error) {
|
2013-09-29 11:17:39 +04:00
|
|
|
get := tx.Get("key")
|
2013-07-02 15:17:31 +04:00
|
|
|
if err := get.Err(); err != nil && err != redis.Nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, _ := strconv.ParseInt(get.Val(), 10, 64)
|
|
|
|
|
2013-09-29 12:06:49 +04:00
|
|
|
cmds, err := tx.Exec(func() {
|
2013-09-29 11:17:39 +04:00
|
|
|
tx.Set("key", strconv.FormatInt(val+1, 10))
|
2013-07-02 15:17:31 +04:00
|
|
|
})
|
|
|
|
// Transaction failed. Repeat.
|
|
|
|
if err == redis.Nil {
|
2013-09-29 11:17:39 +04:00
|
|
|
return incr(tx)
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
2013-09-29 12:06:49 +04:00
|
|
|
return cmds, err
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-09-29 12:18:47 +04:00
|
|
|
func ExampleMulti() {
|
2013-09-29 12:15:18 +04:00
|
|
|
client := redis.NewTCPClient(&redis.Options{
|
2013-09-11 20:22:10 +04:00
|
|
|
Addr: ":6379",
|
|
|
|
})
|
2013-07-02 15:17:31 +04:00
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
client.Del("key")
|
|
|
|
|
2013-09-29 11:17:39 +04:00
|
|
|
tx := client.Multi()
|
|
|
|
defer tx.Close()
|
2013-07-02 15:17:31 +04:00
|
|
|
|
2013-09-29 11:17:39 +04:00
|
|
|
watch := tx.Watch("key")
|
2013-07-02 15:17:31 +04:00
|
|
|
_ = watch.Err()
|
|
|
|
|
2013-09-29 12:06:49 +04:00
|
|
|
cmds, err := incr(tx)
|
|
|
|
fmt.Println(err, cmds)
|
2013-07-02 15:17:31 +04:00
|
|
|
|
|
|
|
// Output: <nil> [SET key 1: OK]
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExamplePubSub() {
|
2013-09-29 12:15:18 +04:00
|
|
|
client := redis.NewTCPClient(&redis.Options{
|
2013-09-11 20:22:10 +04:00
|
|
|
Addr: ":6379",
|
|
|
|
})
|
2013-07-02 15:17:31 +04:00
|
|
|
defer client.Close()
|
|
|
|
|
2013-09-29 10:37:09 +04:00
|
|
|
pubsub := client.PubSub()
|
2013-07-02 15:17:31 +04:00
|
|
|
defer pubsub.Close()
|
|
|
|
|
2013-09-29 10:37:09 +04:00
|
|
|
err := pubsub.Subscribe("mychannel")
|
|
|
|
_ = err
|
2013-07-02 15:17:31 +04:00
|
|
|
|
2013-09-29 10:37:09 +04:00
|
|
|
msg, err := pubsub.Receive()
|
2013-09-28 14:24:22 +04:00
|
|
|
fmt.Println(msg, err)
|
2013-07-02 15:17:31 +04:00
|
|
|
|
|
|
|
pub := client.Publish("mychannel", "hello")
|
|
|
|
_ = pub.Err()
|
|
|
|
|
2013-09-29 10:37:09 +04:00
|
|
|
msg, err = pubsub.Receive()
|
2013-09-28 14:24:22 +04:00
|
|
|
fmt.Println(msg, err)
|
2013-07-02 15:17:31 +04:00
|
|
|
|
2013-09-28 14:24:22 +04:00
|
|
|
// Output: &{subscribe mychannel 1} <nil>
|
|
|
|
// &{mychannel hello} <nil>
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-09-29 12:06:49 +04:00
|
|
|
func Get(client *redis.Client, key string) *redis.StringCmd {
|
|
|
|
cmd := redis.NewStringCmd("GET", key)
|
|
|
|
client.Process(cmd)
|
|
|
|
return cmd
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleCustomCommand() {
|
2013-09-29 12:15:18 +04:00
|
|
|
client := redis.NewTCPClient(&redis.Options{
|
2013-09-11 20:22:10 +04:00
|
|
|
Addr: ":6379",
|
|
|
|
})
|
2013-07-02 15:17:31 +04:00
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
get := Get(client, "key_does_not_exist")
|
|
|
|
fmt.Println(get.Err(), get.Val())
|
|
|
|
// Output: (nil)
|
|
|
|
}
|