Cache Level Events
By default, cache level events are disabled (except for Cache Cleared) for any cache configuration and must be enabled for events to be published.
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 API calls.
Note
The application will not be able to receive events unless it registers itself against cache using the specific event registration API call.
Registering Cache Level Events
To utilize the API, include the following namespace in your application:
Alachisoft.NCache.Web.Caching.
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.
Warning
EventDataFilter
must be carefully set to avoid unnecessary network bandwidth
consumption.
// 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 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
}
The events will be notified to their appropriate listeners and handled as implemented by the user.
Un-registering 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
}