Untitled Page

Thursday, July 30, 2009

ASP.NET interview questions on Caching

Home

ASP.NET Interview Question Categories

1)What is caching?
Ans) High-performance Web applications should be designed with caching in mind. Caching is the technique of storing frequently used items in memory so that they can be accessed more quickly. Caching is important to Web applications because each time a Web form is requested, the host server must process the Web form’s HTML and run Web form code to create a response. By caching the response, all that work is bypassed. Instead, the request is served from the reponse already stored in memory.

Caching an item incurs considerable overhead, so it’s important to choose the items to cache wisely. A Web form is a good candidate for caching if it is frequently used and does not contain data that frequently changes. By storing a Web form in memory, you are effectively freezing that form’s server-side content so that changes to that content do not appear until the cache is refreshed.

2)What directive is used to cache a web form?
Ans) The @OutputCache page directive is used to cache a Web form in the server’s memory.

3)What is the use of duration attribute of @OutputCache page directive?
Ans)The @OutputCache directive’s Duration attribute controls how long the page is cached. For example if you set the duration attribute to 60 seconds, the Web form is cached for 60 seconds.
The first time any user requests the Web form, the server loads the response in memory and retains that response for 60 seconds. Any subsequent requests during that time receive the cached response.
After the cache duration has expired, the next request for the Web form generates a new response, which is then cached for another 60 seconds. Thus the server processes the Web form once every 60 seconds at most.

4)What are the 2 required attributes of the @OutputCache directive?
Ans) The @OutputCache directive has two required attributes:
1. Duration
2. VaryByParam.

5)How do you cache multiple responses from a single Web form?
Ans) The VaryByParam attribute lets you cache multiple responses from a single Web form based on varying HTTP POST or query string parameters. Setting VaryByParam to None caches only one response for the Web form, regardless of the parameters sent.

You can also cache multiple responses from a single Web form using the VaryByHeaders or VaryByCustom attribute.

The VaryByCustom attribute lets you cache different responses based on a custom string. To use VaryByCustom, override the GetVaryByCustomString method in the Web application’s Global.asax file.

6)Is it possible to cache a web form without using @OutputCache directive?
Ans) 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.

7)Give a simple example to show how to cache a web form without using @OutputCache directive?
Ans) For example, the following code caches the Web form’s response for 60 seconds:
private void Page_Load(object sender, System.EventArgs e)
{
// Cache this page
DateTimeLabel.Text = System.DateTime.Now.ToString();
// Set OutputCache Duration. Response.Cache.SetExpires(System.DateTime.Now.AddSeconds(60));
// Set OutputCache VaryByParams.
Response.Cache.VaryByParams["None"] = true;
// Set OutputCache Location.
Response.Cache.SetCacheability(HttpCacheability.Public);
}

The preceding code is equivalent to the following OutputCache directive:
@ OutputCache Duration="5" VaryByParam="None" Location="Any"

8)What is @OutputCache directive’s Location attribute and the HttpCachePolicy object’s SetCacheability property used for?
Ans) The @OutputCache directive’s Location attribute and the HttpCachePolicy object’s SetCacheability property determine where Microsoft ASP.NET stores cached responses. By default, ASP.NET caches responses at any available location that accepts cache items - the client, proxy servers, or the host server. In practice, those locations might or might not allow caching, so you can think of the Location/SetCacheability setting as more of a request than a command.

9)What is HttpCachePolicy object’s SetAllowResponseInBrowserHistory method used for?
Ans) You can override the cache location settings using the HttpCachePolicy object’s SetAllowResponseInBrowserHistory method. Setting that method to True allows the response to be stored in the client’s history folder even if the location setting is None or Server.

10) What is an application object ?
Ans)Application object can be used in situation where we want data to be shared across users globally.
(11)What’s the difference between Cache object and application object ?
Ans)The main difference between the Cache and Application objects is that the Cache object provides cache-specific features, such as dependencies and expiration policies.
(12)How can get access to cache object ?
Ans) The Cache object is defined in the System.Web.Caching namespace. You can get a reference to the Cache object by using the Cache property of the HttpContext class in the System.Web namespace or by using the Cache property of the Page object.
(13)What are dependencies in cache and types of dependencies ?
Ans) When you add an item to the cache, you can define dependency relationships that can force that item to be removed from the cache under specific activities of dependencies.Example if the cache object is dependent on file and when the file data changes you want the cache object to be update. Following are the supported dependency :-
√ File dependency :- Allows you to invalidate a specific cache item when a disk
based file or files change.
√ Time-based expiration :- Allows you to invalidate a specific cache item
depending on predefined time.
√ Key dependency :-Allows you to invalidate a specific cache item depending
when another cached item changes.

14)What is Cache Callback in Cache ?
Ans) Cache object is dependent on its dependencies example file based, time based etc...Cache items remove the object when cache dependencies change.ASP.NET provides capability to execute a callback method when that item is removed from cache.

15)What is scavenging ?
Ans)When server running your ASP.NET application runs low on memory resources, items
are removed from cache depending on cache item priority. Cache item priority is set when you add item to cache. By setting the cache item priority controls the items scavenging are removed first.
(16) What are different types of caching using cache object of ASP.NET?
Ans) You can use two types of output caching to cache information that is to be transmitted to and displayed in a Web browser:
√ Page Output Caching
Page output caching adds the response of page to cache object. Later
when page is requested page is displayed from cache rather than
creating the page object and displaying it. Page output caching
is good if the site is fairly static.
√ Page Fragment Caching
If parts of the page are changing, you can wrap the static sections as user
controls and cache the user controls using page fragment caching.

17)How can you cache different version of same page using ASP.NET
cache object ?

Ans) Output cache functionality is achieved by using “OutputCache” attribute on ASP.NET page header. Below is the syntax
<%@ OutputCache Duration="20" Location="Server" VaryByParam="state"
VaryByCustom="minorversion" VaryByHeader="Accept-Language"%>
√ VaryByParam :- Caches different version depending on input parameters send
through HTTP POST/GET.
√ VaryByHeader:- Caches different version depending on the contents of the
page header.
√ VaryByCustom:-Lets you customize the way the cache handles page variations
by declaring the attribute and overriding the GetVaryByCustomString handler.
√ VaryByControl:-Caches different versions of a user control based on
the value of properties of ASP objects in the control.
18) How will implement Page Fragment Caching ?
Ans)Page fragment caching involves the caching of a fragment of the page, rather than the entire page. When portions of the page are need to be dynamically created for each user request this is best method as compared to page caching. You can wrap Web Forms user control and cache the control so that these portions of the page don’t need to be recreated each time.

Home
ASP.NET Interview Question Categories

No comments:

Post a Comment