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-06 18:14:29 +04:00
|
|
|
Supports:
|
|
|
|
|
|
|
|
- Redis 2.6 commands except QUIT command.
|
|
|
|
- Pub/sub.
|
|
|
|
- Transactions.
|
|
|
|
- Pipelining.
|
|
|
|
- Connection pool.
|
|
|
|
- TLS connections.
|
|
|
|
- Thread safety.
|
|
|
|
|
2012-08-06 18:17:18 +04:00
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
|
2012-08-09 14:12:41 +04:00
|
|
|
Install:
|
2012-08-06 18:17:18 +04:00
|
|
|
|
|
|
|
go get github.com/vmihailenco/redis
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
Contributing
|
|
|
|
------------
|
|
|
|
|
|
|
|
Configure Redis to allow maximum 10 clients:
|
|
|
|
|
|
|
|
maxclients 10
|
|
|
|
|
2012-08-06 18:17:18 +04:00
|
|
|
Run tests:
|
|
|
|
|
|
|
|
go test -gocheck.v
|
|
|
|
|
|
|
|
Run benchmarks:
|
|
|
|
|
|
|
|
go test -gocheck.b
|
|
|
|
|
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
|
|
|
|
2012-08-11 19:04:02 +04:00
|
|
|
address := "localhost:6379"
|
2012-08-06 12:33:49 +04:00
|
|
|
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-11 18:47:33 +04:00
|
|
|
openConn := func() (io.ReadWriteCloser, 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-11 18:47:33 +04:00
|
|
|
closeConn := func(conn io.ReadWriteCloser) error {
|
2012-07-25 17:00:50 +04:00
|
|
|
fmt.Println("Disconnecting...")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:49 +04:00
|
|
|
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)
|
|
|
|
|
2012-08-06 12:33:49 +04:00
|
|
|
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")
|
2012-08-06 18:14:29 +04:00
|
|
|
if get.Err() != nil && get.Err() != redis.Nil {
|
|
|
|
panic(get.Err())
|
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
|
|
|
|
----------
|
|
|
|
|
2012-08-09 18:06:26 +04:00
|
|
|
Client has ability to run commands in batches:
|
2012-07-29 13:42:00 +04:00
|
|
|
|
2012-08-09 18:06:26 +04:00
|
|
|
pipeline, err := redisClient.PipelineClient()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer pipeline.Close()
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2012-08-09 18:06:26 +04:00
|
|
|
setReq := pipeline.Set("foo1", "bar1") // queue command SET
|
|
|
|
getReq := pipeline.Get("foo2") // queue command GET
|
2012-07-29 13:42:00 +04:00
|
|
|
|
2012-08-09 18:06:26 +04:00
|
|
|
reqs, err := pipeline.RunQueued() // run queued commands
|
2012-07-29 13:42:00 +04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2012-08-06 18:14:29 +04:00
|
|
|
for _, req := range reqs {
|
|
|
|
// ...
|
|
|
|
}
|
2012-07-29 13:42:00 +04:00
|
|
|
|
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-06 18:14:29 +04:00
|
|
|
Example:
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-09 18:06:26 +04:00
|
|
|
multiClient, err := redisClient.MultiClient()
|
2012-07-25 17:00:50 +04:00
|
|
|
if err != nil {
|
2012-08-06 18:14:29 +04:00
|
|
|
panic(err)
|
|
|
|
}
|
2012-08-09 18:06:26 +04:00
|
|
|
defer multiClient.Close()
|
|
|
|
|
2012-08-11 18:50:55 +04:00
|
|
|
watch := multiClient.Watch("foo")
|
2012-08-09 18:06:26 +04:00
|
|
|
if watch.Err() != nil {
|
|
|
|
panic(watch.Err())
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-08-11 18:50:55 +04:00
|
|
|
get := multiClient.Get("foo")
|
|
|
|
if get.Err() != nil {
|
|
|
|
panic(get.Err())
|
|
|
|
}
|
|
|
|
|
2012-08-09 18:06:26 +04:00
|
|
|
// Start transaction.
|
|
|
|
multiClient.Multi()
|
2012-08-06 18:14:29 +04:00
|
|
|
|
2012-08-11 18:50:55 +04:00
|
|
|
set := multiClient.Set("foo", get.Val() + "1")
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-11 18:50:55 +04:00
|
|
|
// Commit transaction.
|
2012-08-09 18:06:26 +04:00
|
|
|
reqs, err := multiClient.Exec()
|
|
|
|
if err == redis.Nil {
|
|
|
|
// Repeat transaction.
|
|
|
|
} else if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, req := range reqs {
|
|
|
|
// ...
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
2012-08-09 18:06:26 +04:00
|
|
|
|
|
|
|
ok := set.Val()
|
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-08-09 18:06:26 +04:00
|
|
|
defer pubsub.Close()
|
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
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
|
2012-08-06 12:33:49 +04:00
|
|
|
redisClient.ConnPool.(*redis.MultiConnPool).MaxCap = 1
|