ASP. NET has a built-in powerful cache function. You can use this function through the key and item data access model of the cached object. This model is very intuitive and familiar to everyone. With this model, you can naturally write code that uses cached objects, just as if you were using a set.
You can also search for cached data by key. In addition, if a key does not exist, it indicates that data needs to be retrieved from a data storage (rather than from the cache. More advanced developers will encapsulate the cache behavior into their own data retrieval methods, but they still need to rely on a key to determine whether the code needs to retrieve data, or directly return the cached copy.
However, a better data cache technology utilizes the general asynchronous programming mode used in the. NET Framework. With this technology, the cache engine can tell you when to retrieve expired data.
Delegated cache proxy
To use this technique, you must first declare a delegate variable of the cacheitemremovedcallback type, as shown in the following C # code.
Cacheitemremovedcallback onremove = NULL;
The essence of delegation is the type-safe function pointer, which is used to store the address of a method and facilitate subsequent execution of this method. In asynchronous programming and in any situation that requires loose coupling communication (such as event processing code), delegation is widely used. If you want to learn more about delegation, we recommend that you read the notification on using delegation to implement callback.
Next, create a method with an appropriate signature for the cacheitemremovedcallback delegate to put the code used to retrieve the cached data in this method:
Public void removedproducts (string K, object v, cacheitemremovedreason R)
{
// Go get the data again
}
Parameters accepted by the callback method include the key of the item to be deleted, the item itself, and the reason for deleting the item from the cache. These causes are encapsulated in the cacheitemremovedreason enumeration. Optional values include expired (expired), removed (removed), and underused (need to be removed to release memory) and dependencychanged (the dependency has been changed ). The first two values do not need further explanation, but the third value must be noted. This happens if ASP. Net needs to reclaim the memory and remove the items from the cache. The fourth value is discussed in the next section.
The last step is to declare an instance entrusted by cacheitemremovedcallback and pass the instance to the insert method when adding an item to the buffer. To Cache a able named DT (which has a products key value) and set a 6-hour deadline, use the following code:
Onremove = new cacheitemremovedcallback (this. removedproducts );
This. cache. insert ("Products", DT, null,
Datetime. Now. addhours (6), timespan. Zero,
Cacheitempriority. High, onremove );
After the value associated with the products key is removed for whatever reason, the cache engine calls the delegate function removedproducts. If the datatable is removed from the cache because it expires, a parameter explaining the reason for the removal will be passed to it when removedproducts is called. This means you need to refresh the data and cache it again.
Trace Cache dependent objects
As the dependencychanged Member of the cacheitemremovedcallback enumeration implies, the ASP. NET cache engine can track the dependencies between cache items and other elements. Dependency can be created between two items in the cache, or between the cache item and a file system object (such as a file, directory, or a combination of the two. For example, the following VB. NET code snippet references one item in the cache as parent, and the other item as child, thus establishing the dependency between the two.
Me. cache ("parent") = "parent data"
Dim dependencykey (0) as string dependencykey (0) = "parent"
Dim Dep as new cachedependency (nothing, dependencykey)
Me. cache. insert ("child", "Child Data", DEP)
After the parent item is removed from the cache, the Child item is also removed. By using it and the delegation mechanism described earlier, you can obtain a Complex cache system, which is composed of groups of related objects. These objects can be automatically refreshed after they expire in your application cache.