Simple way to use cache in ASP.Net/C#
Caching in ASP.Net is a very useful mechanism for developers in the optimisation part. Once we developed a website with ASP.Net, next importatnt thing that we need to consider is performance of the site. So its better to write optimised codes and techniques from the beginning of code writing. But the caching mechanism we can implemented easily even after completed the development.
How we can optimise a website using cache in ASP.Net
In some cases, we are displaying same results by fetching data from databases or other servers or static data to the user. As a optimisation part, we can avoid this same data visualisation which will take very long time. For achieving this, we can store this static data or dynamic data by same request Into webserver cache memory so that webserver can easily retrieve this data for the next time.
Types of data that we can use cache mechanisam
Cache in Static Pages : All static pages in the aspx application like ContactUs, AboutUs can be stored in cache and can be reuse once the user request again this pages. In order to cache this pages, we just include following code to the top of the static aspx pages that we are planning to apply cache.
<%@OutputCache Duration="60" VaryByParam="none" %>
The Duration parameter specifies how long, in seconds, the HTML output of the Web page should be held in the cache. When the duration expires, the cache becomes invalid and, with the next visit, the cached content is flushed, the ASP.NET Web page’s HTML dynamically generated, and the cache repopulated with this HTML. The VaryByParam parameter is used to indicate whether any GET (QueryString) or POST (via a form submit with method=”POST”) parameters should be used in varying what gets cached. In other words, multiple versions of a page can be cached if the output used to generate the page is different for different values passed in via either a GET or POST.
Cache in partial pages/usercontrol: Suppose we are including some usercontrols and a usercontrol’s data is static then we can apply cache mechanism for this portion. In order to achieve this , we can just include the following code in to the top of the usercontrol that we are planning to cache.
The problem in the usercontrol cache, we are not able to get events dispatched from user controls. If we are not dispatch any events from usercontrol to aspx page, we can use cache for the usercontrol.
Cache in dynamic pages : We can use same technique also for dynamic pages that having dynamic data according to the qery string. Eg: If a itemDetails page in a shopping site display the selected item details by getting item id from query string (mydomain.com/item.aspx?itemid=2). Here also we can cache this dynamic data using following code.
<%@OutputCache Duration="60" VaryByParam="itemid" %>
Once the query string itemid changing cache autamatically expired, otherwise page will display from cache. Cache for dynamic data : If we are listing all items in a database to a page, we can store this results of data into the cache and can be reuse for the second time. Same as we can store a variable, dataset, object, datatable etc.. into cache and reuse for the second time for the same request.
Cache[“myString”] = “String to Cache”; (Assign a string to cache) String cacheString = Cache[“myString”]; (Retrieve string from cache) DataTable itemsDatatable = getItems(); Cache[“dtItems”] = itemsDatatable; (Store datatable to cache) itemsDatatable = (DataTable)Cache[“dtItems”]; (Retrieve datatable from cache)