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);