Using Azure Redis Cache in ASP.Net Core Applications

Redis is a high performance distributed cache. It’s useful for storing data that you are going to need again and again in a short period of time when you don’t want to use processing power to “create” that data again. The potential usage of Redis Cache can be in a variety of cases, including temporarily storing User Session state.

The way of adding a new Redis Cache service to an existing Azure Subscription is simple. “Redis” can be found directly via search on the Azure Portal, and then the mandatory details of DNS Name, Subscription, Location and Pricing Tier need to be selected before the Redis Cache service can go up and running.


Figure 1: Choosing and Setting up a new Redis Cache Service from the Azure Portal

For this example, we shall be demonstrating the use of Redis Cache for ASP.NET Core 2.0 Applications.

The first thing one needs to do, is add the Redis caching package provided by Microsoft to the Application. This can be achieved from the package manager console with the following command :

Install-Package Microsoft.Extensions.Caching.Redis

In the Startup.cs, then the following lines need to be added in the ConfigureServices method.


public void ConfigureServices(IServiceCollection services)



   services.AddDistributedRedisCache(option =>


      option.Configuration = Configuration.GetConnectionString("RedisConnection");




“RedisConnection” is nothing but the key for the ConnectionString required for connecting to the Azure Redis Cache, which in case of ASP.NET Core 2.0 is defined at the appsettings.json file. The value for this ConnectionString can be easily fetched from the Azure Portal by opening the instance of the RedisCache already created, navigating to “Access Keys”, and copying the Primary Connection String (refer to Figure 3).


Fig 2 : Location of Startup.cs and appsettings.json inside a sample ASP.NET Core 2.0 WebApp Project


Figure 3: Connection String details for Redis Cache from the Azure Portal


AddDistributedRedisCache actually adds an interface automatically to your service collection called “IDistributedCache” that you can then use to set/retrieve values using your RedisCache instance. As can be seen in the code examples below, one can use controller dependency injection to get this anywhere in the app. So say there’s a Controller class called HomeController, and RedisCache (which has been already configured in Startup.cs) needs to be used there. The code should look like:


public class HomeController : Controller


   private readonly IDistributedCache _distributedCache;

   public HomeController(IDistributedCache distributedCache)


     _distributedCache = distributedCache;



   public async Task Get()


   var cacheKey = "TheTime";

   var existingTime = _distributedCache.GetString(cacheKey);

   if (!string.IsNullOrEmpty(existingTime))


     return "Fetched from cache : " + existingTime;




    existingTime = DateTime.UtcNow.ToString();

    _distributedCache.SetString(cacheKey, existingTime);

    return "Added to cache : " + existingTime;





The first time we view this page, there is nothing in the cache so we are given a new time. And it’s added to the cache. If we refresh the page, we then end up with the time that was cached. So, for example:

Added to cache: 5/01/2017 1:27:24 AM

*refresh page a few seconds later*

Fetched from cache: 5/01/2017 1:27:24 AM

A couple more notes.

  • IDistributedCache has async methods. You should use these in all scenarios that are possible.
  • IDistributedCache allows for storing either string values or byte values. If you want to serialize an object and store the entire thing, you can either serialize it to bytes and save it as bytes, or serialize it to JSON and save it as a string if you prefer.
  • Erstellt am .
Copyright by Orange Networks GmbH