redis/v2/example_test.go

159 lines
3.0 KiB
Go
Raw Normal View History

2013-07-02 15:17:31 +04:00
package redis_test
import (
"fmt"
"strconv"
"github.com/vmihailenco/redis/v2"
)
2013-11-04 12:08:03 +04:00
var client *redis.Client
func init() {
client = redis.NewTCPClient(&redis.Options{
Addr: ":6379",
})
}
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()
2014-01-09 12:43:27 +04:00
pong, err := client.Ping().Result()
fmt.Println(pong, err)
2013-09-29 12:55:26 +04:00
// Output: PONG <nil>
2013-07-02 15:17:31 +04:00
}
2013-09-29 12:40:17 +04:00
func ExampleClient() {
2013-07-02 15:17:31 +04:00
set := client.Set("foo", "bar")
2014-01-09 12:43:27 +04:00
fmt.Println(set.Err())
2013-07-02 15:17:31 +04:00
2014-01-09 12:43:27 +04:00
v, err := client.Get("hello").Result()
fmt.Printf("%q %s %v", v, err, err == redis.Nil)
2013-07-02 15:17:31 +04:00
2014-01-09 12:43:27 +04:00
// Output: <nil>
2013-11-04 12:08:03 +04:00
// "" redis: nil true
2013-07-02 15:17:31 +04:00
}
2013-09-29 12:48:45 +04:00
func ExampleClient_Pipelined() {
2013-09-29 12:06:49 +04:00
cmds, err := client.Pipelined(func(c *redis.Pipeline) {
2013-09-29 12:31:57 +04:00
c.Set("key1", "hello1")
2013-11-04 11:47:36 +04:00
c.Get("key1")
2013-09-29 12:31:57 +04:00
})
2014-01-09 12:43:27 +04:00
fmt.Println(err)
2013-11-04 11:47:36 +04:00
set := cmds[0].(*redis.StatusCmd)
2014-01-09 12:43:27 +04:00
fmt.Println(set)
2013-11-04 11:47:36 +04:00
get := cmds[1].(*redis.StringCmd)
2014-01-09 12:43:27 +04:00
fmt.Println(get)
// Output: <nil>
// SET key1 hello1: OK
// GET key1: hello1
2013-09-29 12:31:57 +04:00
}
func ExamplePipeline() {
pipeline := client.Pipeline()
set := pipeline.Set("key1", "hello1")
2013-11-04 12:08:03 +04:00
get := pipeline.Get("key1")
2013-09-29 12:31:57 +04:00
cmds, err := pipeline.Exec()
fmt.Println(cmds, err)
2013-07-02 15:17:31 +04:00
fmt.Println(set)
fmt.Println(get)
2013-11-04 12:08:03 +04:00
// Output: [SET key1 hello1: OK GET key1: hello1] <nil>
2013-07-02 15:17:31 +04:00
// SET key1 hello1: OK
2013-11-04 12:08:03 +04:00
// GET key1: hello1
2013-07-02 15:17:31 +04:00
}
2013-09-29 12:31:57 +04:00
func ExampleMulti() {
incr := func(tx *redis.Multi) ([]redis.Cmder, error) {
2014-01-09 12:43:27 +04:00
s, err := tx.Get("key").Result()
if err != nil && err != redis.Nil {
2013-09-29 12:31:57 +04:00
return nil, err
}
2014-01-09 12:43:27 +04:00
n, _ := strconv.ParseInt(s, 10, 64)
2013-07-02 15:17:31 +04:00
2013-09-29 12:31:57 +04:00
return tx.Exec(func() {
2014-01-09 12:43:27 +04:00
tx.Set("key", strconv.FormatInt(n+1, 10))
2013-09-29 12:31:57 +04:00
})
2013-07-02 15:17:31 +04:00
}
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:31:57 +04:00
for {
cmds, err := incr(tx)
2013-09-29 13:41:04 +04:00
if err == redis.TxFailedErr {
2013-09-29 12:31:57 +04:00
continue
} else if err != nil {
panic(err)
}
2013-09-29 12:55:26 +04:00
fmt.Println(cmds, err)
2013-09-29 12:31:57 +04:00
break
}
2013-07-02 15:17:31 +04:00
2013-09-29 12:55:26 +04:00
// Output: [SET key 1: OK] <nil>
2013-07-02 15:17:31 +04:00
}
func ExamplePubSub() {
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()
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()
fmt.Println(msg, err)
2013-07-02 15:17:31 +04:00
// Output: &{subscribe mychannel 1} <nil>
// &{mychannel hello} <nil>
2013-07-02 15:17:31 +04:00
}
2013-10-15 14:49:21 +04:00
func ExampleScript() {
setnx := redis.NewScript(`
2013-10-15 14:55:00 +04:00
if redis.call("get", KEYS[1]) == false then
redis.call("set", KEYS[1], ARGV[1])
return 1
end
return 0
`)
2013-10-15 14:49:21 +04:00
2014-01-09 12:43:27 +04:00
v1, err := setnx.Run(client, []string{"keynx"}, []string{"foo"}).Result()
fmt.Println(v1.(int64), err)
2013-10-15 14:49:21 +04:00
2014-01-09 12:43:27 +04:00
v2, err := setnx.Run(client, []string{"keynx"}, []string{"bar"}).Result()
fmt.Println(v2.(int64), err)
2013-10-15 14:49:21 +04:00
get := client.Get("keynx")
fmt.Println(get)
// Output: 1 <nil>
// 0 <nil>
// GET keynx: foo
}
2013-09-29 12:49:54 +04:00
func Example_customCommand() {
2013-09-29 12:48:45 +04:00
Get := func(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
2014-01-09 12:43:27 +04:00
v, err := Get(client, "key_does_not_exist").Result()
fmt.Printf("%q %s", v, err)
2013-11-04 12:08:03 +04:00
// Output: "" redis: nil
2013-07-02 15:17:31 +04:00
}