Redis cache in .NET Core

Matias Paulo
4 min readSep 22, 2021

--

Hi! Today I want to present you a series of stories about how to effectively implement Redis Cache in ASP .NET Core. The idea of this series of posts is not to show you how you can use Redis in a highly performant way, instead, I’ll try to explain to you the bases and if you are interested in the performance you can explore it by yourself.

The idea for today is to use a Redis docker image for the cache and create an API that will store and retrieve a key/value.

As usual, let’s start from the beginning, what is Redis? The short answer is that Redis is an in-memory data structure.

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.

If you want to know more about Redis, please check the documentation section, they have a lot of information about the features and configurations.

Enough talk, let’s start downloading the Redis docker image. This is an easy step, we only need to open a “Powershell” window and run the following command: docker run -p 6379:6379 — name redis -e ALLOW_EMPTY_PASSWORD=yes bitnami/redis:latest

Let me explain to you a little bit the different command arguments:

  • docker run: The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.
  • -p 6379:6379: Maps the 6379 port in the container on the 6379 port on the host.
  • — name redis: Assign the “redis” name to the container, if we don’t specify one, a random name will be generated.
  • -e ALLOW_EMPTY_PASSWORD=yes: In this case, we are allowing the use of an empty password.
  • bitnami/redis:latest: Here we are specifying the Redis image version, in this case, the latest.

If docker is not able to find the image, will pull it automatically.

Now that we have our docker image running in a container, we can start creating a new .NET Core API project.

Once we have our project created, we need to include “StackExchange.Redis” package in the API project. You can find the source code here.

In this article we will explore the basic usage of the library, you can check it here: https://stackexchange.github.io/StackExchange.Redis/Basics

The first thing that we need to do, its stablish the connection with Redis, we are going to accomplish it adding the following in the “Startup.cs” class.

As you can see in the line 29, we are registering as singleton the “IConnectionMultiplexer” interface and reading the connection value from the “Appsettings.json” file. It is important register it as singleton in order to avoid recreate connections, which can produce a pool starvation.

Appsettings.json file

Now, the only thing remaining is start using it, for that we are going to expose 2 endpoints: one for save a student and other to retrieve it.

Student controller

For saving we are exposing a “HttpPost” endpoint which receives in the body the data. In order to store the student in “Redis” we are resolving the “IConnectionMultiplexer” interface.

First to all we need to get the database and then we are just saving a key/value pair. The key is the student name and the value is the entire student object serialized.

The “Get” endpoint works in a similar way, the only difference is that we are only using the key in order to get the saved object, if exists, we are deserializing the value and return it.

If we want to check what we have stored in Redis, we can make use of some management tool, in my case I’m using this one: https://github.com/qishibo/AnotherRedisDesktopManager/releases

Let’s take a quick view:

Save data — Postman call

Here we can see how the data is being stored.

Data view
Retrieve data — Postman call

In next articles we will see another features from Redis and another way to use it.

As always you can check the source code from here.

Enjoy and happy coding! : )

--

--