redis/README.md

206 lines
4.1 KiB
Markdown
Raw Normal View History

2013-10-15 14:51:40 +04:00
New version
===========
Make sure to check new version of the client that got better API and timeout support: https://github.com/vmihailenco/redis/tree/master/v2
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.
2013-09-25 21:54:59 +04:00
API docs: http://godoc.org/github.com/vmihailenco/redis
2012-08-25 17:29:50 +04:00
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
Let's start with connecting to Redis using TCP:
2012-07-25 17:00:50 +04:00
```go
password := "" // no password set
db := int64(-1) // use default DB
client := redis.NewTCPClient("localhost:6379", password, db)
defer client.Close()
ping := client.Ping()
fmt.Println(ping.Err(), ping.Val())
// Output: <nil> PONG
```
or using Unix socket:
```go
client := redis.NewUnixClient("/tmp/redis.sock", "", -1)
defer client.Close()
ping := client.Ping()
fmt.Println(ping.Err(), ping.Val())
// Output: <nil> PONG
```
2012-08-25 17:27:05 +04:00
Then we can start sending commands:
2012-08-05 16:09:43 +04:00
```go
set := client.Set("foo", "bar")
fmt.Println(set.Err(), set.Val())
2012-08-05 16:09:43 +04:00
get := client.Get("foo")
fmt.Println(get.Err(), get.Val())
// Output: <nil> OK
// <nil> bar
```
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
```go
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")
})
fmt.Println(err, reqs)
fmt.Println(set)
fmt.Println(get)
// Output: <nil> [SET key1 hello1: OK GET key2: (nil)]
// SET key1 hello1: OK
// GET key2: (nil)
```
2012-08-25 17:27:05 +04:00
or:
2012-07-29 13:42:00 +04:00
```go
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")
})
fmt.Println(err, reqs)
fmt.Println(set)
fmt.Println(get)
// Output: <nil> [SET key1 hello1 GET key2]
// SET key1 hello1
// GET key2
```
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
```go
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
get := multi.Get("key")
if err := get.Err(); err != nil && err != redis.Nil {
return nil, err
2012-08-13 15:45:32 +04:00
}
val, _ := strconv.ParseInt(get.Val(), 10, 64)
2012-08-09 18:06:26 +04:00
reqs, err := multi.Exec(func() {
multi.Set("key", strconv.FormatInt(val+1, 10))
})
// Transaction failed. Repeat.
if err == redis.Nil {
return transaction(multi)
}
return reqs, err
}
multi, err := client.MultiClient()
_ = err
defer multi.Close()
2012-07-25 17:00:50 +04:00
watch := multi.Watch("key")
_ = watch.Err()
reqs, err := transaction(multi)
fmt.Println(err, reqs)
// Output: <nil> [SET key 1: OK]
```
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
```go
pubsub, err := client.PubSubClient()
defer pubsub.Close()
2012-07-27 13:51:21 +04:00
ch, err := pubsub.Subscribe("mychannel")
_ = err
2012-07-25 17:00:50 +04:00
subscribeMsg := <-ch
fmt.Println(subscribeMsg.Err, subscribeMsg.Name)
pub := client.Publish("mychannel", "hello")
_ = pub.Err()
msg := <-ch
fmt.Println(msg.Err, msg.Message)
// Output: <nil> subscribe
// <nil> hello
```
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
```go
func Get(client *redis.Client, key string) *redis.StringReq {
req := redis.NewStringReq("GET", key)
client.Process(req)
return req
}
2012-07-27 15:53:23 +04:00
get := Get(client, "key_does_not_exist")
fmt.Println(get.Err(), get.Val())
// Output: (nil)
```
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
```go
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