conn.flush()不会将所有记录刷新到Redis。

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

conn.flush() will not flush all record to redis

问题

这是代码:

func main() {
  ...
  pool := createPool(*redis_server, *redis_pass)
  defer pool.Close()
  c := pool.Get() 
  var i int64
  st := tickSec()
  for i = 0; i < *total; i++ {
    r := time.Now().Unix() - rand.Int63n(60*60*24*31*12)
    score, _ := strconv.Atoi(time.Unix(r, 0).Format("2006010215"))
    id := utee.PlainMd5(uuid.NewUUID().String())
    c.Send("ZADD", "app_a_5512", score, id)
    if i%10000 == 0 {
      c.Flush()
      log.Println("current sync to redis", i)
    }
  }
  //c.Flush()
  c.Close()
  ...
}

如果我使用c.Close()总共设置为100000实际的有序集合数量为100000
但是如果我使用c.Flush()总共设置为100000实际的有序集合数量少于10000096932);如果我在main函数的末尾使用time.Sleep()总共也是100000

当main函数退出时flush函数没有完成吗为什么谢谢

<details>
<summary>英文:</summary>

this is code 
     

     func main() {
       ...
       pool := createPool(*redis_server, *redis_pass)
	   defer pool.Close()
	   c := pool.Get() 
	   var i int64
	   st := tickSec()
	   for i = 0; i &lt; *total; i++ {
		  r := time.Now().Unix() - rand.Int63n(60*60*24*31*12)
		  score, _ := strconv.Atoi(time.Unix(r, 0).Format(&quot;2006010215&quot;))
		  id := utee.PlainMd5(uuid.NewUUID().String())
		  c.Send(&quot;ZADD&quot;, &quot;app_a_5512&quot;, score, id)
		  if i%10000 == 0 {
			  c.Flush()
			  log.Println(&quot;current sync to redis&quot;, i)
		  }
	  }
	  //c.Flush()
	  c.Close()
      ...
    }

if i use c.Close(),the total set 100000,the real sortedset count 100000.
but if i use c.Flush(),the total also set 100000, the real sortedset count less than 100000(96932);if i use time.Sleep() in the end of the main func,the total is 100000 too.

when main func exit,the flush func is not complete?and why? thank you!


</details>


# 答案1
**得分**: 1

程序在循环之后调用Close()时能够正常工作的原因是连接池的Close()方法会读取并丢弃所有待处理的响应

应用程序应该接收所有命令的响应而不是让响应积压并消耗服务器上的内存在循环中没有必要刷新

```go
go func() {
  for i = 0; i < *total; i++ {
     r := time.Now().Unix() - rand.Int63n(60*60*24*31*12)
     score, _ := strconv.Atoi(time.Unix(r, 0).Format("2006010215"))
     id := utee.PlainMd5(uuid.NewUUID().String())
     c.Send("ZADD", "app_a_5512", score, id)
  }
  c.Flush()
}

for i = 0; i < *total; i++ {
   c.Receive()
}
c.Close()

此外,应用程序应该检查和处理Send、Flush和Receive返回的错误。

英文:

The reason that the program works when Close() is called after the loop is that the pooled connection's Close() method reads and discards all pending responses.

The application should Receive the responses for all commands instead of letting the respones backup and consume memory on the server. There's no need to flush in the loop.

go func() {
for i = 0; i &lt; *total; i++ {
r := time.Now().Unix() - rand.Int63n(60*60*24*31*12)
score, _ := strconv.Atoi(time.Unix(r, 0).Format(&quot;2006010215&quot;))
id := utee.PlainMd5(uuid.NewUUID().String())
c.Send(&quot;ZADD&quot;, &quot;app_a_5512&quot;, score, id)
}
c.Flush()
}
for i = 0; i &lt; *total; i++ {
c.Receive()
}
c.Close()

Also, the application should check and handle the errors returns from Send, Flush and Receive.

huangapple
  • 本文由 发表于 2017年1月9日 15:47:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/41543210.html
匿名

发表评论

匿名网友

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

确定