Programming Microsoft ASP.NET 4 - Dino Esposito [377]
Note
On a multiprocessor machine with more than one CPU affinitized with the ASP.NET worker process, each processor ends up getting its own Cache object. The various cache objects are not synchronized. In a Web garden configuration, you can’t assume that users will return to the same CPU (and worker process) on subsequent requests. So the status of the ASP.NET cache is not guaranteed to be aligned with what the same page did last time. Later in the chapter, we’ll discuss a variation of the Cache object that addresses exactly this scenario.
Working with the ASP.NET Cache
An instance of the Cache object is associated with each running application and shares the associated application’s lifetime. Each item when stored in the cache can be given special attributes that determine a priority and an expiration policy. All these are system-provided tools to help programmers control the scavenging mechanism of the ASP.NET cache.
Inserting New Items in the Cache
A cache item is characterized by a handful of attributes that can be specified as input arguments of both Add and Insert. In particular, an item stored in the ASP.NET Cache object can have the following properties:
Key A case-sensitive string, it is the key used to store the item in the internal hash table the ASP.NET cache relies upon. If this value is null, an exception is thrown. If the key already exists, what happens depends on the particular method you’re using: Add fails, while Insert just overwrites the existing item.
Value A non-null value of type Object that references the information stored in the cache. The value is managed and returned as an Object and needs casting to become useful in the application context.
Dependencies An object of type CacheDependency, tracks a physical dependency between the item being added to the cache and files, directories, database tables, or other objects in the application’s cache. Whenever any of the monitored sources are modified, the newly added item is marked obsolete and automatically removed.
Absolute Expiration Date A DateTime object that represents the absolute expiration date for the item being added. When this time arrives, the object is automatically removed from the cache. Items not subject to absolute expiration dates must use the NoAbsoluteExpiration constants representing the farthest allowable date. The absolute expiration date doesn’t change after the item is used in either reading or writing.
Sliding Expiration A TimeSpan object, represents a relative expiration period for the item being added. When you set the parameter to a non-null value, the expiration-date parameter is automatically set to the current time plus the sliding period. If you explicitly set the sliding expiration, you cannot set the absolute expiration date too. From the user’s perspective, these are mutually exclusive parameters. If the item is accessed before its natural expiration time, the sliding period is automatically renewed.
Priority A value picked out of the CacheItemPriority enumeration, denotes the priority of the item. It is a value ranging from Low to NotRemovable. The default level of priority is Normal. The priority level determines the importance of the item; items with a lower priority are removed first.
Update Callback If specified, indicates the function that the ASP.NET Cache object calls back when the item will be removed from the cache because it expired or the associated dependency changed. The function won’t be called if the item is programmatically removed from the cached or scavenged by the cache itself. The delegate type used for this callback is CacheItemUpdateCallback.
Removal Callback If specified, indicates the function that the ASP.NET Cache object calls back when the item will be removed from the cache. In this way, applications can be notified when their own items are removed from the cache, no matter what the reason is. As