dtb81443 2018-01-22 08:35
浏览 609
已采纳

获取redis golang的队列长度

I am using go-redis package. I fired below query:

rd.LLen("queue_1")

Which returning me below result with type *redis.IntCmd:

llen queue_1: 100001

Count is perfect but I want only count with type of int. Can anyone help?

  • 写回答

3条回答 默认 最新

  • doushi3189 2018-01-22 08:39
    关注

    the LLen func looks like: func (c *cmdable) LLen(key string) *IntCmd

    So it returns an IntCmd type. IntCmd provides this function: func (cmd *IntCmd) Val() int64

    So you can do e.g.

    cmd := rd.LLen("queue_1")
    i := cmd.Val() //i will be an int64 type
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • doutan8775 2018-01-22 08:37
    关注

    Use the Result or Val method. As documented, these return the int64 value returned from the command.

    评论
  • doulian8554 2018-01-22 08:38
    关注

    The documentation for IntCmd shows the following:

    func (cmd *IntCmd) Result() (int64, error)
    
    func (cmd *IntCmd) Val() int64
    

    So it seems you can either get the int64 value and any error to check, or just the int64 value. Is there a reason this is not sufficient?

    You can call them as follows:

    // Get the value and check the error.
    if val, err := res.Result(); err != nil {
      panic(err)
    } else {
      fmt.Println("Queue length: ", val)
    }
    
    // Get the value, but ignore errors (probably a bad idea).
    fmt.Println("Queue length: ", res.Val())
    
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Google Chrome 所有页面崩溃,三种解决方案都没有解决,我崩溃了
  • ¥20 使用uni-app发起网络请求,获取重定向302返回的cookie
  • ¥20 手机外部浏览器拉起微信小程序支付 (相关搜索:微信小程序)
  • ¥20 怎样通过一个网址找到其他同样模版的网址
  • ¥30 XIAO esp32c3 读取FDC2214的数据
  • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信
  • ¥15 关于PROCEDURE和FUNCTION的问题
  • ¥100 webapi的部署(标签-服务器)
  • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)
  • ¥15 C语言除0问题的检测方法