redis/example_test.go

310 lines
5.9 KiB
Go
Raw Normal View History

package redis_test
import (
"fmt"
"strconv"
"sync"
2015-05-23 14:15:05 +03:00
"time"
2015-05-14 15:19:29 +03:00
"gopkg.in/redis.v3"
)
2014-05-11 11:42:40 +04:00
var client *redis.Client
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()
}
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() {
// 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"},
})
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()
}
func ExampleNewRing() {
client := redis.NewRing(&redis.RingOptions{
Addrs: map[string]string{
"shard1": ":7000",
"shard2": ":7001",
"shard3": ":7002",
},
})
client.Ping()
}
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)
}
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
}
2015-08-07 17:02:17 +03:00
func ExampleClient_Set() {
// Last argument is expiration. Zero means the key has no
// expiration time.
err := client.Set("key", "value", 0).Err()
if err != nil {
panic(err)
}
// key2 will expire in an hour.
err = client.Set("key2", "value", time.Hour).Err()
if err != nil {
panic(err)
}
}
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>
}
2015-11-24 10:09:53 +03:00
func ExampleClient_BLPop() {
if err := client.RPush("queue", "message").Err(); err != nil {
panic(err)
}
// use `client.BLPop(0, "queue")` for infinite waiting time
result, err := client.BLPop(1*time.Second, "queue").Result()
if err != nil {
panic(err)
}
fmt.Println(result[0], result[1])
// Output: queue message
}
2015-08-19 11:44:46 +03:00
func ExampleClient_Scan() {
client.FlushDb()
for i := 0; i < 33; i++ {
err := client.Set(fmt.Sprintf("key%d", i), "value", 0).Err()
if err != nil {
panic(err)
}
}
var cursor int64
var n int
for {
var keys []string
var err error
cursor, keys, err = client.Scan(cursor, "", 10).Result()
if err != nil {
panic(err)
}
n += len(keys)
if cursor == 0 {
break
}
}
fmt.Printf("found %d keys\n", n)
// Output: found 33 keys
}
2014-05-11 11:42:40 +04:00
func ExampleClient_Pipelined() {
var incr *redis.IntCmd
_, err := client.Pipelined(func(pipe *redis.Pipeline) error {
incr = pipe.Incr("counter1")
pipe.Expire("counter1", time.Hour)
2014-07-02 18:55:00 +04:00
return nil
})
fmt.Println(incr.Val(), err)
// Output: 1 <nil>
}
2014-05-11 11:42:40 +04:00
func ExamplePipeline() {
pipe := client.Pipeline()
defer pipe.Close()
incr := pipe.Incr("counter2")
pipe.Expire("counter2", time.Hour)
_, err := pipe.Exec()
fmt.Println(incr.Val(), err)
// Output: 1 <nil>
}
func ExampleClient_Watch() {
2015-11-15 11:23:00 +03:00
var incr func(string) error
// Transactionally increments key using GET and SET commands.
2015-11-15 11:23:00 +03:00
incr = func(key string) error {
tx, err := client.Watch(key)
if err != nil {
return err
}
2015-11-15 11:23:00 +03:00
defer tx.Close()
n, err := tx.Get(key).Int64()
2014-05-11 11:42:40 +04:00
if err != nil && err != redis.Nil {
return err
2014-05-11 11:42:40 +04:00
}
_, err = tx.Exec(func() error {
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
})
2015-11-15 11:23:00 +03:00
if err == redis.TxFailedErr {
return incr(key)
}
return err
2014-05-11 11:42:40 +04:00
}
var wg sync.WaitGroup
2015-11-15 11:23:00 +03:00
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
2015-11-15 11:23:00 +03:00
err := incr("counter3")
if err != nil {
panic(err)
}
}()
2014-05-11 11:42:40 +04:00
}
wg.Wait()
n, err := client.Get("counter3").Int64()
fmt.Println(n, err)
2015-11-15 11:23:00 +03:00
// Output: 100 <nil>
}
func ExamplePubSub() {
2015-07-11 13:42:44 +03:00
pubsub, err := client.Subscribe("mychannel")
2015-05-23 14:15:05 +03:00
if err != nil {
panic(err)
}
2015-07-11 13:42:44 +03:00
defer pubsub.Close()
2015-05-23 14:15:05 +03:00
err = client.Publish("mychannel", "hello").Err()
if err != nil {
panic(err)
}
2015-09-06 13:50:16 +03:00
msg, err := pubsub.ReceiveMessage()
if err != nil {
panic(err)
}
fmt.Println(msg.Channel, msg.Payload)
// Output: mychannel hello
}
func ExamplePubSub_Receive() {
pubsub, err := client.Subscribe("mychannel")
if err != nil {
panic(err)
}
defer pubsub.Close()
err = client.Publish("mychannel", "hello").Err()
if err != nil {
panic(err)
}
for i := 0; i < 2; i++ {
2015-11-22 15:44:38 +03:00
// ReceiveTimeout is a low level API. Use ReceiveMessage instead.
2016-03-12 15:42:12 +03:00
msgi, err := pubsub.ReceiveTimeout(time.Second)
2015-05-23 14:15:05 +03:00
if err != nil {
2015-09-06 13:50:16 +03:00
panic(err)
2015-05-23 14:15:05 +03: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))
}
}
2015-05-23 14:15:05 +03:00
// Output: subscribe mychannel
// mychannel hello
}
2014-05-11 11:42:40 +04:00
func ExampleScript() {
2015-06-16 10:31:21 +03:00
IncrByXX := redis.NewScript(`
if redis.call("GET", KEYS[1]) ~= false then
return redis.call("INCRBY", KEYS[1], ARGV[1])
end
return false
`)
n, err := IncrByXX.Run(client, []string{"xx_counter"}, []string{"2"}).Result()
fmt.Println(n, err)
2014-05-11 11:42:40 +04:00
2015-06-16 10:31:21 +03:00
err = client.Set("xx_counter", "40", 0).Err()
if err != nil {
panic(err)
}
2014-05-11 11:42:40 +04:00
2015-06-16 10:31:21 +03:00
n, err = IncrByXX.Run(client, []string{"xx_counter"}, []string{"2"}).Result()
fmt.Println(n, err)
2014-05-11 11:42:40 +04:00
2015-06-16 10:31:21 +03:00
// Output: <nil> redis: nil
// 42 <nil>
}
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
}