Caching application data in asp net Part 126

  • دعنا نشارك هذا :
ترتيب الدرس : 126
المدة : 00:17:27
التحكم :
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 parts 119 to 125 of the video tutorial, we discussed about
1. Caching webforms
2. Caching multiple responses of webforms
3. Fragment caching using user controls
4. Caching multiple versions of usercontrols

Please watch these videos, from the video tutorial using the link below

In this video we will discuss about caching application data. It is possible to store application data in the web server memory, using the CACHE object, so that the data can be retrieved faster. For example, let us say, we have a stored procedure that takes 5 seconds to execute and return data. We can cache the data returned by this stored procedure with in an web application using the CACHE object, so that, next time when we try to access the data, we can get it from the cache, rather than reprocessing the stored procedure again.

We will be using "tblProducts" table for this demo. If you need the script to create and populate this table, please refer to Part 122, using the link below.

Text version of this video is present at the following link

The following stored procedure takes 5 seconds to execute and return data. We are using WAITFOR DELAY, to introduce artificial query processing time of 5 seconds.
CREATE Procedure spGetProducts
Waitfor Delay '00:00:05'
Selectfrom tblProducts

Copy and paste the following code in WebForm1.aspx.cs. The code is well documented and is self explanatory.
protected void btnGetProducts_Click(object sender, EventArgs e)
DateTime dtStartDateTimeDateTime.Now;
System.Text.StringBuilder sbMessagenew System.Text.StringBuilder();
// Check if the data is already cached
if (Cache["ProductsData"] ! null)
// If data is cached, retrieve data from Cache using the key "ProductsData"
DataSet ds(DataSet)Cache["ProductsData"];
// Set the dataset as the datasource
// Retrieve the total rows count
sbMessage.Append(ds.Tables[0].Rows.Count.ToString() + " rows retrieved from cache.");
// If the data is not cached
// Get the data from the database
DataSet dsGetProductsData();
// Cache the dataset using the key "ProductsData"
// Set the dataset as the datasource
sbMessage.Append(ds.Tables[0].Rows.Count.ToString() + " rows retrieved from database.");
DateTime dtEndDateTimeDateTime.Now;
sbMessage.Append((dtEndDateTime - dtStartDateTime).Seconds.ToString() + " Seconds Load Time");

private DataSet GetProductsData()
string CSConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection connew SqlConnection(CS);
SqlDataAdapter danew SqlDataAdapter("spGetProducts", con);

DataSet dsProductsnew DataSet();

return dsProducts;

In this video, we discussed about storing application data in cache, using direct assignment. That is using a key and assiging value to it, as shown below.

In our next video, we will discuss about all the other options that are available, to store data in the Cache object.

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

Sep 23, 2020 07:32:26