Redis Compass (The Hash Datastructure) –Part 6

In case you are new to programming, you need to first examine what a ‘hash-table’ is in order to understand the redis hash datastrucure.

Hash Table

A hash-table is a type of data-structure which involves representation of data in an “associative” manner, where each data has a unique index associated with it that is computed by a hash function.  This stack-overflow answer perfectly explains hash-tables.

Redis Hash

The redis hash is a field-value pair data-structure that stores a mapping of keys to values.  You can use the hash data-structure when you need to store things like objects e.g say you want to store the details of a customer such as name, email e.t.c, it would be a perfect use-case for the redis hash.  The following are some of the commands which redis provides to facilitate hash manipulation.

HMSET

This command is used when assigning multiple field-value pair to a key.  Take for example we were building an analytics engine for a blog, to store the analytics for a post we can simply do this:

“posts:120” is the key, while we have an alternation of field and value, so “title” is a field while “redis-hash-datastructures” is a value, “views” is a field while “20000” is a value e.t.c.

HGET

This command is used for retrieving the value of a field belonging to a particular key.  Let’s say from our previous example, we wanted to retrieve the number of “likes” our post got, we can use the HGET command to achieve this, we simply do this:

where “posts:120” is the key and “likes” is our field, the result “1000” being returned is our value.

HMGET

This is another interesting command, we saw how we could use HGET to retrieve the value in a field-value pair belonging to a key, in a scenario where we would like to retrieve the values of more than one field belonging to a particular key, we use HMGET.  Still going by our former example, we can do this:

where “posts:120” is the key, “title” “views” & “likes” are fields while “redis-hash-datastructures”, “20000” & “1000” are the values of their respective fields.

HSET

This is quite similar to HMSET in the sense that they both enable assignment of field-value mapping to a key but different in the sense that HSET only allows for assingment of a singular field-value mapping while HMSET allows for multiple assignments. Let’s say we wanted to add a new field for our post analytics example, let’s call our fields “stars” which will hold the star rating for our post, to do this we simply say:

where “posts:120” is our key, “stars” is our field and “5” our value.

HDEL

CRUD( Create, Read, Update, Delete) can’t be complete without the D( Delete) in it.  The HDEL command is used for “deleting” a field-value mapping from a key, assuming we wanted to delete “likes” from our previous exampe we simply do this:

when we try to retrieve the field “likes”, we receive a “nil” response:

HGETALL

This command right here enables retrieval of all field-value mapping assigned to a key, applying that on our post will give us:

 

Conclusion

There are much more awesome commands that redis provides, feel free to checkout the redis hash command-list documentation.  Next we will look deeper into the Set datastructure.

 

2 comments on “Redis Compass (The Hash Datastructure) –Part 6

Leave a Reply

Your email address will not be published. Required fields are marked *