Cache Level Events
Cache level events are general events for cache which are triggered upon the execution of the registered activities/event types. The user can register cache level events for any change in the cache data set i.e add update or remove.
By default, cache level events are disabled (except for Cache Cleared) for any cache configuration and must be enabled for events to be published using NCache Manager.
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. Following are the operations on performing which event notifications are triggered:
Add Notification
Add notification is triggered when a new item is added to the cache. All applications which have registered this event will receive a notification when data is added to the cache.
Update Notification
Update notification is fired when an existing item is updated in the cache. All applications which have registered this event will receive a notification when data is updated in the cache.
Remove Notification
Remove notification is raised when an item is removed from the cache. All applications which have registered this event will receive a notification when data is removed from the cache.
Note
The application will not be able to receive events unless it registers itself against cache using the specific event registration API call.
Pre-Requisites for Using Cache Level Events
- Make sure that event notifications are enabled using NCache Manager.
- Include the following namespaces in your application:
Alachisoft.NCache.Web.Caching
Alachisoft.NCache.Runtime.Events
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Using 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 is used to restrict the cache for monitoring only the specific client operation. Event type includes ItemAdded
, ItemUpdated
and ItemRemoved
event type that must be specified by a separate method call. Multiple event types can also be specfied in a single method call.
EventDataFilter is specified to quantify the amount of information returned upon an event execution. Events that are registered then provide the user with the information based on these data filters. Following are the data filters that can be specified by the user.
None:
On specifying this filter, only the affected keys are returned in the event notification.
MetaData:
On specifying this filter, the afftected keys along with their metadata are returned in the event notification. The metadata that is returned includes the groups and subgroups, cache item priority, provider name and the expiration of the items.
DataWithMetadata:
On specifying this filter, the keys along with the metadata as well as their entire values are returned. It provides the entire data and metadata in the event notification.
For more detail please refer to the Events Overview section.
Warning
EventDataFilter
must be carefully set to avoid unnecessary network bandwidth
consumption.
Step 1: Registering Callback for Event Notifications
First step is to register a callback for multiple events. Below is an example showing how to register callbacks for multiple events.
Important
Make sure that event notifications are enabled using NCache Manager.
// Create a target method
// Pre-condition: Events have been enabled through NCache Manager/ Config Files
public static void OnCacheDataModification(string key, CacheEventArg args)
{
switch (args.EventType)
{
case EventType.ItemAdded:
// Key has been added to the cache
break;
case EventType.ItemUpdated:
// 'key' has been updated in the cache
// Get the updated product
if (arg.Item != null)
{
Product updatedProduct = arg.Item.Value as Product;
// Perform operations
}
break;
case EventType.ItemRemoved:
// 'key' has been removed from the cache
break;
}
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Step 2: Register Cache Notifications
After registering the callbacks, a target method is created which can have multiple callbacks. The method contains an event type and an event filter. The EventType is adjusted according to the type of operation whose notifications the user want. The EventDataFilter can have one of the three possible values(none, metadata, datawithmetadata).
The following example creates a method which registers callbacks for cache notifications with certain event types.
Important
Make sure that event notifications are enabled using NCache Manager.
try
{
// Pre-conditions: Cache is already initialized &
// Events have been enabled through NCache Manager/Config Files
// Register target method
CacheDataNotificationCallback dataNotificationCallback = new CacheDataNotificationCallback(OnCacheDataModification);
// Register cache notification with "ItemAdded" EventType and "None"
// EventDataFilter which means only keys will be returned
CacheEventDescriptor EventDescriptor = cache.RegisterCacheNotification(dataNotificationCallback,
EventType.ItemAdded,
EventDataFilter.None);
// Register cache notifications with all three event types and sets the
// EventDataFilter as DataWithMetadata which returns keys along with their
// entire data
CacheEventDescriptor EventDescriptor = cache.RegisterCacheNotification(dataNotificationCallback,
EventType.ItemAdded | EventType.ItemRemoved | EventType.ItemUpdated,
EventDataFilter.DataWithMetadata);
// Save the event descriptor for further usage
// Perform your business logic
}
catch (OperationFailedException ex)
{
// NCache specific exceptions:
// Connection Failure
// Operation Timeout
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
The events will be notified to their appropriate listeners and handled as implemented by the user.
Step 3: Unregistering Cache Level Events
A previously registered cache level notification can also be unregistered if they are no longer required using the UnRegisterCacheNotification method. Using this method, the EventDescriptor previously returned upon registering of event, needs to be specified in order to unregister the notifications.
The following example shows how to unregister notifications using this method.
try
{
// Unregister Notifications using the EventDescriptor
cache.UnRegisterCacheNotification(EventDescriptor);
}
catch (OperationFailedException ex)
{
// NCache specific exceptions:
// Connection Failure
// Operation Timeout
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Note
Using Cache Level events might affect the performance of your application, since it fires notifications for all specified operations performed on the entire cache data set. Item Level Events is the better recommended approach to avoid this problem.
Additional Resources
For a full functioning .NET application executing events, you can use the sample shipped with NCache which is placed at:
- .NET Framework: %NCHOME%\samples\dotnet\Events
- .NET Core: %NCHOME%\samples\dotnetcore\Events
- .NET JAVA: %NCHOME%\samples\java\Events
See Also
Item Level Events
Custom Events
Pub/Sub Messaging
Search Cache with LINQ