redis/README.md

261 lines
4.9 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-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-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"
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
}
initConn := func(client *redis.Client) error {
2012-08-17 22:36:48 +04:00
auth := client.Auth("key")
2012-08-06 16:09:48 +04:00
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-17 22:36:48 +04:00
set := redisClient.Set("key", "hello")
2012-08-06 16:09:48 +04:00
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-17 22:36:48 +04:00
get := redisClient.Get("key")
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-17 22:36:48 +04:00
reqs, err := redisClient.Pipelined(func(c *redis.PipelineClient) {
c.Set("key1", "hello1") // queue command SET
c.Set("key2", "hello2") // queue command SET
})
if err != nil {
panic(err)
}
for _, req := range reqs {
// ...
}
Or:
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-17 22:36:48 +04:00
setReq := pipeline.Set("key1", "hello1") // queue command SET
getReq := pipeline.Get("key2") // 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-13 15:45:32 +04:00
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
2012-08-17 22:36:48 +04:00
get := multiClient.Get("key")
2012-08-13 15:45:32 +04:00
if get.Err() != nil {
panic(get.Err())
}
reqs, err = multiClient.Exec(func() {
2012-08-17 22:36:48 +04:00
multi.Set("key", get.Val() + "1")
2012-08-13 15:45:32 +04:00
})
if err == redis.Nil {
return transaction()
}
return reqs, err
}
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-17 22:36:48 +04:00
watch := multiClient.Watch("key")
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-13 15:45:32 +04:00
reqs, err := transaction(multiClient)
if err != nil {
2012-08-09 18:06:26 +04:00
panic(err)
}
for _, req := range reqs {
// ...
2012-07-25 17:00:50 +04:00
}
2012-08-09 18:06:26 +04:00
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-17 22:36:48 +04:00
redisClient.Incr("key")
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-17 22:36:48 +04:00
get := Get(redisClient, "key")
2012-08-06 16:09:48 +04:00
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
2012-08-17 22:36:48 +04:00
Look and feel
-------------
Some corner cases:
SORT list LIMIT 0 2 ASC
client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC"})
ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2
client.ZRangeByScoreWithScores("zset", "-inf", "+inf", 0, 2)
ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM
client.ZInterStore("out", 2, redis.ZStore{Weights: []int64{2, 3}}, "zset1", "zset2")
Contributing
------------
Configure Redis to allow maximum 10 clients:
maxclients 10
Run tests:
go test -gocheck.v
Run benchmarks:
go test -gocheck.b