Alachisoft NCache 4.1 - Online Documentation

Bulk Operations

 
NOTE: This feature is not available in NCache Express and Professional edition.
 
Where normal add, insert and remove methods let you work with single key-value pairs, bulk operations let you add, update or remove a collection of key-value pairs in a single method call. The idea behind bulk operations is to minimize the cluster traffic by bundling multiple operations into a single call. This is handy if you want to work with collection of items.
 
Note: Here users are supposed to provide unique keys for all bulk operations.
 
 
  1. Include the following namespace in your application.
     
    using Alachisoft.NCache.Web.Caching;
     
  2. Create a new Class named 'Customer' in your already created Test Application.
  3. Insert the following code in the 'Customer' class.
     
    [Serializable]
    class Customer
    {
    ...
    }
     
  4. Now insert the following code in the 'Main' of the project:
     
    For Adding Collection of Items
     
    Cache _cache = NCache.InitializeCache( "mycache" );
    _cache.Clear();
     
    Customer customer = new Customer ();
    customer.name = "David" ;
    customer.customerID = 1001;
    customer.City = "London" ;
    customer.ContactNumber = "+44-xxx-xxxx-xxxx" ;
    customer.IsOrderInProcess = true ;
     
    Customer customer1 = new Customer ();
    customer1.name = "Alex" ;
    customer1.customerID = 1002;
    customer1.City = "Chicago" ;
    customer1.ContactNumber = "+44-xxx-xxxx-xxxx" ;
    customer1.IsOrderInProcess = false ;
     
    string [] keys = new string [] { "Customer:David:1001" , "Customer:Alex:1002" };
    CacheItem [] items = new CacheItem [2];
    items[0] = new CacheItem (customer);
    items[1] = new CacheItem (customer1);
     
    //Add items to cache in Bulk
    _cache.AddBulk(keys, items, DSWriteOption.None, null );
     
    For Retrieving Collection of Items
     
    //Get items from cache in Bulk
    IDictionary objectRecieved = _cache.GetBulk(keys, DSReadOption.None);
    IEnumerator iter = objectRecieved.Values.GetEnumerator();
     
    while (iter.MoveNext())
    {
    Customer customerInformation = (Customer)iter.Current;
    Console.WriteLine("Customer ID: " + customerInformation.customerID);
    Console.WriteLine("Name: " + customerInformation.name);
    Console.WriteLine("City: " + customerInformation.City);
    Console.WriteLine("Contact Number: " + customerInformation.ContactNumber);
    Console.WriteLine("Order placed: " + customerInformation.IsOrderInProcess);
    Console.WriteLine();
    }
     
    For Updating Collection of Items
     
    //Insert Bulk
    _cache.InsertBulk(keys, items, DSWriteOption.None, null);
     
    //Get items from cache in Bulk
    IDictionary objectRecieved = _cache.GetBulk(keys, DSReadOption.None);
    IEnumerator iter = objectRecieved.Values.GetEnumerator();
     
    while (iter.MoveNext())
    {
    Customer customerInformation = (Customer)iter.Current;
    Console.WriteLine("Customer ID: " + customerInformation.customerID);
    Console.WriteLine("Name: " + customerInformation.name);
    Console.WriteLine("City: " + customerInformation.City);
    Console.WriteLine("Contact Number: " + customerInformation.ContactNumber);
    Console.WriteLine("Order placed: " + customerInformation.IsOrderInProcess);
    Console.WriteLine();
    }
     
    For Removing Collection of Items
     
    //Remove items from cache in Bulk
    IDictionary removedRecieved = _cache.RemoveBulk(keys, DSWriteOption.None, null);
    IEnumerator iter = removedRecieved.Values.GetEnumerator();
     
    while (iter.MoveNext())
    {
    Customer customerInformation = (Customer)iter.Current;
    Console.WriteLine("Customer ID: " + customerInformation.customerID);
    Console.WriteLine("Name: " + customerInformation.name);
    Console.WriteLine("City: " + customerInformation.City);
    Console.WriteLine("Contact Number: " + customerInformation.ContactNumber);
    Console.WriteLine("Order placed: " + customerInformation.IsOrderInProcess);
    Console.WriteLine();
    }
     
    For Deleting Collection of Items
     
    The following code deletes a collection of items from the cache. This API performs the same functionality as "RemoveBulk" but it does not return any value.
     
    //Delete items from cache in Bulk
    _cache.DeleteBulk(keys, DSWriteOption.None, null);
     
     
  5. Start the Local Cache 'myCache'.
  6. Run the project.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.