1 Introduction
1.1 PurposeIt describes how to configure a section in a page as non-cacheable while the page is cached. When a section of a page is non-cacheable, it means the particular section will be processes on each page request.Post-cache substitution is generally helpful where page is almost a static one and some part of the page is generated dynamically.In such type of scenario, we can cache the whole page and some part will be exempted from caching.By doing so the performance improvement can be achieved to a larger extent.
1.2 Need
Caching a page will improve the performance of a web application. But it is very much advisable not to cache a page since some section of a web page may need a reprocessing on each request. To cache a portion of a page there are two methods to do.
1. Control caching/Fragment Caching
2. Post Cache Substitution
Control Caching:
Enabling user controls to cache the content allows us to separate the section of a page that consumes precious processing time to create, such as database interaction. The fragments of the page that require lesser server resources can be generated dynamically for each request. Once the portion of the page which needs to be cached is identified, a user control can be created which contain these portions. So control caching allows specific content within the page to be cached while the overall page is regenerated at every time.
Post-Cache Substitution:
Post cache substitution works exactly opposite of control caching/fragment caching. By post-cache substitution the total page will be cached but some section of the page can be substituted dynamically i.e. the entire page is output cached with certain parts marked as exempt from caching.
2 How Post Cache Substitution works?
Post cache substitution generates a substitution buffer which is added to the response. The cacheability context is changed to server side to ensure that the output cache is not stored at the client-side. Always a response from the server is required to replace the substitution buffer i.e. a server trip.
When the substitution buffer is updated, the substitution delegate is invoked each time to produce true output for the dynamic fragment. The substitution API always delegates a method which is used to generate the true dynamic content.
3 Methods to obtain Post-Cache Substitution
There are three ways to obtain post-cache substitution:
1. Using the substitution control (declaratively).
2. Using the substitution control API (programmatically).
3. Using the AdRotator control (implicitly).
3.1 Using the Substitution control
Substitution control is provided by asp.net which specifies the section of a cached page to be created dynamically. Substitution control is placed in a location on the page where dynamic controls need to be appeared. During runtime the substitution control invokes a method which is specified in the MethodName property.
The constraints to specify the definition of the method while using substitution control are
1. The method must return a string which overwrites the content of the substitution control.
2. The method must be a static method on the containing page or the user control and it should be thread safe.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<%@ OutputCache Duration="30" VaryByParam="none" %>
By using the substitution control, client-side cacheability is changed to server–side cacheability i.e. the page will not be cached on the client. This ensures any further requests to the page will invoke the method again to generate dynamic content.
3.2 Using the Substitution control API
The advantage of calling a substitution API instead of using substitution control declaratively is that, method of any arbitrary object can be invoked rather than calling the static method of the page or the user control.
So to create a dynamic content in a cached page, we just need to call the WriteSubstitution Method in the page code and the parameter will be the method name. The method should handle the creation of the dynamic content and it talkes the HttpContext as an instance and returns a string which will be substituted in the content.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %><%@ OutputCache Duration="30" VaryByParam="none" %>
3.3 using the AdRotator control
The AdRotator server control takes the advantage of post cache substitution because it internally supports this caching mechanism. The AdRotator server control renders unique advertisement on each request regardless the host page is cached or not.Internally, the AdRotator creates an arbitary object with a single callback method which renders the Ad content. This object is passed as the delegate to the Response.WriteSubstitution method. This render object contains enough contexts for each of the AdRotator's properties to be able to generate Ads independently of the page.
4 Precautions while using Post-Cache Substituion
1. Using Post-cache substituion dynamically disables public (or client) caching and switches the page to use server caching. This is because the substitutions need to happen via server-side logic.
2. Post-cache substitution is not supported within a user control when the cached user control is setup to provide fragment caching.
Conclusion
Post-cache substitution enables developers to output cache entire web pages and then identify regions of the cached web page that are exempt from caching. Using post-cache substitution, dynamic content can be generated for each user request even if the parent page is cached. In one embodiment, a page developer is relieved for coding a web page as the present invention is provided as a control.Uses of post cache substituion can dramatically increase the performance if the page contains the small amount of dynamic data.
No comments :
Post a Comment