2012-08-25 17:27:05 +04:00
|
|
|
/*
|
|
|
|
Package github.com/vmihailenco/redis implements a Redis client.
|
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
Let's start with connecting to Redis using TCP:
|
2012-08-25 17:27:05 +04:00
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
password := "" // no password set
|
|
|
|
db := int64(-1) // use default DB
|
2012-08-25 17:27:05 +04:00
|
|
|
client := redis.NewTCPClient("localhost:6379", password, db)
|
|
|
|
defer client.Close()
|
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
ping := client.Ping()
|
|
|
|
fmt.Println(ping.Err(), ping.Val())
|
|
|
|
// Output: <nil> PONG
|
|
|
|
|
|
|
|
or using Unix socket:
|
|
|
|
|
|
|
|
client := redis.NewUnixClient("/tmp/redis.sock", "", -1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
ping := client.Ping()
|
|
|
|
fmt.Println(ping.Err(), ping.Val())
|
|
|
|
// Output: <nil> PONG
|
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
Then we can start sending commands:
|
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
set := client.Set("foo", "bar")
|
|
|
|
fmt.Println(set.Err(), set.Val())
|
2012-08-25 17:27:05 +04:00
|
|
|
|
|
|
|
get := client.Get("foo")
|
2013-02-17 22:43:14 +04:00
|
|
|
fmt.Println(get.Err(), get.Val())
|
|
|
|
|
|
|
|
// Output: <nil> OK
|
|
|
|
// <nil> bar
|
2012-08-25 17:27:05 +04:00
|
|
|
|
|
|
|
We can also pipeline two commands together:
|
|
|
|
|
|
|
|
var set *redis.StatusReq
|
|
|
|
var get *redis.StringReq
|
2013-02-17 22:43:14 +04:00
|
|
|
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
2012-08-25 17:27:05 +04:00
|
|
|
set = c.Set("key1", "hello1")
|
|
|
|
get = c.Get("key2")
|
2013-02-17 22:43:14 +04:00
|
|
|
})
|
|
|
|
fmt.Println(err, reqs)
|
|
|
|
fmt.Println(set)
|
|
|
|
fmt.Println(get)
|
|
|
|
// Output: <nil> [SET key1 hello1: OK GET key2: (nil)]
|
|
|
|
// SET key1 hello1: OK
|
|
|
|
// GET key2: (nil)
|
2012-08-25 17:27:05 +04:00
|
|
|
|
|
|
|
or:
|
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
var set *redis.StatusReq
|
|
|
|
var get *redis.StringReq
|
|
|
|
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
|
|
|
|
set = c.Set("key1", "hello1")
|
|
|
|
get = c.Get("key2")
|
|
|
|
})
|
|
|
|
fmt.Println(err, reqs)
|
|
|
|
fmt.Println(set)
|
|
|
|
fmt.Println(get)
|
|
|
|
// Output: <nil> [SET key1 hello1 GET key2]
|
|
|
|
// SET key1 hello1
|
|
|
|
// GET key2
|
2012-08-25 17:27:05 +04:00
|
|
|
|
|
|
|
We can also send several commands in transaction:
|
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
|
2012-08-25 17:27:05 +04:00
|
|
|
get := multi.Get("key")
|
2013-02-17 22:43:14 +04:00
|
|
|
if err := get.Err(); err != nil && err != redis.Nil {
|
|
|
|
return nil, err
|
2012-08-25 17:27:05 +04:00
|
|
|
}
|
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
val, _ := strconv.ParseInt(get.Val(), 10, 64)
|
2012-08-25 17:27:05 +04:00
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
reqs, err := multi.Exec(func() {
|
|
|
|
multi.Set("key", strconv.FormatInt(val+1, 10))
|
2012-08-25 17:27:05 +04:00
|
|
|
})
|
|
|
|
// Transaction failed. Repeat.
|
|
|
|
if err == redis.Nil {
|
2013-02-17 22:43:14 +04:00
|
|
|
return transaction(multi)
|
2012-08-25 17:27:05 +04:00
|
|
|
}
|
|
|
|
return reqs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
multi, err := client.MultiClient()
|
2013-02-17 22:43:14 +04:00
|
|
|
_ = err
|
2012-08-25 17:27:05 +04:00
|
|
|
defer multi.Close()
|
|
|
|
|
|
|
|
watch := multi.Watch("key")
|
2013-02-17 22:43:14 +04:00
|
|
|
_ = watch.Err()
|
2012-08-25 17:27:05 +04:00
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
reqs, err := transaction(multi)
|
|
|
|
fmt.Println(err, reqs)
|
|
|
|
|
|
|
|
// Output: <nil> [SET key 1: OK]
|
2012-08-25 17:27:05 +04:00
|
|
|
|
|
|
|
To subscribe to the channel:
|
|
|
|
|
|
|
|
pubsub, err := client.PubSubClient()
|
|
|
|
defer pubsub.Close()
|
|
|
|
|
|
|
|
ch, err := pubsub.Subscribe("mychannel")
|
2013-02-17 22:43:14 +04:00
|
|
|
_ = err
|
2012-08-25 17:27:05 +04:00
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
subscribeMsg := <-ch
|
|
|
|
fmt.Println(subscribeMsg.Err, subscribeMsg.Name)
|
|
|
|
|
|
|
|
pub := client.Publish("mychannel", "hello")
|
|
|
|
_ = pub.Err()
|
|
|
|
|
|
|
|
msg := <-ch
|
|
|
|
fmt.Println(msg.Err, msg.Message)
|
|
|
|
|
|
|
|
// Output: <nil> subscribe
|
|
|
|
// <nil> hello
|
2012-08-25 17:27:05 +04:00
|
|
|
|
|
|
|
You can also write custom commands:
|
|
|
|
|
|
|
|
func Get(client *redis.Client, key string) *redis.StringReq {
|
|
|
|
req := redis.NewStringReq("GET", key)
|
|
|
|
client.Process(req)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2013-02-17 22:43:14 +04:00
|
|
|
get := Get(client, "key_does_not_exist")
|
|
|
|
fmt.Println(get.Err(), get.Val())
|
|
|
|
// Output: (nil)
|
2012-08-25 17:27:05 +04:00
|
|
|
|
|
|
|
Client uses connection pool to send commands. You can change maximum number of connections with:
|
|
|
|
|
|
|
|
client.ConnPool.(*redis.MultiConnPool).MaxCap = 1
|
|
|
|
*/
|
|
|
|
package redis
|