0
0
Redisquery~5 mins

HMSET and HMGET for bulk in Redis

Choose your learning style9 modes available
Introduction
HMSET and HMGET let you save and get many pieces of information in one go. This saves time and makes your work faster.
When you want to save many details about a user, like name, age, and city, all at once.
When you need to get several settings or preferences for an app quickly.
When you want to update many fields of a product in your store in one command.
When you want to read multiple pieces of data from a profile without many separate requests.
Syntax
Redis
HMSET key field1 value1 field2 value2 ...
HMGET key field1 field2 ...
HMSET sets multiple fields and values in a hash stored at key.
HMGET retrieves the values of the specified fields from the hash.
Examples
Save name, age, and city for user with ID 1.
Redis
HMSET user:1 name "Alice" age 30 city "Paris"
Get the name and age fields for user with ID 1.
Redis
HMGET user:1 name age
Set price and stock for product with ID 100.
Redis
HMSET product:100 price 19.99 stock 50
Retrieve price and stock for product 100.
Redis
HMGET product:100 price stock
Sample Program
First, save three fields for user 42. Then get all three fields in one command.
Redis
HMSET user:42 name "Bob" age 25 city "London"
HMGET user:42 name age city
OutputSuccess
Important Notes
HMSET is deprecated in newer Redis versions; use HSET with multiple field-value pairs instead.
HMGET returns nil for fields that do not exist in the hash.
Using these commands reduces the number of calls to Redis, improving speed.
Summary
HMSET saves many fields and values in one hash at once.
HMGET fetches many fields from a hash in one command.
These commands help work faster by handling multiple pieces of data together.