Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4219 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Data Caching with File Dependency in Asp.net

Data Caching with File Dependency in Asp.net

Article posted by Goldytech on 2/18/2010 | Views: 6998 | Category: ASP.NET | Level: Beginner 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.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:11 year(s)
Home page:http://goldytech.wordpress.com
Member since:Tuesday, August 04, 2009
Level:Bronze
Status: [Member]
Biography: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
 Responses
Posted by: Wes | Posted on: 03 Feb 2011 04:52:39 AM | 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,

>> Write Response - Respond to this post and get points
Related Posts

Till now we have not created any screen for data entry. In this tutorial we will create a simple data entry, flourish a simple model and display the model on a simple ASP.NET view.

It is a control used to move to any pages that we need .We can set the page size of the grid using this control. We can sort any column in ascending and descending order by just clicking corresponding heading with this control. Using this control users can move to the first and last page with just a button click. It also displays the total records and total number of pages available with respect to the given page size.

Many a time we come into the situation where we need to pass ProductIds or ItemIds separated by comma from UI to the database. The first thing that comes in our mind is its easy by using IN keyword in the SQL Server but this is not as easy as it looks like. In this article, I have shown how to handle this situation easily in the client side itself.

Listing all the steps to show how to configure the SQL Server so that our .Net application can store and retrieve the Session values in and from SQL server.

While working with XML file, we sometimes need to add/ remove or even modify certain xml attributes, this article shows how to add/remove xml attributes from a XML file.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:47:57 AM