w3resource

Redis Hash: HMSET

Redis HMSET Command

Redis HMSET command is used to set the specified fields to their respective values in the hash stored at key. This command overwrites any existing fields in the hash. If the key does not exist, a new key holding a hash is created.

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

  • Batch Updates: Set multiple field values in a single operation.
  • Data Initialization: Initialize a hash with multiple fields and values at once.
  • Configuration Management: Update multiple configuration settings stored in a hash simultaneously.
  • Session Management: Set multiple attributes or data points for a session or user in one command.
  • Performance Optimization: Reduce the number of round-trips to the Redis server by updating multiple fields at once.

Syntax:

HMSET KEY_NAME FIELD1 VALUE1 ...FIELDN VALUEN   

Available since

2.0.0.

Return Value

Simple string reply

Note: Simple Strings are encoded in the following way: a plus character, followed by a string that cannot contain a CR or LF character (no newlines are allowed), terminated by CRLF (that is "\r\n").

Return Value Type

Integer

Example: Redis HMSET

127.0.0.1:6379> HMSET langhash lang1 "PHP" lang2 "JavaScript" lang3 "Redis"
OK
127.0.0.1:6379> HGET langhash lang1
"PHP"
127.0.0.1:6379> HGET langhash lang2
"JavaScript"
127.0.0.1:6379> HGET langhash lang3
"Redis"

Example: Redis HMSET another example

127.0.0.1:6379> HMSET user-y email [email protected] language English gender Male
OK
127.0.0.1:6379> HGETALL user-y
1) "email"
2) "[email protected]"
3) "language"
4) "English"
5) "gender"
6) "Male"

Previous: HMGET
Next: HSET



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://w3resource.com/redis/redis-hmset-key-field1-value1.php