How to do it...

To understand how to use the hash data type, take the following steps:

  1. Open a Terminal and connect to Redis with redis-cli.
  2. Now, let's set the information properties of the "Kyoto Ramen" restaurant using the HMSET command:
127.0.0.1:6379> HMSET "Kyoto Ramen" "address" "801 Mission St, San Jose, CA" "phone" "555-123-6543" "rating" "5.0" 
OK  
  1. Use the HMGET command to retrieve values in a hash:
127.0.0.1:6379> HMGET "Kyoto Ramen" "address" "phone" "rating" 
1) "801 Mission St, San Jose, CA" 
2) "555-123-6543" 
3) "5.0" 
  1. To retrieve the value for a single field, use HGET:
127.0.0.1:6379> HGET "Kyoto Ramen" "rating" 
"5.0" 
  1. To test if a field exists in the hash, use HEXISTS:
127.0.0.1:6379> HEXISTS "Kyoto Ramen" "phone" (integer) ...

Get Redis 4.x Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.