You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.4 KiB

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