重用 Redigo 连接而不是每次都重新创建它。

huangapple go评论102阅读模式
英文:

Re-using Redigo connection instead of recreating it every time

问题

连接到Redigo并在函数内部操作数据就像涂抹黄油一样容易,但当你需要重复使用连接时,问题就来了,显然是出于性能/实用性的原因。

像这样在函数内部操作是可以的:

  1. func main() {
  2. client, err := redis.Dial("tcp", ":6379")
  3. if err != nil {
  4. panic(err)
  5. }
  6. defer client.Close()
  7. client.Do("GET", "test:1")
  8. }

但是将其放在外部就不行:

  1. var Client = redis.Dial("tcp", ":6379")
  2. defer Client.Close()
  3. func main() {
  4. Client.Do("GET", "test:1")
  5. }

会返回以下错误:

  1. ./main.go:1: multiple-value redis.Dial() in single-value context
  2. ./main.go:2: non-declaration statement outside function body

我尝试将连接放在const(常量)中,将defer放在主函数中,但都没有奏效。

这是一个更大的问题,因为我有许多其他函数需要与Redis通信,但每次重新创建Redis连接似乎很愚蠢。

Redigo API只展示了如何创建一个Dial实例,但没有进一步解释如何重复使用它。

也许你在我的讲话中迷失了,但我想在这里提供一些背景信息,所以我的明确而简明的问题是:你如何重复使用(而不是每次重新创建)Redigo连接?

英文:

Connecting to Redigo and manipulating data inside a function is easy like butter, but the problem comes when you have to re-use its connection, obviously for performance/practicality reasons.

Doing it inside a function like this works:

  1. func main() {
  2. client, err := redis.Dial("tcp", ":6379")
  3. if err != nil {
  4. panic(err)
  5. }
  6. defer client.Close()
  7. client.Do("GET", "test:1")
  8. }

But bringing it outside doesn't:

  1. var Client = redis.Dial("tcp", ":6379")
  2. defer Client.Close()
  3. func main() {
  4. Client.Do("GET", "test:1")
  5. }

With the following error(s) returned:

  1. ./main.go:1: multiple-value redis.Dial() in single-value context
  2. ./main.go:2: non-declaration statement outside function body

I've tried putting the connection as a const(ant), putting defer inside the main function to my dismay not working too.

This is an even bigger concern as I have many other functions that have to communicate to Redis, but recreating the connection to Redis everytime seems silly.

The Redigo API just shows how to create a Dial instance but doesn't go further by explaining how to re-use it.

You may've been lost in my talk, but I wanted to put a bit of context here, so my clear and concise question is: How do you go about re-using (not recreating everytime) a Redigo connection?

答案1

得分: 21

最好的方法是使用Pools,这里简要介绍了它的用法:Redigo Pools

全局变量最终不会重用连接,所以我最终使用了类似下面的代码(使用了之前提到的Pools):

  1. func newPool() *redis.Pool {
  2. return &redis.Pool{
  3. MaxIdle: 80,
  4. MaxActive: 12000, // 最大连接数
  5. Dial: func() (redis.Conn, error) {
  6. c, err := redis.Dial("tcp", ":6379")
  7. if err != nil {
  8. panic(err.Error())
  9. }
  10. return c, err
  11. },
  12. }
  13. }
  14. var pool = newPool()
  15. func main() {
  16. c := pool.Get()
  17. defer c.Close()
  18. test, _ := c.Do("HGETALL", "test:1")
  19. fmt.Println(test)
  20. }
  21. 如果你想在另一个函数中重用这个连接池可以这样做
  22. ```go
  23. func test() {
  24. c := pool.Get()
  25. defer c.Close()
  26. test2, _ := c.Do("HGETALL", "test:2")
  27. fmt.Println(test2)
  28. }
英文:

The best way turned out to be using Pools, which are briefly documented here: Redigo Pools.

A global variable won't eventually reuse a connection, so I ended up with something like this (using Pools as noted before):

  1. func newPool() *redis.Pool {
  2. return &redis.Pool{
  3. MaxIdle: 80,
  4. MaxActive: 12000, // max number of connections
  5. Dial: func() (redis.Conn, error) {
  6. c, err := redis.Dial("tcp", ":6379")
  7. if err != nil {
  8. panic(err.Error())
  9. }
  10. return c, err
  11. },
  12. }
  13. }
  14. var pool = newPool()
  15. func main() {
  16. c := pool.Get()
  17. defer c.Close()
  18. test,_:=c.Do("HGETALL", "test:1")
  19. fmt.Println(test)
  20. }

If for example you want to reuse a pool inside another function you do it like this:

  1. func test() {
  2. c := pool.Get()
  3. defer c.Close()
  4. test2,_:=c.Do("HGETALL", "test:2")
  5. fmt.Println(test2)
  6. }

答案2

得分: -1

redis.Dial()方法返回客户端错误。要修复它,你应该将以下代码替换为:

  1. var Client, _ = redis.Dial("tcp", ":6379")
英文:

The redis.Dial() method returns client error. To fix it, you should replace:

  1. var Client = redis.Dial("tcp", ":6379")

with:

  1. var Client, _ = redis.Dial("tcp", ":6379")

huangapple
  • 本文由 发表于 2014年6月24日 21:01:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/24387350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定