Using the Cache Object

When Microsoft designed ASP.NET, one of its main goals was to give ASP.NET great performance. One way Microsoft offers ASP.NET developers the capability to increase performance is by offering a Cache API. A Cache object can store any type of information or data so that it can be used at any time within an ASP.NET application. This means that for data such as recordsets, the database does not have to be queried to use the data; the data can simply be retrieved from the Cache object.

The Cache object is simple to use. If you want to use this basic functionality, then the syntax is identical to using the application and session variables, as show below:

C#
 Cache["SomeData"] = "This or that"; 
VB
 Cache("SomeData") = "This or that"; 

But using the simple Cache object syntax doesn't give you the kind of flexibility that the Cache object is intended to provide. As a matter of fact, the simplified use of the Cache object is no more beneficial than using an application or session variable. A Cache object, though, will be global to your entire application, which means that the simplified cache syntax will give you the same results as using an application variable.

To really use the Cache object's extended features, you should add data with the Add() method. The Add() method requires seven parameters. The first parameter is the key, which is how you name the data that you are saving. The second parameter is the item that is to be added to the cache. The third parameter gives you the capability to add dependencies to the Cache object. A dependency can be either a file or another cache key. When either the file changes or the cache that is referenced by the key changes, the added Cache object becomes invalid and is removed from the cache. If you don't want to pin any dependencies on the cache data you are adding, you simply make the third parameter null in C# or Nothing in VB. The fourth parameter specifies an automatic expiration time, at which point the object expires and is removed from the cache. If you don't want an automatic expiration (for instance, if you want a sliding expiration), then you should make this parameter null for C# or Nothing for VB. The fifth parameter gives you the opportunity to provide a sliding expiration, which indicates how long after the last access to this Cache object a time-out will occur. The difference between an absolute and sliding expiration is this: An absolute expiration will occur regardless of any access that is made to cache data, while a sliding expiration begins its countdown after each access to a cached piece of data. Table 7.1 shows the CachePriority enumeration. These values are what you use to set the priority of a cache item. The seventh and last parameter gives you the opportunity to provide a call-back function so that your code can be notified when and if an object is removed from the cache. You will make this last parameter null in ASP.NET applications.

I'd like to give you a few examples now of using a Cache.Add() method. The following code adds a data reader (named OBJReader) to the cache. The key with which it will identify this data is named Categories. For this example, this is a SqlDataReader that contains a list of categories from a SQL database. No dependencies are specified as the third argument, and it is null or Nothing. An absolute expiration of 20 seconds is specified by passing as the fourth argument a date object with 20 seconds added to it. No sliding expiration is specified, the cache item priority is high, and there is no call back.

Table 7.1. The CacheItemPriority Enumeration

Member Name

Description

AboveNormal

Cache items with this priority level are less likely to be deleted as the server frees system memory than those assigned a Normal priority.

BelowNormal

Cache items with this priority level are more likely to be deleted from the cache as the server frees system memory than items assigned a Normal priority.

Default

The default value for a cached item's priority is Normal.

High

Cache items with this priority level are the least likely to be deleted from the cache as the server frees system memory.

Low

Cache items with this priority level are the most likely to be deleted from the cache as the server frees system memory.

Normal

Cache items with this priority level are likely to be deleted from the cache as the server frees system memory only after those items with Low or BelowNormal priority. This is the default.

NotRemovable

The cache items with this priority level will not be deleted from the cache as the server frees system memory.

C#
 Cache.Add("Categories", objReader, null, Date.Now.AddSeconds(20),   null, CacheItemPriority.High, null); 
VB
 Cache.Add("Categories", objReader, Nothing, Date.Now.AddSeconds(20), _   Nothing, CacheItemPriority.High, Nothing) 

The next code example adds the same SqlDataReader to the cache and identifies it by the key named categories. It does not specify any dependencies or an absolute expiration. It does, however, specify a sliding expiration with a TimeSpan object that is passed in to the fifth argument. In this case, the TimeSpan is specified to 20 minutes, which will cause the data to time out in 20 minutes. The cache item priority is set to high, and, again, no call back is specified.

C#
 Cache.Add("Categories", objReader, null, null,   new TimeSpan(0, 0, 20, 0, 0), CacheItemPriority.High, null); 
VB
 Cache.Add("Categories", objReader, Nothing, Nothing, _   new TimeSpan(0, 0, 20, 0, 0), CacheItemPriority.High, Nothing) 


ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
EAN: N/A
Year: 2003
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net