Programming Microsoft ASP.NET 4 - Dino Esposito [380]
contents.Text = (String) data;
}
void Remove_Click(Object sender, EventArgs e)
{
Cache.Remove("MyData");
}
Note that the item removal callback is a piece of code defined by a user page but automatically run by the Cache object as soon as the removal event is fired. The code contained in the removal callback runs asynchronously with respect to the page. If the removal event is related to a broken dependency, the Cache object executes the callback as soon as the notification is detected.
If you add an object to the Cache and make it dependent on a file, directory, or key that doesn’t exist, the item is regularly cached and marked with a dependency as usual. If the file, directory, or key is created later, the dependency is broken and the cached item is invalidated. In other words, if the dependency item doesn’t exist, it’s virtually created with a null timestamp or empty content.
Setting the Item’s Priority
Each item in the cache is given a priority—that is, a value picked up from the CacheItemPriority enumeration. A priority is a value ranging from Low (lowest) to NotRemovable (highest), with the default set to Normal. The priority is supposed to determine the importance of the item for the Cache object. The higher the priority is, the more chances the item has to stay in memory even when the system resources are going dangerously down.
If you want to give a particular priority level to an item being added to the cache, you have to use either the Add or Insert method. The priority can be any value listed in Table 18-5.
Table 18-5. Priority Levels in the Cache Object
Priority
Value
Description
Low
1
Items with this level of priority are the first items to be deleted from the cache as the server frees system memory.
BelowNormal
2
Intermediate level of priority between Normal and Low.
Normal
3
Default priority level. It is assigned to all items added using the Item property.
Default
3
Same as Normal.
AboveNormal
4
Intermediate level of priority between Normal and High.
High
5
Items with this level of priority are the last items to be removed from the cache as the server frees memory.
NotRemovable
6
Items with this level of priority are never removed from the cache. Use this level with extreme care.
The Cache object is designed with two goals in mind. First, it has to be efficient and built for easy programmatic access to the global repository of application data. Second, it has to be smart enough to detect when the system is running low on memory resources and to clear elements to free memory. This trait clearly differentiates the Cache object from HttpApplicationState, which maintains its objects until the end of the application (unless the application itself frees those items). The technique used to eliminate low-priority and seldom-used objects is known as scavenging.
Controlling Data Expiration
Priority level and changed dependencies are two of the factors that can lead a cached item to be automatically garbage-collected from the Cache. Another possible cause for a premature removal from the Cache is infrequent use associated with an expiration policy. By default, all items added to the cache have no expiration date, neither absolute nor relative. If you add items by using either the Add or Insert method, you can choose between two mutually exclusive expiration policies: absolute expiration and sliding expiration.
Absolute expiration is when a cached item is associated with a DateTime value and is removed from the cache as the specified time is reached. The DateTime.MaxValue field, and its more general alias NoAbsoluteExpiration, can be used to indicate the last date value supported by the .NET Framework and to subsequently indicate that the item will never expire.
Sliding expiration implements a sort of relative expiration policy. The idea is that the object expires after a certain interval. In this case, though, the interval is automatically renewed after each access to the item. Sliding expiration is rendered through a TimeSpan object