Item Level Events
Item Level Data Notifications can be used when you want to be notified about a limited set of data rather than every operation performed on cache.
Applications register callback method for a specific key in cache using API calls and get a notification when that key gets updated or removed from cache.
These Notifications are asynchronously sent to the client so there is no overhead on client’s activities.
To utilize the API, include the following namespace in your application:
Alachisoft.NCache.Web.Caching.
Registering Event with a Particular Item
NCache provides a separate classification of events which can be registered with individual keys. To register update or remove event with particular key(s) RegisterCacheNotification method should be used.
Note
The key must exist in cache for the event to be registered. Only update
or
remove
event types can be registered in item level events.
In the following example, the cache will generate a notification when the appropriate operation is performed on the key.
// Create a target method
public static void OnCacheDataModification(string key, CacheEventArg args)
{
switch (args.EventType)
{
case EventType.ItemAdded:
//perform appropriate operations
break;
case EventType.ItemRemoved:
//perform appropriate operations
break;
case EventType.ItemUpdated:
//perform appropriate operations
break;
}
}
//Register target method
CacheDataNotificationCallback dataNotificationCallback = new CacheDataNotificationCallback(OnCacheDataModification);
try
{
// Register notification with appropriate event type on existing key
cache.RegisterCacheNotification(key, dataNotificationCallback,
EventType.ItemRemoved | EventType.ItemUpdated,
EventDataFilter.DataWithMetadata);
}
catch (Exception ex)
{
// handle exception
}
Registering Events with CacheItem
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.
// Create a target method
public static void OnCacheDataModification(string key, CacheEventArg args)
{
switch (args.EventType)
{
case EventType.ItemAdded:
//perform appropriate operations
break;
case EventType.ItemRemoved:
//perform appropriate operations
break;
case EventType.ItemUpdated:
//perform appropriate operations
break;
}
}
// 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;
// Register
cache.Insert(key, cacheItem);
}
catch (Exception ex)
{
// handle exception
}
Un-registering Item Level Notification
You can also unregister a previously registered item level notification using
the
UnRegisterCacheNotification
method. In this example, the appropriate key, callback CacheDataNotificationCallback
and EventType
must be specified.
try
{
cache.UnRegisterCacheNotification(key, callback, EventType);
}
catch (Exception ex)
{
// handle exception
}