Subscribe RSS 2.0 cshandler feeds

Monday, 4 July 2011

Partial Caching using dependencies and TimeSliding Expiration


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

No comments:

Post a Comment