Wednesday, September 23, 2015

Cache dependency on files in asp.net

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.


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

0 comments:

Post a Comment