Data Caching with File Dependency in


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.


Caching is one of critical ingredient which can increase the performance of your web application. There are three major types of caching available in

  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"?>
    <Currency ID="1">
      <Text>US Dollars</Text>
    <Currency ID="2">
      <Text>Great Britian Pound</Text>
    <Currency ID="3">
  <Currency ID="4">
    <Text>Arab Emirates Dhirams</Text>

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;
                Cache.Insert("DropDownData", Queries,new System.Web.Caching.CacheDependency(Server.MapPath("~/Currencies.xml")));
                DropDownList1.DataTextField = "Text";
                DropDownList1.DataValueField = "Value";
                this.DropDownList1.DataSource = Cache["DropDownData"];


The above listing of code utilizes the Cache object of .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.


Caching should be used wherever it is possible. I hope this post helps the community developers. Happy Learning … Cheers.


One thought on “Data Caching with File Dependency in

Leave a Reply

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

You are commenting using your 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