"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Wednesday, January 23, 2013

I started loving Cache :) Part 1

Hi *.*,

When it comes to build a high-performance and scalable ASP.Net Web applications Caching is inevitable. It has the ability to store objects whether its data object or pages or controls or even parts of a page. The plan is to keep them in memory while initially requested. You can store them in web-server or in proxy-server or in browser, your choice. Using this feature prevents you recreating information while it already satisfied in previous request. The motive is reusing the objects when they are needed.

Basically ASP.Net provides two types of caching. Output caching and another one is traditional application data caching. There are few good practices to cache your data. When the data is going to be used more than once it’s a candidate for caching and if data is general rather than specific to a given request or user, it’s a great candidate for the cache. But the thing sometimes developers often overlook is that they can cache too much and it causes out of memory exception. Therefore, caching should be bounded. In my experience ASP.NET out-of-memory errors caused by overcaching, especially of large datasets.

So how do you use this? – If your components are running within an ASP.NET application, you simply need to include a reference to System.Web.dll in your application project. When you need access to the Cache, use the HttpRuntime.Cache property (the same object is also accessible through Page.Cache and HttpContext.Cache).

By default Location property sets to "Any" which means this stores the output cache in the client’s browser, on the proxy server (or any other server) that participates in the request, or on the server where the request is processed.

The choices you have are "Server", "Client", "Downstream" or "None".

And the programmatic approach for Output cache is

TimeSpan freshness = new TimeSpan(0,0,0,60);
DateTime now = DateTime.Now;
Response.Cache.SetExpires(now.Add(freshness));
Response.Cache.SetMaxAge(freshness);
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetValidUntilExpires(true);

 Here is an example. Suppose your target is to fill a DataGrid, if the DataSet is already been stored you’ll ignore re-filling it, otherwise you’ll fill the DataSet and will cache it.



DataView Source;
        // Retrieve the DataView object from Cache. If not exist, then add DataView object to the Cache.
        Source = (DataView)Cache["MyDataSet"];
        if (Source == null)
        {
            SqlConnection myConnection = new SqlConnection("Server=ServerName; database=Pubs; user id=UID; password=PWD;");
            SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);

            DataSet ds = new DataSet();
            myCommand.Fill(ds, "Authors");

            Source = new DataView(ds.Tables["Authors"]);
            Cache["MyDataSet"] = Source;
            CacheMsg.Text = "Dataset created explicitly";
        }
        else
        {
            CacheMsg.Text = "Dataset retrieved from cache";
        }

        // Binding the DataView object with DataGrid.
        DataGrid1.DataSource = Source;
        DataGrid1.DataBind();

 Now u got y i titled this post so...
 

If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: