2012-07-25 17:00:50 +04:00
|
|
|
Readme
|
|
|
|
======
|
|
|
|
|
2012-07-27 13:51:21 +04:00
|
|
|
Redis client for Golang.
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
Getting Client instance
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
Example 1:
|
|
|
|
|
|
|
|
import "github.com/vmihailenco/redis"
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
|
|
|
|
redisClient := redis.NewTCPClient(":6379", "", 0)
|
|
|
|
|
|
|
|
Example 2:
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-07-27 13:51:21 +04:00
|
|
|
import "github.com/vmihailenco/redis"
|
|
|
|
|
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
openConn := func() (io.ReadWriter, error) {
|
2012-07-25 17:00:50 +04:00
|
|
|
fmt.Println("Connecting...")
|
2012-08-05 16:09:43 +04:00
|
|
|
return net.Dial("tcp", ":6379")
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
closeConn := func(conn io.ReadWriter) error {
|
2012-07-25 17:00:50 +04:00
|
|
|
fmt.Println("Disconnecting...")
|
|
|
|
conn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
initConn := func(client *Client) error {
|
|
|
|
_, err := client.Auth("foo").Reply()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.Ping().Reply()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
redisClient := redis.NewClient(openConn, closeConn, initConn)
|
|
|
|
|
|
|
|
`closeConn` and `initConn` functions can be `nil`.
|
|
|
|
|
|
|
|
Running commands
|
|
|
|
----------------
|
2012-07-25 17:00:50 +04:00
|
|
|
|
|
|
|
_, err := redisClient.Set("foo", "bar").Reply()
|
|
|
|
if err != nil {
|
2012-07-27 13:51:21 +04:00
|
|
|
panic(err)
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
value, err := redisClient.Get("foo").Reply()
|
|
|
|
if err != nil {
|
2012-07-27 13:51:21 +04:00
|
|
|
if err != redis.Nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-07-29 13:42:00 +04:00
|
|
|
Pipelining
|
|
|
|
----------
|
|
|
|
|
|
|
|
Client has ability to run several commands with one read/write:
|
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
multiClient := redisClient.Multi()
|
|
|
|
|
|
|
|
setReq := multiClient.Set("foo1", "bar1") // queue command SET
|
|
|
|
getReq := multiClient.Get("foo2") // queue command GET
|
2012-07-29 13:42:00 +04:00
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
reqs, err := multiClient.RunQueued() // run queued commands
|
2012-07-29 13:42:00 +04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := setReq.Reply()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := getReq.Reply()
|
|
|
|
if err != nil {
|
|
|
|
if err != redis.Nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-25 17:00:50 +04:00
|
|
|
Multi/Exec
|
|
|
|
----------
|
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
Example 1:
|
2012-07-25 17:00:50 +04:00
|
|
|
|
|
|
|
multiClient := redisClient.Multi()
|
2012-07-27 13:51:21 +04:00
|
|
|
|
2012-07-25 17:00:50 +04:00
|
|
|
futureGet1 := multiClient.Get("foo1")
|
|
|
|
futureGet2 := multiClient.Get("foo2")
|
|
|
|
_, err := multiClient.Exec()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
value1, err := futureGet1.Reply()
|
|
|
|
if err != nil {
|
2012-07-27 13:51:21 +04:00
|
|
|
if err != redis.Nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
value2, err := futureGet2.Reply()
|
|
|
|
if err != nil {
|
2012-07-27 13:51:21 +04:00
|
|
|
if err != redis.Nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
Example 2:
|
2012-07-25 17:00:50 +04:00
|
|
|
|
|
|
|
multiClient := redisClient.Multi()
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2012-07-25 17:00:50 +04:00
|
|
|
multiClient.Get("foo1")
|
|
|
|
multiClient.Get("foo2")
|
|
|
|
reqs, err := multiClient.Exec()
|
|
|
|
if err != nil {
|
2012-07-27 13:51:21 +04:00
|
|
|
panic(err)
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for req := range reqs {
|
|
|
|
value, err := req.Reply()
|
|
|
|
if err != nil {
|
2012-07-27 13:51:21 +04:00
|
|
|
if err != redis.Nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Pub/sub
|
|
|
|
-------
|
|
|
|
|
|
|
|
Publish:
|
|
|
|
|
|
|
|
_, err := redisClient.Publish("mychannel", "hello").Reply()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
Subscribe:
|
|
|
|
|
|
|
|
pubsub := redisClient.PubSubClient()
|
2012-07-27 13:51:21 +04:00
|
|
|
|
2012-07-25 17:00:50 +04:00
|
|
|
ch, err := pubsub.Subscribe("mychannel")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for msg := range ch {
|
|
|
|
if msg.Err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println(msg.Message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread safety
|
|
|
|
-------------
|
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
redis.Client methods are thread safe. Following code is correct:
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
go func() {
|
|
|
|
redisClient.Incr("foo").Reply()
|
|
|
|
}()
|
|
|
|
}
|
2012-07-27 15:53:23 +04:00
|
|
|
|
|
|
|
Custom commands
|
|
|
|
---------------
|
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
Example:
|
2012-07-27 15:53:23 +04:00
|
|
|
|
|
|
|
func Get(client *redis.Client, key string) *redis.BulkReq {
|
|
|
|
req := redis.NewBulkReq("GET", key)
|
2012-07-29 13:42:00 +04:00
|
|
|
client.Queue(req)
|
2012-07-27 15:53:23 +04:00
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2012-07-27 18:23:18 +04:00
|
|
|
value, err := Get(redisClient, "foo").Reply()
|
2012-07-29 13:42:00 +04:00
|
|
|
if err != nil {
|
|
|
|
if err != redis.Nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 13:54:09 +04:00
|
|
|
Connection pool
|
|
|
|
---------------
|
|
|
|
|
2012-08-05 16:09:43 +04:00
|
|
|
Client uses connection pool with default capacity of 10 connections. To change pool capacity:
|
|
|
|
|
|
|
|
redisClient.ConnPool.MaxCap = 1
|