Data Caching with File Dependency in Asp.net

Goldytech
Posted by in ASP.NET category on for Beginner level | Views : 15136 red flag

In this article I teach you how you can increase the performance of your web application using Data Caching
INTRODUCTION
I am in great pain while I am writing this post. Working as a consultant for the IT companies one of my job role is to do the technical interviews of the candidates. Generally my questions are the scenarios based, I ask the candidate what would be your approach in this scenario. One of the common question I ask that assume you have the dropdown list which is bound to some xml file for its values. How would you ensure that it remains sync with the underlying data source and does not hit the source with every request? Well the answer is simple you need to bind the dropdown ,using Data Caching with file dependency. Almost 70% of the candidates didn't answer to my satisfactory level. Hence I thought it was high time that I do a blog post of it which explains the solution.
DATA CACHING
Caching is one of critical ingredient which can increase the performance of your asp.net web application. There are three major types of caching available in asp.net
  1. Output Cache
  2. Partial or Fragment Caching
  3. Data Caching
I will give you the walkthrough of the third one. The scenario remains the same. We have an xml file which has currency data, this file is bound to your dropdownlist.Let us look at the sample data.
<?xml version="1.0" encoding="utf-8"?> 
<Currencies>
    <Currency ID="1">
      <Text>US Dollars</Text>
      <Value>USD</Value>
    </Currency>
    <Currency ID="2">
      <Text>Great Britain Pound</Text>
      <Value>GBP</Value>
    </Currency>
    <Currency ID="3">
      <Text>Euro</Text>
      <Value>EUR</Value>
    </Currency>
  <Currency ID="4">
    <Text>Arab Emirates Dhirams</Text>
    <Value>AED</Value>
  </Currency>
</Currencies>

Now let us look at the code which binds the dropdownlist based on the above data.
private void BindDropDown() 
        {
            if (Cache["DropDownData"] == null)
            {
                XDocument xDoc = new XDocument();
                xDoc = XDocument.Load(Server.MapPath("~/Currencies.xml"));
                var Queries = from XElem in xDoc.Descendants("Currency")
                              select new { Text = XElem.Element("Text").Value, Value = XElem.Element("Value").Value };
                DropDownList1.DataTextField = "Text";
                DropDownList1.DataValueField = "Value";
                this.DropDownList1.DataSource = Queries;
                this.DropDownList1.DataBind();
                Cache.Insert("DropDownData", Queries,new System.Web.Caching.CacheDependency(Server.MapPath("~/Currencies.xml")));
            }
            else
            {
                DropDownList1.DataTextField = "Text";
                DropDownList1.DataValueField = "Value";
                this.DropDownList1.DataSource = Cache["DropDownData"];
                this.DropDownList1.DataBind();
            }
        }
The above listing of code utilizes the Cache object of asp.net .Using Linq to XML I am doing the query and storing the query result in the Cache object. The Cache object stores the information in KeyValue Pair format. The third paramter of Insert method is CacheDependency and here I specify my XML file path. So now whenever my XML file is changed ,that is new currencies or added or existing currencies are edited or deleted. The cache will be invalidated and it will freshly read the data from the XML file. But till then if it finds the valid data in cache it will read the data from the cache (IIS Memory) , which will be more faster than doing physical file read operation with every request.
CLOSURE
Caching should be used wherever it is possible. I hope this post helps the community developers. Happy Learning ... Cheers.
Page copy protected against web site content infringement by Copyscape

About the Author

Goldytech
Full Name: Muhammad Afzal Qureshi
Member Level: Bronze
Member Status: Member
Member Since: 8/4/2009 10:58:17 PM
Country: India

http://goldytech.wordpress.com
Hello Everyone Myself Muhammad Afzal , aka GoldyTech. Thats my pen name. I reside in India and work as a solution Architect on .NET platform. I hope you must have enjoyed reading my blog. Please leave your comments or suggestions good or bad and help me to improve

Login to vote for this post.

Comments or Responses

Posted by: Wes on: 2/3/2011 | Points: 25
its true that caching can enhance the performance of an asp.net app. frequent trips to database can effect the performance of the app so caching can be a good option. the build in cache in asp.net is in process in nature so it cant fit in a multi-server environment. performance and scalability can be some and frequent issues in such cases. the use of a distributed cache can be a better option in these sorts of situations as the cache is distributed over multiple server so it not only enhance the performance of the app but it also provides the scalable data every time. there are some third party distributed caching solution are available for free like this one
http://www.alachisoft.com/ncache/ncache_express.html

cheers,

Login to post response

Comment using Facebook(Author doesn't get notification)