Using the Redis NoSql database with .NET Part 6: the hash data type in C#

Introduction

In the previous post we looked at the sorted set data type in Redis. A sorted set is not simply a set where the values are sorted. It is a container of unique strings where each member also has a score. The members in the sorted set are sorted by their scores. Sorted sets can be applicable for scores reached in a competition, the time it took to complete various activities, the energy lost during a physical exercise etc., i.e. anywhere where a member also has a numeric value attached to it. We also discussed the basics of persistence and how it is configured for the Windows Redis service through a configuration file.

In this post we’ll look at one remaining data type in Redis, namely hashes.

Hashes

A hash in Redis is like a dictionary in .NET or a map in Java. It is a key-value container where each key is unique. The most basic hash command is HSET which accepts the hash name, the key and the value. Here’s how to set the name of country with id 1:

HSET country:1 name “Estonia”

With HMSET we can set multiple values:

HMSET country:1 capital “Tallinn” language “Estonian” local-name “Eesti” currency “Euro” dialing-code “+372” population “1317797” internet-code “.ee”

HGET helps retrieve the value of a single key from a dictionary:

HGET country:1 language

…which returns Estonian.

HMGET accepts an array of keys and returns their values:

HMGET country:1 capital local-name currency

…which yields the following output:

1) “Tallinn”
2) “Eesti”
3) “Euro”

HGETALL retrieves all keys and values:

HGETALL country:1

1) “name”
2) “Estonia”
3) “capital”
4) “Tallinn”
5) “language”
6) “Estonian”
7) “local-name”
8) “Eesti”
9) “currency”
10) “Euro”
11) “dialing-code”
12) “+372”
13) “population”
14) “1317797”
15) “internet-code”
16) “.ee”

HLEN returns the number of keys:

HLEN country:1

…which is 8.

HKEYS gets the keys…:

HKEYS country:1

1) “name”
2) “capital”
3) “language”
4) “local-name”
5) “currency”
6) “dialing-code”
7) “population”
8) “internet-code”

…whereas HVALS returns the values:

HVALS country:1

1) “Estonia”
2) “Tallinn”
3) “Estonian”
4) “Eesti”
5) “Euro”
6) “+372”
7) “1317797”
8) “.ee”

We can delete a field with HDEL:

HDEL country:1 internet-code

HEXISTS checks for the existence of a key:

HEXISTS country:1 name

…which responds with 1 for true and 0 for false.

With HINCRBY we can increment a numeric value. Let’s say that Estonias population has increased by 153:

HINCRBY country:1 population 153

…which responds with the updated value, i.e. 1317950.

HINCRBY accepts a negative integer for a decrease:

HINCRBY country:1 population -20

The last command we can look at here is HSETNX which is the same as HSET but only sets a value if it doesn’t exist already.

HSETNX country:1 internet-code “.ee”

…will put the internet-code back to the hash, but…

HSETNX country:1 population “13178000”

…will be ignored. HSETNX returns 1 for an insertion and 0 if the command was ignored.

We’ll continue in the next post.

You can view all posts related to data storage on this blog here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: