Pages

Thursday, January 13, 2011

Call Back method creation for a cached item

Ensures cache to be updated based on changes after removal of corresponding cached item.
 
CacheItemRemovedCallback delegate: An instance of this delegate is passed to the insert method when we insert a cache item.


Signature of the delegate:

public delegate void CacheItemRemovedCallback( string key, 
    object value, CacheItemRemovedReason reason );

Parameter Description
key The index location for the item removed from the cache.
value The object item removed from the cache.
reason The reason the item was removed from the cache, as specified by the CacheItemRemovedReason enumeration.

CacheItemRemovedReason enumeration: a very useful enumeration used for getting reason of expiration.

Parameter Description
DependencyChanged The item is removed from the cache because a file or key dependency changed.
Expired The item is removed from the cache because it expired.
Removed The item is removed from the cache by a Remove method call, or by an Insert method call that specified the same key.
Underused The item is removed from the cache because the system removed it to free memory.

e.g.

public void RemovedCallback(String k,
Object v,CacheItemRemovedReason r) {
// Code to check the reason of expiration.

// Logic to repopulate cache item using new values

}

No comments: