redis/README.md

198 lines
3.7 KiB
Markdown
Raw Normal View History

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
address := ":6379"
password := "secret"
db := 0
redisClient := redis.NewTCPClient(address, password, db)
2012-08-05 16:09:43 +04:00
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
}
initConn := func(client *redis.Client) error {
2012-08-06 16:09:48 +04:00
auth := client.Auth("foo")
if auth.Err() != nil {
return auth.Err()
2012-08-05 16:09:43 +04:00
}
2012-08-06 16:09:48 +04:00
ping := client.Ping()
if ping.Err() != nil {
return ping.Err()
2012-08-05 16:09:43 +04:00
}
return nil
}
redisClient := redis.NewClient(openConn, closeConn, initConn)
Both `closeConn` and `initConn` functions can be `nil`.
2012-08-05 16:09:43 +04:00
Running commands
----------------
2012-07-25 17:00:50 +04:00
2012-08-06 16:09:48 +04:00
set := redisClient.Set("foo", "bar")
if set.Err() != nil {
panic(set.Err())
2012-07-25 17:00:50 +04:00
}
2012-08-06 16:09:48 +04:00
ok := set.Val()
2012-07-25 17:00:50 +04:00
2012-08-06 16:09:48 +04:00
get := redisClient.Get("foo")
if get.Err() != nil {
if get.Err() != redis.Nil {
panic(get.Err())
2012-07-27 13:51:21 +04:00
}
2012-07-25 17:00:50 +04:00
}
2012-08-06 16:09:48 +04:00
val := get.Val()
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)
}
2012-08-06 16:09:48 +04:00
if setReq.Err() != nil {
panic(setReq.Err())
2012-07-29 13:42:00 +04:00
}
2012-08-06 16:09:48 +04:00
if getReq.Err() != nil && getReq.Err() != redis.Nil {
panic(getReq.Err())
2012-07-29 13:42:00 +04:00
}
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-08-06 16:09:48 +04:00
get1 := multiClient.Get("foo1")
get2 := multiClient.Get("foo2")
2012-07-25 17:00:50 +04:00
_, err := multiClient.Exec()
if err != nil {
panic(err)
}
2012-08-06 16:09:48 +04:00
if get1.Err() != nil && get1.Err() != redis.Nil {
panic(get1.Err())
2012-07-25 17:00:50 +04:00
}
2012-08-06 16:09:48 +04:00
val1 := get1.Val()
2012-07-25 17:00:50 +04:00
2012-08-06 16:09:48 +04:00
if get2.Err() != nil && get2.Err() != redis.Nil {
panic(get2.Err())
2012-07-25 17:00:50 +04:00
}
2012-08-06 16:09:48 +04:00
val2 := get2.Val()
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 {
2012-08-06 16:09:48 +04:00
if req.Err() != nil && req.Err() != redis.Nil {
panic(req.Err())
2012-07-25 17:00:50 +04:00
}
}
Pub/sub
-------
Publish:
2012-08-06 16:09:48 +04:00
pub := redisClient.Publish("mychannel", "hello")
if pub.Err() != nil {
panic(pub.Err())
2012-07-25 17:00:50 +04:00
}
Subscribe:
2012-08-06 16:09:48 +04:00
pubsub, err := redisClient.PubSubClient()
if err != nil {
panic(err)
}
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)
}
2012-08-06 16:09:48 +04:00
message := msg.Message
2012-07-25 17:00:50 +04:00
}
}
Thread safety
-------------
2012-08-06 16:09:48 +04:00
Commands are thread safe. Following code is correct:
2012-08-05 16:09:43 +04:00
for i := 0; i < 1000; i++ {
go func() {
2012-08-06 16:09:48 +04:00
redisClient.Incr("foo")
2012-08-05 16:09:43 +04:00
}()
}
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-08-06 16:09:48 +04:00
client.Process(req)
2012-07-27 15:53:23 +04:00
return req
}
2012-08-06 16:09:48 +04:00
get := Get(redisClient, "foo")
if get.Err() != nil && get.Err() != redis.Nil {
panic(get.Err())
2012-07-29 13:42:00 +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.(*redis.MultiConnPool).MaxCap = 1