NCache 4.3 - Online Documentation

Getting notified when an item is removed

 
NOTE: This feature is not available in NCache Express and Professional edition.
 
NCache provides the CacheItemRemovedCallback delegate. It defines the signature to use when you write event handlers to respond when an item is deleted from the cache. NCache also provides the CacheItemRemovedReason enumeration to respond to the cause of removal appropriately.
 
To notify an application when an item is deleted from the cache
 
  1. Add the following namespaces in your application for using events:
     
    using Alachisoft.NCache.Web.Caching;
    using Alachisoft.NCache.Runtime;
     
  2. Create a local variable that raises the event for the CacheItemRemovedCallback delegate. For example, the following code creates an onRemove local variable of type CacheItemRemovedCallback.
     
    private static CacheItemRemovedCallback onRemove = null;
     
  3. Create an event handler procedure that responds when item is removed from the cache.
     
    public void RemovedCallback(string key, object value, CacheItemRemovedReason callbackreason)
    {
    MessageBox.Show(callbackreason);
    }
     
  4. Create an instance of the CacheItemRemovedCallback delegate that calls the event handler.
     
    onRemove = new CacheItemRemovedCallback (this.RemovedCallback);
     
  5. Add item to the Cache by either using Add method or Insert method and specify the local variable of type CacheItemRemovedCallback, created in step one. Following code uses the Insert method to add an item to the cache with a key "Key1" and a cacheItem as value.
     
    Cache _cache = NCache.InitializeCache("thecache");
    CacheItem cacheItem = new CacheItem ("some-value");
    cacheItem.ItemRemoveCallback = onRemove;
    _cache.Add("Customer:David:1001", cacheItem, DSWriteOption.None, null);
    _cache.Remove("Customer:David:1001", cacheItem, DSWriteOption.None, null);
     
    When the item added in step four is removed from the Cache for any reason, the RemovedCallback method is called, and the code within it can be accessed to render new content to a requesting client or to notify your application in a way you choose as appropriate.
 
 
See Also
 
© Copyright  2014 Alachisoft. All rights reserved.