redis/v2/example_test.go

156 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()
ping := client.Ping()
2013-09-29 12:55:26 +04:00
fmt.Println(ping.Val(), ping.Err())
// 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")
2013-11-04 11:47:36 +04:00
fmt.Println(set.Val(), set.Err())
2013-07-02 15:17:31 +04:00
2013-11-04 12:08:03 +04:00
get := client.Get("hello")
fmt.Printf("%q %s %v", get.Val(), get.Err(), get.Err() == redis.Nil)
2013-07-02 15:17:31 +04:00
2013-11-04 11:47:36 +04:00
// Output: OK <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
})
2013-11-04 11:47:36 +04:00
set := cmds[0].(*redis.StatusCmd)
get := cmds[1].(*redis.StringCmd)
fmt.Println(set, get, err)
// Output: SET key1 hello1: OK GET key1: hello1 <nil>
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) {
get := tx.Get("key")
if err := get.Err(); err != nil && err != redis.Nil {
return nil, err
}
2013-07-02 15:17:31 +04:00
2013-09-29 12:31:57 +04:00
val, _ := strconv.ParseInt(get.Val(), 10, 64)
2013-07-02 15:17:31 +04:00
2013-09-29 12:31:57 +04:00
return tx.Exec(func() {
tx.Set("key", strconv.FormatInt(val+1, 10))
})
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
run1 := setnx.Run(client, []string{"keynx"}, []string{"foo"})
fmt.Println(run1.Val().(int64), run1.Err())
run2 := setnx.Run(client, []string{"keynx"}, []string{"bar"})
fmt.Println(run2.Val().(int64), run2.Err())
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
get := Get(client, "key_does_not_exist")
2013-09-29 12:55:26 +04:00
fmt.Printf("%q %s", get.Val(), get.Err())
2013-11-04 12:08:03 +04:00
// Output: "" redis: nil
2013-07-02 15:17:31 +04:00
}