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);
}
catch (Exception ex){
// handle exception
}