Frequently Asked Interview Questions

ASP.NET Caching FAQS

ASP.NET Caching FAQS page 1
Caching is a way to store the frequently used data into the server memory which can be retrieved very quickly. And so provides both scalability and performance.
ASP.NET supports three types of caching for Web-based applications:
  1. Page Level Caching (called Output Caching)
  2. Page Fragment Caching (often called Partial-Page Output Caching)
  3. Programmatic or Data Caching
Page level, or output caching, caches the HTML output of dynamic requests to ASP.NET Web pages.
To cache a page's output, we need to specify an @OutputCache directive at the top of the page. The syntax is as shown below:
<%@ OutputCache Duration="5" 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.
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.
Example:
<%@ OutputCache Duration="45" VaryByParam="id" %> --> It cache Pages varied by parameter id for 45 seconds.
<%@ OutputCache Duration="45" VaryByParam="id;code" --> It cache Pages varied by parameters id or code for 45 seconds.
<%@ OutputCache Duration="45" VaryByParam="*" --> The cached content is varied for all parameters passed through the querystring.
<%@ OutputCache Duration="45" VaryByParam="none" --> No page cache for multiple versions
Yes, you can cache a web form using the Response object’s Cache property, which returns an HttpCachePolicy object for the response. The HttpCachePolicy object provides members that are similar to the OutputCache directive’s attributes.
Instead of caching the complete page, some portion of the rendered html page is cached. E.g.: User Control used into the page is cached and so it doesn’t get loaded every time the page is rendered.
We can store objects in memory and use them across various pages in our application. This feature is implemented using the Cache class.
Cache["name"]=“XXXXXX"; --> string value can be inserted into the cache
You can retrieve the value of a cache item stored in the servers memory through the item’s key, just as you do with the Application and Session objects. Because cached items might be removed from memory, you should always check for their existence before attempting to retrieve their value
private void Button1_Click(object sender, EventArgs e)
{
if (Cache["ChachedItem"] == null)
{
Lable1.Text = "Cached Item not found.";
}
else
{
Lable1.Text = Cache["ChachedItem"].ToString();
}
}

Most Visited Pages

Home | Site Index | Contact Us