How to implement Redis Cache in ASP.NET

03 August 2020 at 14:00 by ParTech Media - Post a comment

By now you would be knowing what caching is and why it is often used by developers to enhance the performance of their application. If not, we recommend you read our previous post on the basics of caching to help you cruise through this post. Caching has been greatly used to reduce the consumption of resources and Redis cache is one of the popular means to implement caching.

There may be plenty of frameworks available to implement caching out there, but we picked Redis because of its fast performance, scalability, support for multiple languages, to name a few features.

In this post, we are going to take you one step ahead in your caching journey by helping you to understand how to implement Redis Cache in ASP.NET application.

Table of Contents

  1. How to install Redis Cache?
  2. Setting up the client
  3. How your application interacts with Redis cache?
  4. Wrapping Up

How to install Redis Cache?

The first step in your pursuit of an answer to the question ‘How to implement Redis cache in ASP.NET application’ is to install Redis on your local system. GitHub has a downloadable version of Redis Cache for Windows. Here is the link to download it.

You have an option to download an MSI-file or a zip file. The MSI file will typically install Redis on your local machine and will create a windows service. This automatically starts the Redis server on startup. On the other hand, in the zip file version, you have to start the server manually every time you need your application to interact with Redis.

In our case, we have downloaded the zipped version. Once you have unzipped the files to your local system, click on the file server.exe. This will start the Redis server and enables you to view a screen like the one shown below. The screen also shows that the server is now ready to accept the connections on port 6379.

You can also verify that the server has been started by selecting the file redis-cli.exe and entering the command “ping”. If the server is running fine, you will get the response as “pong”.

Here 127.0.0.1:6379 is the localhost that we will be connecting to with our client.

Setting up the client

Now that the Redis cache server is up and running, the next step is to make the client interact with the cache. To do this, let us build a client to store and retrieve data to and from Redis Cache. Begin the process by creating a new console application project in Visual Studio.

Once the console project is successfully created, install the ServiceStack.Redis package with the help of the NuGet package manager. This library is essential to make your client interact with the Redis server.

To install the package, all you have to do is search for ServiceStack.Redis package in the NuGet explorer and install it.

Once the ServiceStack.Redis package is installed, the next step is to define the SAVE and GET functions. But before we understand what the SAVE and GET functions are or how to set it up, let us take a quick recap of how Redis Cache works with the help of an example.

How your application interacts with Redis Cache?

Suppose your application is used by organizations to store the details of employees. One of the fields to capture is their country which we have decided to display in the form of a dropdown menu to the user.

Now let's assume your database doesn’t store the country field for some reason (say the country list keeps changing regularly or your application prohibits to store countries). Instead, you pull the country list from a 3rd party service and populate it to the dropdown every time a user tries to register.

On a given day, thousands of users try to register. This means more than a thousand requests to the third-party service. Now, this becomes an expensive proposition if the third-party service charges for every request to their service.

To avoid this, we have Redis Cache which stores the information temporarily. When the first user tries to register, the country list is pulled from the 3rd party service and gets displayed to the user in the dropdown menu. At the same time, the countries are also stored in the Redis Cache. Once the data is stored in Redis cache, the data can be pulled again much faster (and free of cost) to the next user. The country list can be reset at the end of the day in the cache to start fresh the following day.

Here is a diagram to depict how your application interacts with the Redis cache and the third party service to pull the data.

In the above diagram, you will notice that there are two functions called GET and SAVE. The GET function is used to pull the information from the Redis Cache and display it in the application while SAVE function is used to save the data in Redis Cache that is pulled from the 3rd party service.

In your console project in Visual Studio, you can define the SAVE function as below. To define this, we have considered the same example of saving/pulling a list of countries here, which was explained in the earlier section.

And define the GET function as below.

Let us define the main function to call the GET and SAVE functions.

In the above definition, you would also notice a ValidateKeyIsPresent function defined. Do you know why?

Remember that Redis is a key-based caching system. This means you will be able to get data from cache only if you send the key associated with that data. This check is being performed by the ValidateKeyIsPresent function here. If the key is not present, then the data cannot be fetched from the cache, and instead, it will be fetched from the 3rd party service.

After running the console app, there should be a key name SAARC Countries. If we run redis-cli.exe in a terminal and run command KEYS * we get all current keys stored in Redis.

The get our list of countries simply run the GET command with the key name, GET "SAARC Countries" of with a pattern GET SAA*.

Wrapping Up

This post has shown you how to implement Redis in your ASP.NET application. Redis is a great choice for both commercial and non-commercial use as it is completely free and open source. On top of it, it is extremely fast and operates with negligible performance overhead when managing the data. So go ahead and set up Redis in your application to give your visitors blazingly fast user experience.

Latest