w3resource

Redis Hash: HSET

Redis HSET Command

Redis HSET command is used to set the field in the hash stored at key to value. If the key does not exist, a new key holding a hash is created. If the field already exists in the hash, it is overwritten.

Here are some common uses and scenarios for the "HSET" command:

  • Field Update: Set or update the value of a specific field within a hash.
  • Data Insertion: Insert new fields and values into a hash.
  • Configuration Management: Update individual configuration settings stored in a hash.
  • Session Management: Set specific attributes or data points for a session or user.
  • Atomic Operations: Ensure atomic updates to specific fields within a hash.

Syntax:

HSET KEY_NAME FIELD VALUE   

Return Value

Integer reply

  • 1 if the field is a new field in the hash and value was set.
  • 0 if the field already exists in the hash and the value was updated.

Example: Redis HSET

127.0.0.1:6379> HSET langhash lang1 "PHP"
(integer) 1
127.0.0.1:6379> HSET langhash lang2 "Javascript"
(integer) 1
127.0.0.1:6379> HGET langhash lang1
"PHP"
127.0.0.1:6379> HGET langhash lang2
"Javascript"

Example: Redis HSET another example

127.0.0.1:6379> HSET user email [email protected]
(integer) 1
127.0.0.1:6379> HSET user lang English
(integer) 1
127.0.0.1:6379> HSET user gender Male
(integer) 1
127.0.0.1:6379> HGETALL user
1) "email"
2) "[email protected]"
3) "lang"
4) "English"
5) "gender"
6) "Male"

Previous: HMSET
Next: HSETNX



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-hset-key-field-value.php