Hence, it is essential to understand the commands that you can use to work with hash types in Redis.
Redis Create Hash
To create a hash in Redis, use the HSET command. The command takes the key name, a field, and value as the parameters.
The syntax is as shown:
An example is as shown below:
(integer) 1
In the example above, we create a new hash type called user_info that holds the field firstname and value “Michael.”
Redis HMSET Command
Redis also has the HMSET command that performs the same operation as HSET command. In addition, the command will overwrite the key with the new values if the key already exists.
NOTE: The Redis version 4.0.0 command is considered deprecated. We prefer that you use HSET with multiple field-value pairs instead.
Source: Redis Official
The example below shows how to use the HMSET command.
OK
Redis Get Hash
You can get the value associated with a specified hash field using the HGET command as shown:
"Michael"
To get the values of multiple fields, use the HMGET command as shown:
You can also get all the fields and associated values using the HGETALL command as shown:
1) "firstname"
2) "Michael"
The command should return the hash field and its mapped value.
Conclusion
Understading Redis HMSET command helps to better use hash types in Redis. By following the given guide and easy tutorial, readers should be able to build a non-trivial application with Redis using this hash-type.