redis/example_test.go

172 lines
3.3 KiB
Go
Raw Normal View History

package redis_test
import (
"fmt"
"strconv"
2014-07-02 19:04:04 +04:00
"gopkg.in/redis.v2"
)
2014-05-11 11:42:40 +04:00
var client *redis.Client
2014-05-11 11:42:40 +04:00
func init() {
client = redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
2014-07-02 18:55:00 +04:00
client.FlushDb()
}
2014-05-11 11:42:40 +04:00
func ExampleNewTCPClient() {
client := redis.NewTCPClient(&redis.Options{
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() {
client := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "master",
SentinelAddrs: []string{":26379"},
})
2014-05-11 11:42:40 +04:00
pong, err := client.Ping().Result()
fmt.Println(pong, err)
// Output: PONG <nil>
}
2014-05-11 11:42:40 +04:00
func ExampleClient() {
set := client.Set("foo", "bar")
2014-05-11 11:42:40 +04:00
fmt.Println(set.Err())
2014-05-11 11:42:40 +04:00
v, err := client.Get("hello").Result()
fmt.Printf("%q %s %v", v, err, err == redis.Nil)
2014-05-11 11:42:40 +04:00
// Output: <nil>
// "" redis: nil true
}
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 {
2014-05-11 11:42:40 +04:00
c.Set("key1", "hello1")
c.Get("key1")
2014-07-02 18:55:00 +04:00
return nil
})
2014-05-11 11:42:40 +04:00
fmt.Println(err)
set := cmds[0].(*redis.StatusCmd)
fmt.Println(set)
2014-05-11 11:42:40 +04:00
get := cmds[1].(*redis.StringCmd)
fmt.Println(get)
2014-05-11 11:42:40 +04:00
// Output: <nil>
// SET key1 hello1: OK
2014-05-11 11:42:40 +04:00
// GET key1: hello1
}
2014-05-11 11:42:40 +04:00
func ExamplePipeline() {
pipeline := client.Pipeline()
set := pipeline.Set("key1", "hello1")
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
}
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 {
2014-05-11 11:42:40 +04:00
tx.Set("key", strconv.FormatInt(n+1, 10))
2014-07-02 18:55:00 +04:00
return nil
2014-05-11 11:42:40 +04:00
})
}
client.Del("key")
2014-05-11 11:42:40 +04:00
tx := client.Multi()
defer tx.Close()
2014-05-11 11:42:40 +04:00
watch := tx.Watch("key")
_ = 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
}
2014-05-11 11:42:40 +04:00
// Output: [SET key 1: OK] <nil>
}
func ExamplePubSub() {
2014-05-11 11:42:40 +04:00
pubsub := client.PubSub()
defer pubsub.Close()
2014-05-11 11:42:40 +04:00
err := pubsub.Subscribe("mychannel")
_ = err
2014-05-11 11:42:40 +04:00
msg, err := pubsub.Receive()
fmt.Println(msg, err)
pub := client.Publish("mychannel", "hello")
_ = pub.Err()
2014-05-11 11:42:40 +04:00
msg, err = pubsub.Receive()
fmt.Println(msg, err)
2014-05-11 18:11:55 +04:00
// Output: subscribe: mychannel <nil>
// Message<mychannel: hello> <nil>
}
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
}
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
}
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
}