doulaozhang0238 2018-07-02 20:40
浏览 119
已采纳

如果golang已启动并正在运行以进行连接,如何使用golang查询redis Db?

My redis db has huge number of keys, so when I start my code, at that time if I do info persistence (on redis CLI) it gives me following, Loading: 1 means that redis is not ready to take the connections,while loading :0 means redis up and running and ready to take connections.

loading:1
rdb_changes_since_last_save:1024
rdb_bgsave_in_progress:0
rdb_last_save_time:1530558451
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1 
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0

My code to establish the connection with redis is as follows:

var DefaultPool = NewPool("redis", 6379)

/*NewPool - create a new redis pool accessible at the given address */
func NewPool(host string, port int) *redis.Pool {
  var address string
  if os.Getenv("DOCKER") != "" {
    address = fmt.Sprintf("%v:6379", host)
  } else {
    address = fmt.Sprintf("127.0.0.1:%v", port)
  }
   return &redis.Pool{
     MaxIdle:   80,
     MaxActive: 1000, // max number of connections
     Dial: func() (redis.Conn, error) {
        c, err := redis.Dial("tcp", address)
        if err != nil {
            panic(err.Error())
        }
        return c, err
      },
   }
}

How I can constantly check if loading value is 0.

  • 写回答

1条回答 默认 最新

  • dongzhang5787 2018-07-03 00:10
    关注

    Get the info:

     info, err := redis.String(c.Do("INFO", "persistence"))
     if err != nil {
         // handle error
     }
    

    Check the loading field:

     re := regexp.MustCompile("^loading:0$") // this can be done once and stored as a package level variable.
     if re.MatchString(info) {
           // loading is zero!
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 快手联盟怎么快速的跑出建立模型
  • ¥20 ros环境下运行orb-slam一直报错
  • ¥15 关于腾讯云服务器中,PHP页面无法打开的问题
  • ¥60 如何把照片修复成原始拍摄图
  • ¥80 Exited too quickly (process log may have details)
  • ¥15 爬知乎登录之后内容加载不出来
  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息
  • ¥15 函数r关于两个分量y,z方向上的图像,分开画r随y的图像,r随z的图像