Azure Rest API

How To Use Azure Redis Cache In C#

What is Redis Cache

Azure Cache for Redis provides you access to a secure, dedicated Redis cache. Azure Cache for Redis is managed by Microsoft, hosted within Azure, and accessible to any application within or outside of Azure, key-value database, commonly referred to as a data structure server.

One of the key differences between Redis and other key-value databases is Redis’s ability to store and manipulate high-level data types. These data types are fundamental data structures (lists, maps, sets, and sorted sets) that most developers are familiar with. Redis’s exceptional performance, simplicity, and atomic manipulation of data structures lends itself to solving problems that are difficult or perform poorly when implemented with traditional relational databases

The benefits of using Azure Redis Cache are:

  • High Performance due to the low latency and high throughput capacity of Redis engine. So, as the load on the application increases, it continues to remain available. And since the cache layer is separate, the data tier can be scaled independently.
  • Supports a variety of programming languages like C, C#, Java, PHP, VCL, Fancy, Io, Lua, Perl, Rust, Go, Scala, Matlab, Python.
  • Leverages Redis authentication and supports SSL for securing cache/client communication.
  • Makes Redis cache highly available by having primary and secondary replica cache.
  • If the primary cache fails, the secondary replica is promoted to primary and a new replica is created and populated. This process is automatic and is managed by Microsoft Azure.
  • Monitoring the health and performance of the cache, configuring alerts when a certain threshold is crossed is easy and can be done through the Azure Management Portal.

How to create azure redis cache project


Enable /Disable SSL

To retrieve the access keys and host name

Create Project




Goto Package Manage Console-Execute below commands

Install-Package StackExchange.Redis -Version 2.0.601

Goto : AppSetting

Add below keys and set the values copied from azure access key

<add key="CacheConnection" value="AadicsDemoCache.redis***************************” />

Create Cache generic instance caller class

public static IDatabase GetAzureRedis()
           var lazyConnection = new Lazy<ConnectionMultiplexer>(() =>

                string cacheConnection = ConfigurationManager.AppSettings["CacheConnection"].ToString();

                return ConnectionMultiplexer.Connect(cacheConnection);

            // Connection refers to a property that returns a ConnectionMultiplexer

           // as shown in the previous example.
           IDatabase cache = null;
           cache = lazyConnection.Value.GetDatabase();
           return cache;

For Get Verbose rewrite the code as below

public Employee Get(int id)

            //Instantiate the cache objects.
           IDatabase cache = AzureRedis.GetAzureRedis();
           Employee employeeInfo = null;
           //Get from cache
               employeeInfo = JsonConvert.DeserializeObject<Employee>(cache.StringGet(id.ToString()));
               employeeInfo.IsCache = true;

           catch (Exception) {}
           if (employeeInfo == null)

            { //Write your logic to get from database
               employeeInfo = new Employee() { Id = id.ToString(), Name = "Rupesh Rupak", Age = 30, IsCache = false };

                //Put into Cache

                   cache.StringSet(employeeInfo.Id, JsonConvert.SerializeObject(employeeInfo));
               catch (Exception) { }


           return employeeInfo;

Entity Class

public class Employee

        public string Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public Boolean IsCache { get; set; }

Now run the application and using post man check the get operation

You Might Also Like...

No Comments

    Leave a Reply