Development

How use and work with Redis Hashes

Captain Salem 4 min read

How use and work with Redis Hashes

Redis is one of the most popular and useful databases of the modern time. It is a free and open-source in memory, key-value store database that provides low latency and incredible fast speeds.

Redis is mainly used as a caching mechanism for other databases or a message broker. It is a versatile database with support for a wide range of use cases. It comes with a plethora of data types that help you store almost any type of data.

In this tutorial, we will focus on one of the most useful and common data types provided by Redis: a hash. Using practical examples and simple commands, you will have a good knowledge of how to get started with Redis hashes in no time.

What is a Redis Hash?

Before we dive into how to use a Redis hash, let us first understand what it is.

In simple terms, a Redis hash refers to a map of key-value pairs used to store simple to complex key-value data. Hashes are common in major programming languages such as Ruby and Python. You may know them as dictionaries in Python.

Using a Redis hash, you can have one key holding multiple key-value pairs also known as field-value pairs. You will often encounter Redis hashes where you need to store an object with multiple properties.

For example, you can have a hash called user holding other key value pairs.

How to use Redis Hashes

Let us now explore how we can work with Redis hashes. Before we begin, we assume you have the latest version of Redis server installed and running on your system.

We also recommend using the native Redis CLI to get similar output as shown in this guide.

Redis Create Hash

We can create a Redis hash using the HMSET command. The command takes the name of the hash followed by the field value pairs of the hash.

The syntax is as shown below:

HMSET hash_name field1 value1 field2 value2

If the name of the provided hash does not exist on the database, Redis will create a new one with the specified name. Otherwise, Redis will modify the existing hash with the new values.

The code below creates a new hash called user with the set values.

127.0.0.1:6379> HMSET user username "geekbits.io" password "mypassord" session "abs6s53ghas63gxbvas6ea72"
OK

The command above creates a new hash called username with the fields username, password, and session with their corresponding values.

Redis Fetch Hash Values

To retrieve the values stored in a Redis hash, we can use the HMGET command followed by the name of the hash and the target field

The syntax is as shown below:

HMGET <hash_name> <field_name>

For example, to get the value of the username from the user hash, we can run:

127.0.0.1:6379> HMGET user username
1) "geekbits.io"

The command above returns the value associated with the “username” field from the specified hash.

Redis Fetch All Fields and Values

To retrieve all the fields from a Redis hash, we can use the HKEYS command.

The syntax is as shown below:

HKEYS <hash_name>

For example, to retrieve all the fieldsfrom the has “user”, we can run the command:

127.0.0.1:6379> HKEYS user
1) "username"
2) "password"
3) "session"

As we can see, the command returns all the fields in the specified keys.

Similarly, we can fetch all the values using the HVALS command. The syntax is as shown:

HVALS <hash_name>

An example is as shown below:

127.0.0.1:6379> HVALS user
1) "geekbits.io"
2) "mypassord"
3) "abs6s53ghas63gxbvas6ea72"

As we can see from the output above, the command returns the values associated with the hash fields.

To view both the fields and their corresponding values, we can use the HGETALL command. The syntax is as shown:

HGETALL <hash_name>

Example:

127.0.0.1:6379> HGETALL user
1) "username"
2) "geekbits.io"
3) "password"
4) "mypassord"
5) "session"
6) "abs6s53ghas63gxbvas6ea72"

In this case, the command returns the field and associated value one sequentially.

Redis Check if Hash Field Exists

Before running a command to fetch, update, or delete a specific hash field, it is good to ensure it exists otherwise Redis will nil.

To verify if a hash exists, we can use the HEXISTS command followed by the target hash and field. The syntax is as shown:

HEXISTS <hash_name> <field_name>

Example:

127.0.0.1:6379> HEXISTS user password
(integer) 1

If the specified field exists, Redis will return (integer) 1 . If the field does not exists, it will return (integer) 0

An example is as shown:

127.0.0.1:6379> HEXISTS user pass
(integer) 0

Here, Redis returns 0 as the key pass does not exist on the specified hash.

Redis Delete Hash

To remove a hash field and its associated value, we can use teh HDEL command followed by the name of the hash and the target field.

The syntax is as shown:

HDEL <hash_name> <field_name>

For example, to remove the session field from the user hash, we can run the command:

127.0.0.1:6379> HDEL user session
(integer) 1

Redis will return the number of fields that has been removed.

You can remove multiple fields by specifying them as shown in the syntax below”

HDEL <hash_name> <field_1> <field_2> <field_N>

To delete an entire hash, you can use the DEL command followed by the name of the has:

DEL <hash_name>

Example:

127.0.0.1:6379> DEL user
(integer) 1

Similarly, Redis will return the number of deleted items as an integer value.

Closing

In this article, we explored the fundamentals of working with a Redis hash. We learned how to create a Redis hash, how to update and delete a Redis hash, and more.

Check out our other Redis tutorials to learn more.

Thanks for reading && Catch you in the next one!!!!

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.