Caching with ASP.NET, Part 2By Scott McFarland
In Part 1 we looked at how to use Output Caching and Fragement Caching of an ASP.NET Web page. These two techniques cached either the full HTML output of an ASP.NET Web page, or a portion of the HTML output of an ASP.NET Web page (by caching the HTML output of a User Control). In this part, we'll examine Data Caching, which is an in-memory cache used for caching objects.
Sometimes, more control over what gets cached is desired. ASP.NET provides this power and flexibility by providing a cache engine. Programmatic or data caching takes advantage of the .NET Runtime cache engine to store any data or object between responses. That is, you can store objects into a cache, similar to the storing of objects in Application scope in classic ASP. (As with classic ASP, do not store open database connections in the cache!)
Realize that this data cache is kept in memory and "lives" as long as the host application does. In other words, when the ASP.NET application using data caching is restarted, the cache is destroyed and recreated. Data Caching is almost as easy to use as Output Caching or Fragment caching: you simply interact with it as you would any simple dictionary object. To store a value in the cache, use syntax like this:
To retrieve a value, simply reverse the syntax like this:
Note that after you retrieve a cache value in the above manner you should first verify that the cache value is not null prior to doing something with the data. Since Data Caching uses an in-memory cache, there are times when cache elements may need to be evicted. That is, if there is not enough memory and you attempt to insert something new into the cache, something else has gotta go! The Data Cache engine does all of this scavenging for your behind the scenes, of course. However, don't forget that you should always check to ensure that the cache value is there before using it. This is fairly simply to do - just check to ensure that the value isn't null/Nothing. If it is, then you need to dynamically retrieve the object and restore it into the cache.
For example, if we were storing a string
myString in the cache whose value was set from some method named
SetStringToSomething(), and we wanted to read the value of
myString from the
cache, we'd want to:
- Read the
myStringfrom the cache:
str = Cache("myString")
- Ensure that
strwasn't null/Nothing. If it was, we'd want to get the value of
SetStringToSomething(), and then put it in the cache, like so:
'Try to read the cache entry MyString into str str = Cache("myString") 'Check if str is Nothing If str is Nothing then 'If it is, populate str from SetStringToSomething() str = SetStringToSomething() 'Now insert str into the cache entry myString Cache("myString") = str End If
Besides using the dictionary-like key/value assignment, as shown in the example above, you can also
Add method to add items to the cache. Both of these
methods are overloaded to accommodate a variety of situations. The
Add and the
Insert methods operate exactly the same except the
Add method returns a
reference to the object being inserted to the cache. Because of the similarity of the two methods, I
will concentrate on the
Note that the
Insert method allows you to simply add items to the cache using a key
and value notation as well. For example to simply add an instance of the object
the cache named
foo, use syntax like this:
(Note that this is synonymous to using the
Cache("foo") = bar syntax we looked at earlier.)
Note that with inserting items into the Data Cache using the
Cache(key) = value
method or the
Cache.Insert(key, value) we have no control over when (if ever) the items are evicted from the cache. However, there are times
when we'd like to have control over when items leave the cache. For example, perhaps we want to have
an inserted item in the cache to only live for n seconds, as with Output Caching. Or perhaps we'd
like to have it exit the cache n seconds after it's last accessed. With Data Caching, you can
optionally specify when the cache should have a member evicted.
Additionally, you can have an item evicted from the cache when a file changes. Such an eviction dependency is called a file dependency, and has many real-world applications, especially when working with XML files. For example, if you want to pull data out of an XML file, but you don't want to constantly go to disk to read the data, you can tell the ASP.NET caching engine to expire the cached XML file whenever the XML file on disk is changed. To do this, use the following syntax:
By using this syntax, the cache engine takes care of removing the object
bar from the
BarData.xml file is changed. Very cool!
There are also means to have the inserted cache value expire based on an interval, or at an absolute time,
as discussed before. For more information on these methods, consult the documentation for the
A Cached XML File Example
Hopefully by now you'll agree that one of the most interesting and useful uses of the
Cache.Insertmethod involves using the version of the method that takes advantage of the
CacheDependancyparameter. By using this parameter, developers can create web pages that contain "semi-static" data. In other words, the rendering of the pages is based on configuration-like data which can be stored in an XML file (or anywhere really, I just like to use XML for this type of data). But I digress. The point is, why go back to disk or, worse yet, a SQL Server just to retrieve data that only changes periodically when I can have it done automatically.
To illustrate this point, I've created a simple example. In this example, an XML file is used to house
data that is used to create a simple navigation control. Each
<LinkText> and a
<NavigateURL> tag to house the
appropriate data. Below is a section of this XML file:
In a User Control a DataList control is used to bind the XML data using a HyperLink control.
The Text property is set to the values contained in the
<LinkText> tags. The
NavigateUrl property is set to the values contained in the
<NavigateURL> tags. A Label control is used in the code behind class to output
the time the cache is updated.
(All of the code, and the output, can be seen at this live demo!)
A simple method,
BindDestinations, does the work of binding the XML data to the DataList
control. This method is called everytime the
Page_Load event is fired. A DataSet
object is created and then filled with the XML data by using the DataSet's
BindDestinations method, an attempt is made to pull the data out of the Cache
object. If nothing is found, as is the case when the XML file is changed, the data is re-read from
the XML file and re-inserted into the Cache object. The time this occurs is also displayed via the
lblTime Label control. Finally, the data is bound to the DataList control and displayed.
In this manner, the data of the page is cached until it needs to be refreshed (i.e., the XML file data
is changed). Neat!
The .NET caching services will prove to be one of the most powerful ways to squeeze performance out of new Web-based applications. Whether using Output Caching, Fragment Caching, or the Cache object directly, your web applications will see dramatic improvements in throughput and scalability through the technique of caching expensive dynamic data computations.