NCache notifies client applications whenever an add, insert, remove or clear operation is done against the cache. These are cluster-wide notifications, and Java applications can receive them.
Cache Item Added Notification
-
Create a class with a name (can be any name) 'CacheEventListner' which implements 'CacheListener' from com.alachisoft.ncache.event.
import com.alachisoft.ncache.web.caching.*;
import com.alachisoft.ncache.event.*;
public class CacheEventListner implements CacheListener
{
public void cacheCleared()
{
System.out.println("Cache Cleared");
}
public void cacheItemAdded(CacheEvent ce)
{
System.out.println("Cache Item Added");
}
public void cacheItemRemoved(CacheEvent ce)
{
System.out.println("Cache Item Removed");
}
public void cacheItemUpdated(CacheEvent ce)
{
System.out.println("Cache Item Updated");
}
}
-
Subscriber of the event needs to register itself to get the notifications. Now simply add an item to get the Notification.
CacheEventListner cacheListner = new CacheEventListner();
Cache _cache = NCache.initializeCache("myCache");
_cache.addCacheEventListener(cacheListner);
_cache.registerAddNotification();
_cache.add("Customer:David:1001", "my Item");
See Also