You are on page 1of 8

Que 1. What is caching? Ans 1. 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 forms 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 response already stored in memory. Caching an item incurs considerable overhead, so its 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 forms serverside content so that changes to that content do not appear until the cache is refreshed. Or 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. Que 2. What are different types of caching techniques available in .Net? Ans 2. 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 Output Caching - Entire page output is cached. When the request is generated first time, it will be executed and output will be cached. Next time the request came for same page, it will be accessed from cache Page Fragment Caching - Part of page only will be cached. In general in a webpage, top and left parts of webpage will be static with site logo, menu etc. So, here we can cache the top and left parts of webpage. This is called Page Fragment caching Data Caching - Data used in web page also can be cached. For example, administrator users information of a webpage hardly will be changed. We can cache the data and use it for subsequent requests. We can use the default ASP.NET application cache for this and set dependencies like File based, times based etc.

Que 3. What directive is used to cache a web form? Ans 3. The @OutputCache page directive is used to cache a Web form in the servers memory. Que 4. How can you implement Page or Output Caching? Ans 4. 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" %> Que 5. What is the use of duration attribute of @OutputCache page directive? Ans 5. The @OutputCache directives 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. Or 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. Que 6. What are the 2 required attributes of the @OutputCache directive? Ans 6. The @OutputCache directive has two required attributes:

Duration. VaryByParam.

Que 7. How do you cache multiple responses from a single Web form? Ans 7. 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 applications Global.asax file. Que 8. What is the use of VaryByParam attribute of @OutputCache page directive? Ans 8. 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 Que 9. Is it possible to cache a web form without using @OutputCache directive? Ans 9. Yes, you can cache a web form using the Response objects Cache property, which returns an HttpCachePolicy object for the response. The HttpCachePolicy object provides members that are similar to the OutputCache directives attributes. For example, the following code caches the Web forms 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" Que 10. What is @OutputCache directives Location attribute and the HttpCachePolicy objects SetCacheability property used for? Ans 10. The @OutputCache directives Location attribute and the HttpCachePolicy objects 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. Que 11. What is HttpCachePolicy objects SetAllowResponseInBrowserHistory method used for? Ans 11. You can override the cache location settings using the HttpCachePolicy objects SetAllowResponseInBrowserHistory method. Setting that method to True allows the response to be stored in the clients history folder even if the location setting is None or Server. Que 12. What is fragment caching? Ans 12. 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 doesnt get loaded every time the page is rendered. Que 13. How can you implement Data caching? Ans 13. 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. Que 14. How do you retrieve the value of a cache item stored in the servers memory? Ans 14. You can retrieve the value of a cache item stored in the servers memory through the items 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(); } } Que 15. What is Sliding Expiration and Absolute Expiration in asp.Net Caching? Ans 15. Sliding Expiration: With sliding expiration, ASP.NET waits for a set period of inactivity to dispose of a neglected cache item. Ex: Cache.Insert("MyItem",obj,null,DateTime.MaxValue, TimeSpan.FromMinutes(10)) --> The cached data will be removed only if it is not used within a ten-minute period. Absolute Expiration: With absolute expiration, we set a specific date and time when the cached item will be removed. Heres an example that stores an item for exactly 60 minutes: Cache.Insert("MyItem",obj,null,DateTime.Now.AddMinutes(60), TimeSpan.Zero) Que 16. What is the difference between Cache and Application variable? Ans 16. Following is the difference

The scopes of both variables are in Application level. Cache variable provide cache specific features (i.e. expiration). Cache data depends on cache memory, if server running very low cache memory it will remove cached items based on priority.

Que 17. What is the difference between Cache variable and Session variable? Ans 17. Following is the difference The scope of the Cache variable is Application level, sessions variable can be accessed with in session. Cache variable provide cache specific features (i.e. expiration). Cache data depends on cache memory, if server running very low cache memory it will remove cached items based on priority. Que 18. What will happen when you write the following code Response.Cache.SetNoStore() Ans 18.

HttpCachePolicy.SetNoStore() or Response.Cache.SetNoStore Prevents the browser from caching the ASPX page. HttpCachePolicy.SetNoServerCaching or Response.Cache.SetNoServerCaching Stops all origin-server caching for the current response. Explicitly denies caching of the document on the origin-server. Once set, all requests for the document are fully processed. When this method is invoked, caching cannot be re enabled for the current response. Que 19. What are the different types of sessions in ASP.Net? Or What are the various modes of storing ASP.NET session? Ans 19. Session Management can be achieved in two ways InProc

OutProc OutProc is again of two types.


InProc Adv.:-

State Server. SQL Server.


DisAdv:-

Faster as session resides in the same process as the application. No need to serialize the data. Will degrade the performance of the application if large chunk of data is stored. On restart of IIS all the Session info will be lost.


State Server Adv.:-


DisAdv.:-

Faster than SQL Server session management. Safer then InProc, as IIS restart wont affect the session data. Data need to be serialized. On restart of ASP.NET State Service session info will be lost. Slower as compared to InProc.


SQL Server Adv.:-


DisAdv.:-

Reliable and Durable. IIS and ASP.NET State Service Restart won't affect the session data. Good place for storing large chunk of data. Data need to be serialized. Slower as compare to InProc and State Server. Need to purchase Licensed version of SQL Serve.

Que 20. How can you prevent server-side Caching in ASP.NET? Ans 20. Response.Cache.SetNoServerCaching(); This statement prevents the server-side caching. Example:Write this directive below the Page Directive <%@ OutputCache Duration="40" VaryByParam="None" %> In the code behind write this code protected void Page_Load(object sender, EventArgs e) { //Important Response.Cache.SetNoServerCaching(); Response.Write(DateTime.Now.ToLongTimeString());

} It will prevent Caching even when we have mentioned Caching parameters in the OutputCache Directive. Que 21. What is native image cache? Ans 21. The native image cache is a reserved area of the global assembly cache. Once you create a native image for an assembly, the runtime automatically uses that native image each time it runs the assembly. You do not have to perform any additional procedures to cause the runtime to use a native image. Running Ngen.exe on an assembly allows the assembly to load and execute faster, because it restores code and data structures from the native image cache rather than generating them dynamically. Important: - A native image is a file containing compiled processor-specific machine code. Note that the native image that Ngen.exe generates cannot be shared across Application Domains. Therefore, you cannot use Ngen.exe in application scenarios, such as ASP.NET, that require assemblies to be shared across application domains. Que 22. How to make Output Cache maintain seperate cahce entries for each browser ? Ans 22. This is done using below directive: VaryByCustom="browser" <%@ OutputCache Duration="60" VaryByParam="None" VaryByCustom="browser" %>. Que 23. How to make SQLServer session mode work? Ans 23. Please do below steps to make SQLServer session mode to work properly: 1. To use SQLServer mode, you must first be sure the ASP.NET session state database is installed on SQL Server. You can install the ASP.NET session state database using the Aspnet_regsql.exe tool. 2. To configure an ASP.NET application to use SQLServer mode, do the following in the application's Web.config file: Set the mode attribute of the sessionState element to SQLServer. Set the sqlConnectionString attribute to a connection string for your SQL Server database. Que 24. What is SQL Server session mode in ASP.NET? Ans 24. SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. Que 25. When caching is set at both the Web form and user control levels, How does the cache settings interact? Ans 25. The cache location is determined by the Web form setting. Location settings on a user control have no effect. If the Web forms cache duration is longer than the user controls, both the Web form response and the user control response will expire using the Web form setting. Que 26. If a user control is read from the cache, can you access its members from code?

Ans 26. No, In general, cached controls are used to present data such as queries from a database, rather than as interactive components. However, if you do need to access a cached control from code, you must first check that the control exists. If the control is read from the cache, you cant access its members from code. Control members are available only when the control is not read from the cache, such as when the control is first instantiated and when it is reloaded after its cache duration has expired. Que 27. What are the OutputCache directive attributes that apply only to user controls? Ans 27. Shared Que 28. What is PartialCaching attribute used for? Ans 28. You can include the PartialCaching attribute in the controls class declaration to enable fragment caching. Que 29. What is fragment caching? Ans 29. Caching parts of web form is called as fragment caching. Sometimes you want to cache only part of a Web form response. For instance, a Web form might contain many pieces of variable information plus a single large table that almost never changes. In this case, you might place that table in a Web user control and store the response for that control in cache. This technique is called fragment caching. Que 30. What are the steps to follow to cache parts of web form? Ans 30. To cache part of a Web form, follow these steps: Place the controls and content that you want to cache in a Web user control. Set the caching attributes for that Web user control. Create an instance of the Web user control on the Web form. Que 31. What is PartialCaching attribute used for? Ans 31. You can include the PartialCaching attribute in the controls class declaration to enable fragment caching. Que 32. Is it possible to prevent a browser from caching an ASPX page? Ans 32. Just call SetNoStore on the HttpCachePolicy object exposed through the Response object's Cache property, as demonstrated here: <%@ Page Language="C#" %> <% Response.Cache.SetNoStore (); Response.Write (DateTime.Now.ToLongTimeString ()); %> SetNoStore works by returning a Cache-Control: private, no-store header in the HTTP response. In this example, it prevents caching of a Web page that shows the current time. Que 33. How to prevent client cache? Ans 33. By default, when you enable page output caching, the page is cached on both the server and client. You can control the exact location where they occur by adding the location attribute to the OutputCache page directive.

Ex: < %@ OutputCache Duration =600 VaryByParam=* Location=Client %> Que 34. How can you cache a page based on HTTP header? Ans 34. You can use VaryByHeader attribute of OutputCache directive to cache different versions of cache of a page depending on the type of browser that requests the page. Ex: < %@ OutputCache Duration=60 VaryByParam=* VaryByHeader=User-Agent % > The User-Agent identifies the type of browser being used to request the page. Que 35. Is cache object specific to each session? Ans 35. No, its global for all sessions.