Arthur Wang's Blog
Follow me on
  • My General Blog
  • Software Development
    • Latest Articles on Software Development
    • Complete Resources for Developers >
      • Tutorial Information for Developers
      • .NET Developer Blogs
      • Developer Journals and Magazines
      • Developer's Tools
      • Database Development
      • ​Developer Conference and Events
  • Tech
    • Latest Articles on Technology Development
  • Health
  • Money
  • Services
    • Modern Website Design
    • Web Maintenance of Existing Websites Service
    • Corporate Business Consulting Service
  • About
  • Contact
  • Art
  • 中文部落格

How to Implement Azure Redis Cache using .NET C# in Your Web Application and When to Upgrade Your Azure Service Plan?

2/23/2016

0 Comments

 
How to Implement Azure Redis Cache using .NET C# in Your Web Application
​Redis Cache is like bringing your server's memory to the cloud and you can bring more information out from your database and keep it there as long as you want. No pressure given from the garbage collector to release those memory in case of memory shortage problem. In addition, the more information you bring from the database means that less requests from the database as well.  Overall, Redis Cache gives you better performance in web server and database server at the same time.  Please note that since Redis is a remote type of data structure, it is indeed slower than storing the data in local memory.  So you still need to keep login details and site data that is going to be accessed in every page in the local memory.
 
In this article, I will demonstrate how to use Redis Cache service provided by Microsoft Azure.  First part is about how to setup the Redis Cache from Azure. In the second part, I'll be showing you on how to setup and access the cache by using Visual Studio with C#.  We will see how to determine whether or not you need to upgrade the Redis Cache in the last part.

How to setup the Redis Cache from Azure?

Go to http://portal.azure.com  and sign in to the Azure portal. Select the "+" sign on the top left. 
Then select "Data + Storage", a list of data storage related services will be shown on the right.
Picture
​Select "Redis Cache" from the list.​
Picture
A entry form comes out for you to enter your DNS name in the url: yourDNSname.redis.cache.windows.net.  Make sure the Location is selected to the same location where your web application located. 
For the pricing tier list options, the minimum is C0 Basic Plan with 250 MB cache and up to 256 connections.  You can select this to do the test. Or you can click on "View All" link to see all available plans.  Finally, click on the "Create" button.
Picture
Picture
Please wait a few minutes to have Azure create the Redis Cache for you. You will see something like this when it's done.
Picture
You can find your access keys in the above screen.  You need these keys to access your Redis Cache via your application.  I will now show you how to implement this Redis Cache into your web application.

How to setup and access the Azure Redis cache by using Visual Studio with C#?

You can download my source code in GitHub: https://github.com/Arthurwiz/DemoRedisCache
 
I'll be using Visual Studio program with C# to do the demo project.  I create a new project with ASP.NET Web Application template with .NET Framework 4.5, which I named it, DemoRedisCache.  In the project, I created 3 folders: Common, Managers, Services for a general multi-tier architecture. 
Picture
In this empty project, I highlight the project name from the Solution Explorer, and select "Manage NuGet Packages for Solution".  When the NuGet windows comes out, select the "Online" source from the left, and then type "StackExchange.Redis" in the top right search box.  In this example, I'll create a generic list called List<Fruit> as my data type to demonstrate the process of saving and retrieving the data from the Azure Redis Cache service.  Please note that the StackExchange.Redis client requires .NET Framework 4 or higher.  In our example, we use 4.5.
Picture
Picture
In the Web.config file, I have these keys.
 <appSettings>
  <add key="CacheEndpoint" value="yourDNSname.redis.cache.windows.net" />
  <add key="CachePassword" value="yourRedisCachePassword_here" />
  <add key="CacheSyncTimeout" value="3000" />
  <add key="CacheConnectTimeout" value="4000" />
  </appSettings>

-- Fruit.cs
    public class Fruit
    {
        public int FruitID { get; set; }
        public string FruitName { get; set; }
        public string Color { get; set; }
    }
 
-- RedisCache.cs
 
In the Services folder, I have one file.
-- CacheService.cs
 
In the Managers folder, I have one file.
-- CacheManager.cs
 
In the root, I have a start up webpage that I use it to demo the cache.
-- TestCache.Page.aspx

​It will look like this when app runs.
Picture

How do you know when do you need to upgrade your Redis Cache plan?

There are many reasons that you might need to consider to upgrade your plan, such as the limit on the number of connections you can make to your cache, the cache size and the speed of calling the cache, and finally the bandwidth.  By looking at the Network Bandwidth chart provided by the Azure portal, you can see the flat top when you reach the maximum cache read call KB/s limit. The example shown below is using the Basic C0 plan:
Picture
There are different limits to the plans.  Please see the table below:
Picture
I hope this article is helpful for you.
Read Latest Software Development Articles  Read Latest Tech Articles

Picture
Let's Connect!

Arthur Wang

​Arthur is a software developer at his core and has been developing web applications and leading development projects since 2000.  He enjoys learning new technologies and writing about them.


If you would like to read more articles written by Arthur, please subscribe the weekly newsletter from Uniting Digital since he is also a content contributor for the site.
Subscribe

0 Comments



Leave a Reply.

    Arthur Wang

    @ArthurWangLA
    MCSD App Builder
    MCSD Web Applications
    ​Member of Windows Insider Program & HoloLens Developer Community & Dev Center Insider Program

    Over 17+ years of  experience in web-based software development & management.  Specialized in Microsoft technology with c# language and its architecture design.  MCSD, MCSE, Microsoft Specialist, MCP + Internet, and B.S. from UCLA

    Archives

    August 2018
    March 2018
    January 2017
    December 2016
    May 2016
    April 2016
    March 2016
    February 2016
    April 2014

    Categories

    All
    API
    Arduino
    ASP.NET
    Cognitive
    CSS
    Database
    Deep Learning
    DevOps
    Electronics
    Flexbox
    HTML5
    IoT
    Katana
    Machine Learning
    Management
    .NET
    .NET Core
    Neural Network
    OWIN
    Programming
    Programming Tools
    Recognition
    Security
    SQL Server
    UWP
    Visual Studio
    Web API
    Web Developer

    RSS Feed

    Latest Articles

© 2014-2020 ArthurWiz.com All Rights reserved. | Home | About |
Protected by Copyscape