NCache 4.4 - Online Documentation

Using Bulk Operations

 
Using bulk operations is handy if the user wants to work with collection of items.
 
This feature is not available in NCache Express and Professional edition.
 
 
NCache has multiple bulk API operations. The following operations can be performed in bulk:
 
  • Add
  • Insert
  • Get
  • Remove
  • Delete
 
Here users are supposed to provide unique keys for all bulk operations. Keys[] count has to be equal to items[] count.
 
 
Adding a Collection to Cache
 
A collection of items can be added in cache using AddBulk method. This method returns  a dictionary of all the keys that failed to be added along with the exception.
 
Product product1 = new Product();
product1.ProductID = 1001;
product1.ProductName = "Chai";
product1.UnitsInStock = 15;
string key1="Product:"+product1.ProductID;
 
Product product2= new Product();
product2.ProductID = 1002;
product2.ProductName = "Chang";
product2.UnitsInStock = 25;
string key2="Product:"+product2.ProductID;
 
string[] keys = { key1, key2 };
 
CacheItem[] items = new CacheItem[2];
items[0] = new CacheItem(product1);
items[1] = new CacheItem(product2);
 
try
{
    IDictionary result = cache.AddBulk(keys, items, DSWriteOption.None, null);
}
catch (Exception ex)
{
    // handle exception
}
 
Updating a Collection to Cache
 
An existing collection of items can be updated with the help of InsertBulk method.
 
Product product1 = new Product();
product1.ProductID = 1001;
product1.ProductName = "Chai";
product1.UnitsInStock = 5; //updated units
string key1="Product:"+product1.ProductID;
 
Product product2= new Product();
product2.ProductID = 1002;
product2.ProductName = "Chang";
product2.UnitsInStock = 6; //updated units
 
string key2="Product:"+product2.ProductID;
 
string[] keys = { key1, key2 };
 
CacheItem[] items = new CacheItem[2];
items[0] = new CacheItem(product1);
items[1] = new CacheItem(product2);
 
try
{
    cache.InsertBulk(keys, items, DSWriteOption.None, null);
}
catch (OperationFailedException ex)
{
    // handle exception
}
 
Retrieving a Collection from Cache
 
For fetching a collection from cache, use the GetBulk method. This method returns the collection of items.
 
string[] keys = { "Product:1001", "Product:1002" };
try
{
    IDictionary items = cache.GetBulk(keys, DSReadOption.ReadThru);
    if (items.Count > 0)
    {
        IEnumerator iter = items.Values.GetEnumerator();
 
        while (iter.MoveNext())
        {
            Product info = (Product)iter.Current;
            // do something
        }
    }
 
}
catch (OperationFailedException ex)
{
// handle exception
}
 
Removing a Collection from Cache
 
Just like atomic remove operation, RemoveBulk also removes all cached items in list and returns removed items. Here it is returned in the form of a dictionary. Following is a code example for remove bulk operation:
 
string[] keys = { "Product:1001", "Product:1002" };
try
{
    IDictionary removedItems = cache.RemoveBulk(keys, DSWriteOption.None, null);
 
    foreach (DictionaryEntry entry in removedItems)
    {
        if (entry.Value is Product)
        {
            // do something
        }
    }
}
catch (OperationFailedException ex)
{
// handle exception
}
 
Deleting a Collection from Cache
 
Following is a code snippet for DeleteBulk. This method can be used to delete multiple items from cache in a single bulk call.
 
 
string[] keys = { "Product:1001", "Product:1002" };
try
{
    cache.DeleteBulk(keys, DSWriteOption.None, null);
}
catch (OperationFailedException ex)
{
    // handle exception
 
}
 
See Also