Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Cache dependency on files in ASP.NET - Part 129

Suggested Videos
Part 126 - Caching application data in asp.net
Part 127 - Different ways to cache application data in asp.net
Part 128 - AbsoluteExpiration, SlidingExpiration, and CacheItemPriority

In this video, we will discuss about cache dependency on files in asp.net. Let us understand this with an example. Create an asp.net web application. Add a folder with name = "Data" to your web application. Right click on the "Data" folder and add an xml file with name = "Countries.xml".



Copy and paste the following xml data into Countries.xml file.
<?xml version="1.0" encoding="utf-8" ?>
<Countries>
  <Country>
    <Id>101</Id>
    <Name>India</Name>
    <Continent>Asia</Continent>
  </Country>
  <Country>
    <Id>102</Id>
    <Name>China</Name>
    <Continent>Asia</Continent>
  </Country>
  <Country>
    <Id>103</Id>
    <Name>Frnace</Name>
    <Continent>Europe</Continent>
  </Country>
  <Country>
    <Id>104</Id>
    <Name>United Kingdom</Name>
    <Continent>Europe</Continent>
  </Country>
  <Country>
    <Id>105</Id>
    <Name>United State of America</Name>
    <Continent>North America</Continent>
  </Country>
</Countries>



WebForm1.aspx HTML:
<div style="font-family: Arial">
    <asp:Button ID="btnGetCountries" runat="server" Text="Get Countries" 
        OnClick="btnGetCountries_Click" />
    <br />
    <br />
    <asp:GridView ID="gvCountries" runat="server">
    </asp:GridView>
    <br />
    <asp:Label ID="lblMessage" Font-Bold="true" runat="server"></asp:Label>
</div>

The following code reads xml data from "Countries.xml" file into dataset which is then cached. Notice, that we are using "cache" object's "Insert()" method to cache the DataSet. Since we are passing "null" as the argument for "CacheDependency" parameter of the "Insert()" method, when the data in "Countries.xml" file changes, the data in the cache is unaffected.
protected void btnGetCountries_Click(object sender, EventArgs e)
{
    // Check if the data is already cached
    if (Cache["CountriesData"] != null)
    {
        // If data is cached, retrieve data from Cache
        DataSet ds = (DataSet)Cache["CountriesData"];
        // Set the dataset as the datasource
        gvCountries.DataSource = ds;
        gvCountries.DataBind();
        // Retrieve the total rows count
        lblMessage.Text = ds.Tables[0].Rows.Count.ToString() + " rows retrieved from cache.";
    }
    // If the data is not cached
    else
    {
        // Get data from xml file
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/Data/Countries.xml"));

        //Cache Countries and set dependency on file
        Cache.Insert("CountriesData", ds, null, DateTime.Now.AddSeconds(20), System.Web.Caching.Cache.NoSlidingExpiration);

        // Set the dataset as the datasource
        gvCountries.DataSource = ds;
        gvCountries.DataBind();
        lblMessage.Text = ds.Tables[0].Rows.Count.ToString() + " rows retrieved from the file.";
    }
}

When the data in Countries.xml file changes, we want the data in the cache to be removed automatically. For this to happen we need to establish a dependency on the xml file as shown below.
Cache.Insert("CountriesData", ds, new CacheDependency(Server.MapPath("~/Data/Countries.xml")), DateTime.Now.AddSeconds(20), System.Web.Caching.Cache.NoSlidingExpiration);

Now run the application. After the dataset is cached, change the xml file and click "Get Countries" button. Notice that the data is now retrieved from file directly, as the dataset is removed from cache.

3 comments:

  1. sir pls post video tutorial for wcf.pls pls

    ReplyDelete
  2. Hi Manish, sure, I will work on wcf video tutorial as soon as I can.

    I have organised all the ASP.NET, C#, and SQL Server video tutorials in to playlists, which can be accessed from here.

    If you want to receive email alerts, when new videos are uploaded, please subscribe to my youtube channel by clicking here.

    May I ask you for a favour. I want these tutorials to be helpful for as many people as possible. Please free to share the link with your friends and family who you think would also benefit from them.

    ReplyDelete
  3. Can we create such dependencies on Data base tables also?

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.