duanhui7329 2017-01-09 07:47
浏览 89
已采纳

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

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 < *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()
  ...
}

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!

  • 写回答

1条回答 默认 最新

  • duangewu5234 2017-01-09 14:31
    关注

    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 < *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()
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题