Adding and Updating Data Group in Cache
To utilize the APIs, include the following namespace in your application:
Alachisoft.NCache.Web.Caching.
Adding an Item to Data Group
The following example adds an item to cache with the data group identifier Product and sub group identifier Beverages. In this example, you use Add operation to set group and sub-group of the item.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
string key = "Product:" + product.ProductID;
string groupName = "Product";
string subGroupName = "Beverages";
try
{
cache.Add(key, product, groupName, subGroupName);
}
catch (OperationFailedException ex)
{
// handle exception
}
Adding Data Group with CacheItem
In the following example, data group is set by assigning it to a property of CacheItem
.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
string key = "Product:" + product.ProductID;
CacheItem cacheItem = new CacheItem(product);
cacheItem.Group = "Product";
cacheItem.SubGroup = "Beverages";
try
{
cache.Add(key, cacheItem);
}
catch (Exception ex)
{
// handle exception
}
Updating an Item in a Data Group
If you add an item without any group or want to add group to an already cached item, you have to first remove that item from the cache and then insert/add that item with the desired group and sub group identifiers. Otherwise, the insert
operation throws a data groups mismatch
exception.