HKEYS

作者: NotFoundW | 来源:发表于2020-04-15 16:26 被阅读0次

HKEYS

  1. 如果hash表存在且有字段,则返回所有字段名
  2. 如果key不存在,则返回一个空列表

Command

$ redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> hset snake name kobe
(integer) 1
127.0.0.1:6379> hset snake number 24
(integer) 1
127.0.0.1:6379> hkeys snake
1) "name"
2) "number"
127.0.0.1:6379> del snake
(integer) 1
127.0.0.1:6379> hkeys snake
(empty list or set)

Code

func Hkeys(c redis.Conn) {
    // If hash table is existing and has field(s), will return the name of all the fields.
    c.Do("HSET", "snake", "name", "kobe")
    c.Do("HSET", "snake", "number", "24")
    fields, err := redis.Strings(c.Do("HKEYS", "snake"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    for i, v := range fields {
        fmt.Println("field", i, "is:", v)
    }
    //  If key doesn't exist, will return an empty list.
    c.Do("DEL", "snake")
    fields, err = redis.Strings(c.Do("HKEYS", "snake"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("If key doesn't exist, will return an empty list. List's length is:", len(fields))
}

Output

$ go run main.go 
field 0 is: name
field 1 is: number
If key doesn't exist, will return an empty list. List's length is: 0

相关文章

  • HKEYS

    HKEYS 如果hash表存在且有字段,则返回所有字段名 如果key不存在,则返回一个空列表 Command Co...

  • Redis命令Hash(哈希表)教程

    HDEL HEXISTS HGET HGETALL HINCRBY HINCRBYFLOAT HKEYS HLEN...

  • Redis操作--hash(哈希表)

    目录 HDEL HEXISTS HGET HGETALL HINCRBY HINCRBYFLOAT HKEYS H...

  • Redis hash表

    hash表 练习命令使用,具体如下: hset hmset hgetall hkeys hvals hget hm...

  • rides学习练习笔记

    HMSET 批量新增哈希表的字段HMGET 批量查询哈希表的字段HKEYS 获取哈希表所有的字段名HVALS 获取...

网友评论

      本文标题:HKEYS

      本文链接:https://www.haomeiwen.com/subject/chocvhtx.html