Alachisoft NCache 4.1 - Online Documentation

Bulk Operations

 
Where the 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 import statements in your project:
     
    import com.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.
     
    import java.io.Serializable;
    public class Customer implements Serializable
    {
    ....
    }
     
  4. Now insert the following code in the 'Main' of the project.
     
    Adding Collection of Items:
     
    Performing a bulk fetch gives back a dictionary object. Items which are not present in cache, are not returned in the dictionary
     
    Cache _cache = NCache.initializeCache("myCache");
    _cache.clear();
     
                  Customer customer = new Customer();
customer.name = "David";
customer.customerID = 1001;
customer.setCity("London");
customer.setContactNumber("+44-xxx-xxxx-xxxx");
customer.setOrderInProcess(true);
 
                  Customer customer1 = new Customer();
customer1.name = "Alex";
customer1.customerID = 1002;
customer1.setCity("Chicago");
customer1.setContactNumber("+44-xxx-xxxx-xxxx");
customer1.setOrderInProcess(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);
 
Retrieving Collection of Items:
 
The following code retrieves a collection of items from cache.
 
//Get items from cache in Bulk
                  public HashMap getBulk(String[] keys, String provideName, DSReadOption dsReadOption)
for(Iterator iter = object_recieved.values().iterator(); iter.hasNext();)
{
Customer customerInformation = (Customer)iter.next();
System.out.println("Customer ID: " + customerInformation.customerID);
System.out.println("Name: " + customerInformation.name);
System.out.println("City: " + customerInformation.getCity());
System.out.println("Contact Number: " + customerInformation.getContactNumber());
System.out.println("Order placed: " + customerInformation.isOrderInProcess());
System.out.println();
}
 
Updating Collection of Items:
 
The following code updates a collection of items in cache.
 
//Update items to cache in Bulk
_cache.insertBulk(keys, items, DSWriteOption.None, null);
HashMap object_recieved = _cache.getBulk(keys, DSWriteOption.None, null);
for(Iterator iter = object_recieved.values().iterator(); iter.hasNext();)
{
Customer customerInformation = (Customer)iter.next();
System.out.println("Customer ID: " + customerInformation.customerID);
System.out.println("Name: " + customerInformation.name);
System.out.println("City: " + customerInformation.getCity());
System.out.println("Contact Number: " + customerInformation.getContactNumber());
System.out.println("Order placed: " + customerInformation.isOrderInProcess());
System.out.println();
}
 
Removing Collection of Items:
 
Performing a bulk remove is very much similar to a bulk fetch operation. Items not in the cache are not returned in the dictionary.
 
//Remove items from cache in Bulk
HashMap object_recieved = _cache.removeBulk(keys, DSWriteOption.None, null);
for(Iterator iter = object_recieved.values().iterator(); iter.hasNext();)
{
Customer customerInformation = (Customer)iter.next();
System.out.println("Customer ID: " + customerInformation.customerID);
System.out.println("Name: " + customerInformation.name);
System.out.println("City: " + customerInformation.getCity());
System.out.println("Contact Number: " + customerInformation.getContactNumber());
System.out.println("Order placed: " + customerInformation.isOrderInProcess());
System.out.println();
}
 
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 the cache in Bulk
_cache.deleteBulk(keys, DSWriteOption.None, null);
 
 
  1. Start the Local Cache 'myCache'.
  2. Run the project.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.