Latest web development tutorials

Redis Hsetnx command

Redis hash (Hash)

Redis Hsetnx command is used to hash table does not exist in the field of assignment.

If the hash table does not exist, a new hash table is created and HSET operation.

If the field already exists in the hash table, the operation is invalid.

If the key does not exist, a new hash table is created and executed HSETNX command.

grammar

redis Hsetnx basic command syntax is as follows:

redis 127.0.0.1:6379> HSETNX KEY_NAME FIELD VALUE

Available versions

> = 2.0.0

return value

Set successfully, returns 1. If the given field already exists and no operation is performed, 0 is returned.

Examples

redis 127.0.0.1:6379> HSETNX myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HSETNX myhash field1 "bar"
(integer) 0
redis 127.0.0.1:6379> HGET myhash field1
"foo"

redis 127.0.0.1:6379> HSETNX nosql key-value-store redis
(integer) 1

redis 127.0.0.1:6379> HSETNX nosql key-value-store redis       # 操作无效, key-value-store 已存在
(integer) 0

Redis hash (Hash)