NCache 4.4 - Online Documentation

Using Asynchronous Operations

 
In case of failure for asynchronous operation, server will not throw any exception.
 
 
Basic Asynchronous Operations
 
  • Adding Data to Cache Asynchronously
 
Data can be added to NCache asynchronously using AddAsync method.
In this example an object is added to the cache asynchronously.
 
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
CacheItem cacheItem = new CacheItem(product);
string key = "Product:" + product.ProductID;
 
try
{
    cache.AddAsync(key, cacheItem, DSWriteOption.None, null);
}
catch (Exception ex)
{
// handle exception
}
 
  • Updating Data to Cache Asynchronously
 
An InsertAsync method enables the user to update existing data in the cache in an asynchronous manner.
 
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.Category = 5; // updated category
 
CacheItem cacheItem = new CacheItem(product);
string key = "Product:" +product.ProductID ;
 
try
{
    cache.InsertAsync(key, cacheItem, DSWriteOption.None, null);
}
catch (Exception ex)
{
// handle exception
}
 
  • Removing Data from Cache Asynchronously
 
In case a user wants to perform a remove operation in the background, a RemoveAsync operation can be used to perform that operation. In this example, the key for the data that needs to be removed must be specified.
 
String key= "Product:1001";
try
{
cache.RemoveAsync(key, null, DSWriteOption.None, null);
}
catch (Exception ex)
{
// handle exception
}
 
  • Clearing the Cache Asynchronously
 
To clear cache completely in background, ClearAsync method needs to be called.
try
{
    cache.ClearAsync(null);
}
catch (Exception ex)
{
// handle exception
}
 
 
Using Asynchronous Callback Method
 
Since asynchronous operations do not notify upon the failure or success of the operation themselves, NCache provides a callback mechanism which can be registered while making operation call. These callbacks are triggered upon completion of the operation. Callbacks are integral in tracking operations.
 
In case of failure for asynchronous operation, server will not throw any exception but, with Async callback, user can know the status.
 
  • Adding Data with Registered Callback Method
 
AsyncItemAddedCallback are provided by NCache which can be registered with an asynchronous addition method. It allows the user to perform appropriate functions upon completion of the operation.
In this example, an ItemAddedCallback provided by NCache needs to be registered with an AddAsync call.
 
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
 
CacheItem cacheItem = new CacheItem(product);
cacheItem.AsyncItemAddCallback += new AsyncItemAddedCallback(OnItemAdded);
 
string key = "Product:" +product.ProductID ;
 
try
{
  cache.AddAsync(key, cacheItem, DSWriteOption.None, null);
}
catch (OperationFailedException ex)
{
// handle exception
}
 
// Create a callback
public void OnItemAdded(string key, object status)
{
  if (status.ToString().Equals("Success"))
    {    //do something
    }
  if (status.ToString().Equals("Failure"))
    {    //do something
    }
      if (status is Exception)
      {    //do something
    }
}
 
  • Updating Data with Registered Callback Method
 
AsyncItemAddedCallback are provided by NCache which can be registered with an asynchronous addition method. It allows the user to perform appropriate functions upon completion of the operation.
 
In order to get a notification upon execution of an update operation, AsyncItemUpdatedCallback need to be registered with InsertAsync operation. It is demonstrated in the example below.
 
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.Category = 5; // updated category
 
CacheItem cacheItem = new CacheItem(product);
cacheItem.AsyncItemUpdateCallback += new AsyncItemUpdatedCallback(OnItemUpdated);
 
string key = "Product:" +product.ProductID ;
 
try
{
    cache.InsertAsync(key, cacheItem, DSWriteOption.None, null);
}
catch (OperationFailedException ex)
{
// handle exception
}
 
// Create a callback
public void OnItemUpdate(string key, object status)
{
  if (status.ToString().Equals("Success"))
    {    //do something
    }
  if (status.ToString().Equals("Failure"))
    {    //do something
    }
      if (status is Exception)
      {    //do something
    }
}
 
  • Removing Data with Registered Callback Method
 
NCache provides AsyncItemRemovedCallback to be used in order to receive notification upon completion of item removal.  In this example you need to register the above mentioned callback with NCache to receive a notification.
 
string key= "Product:1001";
try
{
    cache.RemoveAsync(key, new AsyncItemRemovedCallback(OnItemRemoved), DSWriteOption.None, null);
}
catch (OperationFailedException ex)
{
// handle exception
}
 
// Create a callback
public void OnItemRemove(string key, object status)
{
  if (status.ToString().Equals("Success"))
    {    //do something
    }
  if (status.ToString().Equals("Failure"))
    {    //do something
    }
  if (status is Exception)
    {    //do something
} 
}
 
  • Clearing Cache with Registered Callback Method
 
AsynccacheClearedCallback if registered with NCache is triggered whenever a ClearAsync operation is performed successfully. In this example, the user needs to register the callback when calling the method.
 
try
{
    cache.ClearAsync(new AsyncCacheClearedCallback(OnCacheCleared));
}
catch (OperationFailedException ex)
{
// handle exception
}
// Create a callback
void OnCacheCleared(object result)
{
    if (result.ToString().Equals("Success"))
    {    //do something
    }
    if (result.ToString().Equals("Failure"))
    {    //do something
}
  if (result is Exception)
    {    //do something
}
}
 
 
See Also