2013-02-17 20:49:17 +04:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-05-23 14:15:05 +03:00
|
|
|
"net"
|
2013-02-17 20:49:17 +04:00
|
|
|
"strconv"
|
2015-05-23 14:15:05 +03:00
|
|
|
"time"
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2015-05-14 15:19:29 +03:00
|
|
|
"gopkg.in/redis.v3"
|
2013-02-17 20:49:17 +04:00
|
|
|
)
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
var client *redis.Client
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func init() {
|
2015-05-02 16:19:22 +03:00
|
|
|
client = redis.NewClient(&redis.Options{
|
2014-05-11 11:42:40 +04:00
|
|
|
Addr: ":6379",
|
|
|
|
})
|
2014-07-02 18:55:00 +04:00
|
|
|
client.FlushDb()
|
2013-02-17 20:49:17 +04:00
|
|
|
}
|
|
|
|
|
2015-05-02 16:19:22 +03:00
|
|
|
func ExampleNewClient() {
|
|
|
|
client := redis.NewClient(&redis.Options{
|
2014-05-11 11:42:40 +04:00
|
|
|
Addr: "localhost:6379",
|
|
|
|
Password: "", // no password set
|
|
|
|
DB: 0, // use default DB
|
|
|
|
})
|
2014-07-13 16:49:33 +04:00
|
|
|
|
|
|
|
pong, err := client.Ping().Result()
|
|
|
|
fmt.Println(pong, err)
|
|
|
|
// Output: PONG <nil>
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleNewFailoverClient() {
|
2015-05-23 16:35:30 +03:00
|
|
|
// See http://redis.io/topics/sentinel for instructions how to
|
|
|
|
// setup Redis Sentinel.
|
|
|
|
client := redis.NewFailoverClient(&redis.FailoverOptions{
|
2015-01-25 15:33:30 +03:00
|
|
|
MasterName: "master",
|
2014-07-13 16:49:33 +04:00
|
|
|
SentinelAddrs: []string{":26379"},
|
|
|
|
})
|
2015-05-23 16:35:30 +03:00
|
|
|
client.Ping()
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleNewClusterClient() {
|
|
|
|
// See http://redis.io/topics/cluster-tutorial for instructions
|
|
|
|
// how to setup Redis Cluster.
|
|
|
|
client := redis.NewClusterClient(&redis.ClusterOptions{
|
|
|
|
Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"},
|
|
|
|
})
|
|
|
|
client.Ping()
|
2013-02-17 20:49:17 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func ExampleClient() {
|
2015-05-23 14:33:33 +03:00
|
|
|
err := client.Set("key", "value", 0).Err()
|
|
|
|
if err != nil {
|
2014-07-31 16:18:23 +04:00
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2015-05-23 14:33:33 +03:00
|
|
|
val, err := client.Get("key").Result()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println("key", val)
|
|
|
|
|
|
|
|
val2, err := client.Get("key2").Result()
|
|
|
|
if err == redis.Nil {
|
|
|
|
fmt.Println("key2 does not exists")
|
|
|
|
} else if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
fmt.Println("key2", val2)
|
|
|
|
}
|
|
|
|
// Output: key value
|
|
|
|
// key2 does not exists
|
2014-07-31 16:18:23 +04:00
|
|
|
}
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2014-07-31 16:18:23 +04:00
|
|
|
func ExampleClient_Incr() {
|
|
|
|
if err := client.Incr("counter").Err(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := client.Get("counter").Int64()
|
|
|
|
fmt.Println(n, err)
|
|
|
|
// Output: 1 <nil>
|
2013-02-17 20:49:17 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func ExampleClient_Pipelined() {
|
2014-07-02 18:55:00 +04:00
|
|
|
cmds, err := client.Pipelined(func(c *redis.Pipeline) error {
|
2015-01-31 12:08:56 +03:00
|
|
|
c.Set("key1", "hello1", 0)
|
2014-05-11 11:42:40 +04:00
|
|
|
c.Get("key1")
|
2014-07-02 18:55:00 +04:00
|
|
|
return nil
|
2013-02-17 20:49:17 +04:00
|
|
|
})
|
2014-05-11 11:42:40 +04:00
|
|
|
fmt.Println(err)
|
|
|
|
set := cmds[0].(*redis.StatusCmd)
|
2013-02-17 20:49:17 +04:00
|
|
|
fmt.Println(set)
|
2014-05-11 11:42:40 +04:00
|
|
|
get := cmds[1].(*redis.StringCmd)
|
2013-02-17 20:49:17 +04:00
|
|
|
fmt.Println(get)
|
2014-05-11 11:42:40 +04:00
|
|
|
// Output: <nil>
|
2013-02-17 20:49:17 +04:00
|
|
|
// SET key1 hello1: OK
|
2014-05-11 11:42:40 +04:00
|
|
|
// GET key1: hello1
|
2013-02-17 20:49:17 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func ExamplePipeline() {
|
|
|
|
pipeline := client.Pipeline()
|
2015-01-31 12:08:56 +03:00
|
|
|
set := pipeline.Set("key1", "hello1", 0)
|
2014-05-11 11:42:40 +04:00
|
|
|
get := pipeline.Get("key1")
|
|
|
|
cmds, err := pipeline.Exec()
|
|
|
|
fmt.Println(cmds, err)
|
|
|
|
fmt.Println(set)
|
|
|
|
fmt.Println(get)
|
|
|
|
// Output: [SET key1 hello1: OK GET key1: hello1] <nil>
|
|
|
|
// SET key1 hello1: OK
|
|
|
|
// GET key1: hello1
|
2013-02-17 20:49:17 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func ExampleMulti() {
|
|
|
|
incr := func(tx *redis.Multi) ([]redis.Cmder, error) {
|
|
|
|
s, err := tx.Get("key").Result()
|
|
|
|
if err != nil && err != redis.Nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
n, _ := strconv.ParseInt(s, 10, 64)
|
|
|
|
|
2014-07-02 18:55:00 +04:00
|
|
|
return tx.Exec(func() error {
|
2015-01-31 12:08:56 +03:00
|
|
|
tx.Set("key", strconv.FormatInt(n+1, 10), 0)
|
2014-07-02 18:55:00 +04:00
|
|
|
return nil
|
2014-05-11 11:42:40 +04:00
|
|
|
})
|
|
|
|
}
|
2013-02-17 20:49:17 +04:00
|
|
|
|
|
|
|
client.Del("key")
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
tx := client.Multi()
|
|
|
|
defer tx.Close()
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
watch := tx.Watch("key")
|
2013-02-17 20:49:17 +04:00
|
|
|
_ = watch.Err()
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
for {
|
|
|
|
cmds, err := incr(tx)
|
|
|
|
if err == redis.TxFailedErr {
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println(cmds, err)
|
|
|
|
break
|
|
|
|
}
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
// Output: [SET key 1: OK] <nil>
|
2013-02-17 20:49:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExamplePubSub() {
|
2014-05-11 11:42:40 +04:00
|
|
|
pubsub := client.PubSub()
|
2013-02-17 20:49:17 +04:00
|
|
|
defer pubsub.Close()
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
err := pubsub.Subscribe("mychannel")
|
2015-05-23 14:15:05 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2015-05-23 14:15:05 +03:00
|
|
|
err = client.Publish("mychannel", "hello").Err()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2015-05-23 14:15:05 +03:00
|
|
|
for {
|
|
|
|
msgi, err := pubsub.ReceiveTimeout(100 * time.Millisecond)
|
|
|
|
if err != nil {
|
|
|
|
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
|
|
|
|
// There are no more messages to process. Stop.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2015-05-23 14:15:05 +03:00
|
|
|
switch msg := msgi.(type) {
|
|
|
|
case *redis.Subscription:
|
|
|
|
fmt.Println(msg.Kind, msg.Channel)
|
|
|
|
case *redis.Message:
|
|
|
|
fmt.Println(msg.Channel, msg.Payload)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown message: %#v", msgi))
|
|
|
|
}
|
|
|
|
}
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2015-05-23 14:15:05 +03:00
|
|
|
// Output: subscribe mychannel
|
|
|
|
// mychannel hello
|
2013-02-17 20:49:17 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func ExampleScript() {
|
|
|
|
setnx := redis.NewScript(`
|
|
|
|
if redis.call("get", KEYS[1]) == false then
|
|
|
|
redis.call("set", KEYS[1], ARGV[1])
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
`)
|
|
|
|
|
|
|
|
v1, err := setnx.Run(client, []string{"keynx"}, []string{"foo"}).Result()
|
|
|
|
fmt.Println(v1.(int64), err)
|
|
|
|
|
|
|
|
v2, err := setnx.Run(client, []string{"keynx"}, []string{"bar"}).Result()
|
|
|
|
fmt.Println(v2.(int64), err)
|
|
|
|
|
|
|
|
get := client.Get("keynx")
|
|
|
|
fmt.Println(get)
|
|
|
|
|
|
|
|
// Output: 1 <nil>
|
|
|
|
// 0 <nil>
|
|
|
|
// GET keynx: foo
|
2013-02-17 20:49:17 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func Example_customCommand() {
|
|
|
|
Get := func(client *redis.Client, key string) *redis.StringCmd {
|
|
|
|
cmd := redis.NewStringCmd("GET", key)
|
|
|
|
client.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
2013-02-17 20:49:17 +04:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
v, err := Get(client, "key_does_not_exist").Result()
|
|
|
|
fmt.Printf("%q %s", v, err)
|
|
|
|
// Output: "" redis: nil
|
2013-02-17 20:49:17 +04:00
|
|
|
}
|