Using Hashes in Redis

Using Hashes in Redis

Sep 15, 2023 by Robert Gravelle

In Redis, a Hash is a data structure that maps a string key with field-value pairs. Thus, Hashes are useful for representing basic objects and for storing groupings of counters, among other things. This article will go over some of the main commands for managing hashes, both via the redis-cli and using Navicat for Redis.

In Redis, the key is the name of the Hash and the value represents a sequence of field-name field-value entries. For instance, we could describe a vehicle object as follows:

vehicle make Toyota model Crown trim Platinum year 2023 color black

To work with Hashes, we use commands that are similar to what we use with strings, since Hash field values are strings. Case in point, the command HSET sets field in the Hash to value. If key does not exist, a new key storing a hash is created. If field already exists in the hash, it is overwritten.

HSET key field value

For each HSET command, Redis replies with an integer as follows:

  • 1 if field is a new field in the hash and value was set.
  • 0 if field already exists in the hash and the value was updated.

Let’s create the vehicle hash described above:

HSET vehicle make "Toyota"   // 1
HSET vehicle model "Crown"   // 1
HSET vehicle trim "Platinum" // 1
HSET vehicle year 2015       // 1
HSET vehicle color "black"   // 1

Now, if we update the value of the year field to 2022, HSET returns 0:

HSET vehicle year 2022 // 0

In the Navicat for Redis, Hash fields may be added using the built-in Editor. Clicking on the ellipsis […] button on the right of a field opens a special Editor where you can enter individual field values:

vehicle_hash_in_navicat_editor (70K)

Clicking the Apply button adds the new Hash or field.

We can fetch the value associated with field in a Hash using the HGET command:

HGET key field

For example, we can use it to verify that we are getting 2022 as the value of year instead of 2015:

HGET vehicle year // 2022

We can also get all hash contents (fields and values) using the HGETALL command:

HGETALL key

Let’s try it out:

HGETALL vehicle
/* 
Returns:

make
Toyota
model
Crown
trim
Platinum
year
2022
color
black
*/

HGETALL replies with an empty list when the provided key argument doesn’t exist.

The HDEL command removes the specified fields from the hash stored at key. Specified fields that do not exist within this hash are ignored.

HDEL returns the number of fields that were removed from the hash. If a key does not exist, it is treated as an empty hash and HDEL returns 0.

HDEL key field [field ...]

Let’s use HDEL to delete the year and color fields:

HDEL vehicle year color // 2

In the Navicat Editor, we can remove a field by selecting it and clicking the Delete [-] button located under the fields list:

delete_button_in_navicat_editor (25K)

This blog article highlighted some of the main commands for managing Hashes in Redis, both via the redis-cli and using Navicat for Redis.

Interested in giving Navicat for Redis a try. Download it here. The trial version is fully functional for 14 days.