Published: 08 Dec 2008
By: Scott Mitchell

Monthly tip #1: Scott Mitchell shows how to use caching data during the lifespan of a request

Caching is an excellent way to improve the performance of a web application, and ASP.NET offers a rich caching API. Caching data typically involves the Cache object, which serves as an in-memory cache available to all requests to a web application. For example, when a visitor reaches the Top Selling Products page you can store the results retrieved from the database in the Cache object. When the next visitor reaches this page the list of top products can be pulled from the cache rather than from the database. This style of caching is referred to as a per-application cache because the data in the Cache object is shared among all requests to a specific web application and its data survives as long as the application.

There is another style of caching known as per-request caching in which the cache is specific to each request and the items in the cache only exist for the duration of the request. This style of caching is useful for sharing information among the many actors that are involved in a particular request to an ASP.NET resource. Consider an ASP.NET page that uses a Master Page and contains a User Control. When this page is requested there are three actors involved in the request: the Master Page, the User Control, and the ASP.NET page. Each of these actors have their own declarative markup and code, and in many cases they may need to work with the same data. For example, in an online message board website the Master Page might display a synopsis of the currently logged on user's information. This particular ASP.NET page might also load information about the currently logged on user, as might the User Control. Without any form of per-request caching, the Master Page, User Control, and ASP.NET page will each need to go to the database to get the currently logged on user's information, whereas in a perfect world this information would have only been retrieved once.

Every time the ASP.NET engine receives a request for a resource it creates an instance of the HttpContext class responsible for handling that request. The core objects that every ASP.NET developer is familiar with - Response, Request, Session, and so on - are actually properties of this HttpContext object. A lesser known property of the HttpContext class is the Items collection, which provides a key/value collection that can be used to share state during the lifetime of the request. The Items collection is an ideal location for a per-request cache store.

The following code snippet shows the HttpContext class's Items collection in action in terms of the messageboard website example described above. While the Items collection could be accessed directly from the code in the Master Page, User Control, and ASP.NET page, ideally it would be moved outside of these actors and located within the architecture. Within the application architecture there might be a MessageboardUser class with properties describing the attributes of a user and methods for retrieving information about all users, about a particular user, about certain subsets of users, and so on. The following code snippet shows the GetUser(userId) method and the use of the Items collection.

With this small addition to the architecture code the site now takes advantage of a per-request cache and can save unneeded work when there are more than one actor in a single request using the same functionality or information.

Note that to access the Items collection from outside of an ASP.NET page you need to use HttpContext.Current.Items. Furthermore, you need to have added using System.Web to the class file and reference the System.Web.dll assembly in the Class Library project where your architecture is implemented.

Further Reading

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Scott Mitchell

Scott Mitchell, author of eight ASP/ASP.NET books and founder of 4GuysFromRolla.com, has been working with Microsoft Web technologies since 1998. Scott works as an independent consultant, trainer, ...

This author has published 16 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


jQuery Mobile ListView
In this article, we're going to look at what JQuery Mobile uses to represent lists, and how capable ...
JQuery Mobile Widgets Overview
An overview of widgets in jQuery Mobile.
Code First Approach using Entity Framework 4.1, Inversion of Control, Unity Framework, Repository and Unit of Work Patterns, and MVC3 Razor View
A detailed introduction about the code first approach using Entity Framework 4.1, Inversion of Contr...
jQuery Mobile Pages
Brian Mains explains how to create pages with the jQuery Mobile framework.
Exception Handling and .Net (A practical approach)
Error Handling has always been crucial for an application in a number of ways. It may affect the exe...

You might also be interested in the following related blog posts


Introducing Versatile DataSources read more
How to display data from different tables using one data source read more
ASP.NET 4 Beta 2 - New Version, New Docs, New MSDN Site ! read more
Free SQL Server 2008 Express How-To-Guide Series read more
Building a class browser with Microsoft Ajax 4.0 Preview 5 read more
SOA patterns - Reservations read more
Self-reference hierarchy with Telerik TreeView for Silverlight read more
Using Microsoft's Chart Controls In An ASP.NET Application: Plotting Chart Data read more
Using Interceptors with Ado.Net Data Services and Telerik OpenAccess ORM read more
How to download Internet Explorer 8 for Windows 7 E without any Web browser? read more
Top
 
 
 

Discussion


Subject Author Date
placeholder Nice, thanks Dave Brask 3/25/2010 12:55 PM
Very Good Shailendra Singh Chauhan 1/10/2009 7:19 PM

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.