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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?