0
0
Redisquery~5 mins

Why hashes represent objects in Redis

Choose your learning style9 modes available
Introduction

Hashes in Redis store multiple related pieces of information together, just like an object holds different properties about one thing.

Storing user profiles with fields like name, age, and email.
Keeping product details such as price, description, and stock count.
Saving settings or preferences for an application user.
Organizing data about a book with title, author, and year published.
Syntax
Redis
HSET key field value [field value ...]
HGET key field
HGETALL key
Use HSET to add or update fields inside a hash.
HGET retrieves the value of a specific field.
HGETALL returns all fields and values in the hash.
Examples
Adds multiple fields to the hash representing a user.
Redis
HSET user:1000 name "Alice" age 30 email "alice@example.com"
Gets the name field from the user hash.
Redis
HGET user:1000 name
Retrieves all fields and values for the user.
Redis
HGETALL user:1000
Sample Program

This stores details about a book as fields in a hash, then fetches all the details.

Redis
HSET book:1 title "Redis Basics" author "Sam" year 2024
HGETALL book:1
OutputSuccess
Important Notes

Hashes are efficient for storing small objects with multiple fields.

Each hash key is like an object name, and fields are its properties.

Summary

Hashes group related data like an object groups properties.

Use hashes to store and retrieve multiple fields easily.

They help organize data clearly and efficiently in Redis.