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.9 KiB

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