| 1 |
# Redis client for Golang [](https://travis-ci.org/go-redis/redis) |
| 2 |
|
| 3 |
Supports: |
| 4 |
|
| 5 |
- Redis 3 commands except QUIT, MONITOR, SLOWLOG and SYNC. |
| 6 |
- [Pub/Sub](https://godoc.org/gopkg.in/redis.v5#PubSub). |
| 7 |
- [Transactions](https://godoc.org/gopkg.in/redis.v5#Multi). |
| 8 |
- [Pipeline](https://godoc.org/gopkg.in/redis.v5#example-Client-Pipeline) and [TxPipeline](https://godoc.org/gopkg.in/redis.v5#example-Client-TxPipeline). |
| 9 |
- [Scripting](https://godoc.org/gopkg.in/redis.v5#Script). |
| 10 |
- [Timeouts](https://godoc.org/gopkg.in/redis.v5#Options). |
| 11 |
- [Redis Sentinel](https://godoc.org/gopkg.in/redis.v5#NewFailoverClient). |
| 12 |
- [Redis Cluster](https://godoc.org/gopkg.in/redis.v5#NewClusterClient). |
| 13 |
- [Ring](https://godoc.org/gopkg.in/redis.v5#NewRing). |
| 14 |
- [Instrumentation](https://godoc.org/gopkg.in/redis.v5#ex-package--Instrumentation). |
| 15 |
- [Cache friendly](https://github.com/go-redis/cache). |
| 16 |
- [Rate limiting](https://github.com/go-redis/rate). |
| 17 |
- [Distributed Locks](https://github.com/bsm/redis-lock). |
| 18 |
|
| 19 |
API docs: https://godoc.org/gopkg.in/redis.v5. |
| 20 |
Examples: https://godoc.org/gopkg.in/redis.v5#pkg-examples. |
| 21 |
|
| 22 |
## Installation |
| 23 |
|
| 24 |
Install: |
| 25 |
|
| 26 |
```shell |
| 27 |
go get gopkg.in/redis.v5 |
| 28 |
``` |
| 29 |
|
| 30 |
Import: |
| 31 |
|
| 32 |
```go |
| 33 |
import "gopkg.in/redis.v5" |
| 34 |
``` |
| 35 |
|
| 36 |
## Quickstart |
| 37 |
|
| 38 |
```go |
| 39 |
func ExampleNewClient() { |
| 40 |
client := redis.NewClient(&redis.Options{ |
| 41 |
Addr: "localhost:6379", |
| 42 |
Password: "", // no password set |
| 43 |
DB: 0, // use default DB |
| 44 |
}) |
| 45 |
|
| 46 |
pong, err := client.Ping().Result() |
| 47 |
fmt.Println(pong, err) |
| 48 |
// Output: PONG <nil> |
| 49 |
} |
| 50 |
|
| 51 |
func ExampleClient() { |
| 52 |
err := client.Set("key", "value", 0).Err() |
| 53 |
if err != nil { |
| 54 |
panic(err) |
| 55 |
} |
| 56 |
|
| 57 |
val, err := client.Get("key").Result() |
| 58 |
if err != nil { |
| 59 |
panic(err) |
| 60 |
} |
| 61 |
fmt.Println("key", val) |
| 62 |
|
| 63 |
val2, err := client.Get("key2").Result() |
| 64 |
if err == redis.Nil { |
| 65 |
fmt.Println("key2 does not exists") |
| 66 |
} else if err != nil { |
| 67 |
panic(err) |
| 68 |
} else { |
| 69 |
fmt.Println("key2", val2) |
| 70 |
} |
| 71 |
// Output: key value |
| 72 |
// key2 does not exists |
| 73 |
} |
| 74 |
``` |
| 75 |
|
| 76 |
## Howto |
| 77 |
|
| 78 |
Please go through [examples](https://godoc.org/gopkg.in/redis.v5#pkg-examples) to get an idea how to use this package. |
| 79 |
|
| 80 |
## Look and feel |
| 81 |
|
| 82 |
Some corner cases: |
| 83 |
|
| 84 |
SET key value EX 10 NX |
| 85 |
set, err := client.SetNX("key", "value", 10*time.Second).Result() |
| 86 |
|
| 87 |
SORT list LIMIT 0 2 ASC |
| 88 |
vals, err := client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC"}).Result() |
| 89 |
|
| 90 |
ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2 |
| 91 |
vals, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{ |
| 92 |
Min: "-inf", |
| 93 |
Max: "+inf", |
| 94 |
Offset: 0, |
| 95 |
Count: 2, |
| 96 |
}).Result() |
| 97 |
|
| 98 |
ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM |
| 99 |
vals, err := client.ZInterStore("out", redis.ZStore{Weights: []int64{2, 3}}, "zset1", "zset2").Result() |
| 100 |
|
| 101 |
EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello" |
| 102 |
vals, err := client.Eval("return {KEYS[1],ARGV[1]}", []string{"key"}, []string{"hello"}).Result() |
| 103 |
|
| 104 |
## Benchmark |
| 105 |
|
| 106 |
go-redis vs redigo: |
| 107 |
|
| 108 |
``` |
| 109 |
BenchmarkSetGoRedis10Conns64Bytes-4 200000 7621 ns/op 210 B/op 6 allocs/op |
| 110 |
BenchmarkSetGoRedis100Conns64Bytes-4 200000 7554 ns/op 210 B/op 6 allocs/op |
| 111 |
BenchmarkSetGoRedis10Conns1KB-4 200000 7697 ns/op 210 B/op 6 allocs/op |
| 112 |
BenchmarkSetGoRedis100Conns1KB-4 200000 7688 ns/op 210 B/op 6 allocs/op |
| 113 |
BenchmarkSetGoRedis10Conns10KB-4 200000 9214 ns/op 210 B/op 6 allocs/op |
| 114 |
BenchmarkSetGoRedis100Conns10KB-4 200000 9181 ns/op 210 B/op 6 allocs/op |
| 115 |
BenchmarkSetGoRedis10Conns1MB-4 2000 583242 ns/op 2337 B/op 6 allocs/op |
| 116 |
BenchmarkSetGoRedis100Conns1MB-4 2000 583089 ns/op 2338 B/op 6 allocs/op |
| 117 |
BenchmarkSetRedigo10Conns64Bytes-4 200000 7576 ns/op 208 B/op 7 allocs/op |
| 118 |
BenchmarkSetRedigo100Conns64Bytes-4 200000 7782 ns/op 208 B/op 7 allocs/op |
| 119 |
BenchmarkSetRedigo10Conns1KB-4 200000 7958 ns/op 208 B/op 7 allocs/op |
| 120 |
BenchmarkSetRedigo100Conns1KB-4 200000 7725 ns/op 208 B/op 7 allocs/op |
| 121 |
BenchmarkSetRedigo10Conns10KB-4 100000 18442 ns/op 208 B/op 7 allocs/op |
| 122 |
BenchmarkSetRedigo100Conns10KB-4 100000 18818 ns/op 208 B/op 7 allocs/op |
| 123 |
BenchmarkSetRedigo10Conns1MB-4 2000 668829 ns/op 226 B/op 7 allocs/op |
| 124 |
BenchmarkSetRedigo100Conns1MB-4 2000 679542 ns/op 226 B/op 7 allocs/op |
| 125 |
``` |
| 126 |
|
| 127 |
Redis Cluster: |
| 128 |
|
| 129 |
``` |
| 130 |
BenchmarkRedisPing-4 200000 6983 ns/op 116 B/op 4 allocs/op |
| 131 |
BenchmarkRedisClusterPing-4 100000 11535 ns/op 117 B/op 4 allocs/op |
| 132 |
``` |
| 133 |
|
| 134 |
## Shameless plug |
| 135 |
|
| 136 |
Check my [PostgreSQL client for Go](https://github.com/go-pg/pg). |