Remove Group Data from Cache
NCache allows the user to remove items with data groups. It also allows the user to remove data group with CacheItem.
To remove cache items that belong to a specified group and sub-group, the RemoveGroupData method can be used. This method removes all cache items mapped under the group and subgroup passed to this API.
Pre-Requisites for Removing Group
- Include the following namespace in your application:
Alachisoft.NCache.Web.Caching
Alachisoft.NCache.Runtime
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Important
- Passing empty strings for group and subgroup will return empty result set.
- Passing null for group will throw
ArgumentNullException
. - Passing only the value of group (and passing null for subgroup) will remove all cache items mapped under the group irrespective of the subgroup.
The following example removes the data under the group Important Customers and sub-group East Coast Customers.
try
{
// Items are previously added im the cache with the group 'Important Customers'
// and 'East Coast Customers'
string groupName = "Important Customers";
string subGroupName = "East Coast Customers";
// Cache items belonging to the group and sub-group are removed from the cache
cache.RemoveGroupData(groupName, subGroupName);
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
See Also
Add/Update Item in Group
Retrieve Group items
Basic Cache Operations
Using Tags