Management Level Event Notifications
Note
This feature is only available in NCache Enterprise Edition.
Events can be registered for management operations including cache clear, cache stopped, and member joined/left. Here, we describe how to register and unregister management level events.
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 NCache Manager.
- For API details, refer to: ICache, CacheItem, MemberJoined, MemberLeft, CacheStopped, CacheCleared.
Cache Clear Event
In some cases the application must get notified when the cache is cleared. For the clear cache event, the user has to implement method which has the same signature as the CacheClearedCallback. The following method must be implemented in the application to perform desired processing when the cache cleared notification is fired.
// Register cache cleared event
// OnCacheCleared callback will be triggered on cache clear event
cache.NotificationService.CacheCleared += OnCacheCleared;
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
To receive the cache clear event, the following line of code must be incorporated in the application:
// Perform the tasks after getting the cache clear event
Console.WriteLine("Cache has been cleared.");
If you do not want to further receive any notifications for the cache clear event, add the following line in your application:
// UnRegister cache cleared event
cache.NotificationService.CacheCleared -= OnCacheCleared;
Cache Stopped Event
Similar to the cache cleared event, you can also receive notifications when cache is stopped. The following method must be implemented in application to perform processing when cache stopped is fired.
// Register cache stopped event
// OnCacheStopped callback will be triggered when cache is stopped
cache.NotificationService.CacheStopped += OnCacheStopped;
To receive the cache stopped event, the following line of code must be incorporated into application:
// Perform the tasks after getting the cache stopped event
Console.WriteLine($"'{cacheName}' has been stopped.");
If you do not want to further receive any notifications for cache stopped event, add the following line in your application:
// Un-Register cache stopped event
cache.NotificationService.CacheStopped -= OnCacheStopped;
Member Joined Event
The user can also get notified, whenever a member joins a cluster. The following method must be implemented in application to perform processing when a member joins a cluster.
// Perform task after Member Joined event gets fired
Console.WriteLine($"Node with IP:{nodeInfo.IpAddress} has joined the cluster.");
To receive the member joined event, the following line of code must be incorporated into application:
// Register memebr join event
// OnMemeberJoined callback will be triggered when a new member joins cache
cache.NotificationService.MemberJoined += OnMemberJoined;
If you do not want to further receive any notifications for member joined event, add the following line in your application:
// Un-Register memeber join event
cache.NotificationService.MemberJoined -= OnMemberJoined;
Member Left Event
The user can get notified whenever a member leaves a cluster. The following method must be implemented in application to perform processing when a member leaves a cluster.
// Register memebr join event
// OnMemeberJoined callback will be triggered when a new member joins cache
cache.NotificationService.MemberLeft += OnMemberLeft;
To receive the member left event, the following line of code must be incorporated into application:
// Perform task after Member Left event gets fired
Console.WriteLine($"Node with IP:{nodeInfo.IpAddress} has left the cluster.");
If you do not want to further receive any notifications for member left event, add the following line in your application:
// Un-Register memeber left event
cache.NotificationService.MemberLeft -= OnMemberLeft;
Additional Resources
NCache provides a sample application for Management Level Event Notifications on GitHub.
See Also
Cache Level Event Notifications
Item Level Event Notifications
Pub/Sub Messaging
Search Cache with LINQ