Azure Redis Cache using C#

Azure Redis Cache
Share this

Azure Redis Cache from Microsoft is a caching engine which is an in memory database. It is based on open source Redis Cache. Azure Redis Cache is accessible from any application within Microsoft Azure. This gives you access to a secured and dedicated cache managed by Microsoft. You can use Azure Redis Cache in .Net, ASP.Net, Java, Node.js, and Python.

In this article, I will demonstrate how you can create Azure Redis Cache by creating a C# console application which will store and retrieve data from the cache

Step 1 : Create New Azure Redis Cache

Go to Azure Portal, click on ‘Create a Resource’ on top left, search for ‘Redis Cache’. After you get the option ‘Redis Cache’, select it and click ‘Create’ button as shown in the follows.

Azure Redis Cache HomePage

This will open the ‘New Redis Cache’ blade as shown in follows.

Create New Azure Redis Cache

Enter a name for your cache under DNS name. I have chosen my cache name as ‘sudipta’, hence the DNS for the cache is

Select your Azure Subscription. Create a new resource group or choose an existing resource group. I have chosen to create a new resource group named rediscaches.

Select Location for your cache. I have chosen the location as Central India.

Next choose the pricing tier depending upon your subscription, size of the cache needed and replication strategy. I have chosen the pricing tier as Standard C1 (1 GB Cache, Replication).

The last option is if you want to unblock port 6379 (not SSL encrypted) [which I won’t recommend unless you have a good enough reason for doing so] as shown follows.

Azure Redis Cache Unblock Port 6379 SSL

After the deployment is successful, click on ‘Go to Resource’ from the ‘Notifications’ tab or open the Azure Redis Caches option and navigate to the newly created Azure Redis Cache as shown follows.

Azure Redis Cache Sudipta

Under SETTINGS, click on Access keys as shown follows.

Azure Redis Cache Access Keys

Copy the Primary connection string (StackExchange.Redis) which is used in the C# application code to connect to our newly created Azure Redis Cache in the next step.

Step 2 : Create New C# Console Application for Interacting with Azure Redis Cache

Open Visual Studio and create a new C# console application. I am using Visual Studio 2017. Install the following NuGet packages:-

  • StackExchange.Redis
  • Newtonsoft.Json

Use the following code which is explained in detail after the code snippet.

In the above code, I have created a class named Product with three properties – Name, Category and Price.

The first line of Main() method creates an object of StackExchange.Redis.ConnectionMultiplexer class and calls the Connect method by passing the Azure Redis Cache connection string.

Next, an object of type IDatabase is created which calls the GetDatabase() method of connection object to establish a connection to Azure Redis Cache.

Then I have created two objects product1 and product2 of Product class and assign data to the class properties.

Next I have serialized these two objects into string variables serializedProduct1 and serializedProduct2. The objects are needed to be serialized so that they can be stored in Azure Redis Cache.

After this, both the serialized objects are stored in AzureRedisCache using cache object’s StringSet method.

Next, the objects stored in Azure Redis Cache are fetched using StringGet() method as are Deserialized back into Product class type and stored in two objects of Product class named rsProduct1 and rsProduct2 respectively.

In the following lines of code, properties of both the objects from which data has been fetched from Azure Redis Cache are displayed using Console.WriteLine() methods.

Step 3 : Run the C# application and check Azure Redis Cache Activity Log

Finally on running the complete application, the output is shown as follows:

Azure Redis Cache C# Console Application Output

Next, navigate to Azure Portal and open the Azure Redis cache created in Step (1). Click Overview section where you can see the memory usage. This shows that operations are performed by the console application to write data in Azure Redis Cache and read data from the cache.

Azure Redis Cache Memory Usage

Click Activity Log where you can see the various operations performed on the cache like ListKeys. This shows that operations are performed by the console application to write data in Azure Redis Cache and read data from the cache.

Azure Redis Cache Activity Log

This concludes the article of Azure Redis Cache using C#. I hope you liked this article. If you have any comments, questions or suggestions, post your comments using the comments section below this article. I will try to respond at my earliest or somebody else reading the article and your comment will try to respond.

Please subscribe to my blog via email to get updates on the latest articles and also share this article over social networks like Facebook, Twitter etc.

Share this

Leave a Reply