Alachisoft NCache 4.1 - Online Documentation

Getting notified when an item is updated

NOTE: This feature is not available in NCache Express and Professional edition.
NCache provides the CacheItemUpdatedCallback delegate. It defines the signature to use when you write event handlers to respond when an item is updated in the cache.
To notify an application when an item is updated in 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 CacheItemUpdatedCallback delegate. For example, the following code creates an onUpdate local variable of type CacheItemUpdatedCallback.
    private static CacheItemUpdatedCallback onUpdate = null ;
    Create an event handler procedure that responds when item is updated in the cache.
    public void UpdateCallback(string key, object value)
    //Item is updated. Do something
  3. Create an instance of the CacheItemUpdatedCallback delegate that calls the event handler.
    onUpdate = new CacheItemUpdatedCallback (this.UpdateCallback);
  4. Add item to the Cache by either using Add method or Insert method and specify the local variable of type CacheItemUpdatedCallback, 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 ("Updated-value");
    cacheItem.ItemUpdateCallback = onUpdate;
    _cache.Add("Customer:David:1001", cacheItem, DSWriteOption.None, null);
    _cache.Insert("Customer:David:1001", cacheItem, DSWriteOption.None, null);
    When the item added in step four is updated in the Cache the UpdateCallback 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 © 2005-2012 Alachisoft. All rights reserved.