2012-11-11 18:37:50 +04:00
|
|
|
Redis client for Golang
|
|
|
|
=======================
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-06 18:14:29 +04:00
|
|
|
Supports:
|
|
|
|
|
2012-08-25 16:40:49 +04:00
|
|
|
- Redis 2.6 commands except QUIT, MONITOR, SLOWLOG and SYNC.
|
2012-08-06 18:14:29 +04:00
|
|
|
- Pub/sub.
|
|
|
|
- Transactions.
|
|
|
|
- Pipelining.
|
|
|
|
- Connection pool.
|
|
|
|
- TLS connections.
|
|
|
|
- Thread safety.
|
|
|
|
|
2012-08-25 17:29:50 +04:00
|
|
|
API docs: http://go.pkgdoc.org/github.com/vmihailenco/redis
|
|
|
|
|
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-25 17:27:05 +04:00
|
|
|
Getting started
|
|
|
|
---------------
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
Let's start with connecting to Redis:
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
password := "" // no password set
|
|
|
|
db := -1 // use default DB
|
|
|
|
client := redis.NewTCPClient("localhost:6379", password, db)
|
|
|
|
defer client.Close()
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
Then we can start sending commands:
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2013-01-19 21:05:28 +04:00
|
|
|
set := client.Set("foo", "bar");
|
|
|
|
if err := set.Err(); err != nil {
|
|
|
|
panic(err)
|
2013-01-19 20:31:23 +04:00
|
|
|
}
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
get := client.Get("foo")
|
2013-01-19 21:05:28 +04:00
|
|
|
if err := get.Err(); err != nil {
|
|
|
|
panic(err)
|
2013-01-19 20:31:23 +04:00
|
|
|
}
|
2012-08-25 17:27:05 +04:00
|
|
|
fmt.Println(get.Val())
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
We can also pipeline two commands together:
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2012-08-25 17:27:05 +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")
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
2012-08-25 17:27:05 +04:00
|
|
|
if err != nil { panic(err) }
|
|
|
|
if err := set.Err(); err != nil { panic(err) }
|
|
|
|
if err := get.Err(); err != nil { panic(err) }
|
|
|
|
fmt.Println(get.Val())
|
|
|
|
fmt.Println(reqs[0] == set)
|
|
|
|
fmt.Println(reqs[1] == get)
|
2012-07-29 13:42:00 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
or:
|
2012-07-29 13:42:00 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
pipeline, err := client.PipelineClient()
|
2012-08-09 18:06:26 +04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer pipeline.Close()
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
set := pipeline.Set("key1", "hello1")
|
2012-08-25 22:38:36 +04:00
|
|
|
get := pipleine.Get("key2")
|
2012-07-29 13:42:00 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
reqs, err := pipeline.RunQueued()
|
|
|
|
if err != nil { panic(err) }
|
2012-07-29 13:42:00 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
if err := set.Err(); err != nil { panic(err) }
|
|
|
|
if err := get.Err(); err != nil { panic(err) }
|
|
|
|
fmt.Println(get.Val())
|
|
|
|
fmt.Println(reqs[0] == set)
|
|
|
|
fmt.Println(reqs[1] == get)
|
2012-07-29 13:42:00 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
We can also send several commands in transaction:
|
2012-07-29 13:42:00 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
func incrKeyInTransaction(multi *redis.MultiClient) ([]redis.Req, error) {
|
|
|
|
get := multi.Get("key")
|
2012-08-25 16:40:49 +04:00
|
|
|
if err := get.Err(); err != nil {
|
|
|
|
panic(err)
|
2012-08-13 15:45:32 +04:00
|
|
|
}
|
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
val, err := strconv.ParseInt(get.Val(), 10, 64)
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
|
|
|
|
reqs, err = multi.Exec(func() {
|
|
|
|
multi.Set("key", val + 1)
|
2012-08-13 15:45:32 +04:00
|
|
|
})
|
2012-08-25 17:27:05 +04:00
|
|
|
// Transaction failed. Repeat.
|
2012-08-13 15:45:32 +04:00
|
|
|
if err == redis.Nil {
|
2012-08-25 17:27:05 +04:00
|
|
|
return incrKeyInTransaction(multi)
|
2012-08-13 15:45:32 +04:00
|
|
|
}
|
|
|
|
return reqs, err
|
|
|
|
}
|
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
multi, err := client.MultiClient()
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
defer multi.Close()
|
2012-08-09 18:06:26 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
watch := multi.Watch("key")
|
|
|
|
if err := watch.Err(); err != nil { panic(err) }
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
reqs, err := incrKeyInTransaction(multi)
|
|
|
|
if err != nil { panic(err) }
|
2012-08-09 18:06:26 +04:00
|
|
|
for _, req := range reqs {
|
|
|
|
// ...
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
2012-08-09 18:06:26 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
To subscribe to the channel:
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
pubsub, err := client.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")
|
2012-08-25 17:27:05 +04:00
|
|
|
if err != nil { panic(err) }
|
2012-07-25 17:00:50 +04:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for msg := range ch {
|
2012-08-25 17:27:05 +04:00
|
|
|
if err := msg.Err; err != nil { panic(err) }
|
2012-08-06 16:09:48 +04:00
|
|
|
message := msg.Message
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
You can also write custom commands:
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
func Get(client *redis.Client, key string) *redis.StringReq {
|
|
|
|
req := redis.NewStringReq("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-25 17:27:05 +04:00
|
|
|
if err := get.Err(); err != nil && err != redis.Nil { panic(err) }
|
2012-07-29 13:54:09 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
Client uses connection pool to send commands. You can change maximum number of connections with:
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2012-08-25 17:27:05 +04:00
|
|
|
client.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
|
2012-08-20 14:42:33 +04:00
|
|
|
client.ZInterStore("out", redis.ZStore{Weights: []int64{2, 3}}, "zset1", "zset2")
|
|
|
|
|
|
|
|
EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello"
|
|
|
|
client.Eval("return {KEYS[1],ARGV[1]}", []string{"key"}, []string{"hello"})
|
2012-08-17 22:36:48 +04:00
|
|
|
|
|
|
|
Contributing
|
|
|
|
------------
|
|
|
|
|
|
|
|
Configure Redis to allow maximum 10 clients:
|
|
|
|
|
|
|
|
maxclients 10
|
|
|
|
|
|
|
|
Run tests:
|
|
|
|
|
|
|
|
go test -gocheck.v
|
|
|
|
|
|
|
|
Run benchmarks:
|
|
|
|
|
|
|
|
go test -gocheck.b
|