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 5

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
Cache Memory and Automatic Invalidation
It's important to remember that the ASP.NET cache will hold on to items only while there is enough memory available without compromising operation of the server. If you continue to add items to the cache after the free cache memory is exhausted, ASP.NET invalidates and removes the oldest, least used, or lowest priority items. So, just because you cached an item doesn't guarantee it will still be there even if you specified a priority other than the default.

So it's a good idea to test for the presence of an item before or as you retrieve it from the ASP.NET cache. You can also use cache dependencies and an event handler (as demonstrated later on) to detect when items are removed from the cache, though this is not always possible in a Web application as the events are not likely to be fired when your ASP.NET page is executing and can handle them.

Output Cache Profiles in ASP.NET 2.0
If you regularly use the same sets of values in OutputCache directives across different ASP.NET pages, you can set up cache profiles in ASP.NET version 2.0. Simply declare the profile in the <outputCacheProfiles> section of machine.config or web.config using an <add> element. Within the <add> element, specify the attributes you want for this profile—for example:

 
Figure 5. Using a CacheProfile: The figure shows the results of using a CacheProfile value in the OutputCache directive.

<outputCacheProfiles> <add name="MyPageProfile" enabled="true" duration="60" varyByParam="*" /> </outputCacheProfiles>

Then, in any ASP.NET page or user control, you just specify this profile in your OutputCache directive:

<%@ OutputCache CacheProfile="MyPageProfile" %>

The example page named output-cache-profile.aspx demonstrates the use of output caching profiles. When you run the page, it looks just like the example in Figure 3. However, as you can see if you view the source for the page (use the "[view source]" link at the bottom of every example page, as shown in Figure 5), you will see that it uses the OutputCache directive shown above.

In the next part of this article, you'll see how to capitalize on ASP.NET's new SQL Cache invalidation support, which lets you cache and reuse data from SQL Server until that data has changed—a capability that nearly every data-driven Web application sorely needs. SQL Cache Invalidation enlists SQL Server to solve the problem of how often to refresh data stored in a database. Essentially, rather than arbitrarily deciding to refresh data on a schedule or having to run a query to check whether data has changed, you can force SQL Server to notify your application whenever specific data changes. By doing this you can ensure that your application displays the most up-to-date information possible, while simultaneously ensuring that your application queries the database as infrequently as possible. This has the effect of improving the efficiency of your application while reducing the demands on your database server.



Alex Homer is a director of Stonebroom, Ltd., a software development, consulting, and training organization. He was formerly lead technical author and reviewer for Wrox, specializing in Microsoft Web and database technologies. You can reach him through his Web site.
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