Use standardized caching to simplify your development and reduce hard-to-find bugs.

by Thiru Thangarathinam

Aug 15, 2007

Page 3 of 4

Persisting Complex Data
You've seen how to cache, retrieve, and delete simple scalar values, but caching complex data is a little different. To illustrate, create a custom class named Employee that acts as a placeholder for storing the employee details:

Now that you have created the Employee class, add three buttons to the form that store, retrieve, and remove Employee objects from the cache (see Figure 3 for an example). Here's the Click event code for each button:

In addition to the cache key (the EmployeeID) and the data (the emp variable), the overloaded CacheManager.Add() method also accepts a CacheItemPriority enumeration value, a custom object used to refresh the cached item when the item is removed from the cache, and the absolute time at which the cached item needs to expire.

With the buttons and code in place, if you now run the application, you'll see output somewhat similar to Figure 3:

As the name suggests, the CacheItemPriority enumeration allows you to specify priority levels for the cached items. The CacheItemPriority enumeration exposes the values shown in Table 1:

Table 1. CacheItemPriority Enumeration Values: These values are available in the CacheItemPriority enumeration.

Value

Description

High

High priority for scavenging, meaning that the items with this priority level are the least likely to be deleted from the cache as the server frees the system memory.

Low

Low priority for scavenging, meaning that the items with this priority are the most likely to be deleted from the cache as the server frees the system memory.

None

No priority associated with scavenging.

Normal

Normal priority for scavenging, meaning that the items with this priority are likely to be deleted from the cache as the server frees the system memory only after those items with the "Low" priority.

NotRemovable

Non-removable priority for scavenging, meaning that these items are not automatically deleted from the cache as the server frees the system memory. However, even items with this priority are removed according to the item's absolute or sliding expiration time.