Retrieve Group Data from Cache
Once the items are added with specified groups or sub-groups the user can also retrieve certain cache items or keys associated with that group and sub-group.
Pre-Requisites for Retrieving 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.
Retrieve Keys of a Particular Group
To return list of keys that belongs to a specified group and sub-group, you can use the GetGroupKeys method. This method returns a list of keys mapped under the group and subgroup passed to this API.
Important
- Passing empty strings for group and subgroup will return empty result set.
- Passing only the value of group (and passing null for subgroup) will return all keys mapped under the group irrespective of the subgroup.
- Passing null to this API for group will throw ArgumentNullException.
try
{
// Following are the group and sub-group items are added with
string groupName = "Important Customers";
string subGroupName = "East Coast Customers";
// Keys associated with the group and subroup specified are retrieved
// Keys are then stored in the form of an array list
ArrayList keys = cache.GetGroupKeys(groupName, subGroupName);
// Check if any keys are failed to retrieve
foreach (var key in keys)
{
// Perform operation according to your logic
}
}
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.
Retrieve Keys and Values of a Particular Group
To return the dictionary of keys and values that belong to a specified group and sub-group, the GetGroupData method can be used. This method returns a dictionary of keys and values mapped under the group and subgroup passed to this API.
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 return all keys and values mapped under the group irrespective of the subgroup.
The following example retrieves the keys and the values that belong to the group Important Customers and sub-group East Coast Customers.
try
{
// Following group and sub-group are created and items are added in the
// cache with them
string groupName = "Important Customers";
string subGroupName = "East Coast Customers";
// Keys and values are retrieved using group and sub-group as specified
// Retrieved cached items are stored in the form of an IDictionary
IDictionary result = cache.GetGroupData(groupName, subGroupName);
foreach (DictionaryEntry entry in result)
{
if (entry.Value is Customer)
{
Customer customer = entry.Value as Customer;
// Perform operations according to business logic
}
else
{
// Object not of Customer type
}
}
}
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
Removing Group
Basic Cache Operations
Using Tags