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!
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况