Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Take Advantage of ASP.NET 2.0's Data Caching Techniques, Part 1 : Page 4

This two-part article shows you how to take advantage of the many ways that you can boost the performance of ASP.NET applications by caching the data they use. Although some of the techniques apply to both ASP.NET 1.0 and 2.0, the new capabilities in ASP.NET 2.0 make caching even more attractive for busy Web sites.


advertisement
Partial Page Caching (Fragment Caching)
ASP.NET also supports partial caching of a page, sometimes referred to as fragment caching. To use this, you create sections of the page as user controls, containing the Control directive and with the .ascx file extension. Inside each user control you can specify an OutputCache directive (which can specify the ID of any control(s) it contains in a VaryByControl attribute, or use a VaryByParam attribute):

<%@OutputCache Duration="30" VaryByControl="MyListBox" />

However, when you specify output caching for the page hosting the user control, ASP.NET caches the whole page based on the Duration value specified in the hosting page; therefore, any user controls included on the page will be re-executed only when the page itself expires. For this reason, you should set the Duration attribute in the user control(s) to the same, or to a multiple of the Duration attribute value in the hosting page.

 
Figure 4. Partial Page Caching with a User Control: The page cache Duration is 5 seconds, while the cache Duration setting of the user control contained on the page is 10 seconds.
You can see the effects of partial page caching in the example page named output-cache-fragment.aspx. That page hosts a simple user control that contains a drop-down list box. As you can see from Figure 4, the output cache duration for the user control is ten seconds, while the duration for the hosting page is only five seconds. As you refresh the page, the execution times show that the page is re-executed every five seconds, while the user control is only re-executed every ten seconds. However, the hosting page contains the VaryByParam="*" attribute in its OutputCache directive, and so any change to the selected value in the list box—even though it is within the user control—will cause ASP.NET to re-execute both the hosting page and the user control the next time the page is requested.

If your user control generates output that is not specific to one page, or if you use multiple instances in the same page and they do not need to be varied individually, you can reduce output cache requirements by including the Shared attribute:

<%@OutputCache Duration="60" VaryByControl="MyListBox" Shared="True" />

Alternatively, if you create user controls as Class files, you can use an attribute to specify partial caching. In VB.NET the attribute is <PartialCaching(#seconds)>, for example:



<PartialCaching(120)> _ Public Class MyControl Inherits UserControl ... implementation here ... End Class

In C#, the equivalent syntax is:

[PartialCaching(120)] public class MyControl : UserControl { ... implementation here ... }

Configuring Output Caching in ASP.NET 2.0
ASP.NET 2.0 adds new capabilities to the output cache feature, as well as supporting new configuring options for ASP.NET pages and user controls in the machine.config and web.config files. There new version supports a <caching> section where you can define the operation of output caching and SQL database cache invalidation (a topic you'll see explored in more depth in the second part of this article). This is the overall layout of the <caching> section:

<system.web> <caching> <cache disableMemoryCollection="[true|false]" disableExpiration="[true|false]" /> <outputCache enabled="[true|false]"> <diskCache enabled="[true|false]" path="directory-path" maxSizePerApp="size-in-MB" /> </outputCache> <outputCacheSettings> <outputCacheProfiles> <add name="identifying-string" enabled="[true|false]" duration="#seconds" diskCacheable="[true|false]" shared="[true|false]" location="[Any|Client|Downstream|Server|None]" varyByControl="control-name(s)" varyByParam="[None|*|parameter-name(s)]" varyByHeader="header-name(s)" varyByCustom="[Browser|custom-string"] /> </outputCacheProfiles> </outputCacheSettings> <sqlCacheDependency enabled="[true|false]" pollTime="#milliseconds"> <databases> <clear /> <add name="identifying-string" connectionStringName="name-string" pollTime="#milliseconds" /> <remove name="identifying-string" /> </databases> </sqlCacheDependency> </caching> </system.web>

  • The <cache> element defines the settings for memory-based caching and whether cached items will be expired automatically when the server requires the memory or disk space they are occupying (see the section "Cache Memory and Automatic Invalidation").
  • The <outputCache> section specifies the global setting for output caching, whether it is enabled, and where and whether cached items can be saved to disk. Disk caching is enabled by default, and the maximum disk usage per application is set to 5 MB.
  • You use the <outputCacheSettings> section to define individual output caching profiles that you can inherit in your pages, so that you don't have to edit the OutputCache directive in every page (see the section "Output Cache Profiles in ASP.NET 2.0").
  • The <sqlCacheDependency> section defines the databases and connection details that are used for the new automatic SQL data cache invalidation feature in ASP.NET 2.0. You'll see more about that in Part 2 of this article.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date