Item Level Data Event Notifications
Item Level Data Notifications
When the user wants to be notified only about a limited set of data rather than every operation performed on cache, Item Level Data Notifications can be used. Clients register callback for a specific key in cache using RegisterCacheNotification API and gets notifications when that key gets updated or removed from cache. These notifications are asynchronously sent to client so that there is no overhead on client activities.
Using Item Level Events
Item level events are published from cache when particular item is updated or removed from the cache. Item level events are not associated with general events anyways and must be registered against some specific key(s) and events are published only for those specific key(s).
NCache provides a separate classification of event which can be registered with individual keys. To register update or remove event with a particular key(s) RegisterCacheNotification method should be used.
|
The key must exist in cache for event to be registered. Only update or removed event types can be registered in item level events.
|
In this example the cache will generate notification when the appropriate operation is performed on the key.
// Register target method
CacheDataNotificationCallback dataNotificationCallback = new CacheDataNotificationCallback( OnCacheDataModification);
try
{
// Register notification with appropriate event type on existing key
CacheEventDescriptor EventDescriptor = cache.RegisterCacheNotification(key,dataNotificationCallback,
EventType.ItemRemoved | EventType.ItemUpdated,
EventDataFilter.DataWithMetadata);
// Save the event descriptor for further usage
}
catch(Exception ex)
{
// handle exception
}
// Create a target method
static void OnCacheDataModification(string key, CacheEventArg args)
{
switch(args.EventType)
{
case EventType.ItemRemoved:
//perform appropirate operations
break;
case EventType.ItemUpdated:
//perform appropirate operations
break;
}
}
An event can also be registered with a particular key by registering as a property of CacheItem. The SetCacheDataNotification method in CacheItem can be used to provide all appropriate information for registering the notification.
// Register target method
CacheDataNotificationCallback dataNotificationCallback = new CacheDataNotificationCallback( OnCacheDataModification);
try
{
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.UnitsInStock = 15;
CacheItem cacheItem = new CacheItem(product);
// Registering event with cacheItem
cacheItem.SetCacheDataNotification(dataNotificationCallback,EventType.ItemRemoved | EventType.ItemUpdated,EventDataFilter.DataWithMetadata);
string key = "Product:" +product.ProductID ;
// Registering
cache.Insert(key, cacheItem);
}
catch(Exception ex)
{
// handle exception
}
// Create a target method
static void OnCacheDataModification(string key, CacheEventArg args)
{
switch(args.EventType)
{
case EventType.ItemRemoved:
//perform appropirate operations
break;
case EventType.ItemUpdated:
//perform appropirate operations
break;
}
You can also unregister a previously registered item level notification using the UnRegisterCacheNotification method. In this example, the appropriate key, callback as CacheDataNotificationCallback and EventDescriptor previously returned upon registering of event must be specified.
try
{
cache.UnRegisterCacheNotification(key, callback, EventDescriptor);
}
catch(Exception ex)
{ // handle exception
}
See Also