![]() |
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Published: Wednesday, October 9, 2002 By Dimitrios Markatos
What is Data Caching?
In classic ASP we didn't have anything nearly as sophisticated nor as powerful as the ASP.NET caching API that is now available to us. With .NET we have the ability to cache whole pages (output caching), parts of pages or server controls (fragment caching) and data caching with the lower-level Cache API. In this article I will be examining data caching in detail. For more information on output caching see Page Output Caching from the ASP.NET QuickStarts. For more information on fragment caching, see Page Fragment Caching from the ASP.NET QuickStarts.
Applications, Sessions and Cookies Session variables can also be used to cache information in classic ASP, although, as with the cookie approach each session variable is specific to a particular user, and to tie a session variable to a particular user the user's browser must accept cookies. The advantages of using session variable's over cookies is that you can store objects, such as an array, or Dictionary object. Since session variables are stored on the Web server's memory, storing large objects in a user's session on a site with many simultaneous users can lead to reduced memory on the Web server. For more information on session variables see the Session Variables FAQs on ASPFAQs.com. Most often application variables were the means one would use to cache information in classic ASP. Since a given application variable is "global" to the entire Web application, application variables are primarily candidates for caching information that is global across all Web pages on the site. That is, imagine that you ran an eCommerce Web site and that on every page you wanted to list the top 10 selling products. Rather than do a database access on every page, you could cache the results in an application variable. This is an excellent example of when an application variable would be a good use for caching. If you need to cache more user-specific information, such as the 10 most recently purchased items for the user who's visiting the site, you'd likely want to employ the session object or cookies to do this. (For more information on caching database values in an application variable be sure to read : A Real-World Example of Caching Data in the Application Object.) You can use application variables for caching in ASP.NET much like you did in classic ASP. However, since ASP.NET Web pages can utilize the .NET data cache APIs there's really no reason to ever resort to using application variables for caching in an ASP.NET Web application. In fact, caching data through the data cache API as opposed to through application variables in an ASP.NET Web application has its advantages, including: items in the data cache will be evicted from memory if memory becomes scarce; when adding items to the data cache you can specify how long they should persist in the cache in terms of an absolute or slidign time; and many other advantages, which we will examine in this article. So to sum up the use of Application, Session and or Cookie objects for caching in a classic ASP page:
If you're interested in data caching in classic ASP, you can find more on that at Caching Data and Learn More About Caching. But we're here to show the incredible power .NET data caching has. While this article will focus on .NET data caching in detail, a good general article on .NET caching basics can be found here at Caching with ASP.NET.
Using Data Caching
To add an item to the cache you can simply do:
Note that the C# version uses brackets instead of parenthesis when referencing the items of the cache
in the above manner. In the remainder of the examples in this article I will be using VB.NET syntax,
but will point out any major differences between the VB.NET and C# syntax.
The above code adds the item
To explicitly remove an item from the data cache you can use the
Now that we've examined the simple form for adding, removing, and selecting an item from the data cache,
let's take a more in-depth look at adding items to the cache. In Part 2
we'll examine the
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||