SIGN UP MEMBER LOGIN:    
ARTICLE

Partial Caching using dependencies and TimeSliding Expiration

Posted by Amit Choudhary Articles | ASP.NET Programming March 23, 2010
This article is intent to show you how to use Page.Cache class of System.Web.Caching.Cache with partial caching.
Reader Level:

In my previous  article "Page level caching with SqlDependency" you have learned how to create SqlDependency with page level Caching.

This article is intent to show you how to use Page.Cache class of System.Web.Caching.Cache with partial caching.

1. Partial Caching with no Dependency and Sliding Expiration with time

Partial caching means you are not going to cache the complete page only the part of page say userControl or some other control like Grid with large amount of data.

For eg: you have Grid to Bind with Dataset. So every time the page get refreshed or Grid get rebind to the data the data will come from database server every time. To prevent this round trip we can cache the DataSet.

Use the Cache object to insert the DataSet object into memory. Here is the Cache.Insert() static method explained with its options used. Here we use Time expiration with sliding expiration.

Cache.Insert(
                /*name of cache object*/"key",
                /*Item to be saved in Cache*/ds,
                /*Dependency */null,
                /*Expiration Behavior*/
    System.Web.Caching.Cache.NoAbsoluteExpiration,
               /*Time span to expire the cache*/
               new System.TimeSpan(0,0,30)
                );

Retrieve the Cache object When a request come for DataSet.

DataSet  ds = (DataSet)Cache["key"]; // Check if Dataset was saved into Cache

Now check if (ds == nullthen perform your query to DataBase fetch the refresh data and add it to cache again.

Here is the complete code for Caching a DataSet into database.

protected void Page_Load(object sender, EventArgs e)
{
    DataSet ds = null;
    //Retrieve the dataset using key from Cache.
    ds = (DataSet)Cache["key"];
    // Check if Dataset was saved into Cache
    if (ds == null)
    {
        SqlConnection cn = new SqlConnection(@"Data Source=NODE007\SQLSERVER2005;Initial Catalog=MenuDb;Integrated Security=True");
        cn.Open();
        SqlCommand cmd = new SqlCommand("select Text from Menu", cn);
        SqlDataAdapter adp = new SqlDataAdapter("select Text from Menu", cn);
        ds = new DataSet();
        adp.Fill(ds);
        //Insert the fresh dataset into cache
Cache.Insert("key",ds,null,System.Web.Caching.Cache.NoAbsoluteExpiration,new System.TimeSpan(0,0,30));
    }
    GridView1.DataSource = ds;
    GridView1.DataBind();
}

2. Partial Caching with SqlCacheDependency object used with Cache Class

Before using SqlCacheDependency object you have to configure the SqlDependency in your web.config.

Here is how to configure SqlDependency in your web.config

//Insert the DataSet with SqlDependency object
System.Web.Caching.SqlCacheDependency SD = new System.Web.Caching.SqlCacheDependency("_defaultCon","ProductCategories");
Cache.Insert("key", ds, SD);

You can check your performance of page by Turning on the Trace.

Cheers

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks for uploading such nice tutorials. I want to know how to create Chatting application in browser? using simple asp.net concept without using socket & remoting concept.Please replye as you possible.

Posted by sagar Bhosale Feb 01, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor