3 min read

Getting to Know Redis Hashes

Suppose you have familiarized yourself with other Redis datatypes and decided to learn more about hashes. Especially its commands HSET, HGET, HGETALL, and HDEL. Then this post is right for you.
Getting to Know Redis Hashes
Photo by Uwe Conrad / Unsplash

Introduction and Background

Hashes is one of the data structures of Redis. You have probably seen some Redis data structures: strings, lists, sets, and sorted sets, and decided to understand hashes.

Redis hashes store a mapping of keys to values. The values stored in hashes are the same as what we can hold in regular strings. Moreover, hashes in Redis offer the same commands that we can perform on strings but are slightly different.

The difference I've seen is that hashes commands start with the H.

String Command Hash Command
GET HGET
SET HSET
DEL HDEL

OK, let's get started with hash commands, then.

Table of Contents

Hash Commands

Hash Command Description
HSET It stores the value at the key in the hash
HGET It gets the value at the given hash-key
HGETALL It gets the whole hash
HDEL It deletes or removes a key from the hash.

HSET Command

When we want to add an item to a hash, it will return the value that tells whether the item is new inside the hash.

127.0.0.1:6379> HSET product:100 type car description awesome brand Honda
(integer) 3

From the example command above, we have added a hash key which is product:100with the field-value pair: type, description, and brand.

In case we have rerun the command and the item already exists, the command line will return (integer) 0. See the complete code sample below.

127.0.0.1:6379> HSET product:100 type car description awesome brand Honda
(integer) 3
127.0.0.1:6379> HSET product:100 type car description awesome brand Honda
(integer) 0

HGET Command

When we want to get or fetch the value at the hash key.

Let's say we want to get type, description, and brand values.

127.0.0.1:6379> HGET product:100 type
"car"
127.0.0.1:6379> HGET product:100 description
"awesome"
127.0.0.1:6379> HGET product:100 brand
"Honda"

The only problem with this is we need to be aware of the keys (type, description, and brand). But there's another command where we can see the complete key-value pair.

HGETALL Command

The HGET command does its job, but what about getting the entire hash? This is where the HGETALL comes in.

Let's see an example below.

127.0.0.1:6379> HGETALL product:100
1) "type"
2) "car"
3) "description"
4) "awesome"
5) "brand"
6) "Honda"

HDEL Command

The HDEL command removes the key (subkeys, to be exact) inside the hash. One thing to note, when we remove items from the hash, it returns whether the item was there and when we're trying to remove it.

Let's see an example below.

127.0.0.1:6379> HDEL product:100 type
(integer) 1
127.0.0.1:6379> HDEL product:100 type
(integer) 0

From the example above, it returns (integer) 1 is it was successfully removed. Otherwise, it returns zero (integer) 0 if the key doesn't exist.

OK, this time, we'll try to delete every key twice to see the result.

127.0.0.1:6379> HDEL product:100 type
(integer) 1
127.0.0.1:6379> HDEL product:100 type
(integer) 0
127.0.0.1:6379> HDEL product:100 description
(integer) 1
127.0.0.1:6379> HDEL product:100 description
(integer) 0
127.0.0.1:6379> HDEL product:100 brand
(integer) 1
127.0.0.1:6379> HDEL product:100 brand
(integer) 0

Now that everything has been removed. Let's try to retrieve the product:100 and see what does look like.

127.0.0.1:6379> HGETALL product:100
(empty array)

Tada! Empty array (empty array).

Summary

In this post, we have discussed the basics of Redis hash commands. Such as HSET, HGET, HGETALL, and HDEL.

I hope you have enjoyed this article. Till next time, happy programming!