Using Asynchronous Callback Methods
Since asynchronous operations do not notify upon the failure or success of the operation themselves, NCache provides a callback mechanism enabling you to register them while making the operation call. These callbacks are triggered upon success of the operation. Callbacks are integral in tracking operations.
Note
In case of failure of an Async operation, the server will not throw any exception, but using the Async callback mechanism, the user can know the status of an operation.
To utilize the APIs, include the following namespace in your application:
Alachisoft.NCache.Web.Caching.
Adding Data with Registered Callback Method
The AsyncItemAddedCallback function is used to register an asynchronous addition callback allowing the user to perform appropriate functions upon completion of the add 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 (Exception ex){
// handle exception
}
//Create a callback
public static 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
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 (Exception ex){
// handle exception
}
//Create a callback
public static 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 an Item with Registered Callback Method
NCache provides the AsyncItemRemovedCallback interface to be used in order to receive notification upon completion of item removal.
string key = "Product:1001";
try{
cache.RemoveAsync(key, new AsyncItemRemovedCallback(OnItemRemoved), DSWriteOption.None, null);
}
catch (Exception ex){
// handle exception
}
//Create a callback
public static 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
}
}
Clear Cache with Registered Callback Method
In order to get async clear notification, you have to implement the AsynccacheClearedCallback interface:
try{
cache.ClearAsync(new AsyncCacheClearedCallback(OnCacheCleared));
}
catch (Exception ex){
//handle exception
}
//Create a callback
public static void OnCacheCleared(object result)
{
if (result.ToString().Equals("Success")){
//do something
}
if (result.ToString().Equals("Failure")){
//do something
}
if (result is Exception){
//do something
}
}