Cache dependency on files in asp net Part 129

  • دعنا نشارك هذا :
ترتيب الدرس : 129
المدة : 00:10:40
التحكم :
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.


All ASP .NET Text Articles

All ASP .NET Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this video, we will discuss about cache dependency on files in Let us understand this with an example. Create an 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".

Countries.xml file and the HTML of webform1.aspx can be found on my blog at the following link

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
// Retrieve the total rows count
lblMessage.Textds.Tables[0].Rows.Count.ToString() + " rows retrieved from cache.";
// If the data is not cached
// Get data from xml file
DataSet dsnew DataSet();

//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
lblMessage.Textds.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.

سيتم تفعيل التعليقات قريبا

Sep 23, 2020 07:32:26