Wednesday, January 30, 2008

Asp.Net Caching terms.... It means proper performance (Partial Page Caching)

Partial Page Caching makes sense when a page contains both dynamic and static contents.
Two methods to apply this concept :
  1. Post-Cache Substitution:
  2. Caching with a User Control :
Let us start with Post-Cache Substitution:
See this example :
Note :
  • Substitution Control will not cached
  • The method must be a static method because an instance of the class is not created when the page is output cached.
  • Remaining part of page will be cached.
  • Post-cache substitution is used internally by the AdRotator control. Even when you use Page Output Caching to cache a page that contains an AdRotator control, the content rendered by the AdRotator control is not cached.

you can use post-cache substitution programmatically by using the Response.WriteSubstitution() method
see this example :

Note:
  • There are two advantages to using the WriteSubstitution() method. First, the method referenced by the WriteSubstitution() method does not have to be a method of the current class. The second is that you can use it within a custom control to perform post-cache substitutions.
Let us start Caching with a User Control:

Using post-cache substitution is appropriate only when working with a string of text or HTML. If you need to perform more complex partial page caching, then you should take advantage of User Controls.

You can cache the rendered contents of a User Control in memory in the same way as you can cache an ASP.NET page. When you add an directive to a User Control, the rendered output of the User Control is cached

Note :
  • When you cache a User Control, the content is cached on the web server and not on any proxy servers or web browsers. When a web browser or proxy server caches a page, it always caches an entire page.
  • Shared attribute Enables you to share the same cached version of the User Control across multiple pages
Caching with a User Control programmatically ,see this example:
Creating a User Control Cache File Dependency:
You can use the CacheControlPolicy.Dependency property to create a dependency between a cached User Control and a file (or set of files) on the file system.

Caching Dynamically Loaded User Controls:
you need to be aware that when a cached User Control is loaded dynamically, the ASP.NET Framework automatically wraps the User Control in an instance of the PartialCachingControl class. Therefore, you need to cast the control returned by the Page.LoadControl() method to an instance of the PartialCachingControl class.
See this example:

Ref :
Code Project WebSite
Unleashed ASP.NET 2.0 book
AppDev Video

No comments: