Sunday, 16 March 2014

Interview Q&A on Caching in ASP.NET(Part 2)

1 : 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.

2:  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.

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

4:  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.

5:  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.

6:  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.

7:  Is the cache object available for all web forms with in a web application?
Ans : Yes, the Cache object is global, that is, data stored in the Cache object is available anywhere within a Web application. In this way, the Cache object is very similar to the intrinsic Application object.

8:   What are the 3 different ways to store data in the Cache object?
Ans : Use assignment.
Assigning a value to an unused key in the Cache object automatically creates that key and assigns the value to that key. Assigning a value to a key that already exists replaces the cached value with the assigned value.
Use the Insert method.
The Insert method uses parameters rather than assignment to create or change cached data. Insert optionally accepts parameters to establish dependencies and set expiration policy.
Use the Add method.
The Add method is similar to Insert; however, it requires all parameters and returns an object reference to the cached data.

For example, the following Cache statements all add the same item to the cache:

using System.Web.Caching;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Cache["NewItem"] = "Some string data";
Cache.Add("NewItem", "Some string data", null, Cache.NoAbsoluteExpiration, System.TimeSpan.FromMinutes(1),
CacheItemPriority.Default, null);
Cache.Insert("NewItem", "Some string data");
}
}

9:  What are absoluteExpiration and slidingExpiration parmeters of the Insert and Add methods?
Ans :absoluteExpiration
A DateTime object that identifies when the data should be removed from the cache. If you’re using sliding expiration, specify Cache.NoAbsoluteExpiration for this parameter.
slidingExpiration
A TimeSpan object that identifies how long the data should remain in the cache after the data was last accessed. If you’re using absolute expiration, specify Cache.NoSlidingExpiration for this parameter.

10:  Which delegate can be used to notify the application when items are removed from the cache?
Ans : onRemoveCallback is used to notify the application when items are removed from the cache.
11:  How do you retrieve the value of a cache item stored in the servers memory?
Ans : 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, as shown in the following code:

private void Button1_Click(object sender, EventArgs e)
{
if (Cache["ChachedItem"] == null)
{
Lable1.Text = "Cached Item not found.";
}
else
{
Lable1.Text = Cache["ChachedItem"].ToString();
}
}

12:  Which method can be used to remove data from the cache?
Ans : Cache object’s Remove method can be used to remove data from the cache as shown in the following code example / sample:
private void RemoveButton_Click(object sender, System.EventArgs e)
{
Cache.Remove("CachedItem");
}

13:  What is CacheItemPriority enumeration used for?
Ans : CacheItemPriority enumeration is used to set the relative importance of cached items. CacheItemPriority.NotRemoveable has the highest priority and CacheItemPriority.Low has the lowest priority.

14:  How do you update the Cache object when data changes?
Ans : Items stored in the cache are often copies of data that is stored and maintained elsewhere, such as records in a database. Use the Add and Insert methods’ dependency parameter to establish a relationship between a cached data item and an external source, such as a file, a folder, or a group of files.

  The dependency parameter accepts a CacheDependency object, which in turn identifies the file, folder, or set of files to watch for changes. ASP.NET checks the time stamp of the items in the CacheDependency object, if one of those time stamps is later than the DateTime entered for the cached item, ASP.NET unloads that item from the cache.

15:  What is PartialCaching attribute used for?
Ans : You can include the PartialCaching attribute in the control’s class declaration to enable fragment caching.

16:  What’s the difference between Cache object and application object ?
Ans: In cache object you can define dependency properties , in application object you can not. In other words when the data which is cached changes even the cache object gets refreshed , but this is not the case of application object. You need to do a pull to refresh the data.

17:  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.

18:  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.

19:  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"  aryByHeader="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.

20:  How many ways a partial-page caching can work?
Ans:  It can  work in two way 
1>Control caching also known as fragment caghing.
2>post-cache substitution.

21:  what is the difference between Control caghing and post-cach substitution?  
Ans:  Control caghing:-In this approach only control  is cached but rest part of page is dynamic.
post-cach substitution :opposite of control caching.

22:  what is OutputCacheProvider?
Ans:  Output-cache providers can use any storage mechanism to persist HTML content. These storage options can include local or remote disks, cloud storage, and distributed cache engines.


No comments:

Post a Comment