Data Caching with File Dependency in Asp.net

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 binded 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 currencies data, this file is binded 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 Britian 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.

Advertisements

One thought on “Data Caching with File Dependency in Asp.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s