Cache Level Event Notifications
The Cache Level Event notifications are fired when data is added, updated, or removed in the cache from clients, Loader, Backing Source, etc. By default, the Cache Level Events are disabled (except for cache cleared operation) and can be enabled through the NCache Management Center.
The Cache Level Event can be registered using the RegisterCacheNotification
by specifying the implemented callback, EventType
, and EventFilter
. Here, we describe how you can register and unregister the Cache Level Events.
Note
The application will not be able to receive events unless it registers itself against the cache using the specific event registration API call.
Prerequisites
- To learn about the standard prerequisites required to work with all NCache client-side features, please refer to the given page on Client-Side API Prerequisites.
- Make sure to enable event notifications using the NCache Management Center.
- For API details, refer to: ICache, RegisterCacheNotification, EventType, EventDataFilter, UnRegisterCacheNotification, CacheEventDescriptor, CacheDataNotificationCallback.
Implement Callback for Event Notifications
You can implement a callback for events where the EventType
is specified according to the user's logic for ItemAdded
, ItemUpdated
, and ItemRemoved
events. The following example implements a callback method for cache notifications with certain event types.
public void OnCacheDataModification(string key, CacheEventArg args)
{
switch (args.EventType)
{
case EventType.ItemAdded:
Console.WriteLine($"Item with Key '{key}' has been added to cache '{args.CacheName}'");
break;
case EventType.ItemUpdated:
Console.WriteLine($"Item with Key '{key}' has been updated in the cache '{args.CacheName}'");
// Item can be used if EventDataFilter is DataWithMetadata or Metadata
if (args.Item != null)
{
Product updatedProduct = args.Item.GetValue<Product>();
Console.WriteLine($"Updated Item is a Product having name '{updatedProduct.ProductName}', price '{updatedProduct.UnitPrice}', and quantity '{updatedProduct.QuantityPerUnit}'");
}
break;
case EventType.ItemRemoved:
Console.WriteLine($"Item with Key '{key}' has been removed from the cache '{args.CacheName}'");
break;
}
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Register Cache Notifications
To register cache-level notifications, a target method is created which can have multiple callbacks. The method contains an Event Type and Event Filter. The EventType
is adjusted according to the type of operation whose notifications the user wants. The EventDataFilter
can have one of the three possible values: None
, MetaData
, or DataWithMetadata
. The events will be notified to their appropriate listeners and handled as implemented by the user.
Important
The EventDataFilter
must be carefully set to avoid unnecessary network bandwidth consumption.
The following example creates a method that registers callbacks for cache notifications with certain event types.
Note
If the EventDataFilter
is not specified, the EventDataFilter.None
is set automatically.
public void RegisterCacheNotificationsForAllOperations()
{
// create CacheDataNotificationCallback object
var dataNotificationCallback = new CacheDataNotificationCallback(OnCacheDataModification);
// Register cache notification with "ItemAdded" EventType and
// EventDataFilter "None" which means only keys will be returned
CacheEventDescriptor eventDescriptor = cache.MessagingService.RegisterCacheNotification(dataNotificationCallback, EventType.ItemAdded | EventType.ItemUpdated | EventType.ItemRemoved, EventDataFilter.None);
if (eventDescriptor.IsRegistered)
{
Console.WriteLine("Cache level notifications registered successfully");
}
}
Unregister Cache Level Events
The previously registered Cache Level Event Notifications can also be unregistered if they are no longer required using the UnRegisterCacheNotification
method. Using this method, the CacheEventDescriptor
also needs to be specified to unregister the notifications. For Java and Node.js, removeCacheDataModificationListener
is used to unregister the event notifications. The following example shows how to unregister notifications using this method.
// Unregister Notifications using the EventDescriptor
cache.MessagingService.UnRegisterCacheNotification(eventDescriptor);
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. The Item Level Events is the recommended approach to avoid this problem.
Additional Resources
NCache provides a sample application for Cache Level Event Notifications on GitHub.
See Also
.NET: Alachisoft.NCache.Runtime.Events namespace.
Java: com.alachisoft.ncache.events namespace.
Python: ncache.runtime.caching.events class.
Node.js: EventCacheItem class.