NCache 4.4 - Online Documentation

Cache Level Data Event Notifications

 
Cache Level Data Notifications
 
Cache Level Notifications are fired when data is added in cache from clients, loader, read-through etc. Cache Level Data Notifications can be used to share data across different clients. The purpose of this feature is to notify clients about every operation performed on cache [add, insert, remove]. Clients that want to receive these notifications will have to register Cache Level Notification through RegisterCacheNotification API. Using this feature, the performance of cache will be a little low because cache has to send notifications to clients interested in notifications. Cache Level Data Notifications can be used in scenarios where the user wants to remain updated about every operation on cache.
 
Using Cache Level Events
 
By default they are disabled (except for Cache Cleared) for any cache configuration and must be enabled for events to be published.
 
The client application that is supposed to receive events must include the specific event handler method to respond to the event(s) raised. Whenever events are raised, the target event handler method executes the entire code written in the method body.
 
 
Client application is not able to receive events unless it registers itself again cache via specific event registration API call.
 
 
 
  • Registering Cache Level Events
 
Cache Level notifications are generic events which will be triggered upon every execution of the appropriate registered event type. To register these events for notification, RegisterCacheNotification method is used.
 
Appropriate EventType to be registered needs to be specified; single as well as multiple event types can be specified in a single method call. Also EventDataFilter needs to be specified to quantify the amount of information returned upon an event execution.
 
EventDataFilter must be carefully set to avoid unnecessary network bandwidth consumption.
 
 
Every target method returns a key and a CacheEventArgs object which contains information according to the data filter.
 
    // Register target method
CacheDataNotificationCallback dataNotificationCallback = new CacheDataNotificationCallback( OnCacheDataModification);
  try
  {
// Register cache notification with appropriate event type
CacheEventDescriptor EventDescriptor = cache.RegisterCacheNotification(dataNotificationCallback,
EventType.ItemAdded | EventType.ItemRemoved | EventType.ItemUpdated,
EventDataFilter.None);
// 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.ItemAdded:
                    //perform appropirate operations
                    break;
                case EventType.ItemRemoved:
                    //perform appropirate operations
                    break;
                case EventType.ItemUpdated:
                    //perform appropirate operations
                    break;
            }
                        
        }
 
  • Unregistering Cache Level Events
 
A previously registered cache level notification can also be unregistered using the UnRegisterCacheNotification method. In this example, the EventDescriptor previously returned upon registering of event, needs to be specified.
 
try
{
cache.UnRegisterCacheNotification(EventDescriptor);
}
catch(Exception ex)
{
// handle exception
}
 
See Also