ASP.NET Caching Strategies
ASP.NET provides developers with the ability to cache dynamically generated pages.This means that it is now possible to cache pages built on posted data and querystrings! For instance, an e-commerce site that generates the same catalog from the database over and over on nearly every user request can now simply cache the catalog pages. Caching saves precious database server CPU cycles and renders pages down to the client much faster. Of course, when the catalog data is updated, the cache can simply refresh itself. Furthermore, developers can define the length of time an item is to be cached, indicate cache dependencies, create cached versions per browser, and indicate where an item should be cached (client, server, proxy, etc.).
Caching is one of the greatest new features that ASP.NET provides regarding application performance.
There is no doubt that caching is one of the best methods of increasing your Web application's performance. ASP.NET and the .NET Framework provide a set of tools that make implementing caching simple. Data that is used over and over in your application, updated semi-regularly (like a catalog, list of contacts, store locations, etc.) make great candidates for caching. The right caching strategy will no doubt increase throughput on your servers and put smiles on your users' faces. This article describes how to implement caching in your applications using the .NET Framework.
Caching Basics (Output Caching)
Output caching involves the strategy of taking a dynamically generated Web page and caching its content so that subsequent requests for the page are fulfilled from an in-memory cache rather than through the execution of code. In high volume Web sites, the right caching strategy can dramatically improve scalability statistics.
ASP.NET gives developers multiple methods of implementing caching. For the most basic page-level caching, developers can use the declarative API, which is simply the term for defining caching parameters at the top of an ASP.NET page using HTML style tags.
First you need to find a candidate page for caching. For example, suppose that you have a Web-based report that displays monthly sales figures by region for the current year. This report's data is updated infrequently, but regularly, as different regions report at different times. The report is accessed many times by many people throughout the organization. Sounds like a good candidate for caching! To do so, you simply place the following declaration at the top of your ASP page. <%@ OutputCache Duration="3600" VaryByParam="None" %>
This declaration instructs the .NET Framework to cache this Web page for a specified number of seconds (in this example, the page will be cached for 3600 seconds, or one hour). Requests made for the page during this time are returned from cache. The page does not execute its code to satisfy these requests, but instead uses in-memory results, thus conserving server resources and increasing page response times.
This declarative API allows you to indicate a simple and effective caching strategy for most of your pages. However, for developers who want more control over caching within their code, ASP.NET also provides a programmatic API. This API is a set of classes, methods and properties that wrap the caching features exposed by the .NET Framework. These classes enable you to set caching directives from within your ASP.NET applications. For example, the caching directive in the previous example could also be indicated programmatically by the following code: Page.Response.Cache.SetExpires(Now.AddHours(1))
This code can be added to your code-behind class inside your Page_Init or Page_Load method. Notice that we call the SetExpires method to indicate a date on which the cached page should expire (again, one hour in our example).
This article will walk you through the classes related to caching with ASP.NET. It will further explore different caching strategies. It will also discuss updating the cache, removing items from the cache, and setting cache dependencies.
By: Lars Powers
Lars has over 12 years of experience designing and implementing solutions on the Microsoft platform. He is the co-author of Visual Basic Programmer's Guide to the .NET Framework Class Library (SAMS Publishing).
By: Mike Snell
Mike has over 12 years of experience writing and designing software. His broad range of experience covers creating enterprise-level, Web-based systems as well as commercial applications using the Microsoft platform.
Caching inside ASP.NET can greatly improve your application's performance.