Pages

Men

rh

6/23/2012

Interview point of Questions on Caching

What is application Object?
 Application object can be used in a situation where we want data to be shared across users globally.

How can get access to cache object?
    The Cache object is available in the ‘System.Web.Caching’ namespace. We 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.

What are dependencies in cache and types of dependencies?   
  •    File Dependency
  •    Time-based expiration
  •    Key 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

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

Example :

 <%@ OutputCache Duration="10" Location="Server" VaryByParam="dsState"
VaryByCustom="mvr" VaryByHeader="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. 

What are the 3 different ways to store data in the Cache object?

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");
}
}


What are absoluteExpiration and slidingExpiration parmeters of the Insert and Add methods?

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.
 
Which delegate can be used to notify the application when items are removed from the cache?
onRemoveCallback is used to notify the application when items are removed from the cache.


How do you retrieve the value of a cache item stored in the servers memory?

We 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();

}

}


Which method can be used to remove data from the cache?

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");

}


How do you control how long data is cached?

The Cache object’s Add and Insert method parameters allow you to control how long an item is stored in the server’s memory. In practice, these parameter settings provide only indirect control of how long data remains in memory. If your server runs low on available memory, ASP.NET recovers as much memory as possible from expired cache items. If that’s not enough, ASP.NET will unload unexpired items from the cache based on their priority and when they were last accessed.

What is CacheItemPriority enumeration used for?

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.

Which is the only "event” provided by Cache object?

 CacheItemRemoved "event” is the only "event” provided by Cache object.

How do you update the Cache object when data changes?

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.

What are the steps to follow to cache parts of web form?
To cache part of a Web form, follow these steps:
1.
Place the controls and content that you want to cache in a Web user control.
2.
Set the caching attributes for that Web user control.
3.
Create an instance of the Web user control on the Web form.

What is PartialCaching attribute used for?

You can include the PartialCaching attribute in the control’s class declaration to enable fragment caching.

What is @OutputCache directive’s Location attribute and the HttpCachePolicy object’s SetCacheability property used for? 
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.

What is HttpCachePolicy object’s SetAllowResponseInBrowserHistory method used for?

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.

No comments :

Post a Comment