Retrieve Cache Data with Groups
Note
This feature is only available in NCache Enterprise.
Once the items are added to the specific groups, the user can also retrieve certain cache items or keys associated with that group.
Prerequisites
- To learn about the standard prerequisites required to work with all NCache client-side features please refer to the given page on Client Side API Prerequisites.
- For API details refer to: ICache, GetGroupKeys, GetGroupData, SearchService.
Retrieve Keys of a Particular Group
To return a list of keys that belong to a specified group, you can use GetGroupKeys` method that returns a list of keys mapped under the group passed to this API.
Important
- Passing empty strings for the group will return an empty result set.
- Passing only the value of the group will return all the keys mapped under the group.
- Passing null to this API for the group will throw an ArgumentNullException.
// Preconditions: Cache is connected
// A user wants to get the cache keys of all the customers from the West Coast
string groupName = "West Coast Customers";
ICollection<string> keys = cache.SearchService.GetGroupKeys(groupName);
if (keys != null && keys.Count > 0)
{
// Iterate over the result
foreach (var key in keys)
{
Console.WriteLine($"Key '{key}' belongs to '{groupName}' group.");
}
}
Note
To ensure the operation is failsafe, 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 the keys and the values that belong to a specified group, the GetGroupData
method can be used. This method returns a dictionary of keys and values mapped under the group passed to this API.
The following example retrieves the keys and the values that belong to the group West Coast Customers.
// A user wants to get the cache keys along with the values of all the customers from the West Coast
string groupName = "West Coast Customers";
IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetGroupData<Customer>(groupName);
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> retrievedItem in retrievedDictionary)
{
Console.WriteLine($"Customer '{retrievedItem.Value.ContactName}' having Key '{retrievedItem.Key}' belongs to West Coast");
}
}
Additional Resources
NCache provides a sample application for Groups on GitHub.
See Also
Add/Update Cache Data with Groups
Remove Cache Data with Group
Basic Operations for Caching Data
Tag Cache Data