Custom Event Notifications
In multiple clients connected to cache environment, the best way to communicate among them is important. Custom Notifications are special type of notification which follows a publisher and subscriber communication model. It is a simple message passing mechanism to control flow among clients connected to a cache.
Custom Notification provides communication directly among the clients. And data or message is shared through it involving the cache as medium. As the data shared through custom event is not stored in cache, so there is no overhead of memory on cache.
Custom Notification is used in scenarios like when you want to raise the notification from the client application or you want to write logic of raising notification on client side rather than cache to decide when to raise notification.
One example for custom notification is that if there are multiple applications running and the discount price for a specific product is being changed on one application, then you can raise a custom event to notify other client applications about the updated discount price so that they can also update the discount price for that product.
Registering Custom Event Notification
The user can create a custom event according to his/her requirements. This event
needs to be registered with CustomEvent
. NCache does not raise the custom
events on its own, therefore in order to trigger it RaiseCustomEvent
method
needs to be called.
To utilize the API, include the following namespace in your application:
Alachisoft.NCache.Web.Caching.
// Create a target method
public static void CacheCustomEvent(object notifId, object data){
// do something
}
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.UnitsInStock = 15;
// Registering custom event
cache.CustomEvent += new CustomEventCallback(CacheCustomEvent);
// perform operations
//Raise Custom event upon according to need.
cache.RaiseCustomEvent("mycache", product);