Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [378]

By Root 5736 0
mentioned in Chapter 17, when the session state works in InProc mode, a removal callback function is used to fire the Session_End event. The delegate type used for this callback is CacheItemRemovedCallback.

There are basically three ways to add new items to the ASP.NET Cache object—the set accessor of the Item property, the Add method, and the Insert method. The Item property allows you to indicate only the key and the value. The Add method has only one signature that includes all the aforementioned arguments. The Insert method is the most flexible of all options and provides the following overloads:

public void Insert(String, Object);

public void Insert(String, Object, CacheDependency);

public void Insert(String, Object, CacheDependency, DateTime, TimeSpan);

public void Insert(String, Object, CacheDependency, DateTime, TimeSpan,

CacheItemUpdateCallback);

public void Insert(String, Object, CacheDependency, DateTime, TimeSpan,

CacheItemPriority, CacheItemRemovedCallback);

The following code snippet shows the typical call that is performed under the hood when the Item set accessor is used:

Insert(key, value, null, Cache.NoAbsoluteExpiration,

Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

If you use the Add method to insert an item whose key matches that of an existing item, no exception is raised, nothing happens, and the method returns null.

Removing Items from the Cache


All items marked with an expiration policy, or a dependency, are automatically removed from the cache when something happens in the system to invalidate them. To programmatically remove an item, on the other hand, you resort to the Remove method. Note that this method removes any item, including those marked with the highest level of priority (NotRemovable). The following code snippet shows how to call the Remove method:

var oldValue = Cache.Remove("MyItem");

Normally, the method returns the value just removed from the cache. However, if the specified key is not found, the method fails and null is returned, but no exception is ever raised.

When items with an associated callback function are removed from the cache, a value from the CacheItemRemovedReason enumeration is passed on to the function to justify the operation. The enumeration includes the values listed in Table 18-3.

Table 18-3. The CacheItemRemovedReason Enumeration

Reason

Description

DependencyChanged

Removed because the associated dependency changed.

Expired

Removed because expired.

Removed

Programmatically removed from the cache using Remove. Notice that a Removed event might also be fired if an existing item is replaced either through Insert or the Item property.

Underused

Removed by the system to free memory.

If the item being removed is associated with a callback, the function is executed immediately after having removed the item.

Note that the CacheItemUpdateReason enumeration contains only the first two items of Table 18-3. Curiously, however, the actual numeric values behind the members in the enumeration don’t match.

Tracking Item Dependencies


Items added to the cache through the Add or Insert method can be linked to an array of files and directories as well as to an array of existing cache items, database tables, or external events. The link between the new item and its cache dependency is maintained using an instance of the CacheDependency class. The CacheDependency object can represent a single file or directory or an array of files and directories. In addition, it can also represent an array of cache keys—that is, keys of other items stored in the Cache—and other custom dependency objects to monitor—for example, database tables or external events.

The CacheDependency class has quite a long list of constructors that provide for the possibilities listed in Table 18-4.

Table 18-4. The CacheDependency Constructor List

Constructor

Description

String

A file path—that is, a URL to a file or a directory name

String[]

An array of file paths

String, DateTime

A file path monitored starting at the

Return Main Page Previous Page Next Page

®Online Book Reader