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.
-
Include the following import statements in your project:
import com.alachisoft.ncache.web.caching.*;
-
Create a new Class named 'Customer' in your already created Test Application.
-
Insert the following code in the 'Customer' class.
import java.io.Serializable;
public class Customer implements Serializable
{
....
}
-
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);
-
Start the Local Cache 'myCache'.
-
Run the project.
See Also