Azure Redis Cache To MVC Application

Learn how to use Azure Cache for Redis, a secure data cache and messaging broker that provides high throughput and low-latency access to data for applications.

Introduction 

In this blog, I explain how to implement Azure Redis cache step by step into your new MVC application or existing MVC application    
 
Implementing Azure Redis Cache To MVC Application
  • Azure Redis Cache in Azure Portal

Step 1 - Create Azure Redis Cache in Azure Portal

To use Azure Redis Cache Log into your Azure portal with your UserId and Password. Once you are logged in it will forward you to the Dashboard.

From the Search menu, search for Database in the Portal as shown below.

Azure Redis Cache To MVC Application 

Once you select the Database option, under that you can search for Redis Cache as shown in the below picture.

Azure Redis Cache To MVC Application

Select the Redis Cache and create a new Redis Cache in the Portal.

While creating Redis cache on the basis of Pricing tier, you will find 3 options,

  • Basic – Single node. Multiple sizes up to 53 GB.
  • Standard – Two-node Primary/Replica. Multiple sizes up to 53 GB. 99.9% SLA.
  • Premium – Two-node Primary/Replica with up to 10 shards. Multiple sizes from 6 GB to 530 GB. All Standard tier features and more including support for Redis cluster, Redis persistence, and Azure Virtual Network. 99.9% SLA.

Here I have shown the Premium One with Redis Cluster in the below image.

Azure Redis Cache To MVC Application

Once you create your Cache just pin it to the Dashboard so that it will be easy to work on it. So in this way you are able to create a Redis Cache in the Azure Portal. 

  • List of files will be modified in out Project

Step 2 - First, we need to install one NuGet package (reference) in our project

  • Install-Package Microsoft.Web.RedisSessionStateProvider

    Show the latest NuGet package from here.

  • Changes in the web.config file

Now go to the Azure Portal and check the access key for the Redis Cache
 
Azure Redis Cache To MVC Application

Comment your session state and put in this code.

  1. <sessionState mode="Custom" customProvider="RedisSessionProvider">  
  2.     <providers>  
  3.         <add name="RedisSessionProvider" type="Microsoft.Web.Redis.RedisSessionStateProvider" port="your port" host="yourhostname" accessKey="your access key" ssl="true" />  
  4.     </providers>  
  5. </sessionState>  

Conclustion

So in this blog, I explain how to implement Redis cache into your project using a web.config file with Azure 
and also, you can check your Azure portal to see if this cache is working or not.