Wednesday, June 27, 2007

Page Data Caching

Page Data Caching

Introduction to Data Caching

ASP.NET provides a full-featured cache engine that can be used by pages to store and retrieve arbitrary objects across HTTP requests. The ASP.NET cache is private to each application and stores objects in memory. The lifetime of the cache is equivalent to the lifetime of the application; that is, when the application is restarted, the cache is recreated.

The cache provides a simple dictionary interface that allows programmers to easily place objects in and retrieve them from the cache. In the simplest case, placing an item in the cache is just like adding an item to a dictionary:

Retrieving the data is just as simple:

For applications that need more sophisticated functionality, the ASP.NET cache supports scavenging, expiration, and file and key dependencies.

Using the Data Cache

The following sample shows a simple use of the cache. It executes a database query and caches the result, which it continues to use for the lifetime of the application. When you run the sample, note the message at the bottom of the page. When first requested, it indicates that the data was explicitly retrieved from the database server. After refreshing the page, the page notes that the cached copy was used.


VB Datacache1.aspx

[Run Sample] | [View Source]
LateBreaking Samples:
  • J#

  • The next example shows a cache item that depends on an XML file. It is similar to the first example, although in this case the data is retrieved from an XML data source instead of a database server. When the data is cached, the XML file is added as a dependency.

    When a new record is added using the form at the bottom of the page, the XML file is updated and the cached item must be recreated.


    VB Datacache2.aspx

    [Run Sample] | [View Source]
    LateBreaking Samples:
  • J#

  • Note that a file dependency is added by using Cache.Insert and supplying a CacheDependency object referencing the XML file.

    A cache item can depend on a single or multiple files or keys. As mentioned previously, an application can also set expiration policy on a cache item. The following code sets an absolute cache expiration time.

    The relevant parameter is the call to DateTime.Now.AddHours(1), which indicates that the item expires 1 hour from the time it is inserted. The final argument, TimeSpan.Zero indicates that there is no relative expiration policy on this item.

    The following code shows how to set a relative expiration policy. It inserts an item that expires 20 minutes after it is last accessed. Note the use of DateTime.MaxValue, which indicates that there is no absolute expiration policy on this item.

    Section Summary

    1. Data caching allows arbitrary objects to be cached programmatically.

    2. The ASP.NET cache supports expiration and dependencies.

    3. The cache is scoped to an application and its lifetime is equivalent to the lifetime of the application.

    Copyright 2002 Microsoft Corporation. All rights reserved.