Azure Redis Cache Sample Implementation

facebooktwittergoogle_plusredditpinterestlinkedinmail

Redis

In the previous blog we have learned about different caching mechanism available in Azure. As I mentioned in the previous post, Azure Redis Cache is the recommended caching service in Azure. So this blog explains the detail implementation of Azure Redis Cache in a sample console application.

How to implement Azure Redis Cache ?

Before using Azure, you need an Azure subscription. If you have a subscription then go to the Azure portal else throw some dollars to get subscribed. In Azure portal, navigate to new option, then click on the Data + Storage then you can see the Azure Redis Cache option there.

Azure Redis Cache - Create

Enter the unique DNS name and select your Azure subscription . Select resource group and location. Currently we have 3 price tiers to choose. Price tiers are

  • Basic – Single node, sizes up to 53 GB.
  • Standard – Two-node, sizes up to 53 GB.
  • Premium – Two-node size up to 530 GB. Supports Redis cluster, Redis persistence, and Azure Virtual Network.
  • You will get a nice price and feature comparison from Azure Website.

    Azure Redis Cache - Enter details

    Click on create button. It will a take few minutes to setup our cache.

    Azure Redis Cache - Create - In progress

    After creating cache in Azure, Go to Visual studio and create one project. To access the Redis Cache, we need to add some packages like StackExchange.Redis .

    Azure Redis Cache - Add NuGet package - StackExchange.Redis

    Go to Azure portal and copy the cache’s primary key.

    Azure Redis Cache - Key

    The connection to the Azure Redis Cache is managed by the ConnectionMultiplexer class. ConnectionMultiplexer class has a static connect() method, we have to call that method with endpoints and key (From Azure portal) to establish the connection. Once the connection is established, call the ConnectionMultiplexer.GetDatabase() . Items can be stored in and retrieved from a cache by using the StringSet and StringGet methods.

      static void Main(string[] args)
            {
                ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(
                    "testcache.redis.cache.windows.net,ssl=true,password=lf8KkbxmQLyCLQZ5vdrvrkkUErbJuuHxnEJ/Jyxxx1v+UZJ5iU=");
     
                IDatabase cache = connection.GetDatabase();
                Console.WriteLine("Enter the name");
                string name = Console.ReadLine();
                cache.StringSet("Name", name);
                Console.WriteLine("Enter the blog");
                string blog = Console.ReadLine();
                cache.StringSet("Blog", blog);
                string key1 = cache.StringGet("Name");
                string key2 = cache.StringGet("Blog");
                Console.WriteLine("Name : " + key1);
                Console.WriteLine("Blog : " + key2);
                Console.ReadLine();
     
            }

    In the preceding program, we pass the parameter and get it from cache using StringSet, StringGet method. Go ahead and run the application .

    Azure Redis Cache - output

    We can also store .NET object in Redis cache by serialize the object into Json.

    Azure Redis Cache - Events

    We can see the each and every event on the cache via Azure Poral .

    The following two tabs change content below.

    Tom Mohan

    Tom Mohan is a technologist who loves to code and build. He enjoys working on Microsoft Technologies. Tom specializes in ASP.NET MVC, Web API , Azure, C# ,WPF, SQL etc and holds a Bachelor engineering degree in Computer Science. Certification : MCSD , MCTS