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.

README.md 5.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Redis client for Go
  2. ![build workflow](https://github.com/go-redis/redis/actions/workflows/build.yml/badge.svg)
  3. [![PkgGoDev](https://pkg.go.dev/badge/github.com/go-redis/redis/v8)](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc)
  4. [![Documentation](https://img.shields.io/badge/redis-documentation-informational)](https://redis.uptrace.dev/)
  5. go-redis is brought to you by :star: [**uptrace/uptrace**](https://github.com/uptrace/uptrace).
  6. Uptrace is an open source and blazingly fast **distributed tracing** backend powered by
  7. OpenTelemetry and ClickHouse. Give it a star as well!
  8. ## Resources
  9. - [Discussions](https://github.com/go-redis/redis/discussions)
  10. - [Documentation](https://redis.uptrace.dev)
  11. - [Reference](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc)
  12. - [Examples](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#pkg-examples)
  13. - [RealWorld example app](https://github.com/uptrace/go-treemux-realworld-example-app)
  14. Other projects you may like:
  15. - [Bun](https://bun.uptrace.dev) - fast and simple SQL client for PostgreSQL, MySQL, and SQLite.
  16. - [BunRouter](https://bunrouter.uptrace.dev/) - fast and flexible HTTP router for Go.
  17. ## Ecosystem
  18. - [Redis Mock](https://github.com/go-redis/redismock)
  19. - [Distributed Locks](https://github.com/bsm/redislock)
  20. - [Redis Cache](https://github.com/go-redis/cache)
  21. - [Rate limiting](https://github.com/go-redis/redis_rate)
  22. ## Features
  23. - Redis 3 commands except QUIT, MONITOR, and SYNC.
  24. - Automatic connection pooling with
  25. [circuit breaker](https://en.wikipedia.org/wiki/Circuit_breaker_design_pattern) support.
  26. - [Pub/Sub](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#PubSub).
  27. - [Transactions](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#example-Client-TxPipeline).
  28. - [Pipeline](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#example-Client.Pipeline) and
  29. [TxPipeline](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#example-Client.TxPipeline).
  30. - [Scripting](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#Script).
  31. - [Timeouts](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#Options).
  32. - [Redis Sentinel](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#NewFailoverClient).
  33. - [Redis Cluster](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#NewClusterClient).
  34. - [Cluster of Redis Servers](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#example-NewClusterClient-ManualSetup)
  35. without using cluster mode and Redis Sentinel.
  36. - [Ring](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#NewRing).
  37. - [Instrumentation](https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#example-package-Instrumentation).
  38. ## Installation
  39. go-redis supports 2 last Go versions and requires a Go version with
  40. [modules](https://github.com/golang/go/wiki/Modules) support. So make sure to initialize a Go
  41. module:
  42. ```shell
  43. go mod init github.com/my/repo
  44. ```
  45. And then install go-redis/v8 (note _v8_ in the import; omitting it is a popular mistake):
  46. ```shell
  47. go get github.com/go-redis/redis/v8
  48. ```
  49. ## Quickstart
  50. ```go
  51. import (
  52. "context"
  53. "github.com/go-redis/redis/v8"
  54. "fmt"
  55. )
  56. var ctx = context.Background()
  57. func ExampleClient() {
  58. rdb := redis.NewClient(&redis.Options{
  59. Addr: "localhost:6379",
  60. Password: "", // no password set
  61. DB: 0, // use default DB
  62. })
  63. err := rdb.Set(ctx, "key", "value", 0).Err()
  64. if err != nil {
  65. panic(err)
  66. }
  67. val, err := rdb.Get(ctx, "key").Result()
  68. if err != nil {
  69. panic(err)
  70. }
  71. fmt.Println("key", val)
  72. val2, err := rdb.Get(ctx, "key2").Result()
  73. if err == redis.Nil {
  74. fmt.Println("key2 does not exist")
  75. } else if err != nil {
  76. panic(err)
  77. } else {
  78. fmt.Println("key2", val2)
  79. }
  80. // Output: key value
  81. // key2 does not exist
  82. }
  83. ```
  84. ## Look and feel
  85. Some corner cases:
  86. ```go
  87. // SET key value EX 10 NX
  88. set, err := rdb.SetNX(ctx, "key", "value", 10*time.Second).Result()
  89. // SET key value keepttl NX
  90. set, err := rdb.SetNX(ctx, "key", "value", redis.KeepTTL).Result()
  91. // SORT list LIMIT 0 2 ASC
  92. vals, err := rdb.Sort(ctx, "list", &redis.Sort{Offset: 0, Count: 2, Order: "ASC"}).Result()
  93. // ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2
  94. vals, err := rdb.ZRangeByScoreWithScores(ctx, "zset", &redis.ZRangeBy{
  95. Min: "-inf",
  96. Max: "+inf",
  97. Offset: 0,
  98. Count: 2,
  99. }).Result()
  100. // ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM
  101. vals, err := rdb.ZInterStore(ctx, "out", &redis.ZStore{
  102. Keys: []string{"zset1", "zset2"},
  103. Weights: []int64{2, 3}
  104. }).Result()
  105. // EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello"
  106. vals, err := rdb.Eval(ctx, "return {KEYS[1],ARGV[1]}", []string{"key"}, "hello").Result()
  107. // custom command
  108. res, err := rdb.Do(ctx, "set", "key", "value").Result()
  109. ```
  110. ## Run the test
  111. go-redis will start a redis-server and run the test cases.
  112. The paths of redis-server bin file and redis config file are defined in `main_test.go`:
  113. ```
  114. var (
  115. redisServerBin, _ = filepath.Abs(filepath.Join("testdata", "redis", "src", "redis-server"))
  116. redisServerConf, _ = filepath.Abs(filepath.Join("testdata", "redis", "redis.conf"))
  117. )
  118. ```
  119. For local testing, you can change the variables to refer to your local files, or create a soft link
  120. to the corresponding folder for redis-server and copy the config file to `testdata/redis/`:
  121. ```
  122. ln -s /usr/bin/redis-server ./go-redis/testdata/redis/src
  123. cp ./go-redis/testdata/redis.conf ./go-redis/testdata/redis/
  124. ```
  125. Lastly, run:
  126. ```
  127. go test
  128. ```
  129. ## Contributors
  130. Thanks to all the people who already contributed!
  131. <a href="https://github.com/go-redis/redis/graphs/contributors">
  132. <img src="https://contributors-img.web.app/image?repo=go-redis/redis" />
  133. </a>