Hexists

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

Hexists

  1. 如果哈希表存在且含有指定字段,返回1
  2. 如果哈希表存在且不含有指定字段,返回0
  3. 如果key不存在,返回0

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> hexists snake name
(integer) 1
127.0.0.1:6379> hexists snake fakeField
(integer) 0
127.0.0.1:6379> exists fakeKey
(integer) 0
127.0.0.1:6379> hexists fakeKey fakeField
(integer) 0

Code

func Hexists(c redis.Conn) {
    defer c.Do("DEL", "snake")
    //  If hash table is existing and field is existing, will return 1.
    c.Do("HSET", "snake", "name", "kobe")
    fieldIsExist, err := c.Do("HEXISTS", "snake", "name")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("If hash table is existing and field is existing, will return:", fieldIsExist)
    //  If hash table is existing but field doesn't exist, will return 0.
    fieldIsExist, err = c.Do("HEXISTS", "snake", "fakeField")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("If hash table is existing but field doesn't exist, will return:", fieldIsExist)
    //  If key doesn't exist, will return 0.
    isExist, _ := c.Do("EXISTS", "fakeKey")
    if isExist == 1 {
        c.Do("DEL", "fakeKey")
    }
    fieldIsExist, err = c.Do("HEXISTS", "fakeKey", "fakeField")
    if err != nil {
        colorlog.Error(err.Error())
    }
    fmt.Println("If key doesn't exist, will return:", fieldIsExist)
}

Output

$ go run main.go 
If hash table is existing and field is existing, will return: 1
If hash table is existing but field doesn't exist, will return: 0
If key doesn't exist, will return: 0

相关文章

网友评论

      本文标题:Hexists

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