Dynamically Update Portions of Cached Web Pages with Post-Cache Substitution - ' Dynamic Pages ' (
Page 3 of 4 )
Cache-Substitution for Dynamic portions of a Cached Page
Let's tackle a simple example. A page on your Web site that imparts the company's history is unlikely to change very often. However, imagine that the Web site design specifies that each page should display the current time. (For simplicity, don't be concerned with the time updating if the user remains on the page for a while. The time stamp should reflect the time when the user first opened the page in the browser.)
ADVERTISEMENT
If we cached the page, the timestamp would never change. Yet, if we want the timestamp to change, then we have to waste server resources to keep spitting out that same page over and over again, just to make the timestamp work.
Post-Cache Substitution
With the Post-Cache Substitution control, we can cache the entire page and have it pulled from the OutputCache, just like any other cached page. However, just before the HTML output is sent back down to the browser, the PostCache Substitution control intercepts the HTML and modifies a portion of the page based on a method that you define in your application. Then the HTML is sent to the browser. In order to do this, the control forces ASP.NET to use server-side caching rather than the browser cache.
The method that populates the substation control must be static (in VB, the keyword is Shared) and it must take the context of the page as a parameter. That enables it to do its magic in the plumbing.
Getting this to work is quite simple.
Create a static method in the code-behind of a Web page that returns a string. This method returns the current time.
[VB]
Public Shared Function GetTime(ByVal context As HttpContext) As String
Return Now.ToLongTimeString
End Function
[C#]
public static String GetTime(HttpContext context)
{
return Now.ToLongTimeString ();
}
Drag the Substitution control from the control Toolbox onto the Web form where you would like this information displayed, as you see in Figure 1.
In the control's property window, change MethodName to GetCurrentTime. Now the control knows how to update its content.
To enable OutputCaching on the page, insert the OutputCache directive into the Source of the page, under the Page directive. Here we will only cache for 30 seconds.
Now you don't really have proof that this is working, so let's insert a timestamp that is cached along with the page, as you see in Figure 2.
Drag a standard ASP.NET Label control onto the page.
Add the following to the Page Load event of the page's code-behind.
Label1.Text = Now.ToLongTimeString
Run the page again, and click on browser's refresh button a number of times.
You will see that the label's timestamp does not change as the page is refreshed because it is being cached; yet, the timestamp displayed by the substitution control is updated each time.
The Subsitution API
Another way to achieve post-cache substation is through code which can be in-line or in your code files.
In the earlier example, instead of dragging the control onto the Web form and modifying its MethodName property, you can embed the following HTML into the source of your page to achieve the same results.
One benefit of Response.WriteSubstitution is that it can also be used in code. This opens up the possibility to incorporate post-cache substitution programmatically into custom server controls or when rendering output in code. The WriteSubstitution method also removes any reliance on a static page method.