Use Caching to Enhance Performance
Take advantage of the .NET Framework's caching functionality to build high-performance, enterprise-ready Web apps.
by Jonathan Lurie
May 2003 Issue
Technology Toolbox: VB.NET, ASP.NET
Enterprise Web applications must be capable of providing good response times, despite having thousands of concurrent users. Server-side caching is one of the most effective means of achieving high levels of performance. I'll explain how to take advantage of the caching functionality within the .NET Framework.
Caching entails placing commonly requested resources, such as product lists and files, into an area in memory where they are accessible quickly, thereby optimizing response times. The performance benefit is significant, because the resource doesn't need to be re-created or retrieved from the database the next time it's requested (see the sidebar, "Don't Confuse Performance With Scalability"). Consider a product list with hundreds of items in it. The application's response time will be relatively slow if it must hit the database every time a user requests the list. This is unnecessary when the product list doesn't change between requests (see Figure 1).
ASP.NET has two caching techniques you must be familiar with in order to build enterprise-ready applications: the Cache object and the output cache. You can use the Cache object to store individual values, such as a file or a computed number. You can use the output cache to store entire ASP.NET pages and user controls. These techniques are significantly different from each other and require individual attention. I'll focus only on the Cache object.
The .NET Framework exposes a class named Cache, which resides in the System.Web.Caching namespace. ASP.NET creates one cache per Web application, and the cache isn't shared across multiple applications. You should use the Cache object for caching, even though many developers use the Application object instead. The Cache object is quite similar to the Application object, in that they both use a key-value pair to store variables. However, the Cache object is tailored for caching, because it supports object expiration from the cache. This is important because as you place more and more resources into the cache, your application begins to consume more and more memory. This has an adverse effect on performance and can even negate any benefit you derive from caching. Expiring objects removes them from the cache. Although no caching solution can implement all available algorithms for determining what should stay and what should go, Microsoft has done a good job of providing you with several alternatives. The Cache object is also different from the Application object in that it implements automatic locking, which means you don't need to worry about concurrent access; you must use an Application.Lock() with the Application object.
Back to top
|