Azure Cache for Redis Implementation in .Net Core

Azure Cache for Redis Implementation in .Net Core


Redis is an open source data structure store that is used as a database or cache or message broker. Redis cache stores data as in-memory as key-value pair. It is distributed cache.

Azure Redis cache is cloud caching platform based on open source Redis and provides by Microsoft Azure. Application performance will increased by story frequently access application's data into in-memory storage and Azure provides access to a secure and dedicated Redis cache. Azure Redis cache stores data in-memory and that is fully managed. It supports data type as strings, hashes, lists and sets.

If you haven't read the previous article Azure Cache for Redis, please read that because that with guide you to Azure portal for creating Azure Cache for Redis on cloud. Let's start to implement Azure cache for Redis in .NET Core application.

Here I am going to create a .NET Core web application. Before proceeding, we have to install few nuget packages. Nuget package Microsoft.Extensions.Caching.Redis used for implementing distributed cache using Redis.

  • Microsoft.Extensions.Caching.StackExchangeRedis - used for implementing distributed cache using Redis.
  • Microsoft.EntityFrameworkCore - used for Entity Framework data access.
  • Microsoft.EntityFrameworkCore.SqlServer - used for Microsoft SQL Server database provider for Entity Framework Core.
  • Newtonsoft.Json - Used for serializing & deserializing the object.

Redis Cache

Now go to Azure portal and open your created resurce Azure cache for Redis, click on Access keys section that will show you keys & connection strings.

Redis Cache

Copy the one of the connection string & paste inside appSettings.json's ConnectionStrings section of .Net Core application. Named connection string as RedisConnection.

Redis Cache

Open Startup.cs file and go to ConfigurationServices method and add service for redis cache AddStackExchangeRedisCache by using the RedisConnection connection string.

Redis Cache

Finally you are ready to use cache by using IDistributedCache. But before we proceed to implemetation of cache let's understand the logic of implementing cache for redis by below flowchart.

Redis Cache

So, we are going to write our logic like below steps:

  • 1. Get employee list from cache by 'employees' key
  • 2. Check cache stored data or not
    • 2(a). stored in cache
      • => Now deserialize the cache data into the object in which it was stored
    • 2(b). not stored in cache
      • => Get employee list from database
      • =>Stored the data into cache as serialize object

IDistributedCache interface is provides implementation of distributed cache like Redis, NCache, SQL Server & Memory cache.

  • Get method will give you stored cache data as byte array by passing key.
  • Set method will store your data in cache by passing key & data as byte array.
  • GetString method will give you stored cache data as string by passing key.
  • SetString method will store your data as string in cache by passing key & string data.
  • Remove method will remove the specifice cached data by passing key.
  • Refresh method will refresh the specific cached data in cache by passing key.

Redis Cache