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:
VB
Retrieving the data is just as simple:
myValue = Cache("mykey")
If myValue <> Null Then
DisplayData(myValue)
End If
VB
For applications that need more sophisticated functionality, the ASP.NET cache supports scavenging, expiration, and file and key dependencies.
- Scavenging means that the cache attempts to remove infrequently used or unimportant items if memory becomes scarce. Programmers who want to control how scavenging occurs can provide hints to the scavenger when items are inserted into the cache that indicate the relative cost of creating the item and the relative rate at which the item must be accessed to remain useful.
- Expiration allows programmers to give cache items lifetimes, which can be explicit (for example, expire at 6:00) or can be relative to an item's last use (for example, expire 20 minutes after the item was last accessed). After an item has expired, it is removed from the cache and future attempts to retrieve it return the null value unless the item is reinserted into the cache.
- File and key dependencies allow the validity of a cache item to be based on an external file or on another cache item. If a dependency changes, the cache item is invalidated and removed from the cache. For an example of how you might use this functionality, consider the following scenario: an application reads financial information from an XML file that is periodically updated. The application processes the data in the file and creates a graph of objects that represent that data in a consumable format. The application caches that data and inserts a dependency on the file from which the data was read. When the file is updated, the data is removed from the cache and the application can reread it and reinsert the updated copy of the data.
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.
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.
Note that a file dependency is added by using Cache.Insert and supplying a CacheDependency object referencing the XML file.
Cache.Insert("MyData", Source, _
New CacheDependency(Server.MapPath("authors.xml")))
VB
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.
Cache.Insert("MyData", Source, null, _
DateTime.Now.AddHours(1), TimeSpan.Zero)
VB
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.
Cache.Insert("MyData", Source, null, DateTime.MaxValue, _
TimeSpan.FromMinutes(20))
VB
- Data caching allows arbitrary objects to be cached programmatically.
- The ASP.NET cache supports expiration and dependencies.
- The cache is scoped to an application and its lifetime is equivalent to the lifetime of the application.
Copyright 2001 Microsoft Corporation. All rights reserved.