Hexists
- 如果哈希表存在且含有指定字段,返回1
- 如果哈希表存在且不含有指定字段,返回0
- 如果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
网友评论