NCache 4.3 - Online Documentation

Item Specific Events

 
NOTE: This feature is not available in NCache Express and Professional edition.
 
NCache allows client applications to register interested events for selected cache keys. Whenever, cached items associated with these keys are updated or removed from the cache, an event notification is sent to those clients who had registered an interest for them. These notifications are cluster-wide and both .NET and Java applications can receive them.
Add the following namespaces in your application for using events:
 
using Alachisoft.NCache.Web.Caching;
using Alachisoft.NCache.Runtime;
 
Register for selective notification
 
Cache.RegisterKeyNotificationCallback method registers the CacheItemUpdatedCallback and/or CacheItemRemovedCallback for a key or list of keys.
 
_cache.RegisterKeyNotificationCallback( string key, CacheItemUpdatedCallback updateCallback, CacheItemRemovedCallback removeCallback)
 
Following code example adds a key into the cache and registers the notifications for that key.
 
public void AddToCache()
{
Cache _cache = NCache.InitializeCache("mycache");
 
_cache.Insert("cache-key-sel-notification", "some-value", null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default);
 
_cache.RegisterKeyNotificationCallback("cache-key-sel-notification", new CacheItemUpdatedCallback(OnItemUpdated), new CacheItemRemovedCallback(OnItemRemoved));
}
void OnItemUpdated(string key)
{
//Item is updated. Do something
}
void OnItemRemoved(string key, object value, Alachisoft.NCache.Web.Caching.CacheItemRemovedReason reason)
{
//Item is removed. Do something
}
 
Unregister from selective notification
 
Cache.UnRegisterKeyNotificationCallback method unregisters the CacheItemUpdatedCallback and/or CacheItemRemovedCallback already registered for the specified key or set of keys.
 
_cache.UnRegisterKeyNotificationCallback( string key, CacheItemUpdatedCallback updateCallback, CacheItemRemovedCallback removeCallback)
 
Following code example unregisters the notifications for key that was registered in above example.
 
_cache.UnRegisterKeyNotificationCallback( "cache-key-sel-notification" ,OnItemUpdated, OnItemRemoved);
 
 
See Also
 
© Copyright  2014 Alachisoft. All rights reserved.