TECHNOLOGIES: Output Caching | Data Caching | Cache Object
Use Caching for Quick Apps
Learn how to use Output Caching, Data Caching, and the Cache object for high-performance applications.
By Brad McCabe
In the cover story of the May 2003 issue of asp.netPRO, 10 Rock-Solid UI Tips, I gave you 10 tips for building interfaces in ASP.NET. But creating a great-looking interface is only part of the equation. Even the prettiest applications will fail to gain acceptance if the interface is slow and unresponsive.
An important factor in keeping the interface responsive to the user is the ability to store information in memory after the initial request. ASP.NET provides two different types of caching allowing you to create high-performance applications.
The first type is output caching. Output caching lets you store either the Web page or user control results dynamically for future requests. On subsequent requests for this page, the information is retrieved from the cache instead of the page being executed again on the server. This increases the speed for the requesting browser greatly because the results are simply retrieved from memory. The load on the Web server also is reduced because no processing time is needed.
Sometimes it does not make sense, however, to cache the entire page. ASP.NET offers a solution to this -fragment caching. By using fragment caching you can take a section of the page that requires extensive processing time and store the results. Then you can generate the other sections of the page on each request.
To cache a section of the page, you must create that section in a user control and set the caching policy for the control. You can set the policy with the @OutputCache directive. You also can change various parameters on the @OutputCache directive to control such things as duration, HTTP headers, post parameters, or user-control parameters. This example uses the cache data for 120 seconds after the initial request; any request after that time causes the page to be reprocessed:
<%@ OutputCache Duration="120" VaryByParam="None" %>
The second type of cache ASP.NET provides is the more traditional application data caching. Using either the Session object or Application object, you can place any objects you need in the server memory instead of re-creating them. A common example of this is to load listbox data that doesn't change often into the Application object on the start of the application (see Figure 1).
Dim ListData As DataSet
With New ListSystem()
ListData = .GetListData()
Application("ListValues") = ListData
Figure 1. The Application_OnStart event fires on the first request to the ASP.NET application. Here you get the values from the middleware for the common listboxes in the application and store them in the Application object. You lock the Application object to ensure no one else gets into this object while loading the values.
Besides the basic Application and Session objects, ASP.NET introduces a new object for caching - the Cache object. The Cache object works similarly to the Application and Session objects but with a few important differences. When objects are placed in the Cache object, the object can expire from the cache. This happens based on numerous properties you can control and events that you can't.
An example of this occurs when too many objects are placed in the cache and system resources start to diminish; the system removes unused or unimportant items. This functionality lets the system remove items that are no longer important but waste memory, unlike the Session and Application objects. You can choose to expire items from the cache yourself based on four categories: absolute time, relative time, file dependency, and cache dependency. For example, if you display financial results in a chart and table based on an XML file, you can cache the values and set a dependency on the file. When the file changes, the Cache object will expire automatically. You must code defensively because the system can remove cached objects before your planned expiration. The Cache object lets you write code to tie to events and receive notification when a cached object is removed from the cache.
Utilizing ASP.NET's extensive cache functionality lets you keep your rich interfaces fast and nimble. Performance and scalability are just as important to an application's success as a snazzy UI.
Brad McCabe is the technical evangelist for Infragistics Inc. He also has been a systems architect and consultant for Verizon Communications and many other clients, and he was a leading .NET evangelist with Ajilon Consulting. His primary interests include ASP.NET, Windows CE .NET, .NET Compact Framework, and Microsoft's networking technologies. E-mail him at mailto:[email protected].